use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.
the class ChunkDataHelper method debugChunksInRange.
/**
* This is a utility methods to detect chunks which are claimed in a certain range.
*
* @param chunkX the chunkX starter position.
* @param chunkZ the chunkZ starter position.
* @param range the range.
* @param buffer the buffer.
* @param world the world.
*/
public static void debugChunksInRange(final int chunkX, final int chunkZ, final int range, final int buffer, final World world) {
final IChunkmanagerCapability chunkManager = world.getCapability(CHUNK_STORAGE_UPDATE_CAP, null).resolve().orElse(null);
if (chunkManager == null) {
Log.getLogger().error(UNABLE_TO_FIND_WORLD_CAP_TEXT, new Exception());
return;
}
final int maxRange = range * 2 + buffer;
for (int i = chunkX - maxRange; i <= chunkX + maxRange; i++) {
for (int j = chunkZ - maxRange; j <= chunkZ + maxRange; j++) {
final BlockPos pos = new BlockPos(i * BLOCKS_PER_CHUNK, 0, j * BLOCKS_PER_CHUNK);
final Chunk chunk = world.getChunkAt(pos);
if (chunk.getCapability(CLOSE_COLONY_CAP, null).map(IColonyTagCapability::getOwningColony).orElse(0) != 0) {
Log.getLogger().warn("Has owner: " + pos.toString());
}
}
}
}
use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.
the class WindowTownHallColonyManage method findNextNearbyColony.
/**
* Finds the first nearby colony claim in the range
*
* @param world world to use
* @param start start position
* @param range search range
* @return the id of the found colony
*/
private static int findNextNearbyColony(final World world, final BlockPos start, final int range) {
int startX = start.getX() >> 4;
int startZ = start.getZ() >> 4;
for (int x = -range; x <= range; x++) {
for (int z = -range; z <= range; z++) {
final int chunkX = startX + x;
final int chunkZ = startZ + z;
final Chunk chunk = world.getChunk(chunkX, chunkZ);
final IColonyTagCapability cap = chunk.getCapability(CLOSE_COLONY_CAP, null).orElseGet(null);
if (cap != null) {
if (cap.getOwningColony() != 0) {
return cap.getOwningColony();
}
if (!cap.getStaticClaimColonies().isEmpty()) {
return cap.getStaticClaimColonies().get(0);
}
}
}
}
return 0;
}
use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.
the class WorkManager method isWorkOrderWithinColony.
/**
* Check if the workOrder is within a colony.
*
* @param order the workorder to check.
* @return true if so.
*/
private boolean isWorkOrderWithinColony(final IWorkOrder order) {
final World world = colony.getWorld();
final Tuple<BlockPos, BlockPos> corners = ColonyUtils.calculateCorners(order.getLocation(), world, new LoadOnlyStructureHandler(world, order.getLocation(), order.getStructureName(), new PlacementSettings(), true).getBluePrint(), order.getRotation(), order.isMirrored());
Set<ChunkPos> chunks = new HashSet<>();
final int minX = Math.min(corners.getA().getX(), corners.getB().getX()) + 1;
final int maxX = Math.max(corners.getA().getX(), corners.getB().getX());
final int minZ = Math.min(corners.getA().getZ(), corners.getB().getZ()) + 1;
final int maxZ = Math.max(corners.getA().getZ(), corners.getB().getZ());
for (int x = minX; x < maxX; x += 16) {
for (int z = minZ; z < maxZ; z += 16) {
final int chunkX = x >> 4;
final int chunkZ = z >> 4;
final ChunkPos pos = new ChunkPos(chunkX, chunkZ);
if (!chunks.contains(pos)) {
chunks.add(pos);
final IColonyTagCapability colonyCap = world.getChunk(pos.x, pos.z).getCapability(CLOSE_COLONY_CAP, null).orElseGet(null);
if (colonyCap == null || colonyCap.getOwningColony() != colony.getID()) {
return false;
}
}
}
}
return true;
}
use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.
the class ChunkClientDataHelper method applyCap.
/**
* Applies the data tuple to the respective chunk
*
* @param chunkCapData colony data to apply
* @param chunk the chunk to apply to
*/
public static void applyCap(final ChunkCapData chunkCapData, final Chunk chunk) {
final IColonyTagCapability cap = chunk.getCapability(CLOSE_COLONY_CAP, null).orElseGet(null);
if (cap != null) {
cap.setOwningColony(chunkCapData.owningColony, chunk);
cap.setCloseColonies(chunkCapData.closeColonies);
}
MinecraftForge.EVENT_BUS.post(new ClientChunkUpdatedEvent(chunk));
}
use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.
the class ChunkDataHelper method tryClaim.
/**
* Add the data to the chunk directly.
*
* @param world the world.
* @param chunkBlockPos the position.
* @param add if add or delete.
* @param id the id.
* @param chunkManager the chunk manager capability.
* @return true if successful.
*/
public static boolean tryClaim(final World world, final BlockPos chunkBlockPos, final boolean add, final int id, final IChunkmanagerCapability chunkManager, boolean forceOwnerChange) {
if (!WorldUtil.isBlockLoaded(world, chunkBlockPos)) {
final ChunkLoadStorage newStorage = new ChunkLoadStorage(id, new ChunkPos(chunkBlockPos).toLong(), add, world.dimension().location(), forceOwnerChange);
chunkManager.addChunkStorage(SectionPos.blockToSectionCoord(chunkBlockPos.getX()), SectionPos.blockToSectionCoord(chunkBlockPos.getX()), newStorage);
return false;
}
final Chunk chunk = (Chunk) world.getChunk(chunkBlockPos);
final IColonyTagCapability cap = chunk.getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
if (cap == null) {
return false;
}
// Before directly adding cap data, apply data from our cache.
final ChunkLoadStorage chunkLoadStorage = chunkManager.getChunkStorage(chunk.getPos().x, chunk.getPos().z);
if (chunkLoadStorage != null) {
chunkLoadStorage.applyToCap(cap, chunk);
}
if (add) {
cap.addColony(id, chunk);
if (forceOwnerChange) {
cap.setOwningColony(id, chunk);
final IColony colony = IColonyManager.getInstance().getColonyByDimension(id, world.dimension());
if (colony != null) {
colony.addLoadedChunk(ChunkPos.asLong(chunk.getPos().x, chunk.getPos().z), chunk);
}
}
} else {
cap.removeColony(id, chunk);
}
Network.getNetwork().sendToTrackingChunk(new UpdateChunkCapabilityMessage(cap, chunk.getPos().x, chunk.getPos().z), chunk);
return true;
}
Aggregations