Search in sources :

Example 11 with IColonyTagCapability

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());
            }
        }
    }
}
Also used : IChunkmanagerCapability(com.minecolonies.api.colony.IChunkmanagerCapability) BlockPos(net.minecraft.util.math.BlockPos) Chunk(net.minecraft.world.chunk.Chunk) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 12 with IColonyTagCapability

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;
}
Also used : Chunk(net.minecraft.world.chunk.Chunk) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 13 with IColonyTagCapability

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;
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) ChunkPos(net.minecraft.util.math.ChunkPos) World(net.minecraft.world.World) LoadOnlyStructureHandler(com.minecolonies.api.util.LoadOnlyStructureHandler) PlacementSettings(com.ldtteam.structurize.util.PlacementSettings) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 14 with IColonyTagCapability

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));
}
Also used : ClientChunkUpdatedEvent(com.minecolonies.api.colony.event.ClientChunkUpdatedEvent) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 15 with IColonyTagCapability

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;
}
Also used : IColony(com.minecolonies.api.colony.IColony) ChunkPos(net.minecraft.util.math.ChunkPos) ChunkLoadStorage(com.minecolonies.api.util.ChunkLoadStorage) Chunk(net.minecraft.world.chunk.Chunk) UpdateChunkCapabilityMessage(com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Aggregations

IColonyTagCapability (com.minecolonies.api.colony.IColonyTagCapability)29 Chunk (net.minecraft.world.chunk.Chunk)21 ChunkPos (net.minecraft.util.math.ChunkPos)7 ChunkLoadStorage (com.minecolonies.api.util.ChunkLoadStorage)6 UpdateChunkCapabilityMessage (com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage)6 BlockPos (net.minecraft.util.math.BlockPos)6 IChunkmanagerCapability (com.minecolonies.api.colony.IChunkmanagerCapability)4 PlacementSettings (com.ldtteam.structurize.util.PlacementSettings)3 LoadOnlyStructureHandler (com.minecolonies.api.util.LoadOnlyStructureHandler)3 World (net.minecraft.world.World)3 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 ClientChunkUpdatedEvent (com.minecolonies.api.colony.event.ClientChunkUpdatedEvent)2 UpdateChunkCapabilityMessage (com.minecolonies.coremod.network.messages.UpdateChunkCapabilityMessage)2 GameProfile (com.mojang.authlib.GameProfile)2 ClientWorld (net.minecraft.client.world.ClientWorld)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 IChunk (net.minecraft.world.chunk.IChunk)2 IColony (com.minecolonies.api.colony.IColony)1 ServerWorld (net.minecraft.world.server.ServerWorld)1 Nullable (org.jetbrains.annotations.Nullable)1