Search in sources :

Example 16 with IColonyTagCapability

use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.

the class ChunkDataHelper method addStorageToChunk.

/**
 * Add a chunk storage to a chunk.
 *
 * @param chunk   the chunk to add it to.
 * @param storage the said storage.
 */
public static void addStorageToChunk(final Chunk chunk, final ChunkLoadStorage storage) {
    final IColonyTagCapability cap = chunk.getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
    storage.applyToCap(cap, chunk);
    if (cap != null) {
        Network.getNetwork().sendToEveryone(new UpdateChunkCapabilityMessage(cap, chunk.getPos().x, chunk.getPos().z));
    }
}
Also used : UpdateChunkCapabilityMessage(com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 17 with IColonyTagCapability

use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.

the class ChunkDataHelper method canClaimChunksInRange.

/**
 * Check if all chunks within a certain range can be claimed, if range is too big this might require to load chunks. Use carefully.
 * <p>
 * --- This is only for dynamic claiming ---
 *
 * @param w     the world.
 * @param pos   the center position.
 * @param range the range to check.
 * @return true if possible.
 */
public static boolean canClaimChunksInRange(final World w, final BlockPos pos, final int range) {
    final IChunkmanagerCapability worldCapability = w.getCapability(CHUNK_STORAGE_UPDATE_CAP, null).resolve().orElse(null);
    if (worldCapability == null) {
        return true;
    }
    final Chunk centralChunk = w.getChunkAt(pos);
    final int chunkX = centralChunk.getPos().x;
    final int chunkZ = centralChunk.getPos().z;
    for (int i = chunkX - range; i <= chunkX + range; i++) {
        for (int j = chunkZ - range; j <= chunkZ + range; j++) {
            final Chunk chunk = w.getChunk(i, j);
            final IColonyTagCapability colonyCap = chunk.getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
            if (colonyCap == null) {
                return true;
            }
            final ChunkLoadStorage storage = worldCapability.getChunkStorage(chunk.getPos().x, chunk.getPos().z);
            if (storage != null) {
                storage.applyToCap(colonyCap, chunk);
            }
            if (colonyCap.getOwningColony() != 0) {
                return false;
            }
        }
    }
    return true;
}
Also used : IChunkmanagerCapability(com.minecolonies.api.colony.IChunkmanagerCapability) ChunkLoadStorage(com.minecolonies.api.util.ChunkLoadStorage) Chunk(net.minecraft.world.chunk.Chunk) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 18 with IColonyTagCapability

use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.

the class Tree method checkIfInColonyAndNotInBuilding.

/**
 * Calculates with a colony if the position is inside the colony and if it is inside a building.
 *
 * @param pos    the position.
 * @param colony the colony.
 * @param world  the world to use
 * @return return false if not inside the colony or if inside a building.
 */
public static boolean checkIfInColonyAndNotInBuilding(final BlockPos pos, final IColony colony, final IWorldReader world) {
    final IChunk chunk = world.getChunk(pos);
    if (!(chunk instanceof Chunk)) {
        return false;
    }
    final IColonyTagCapability cap = ((Chunk) chunk).getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
    if (cap != null && cap.getOwningColony() != colony.getID()) {
        return false;
    }
    // Dynamic tree's are never part of buildings
    if (Compatibility.isDynamicBlock(world.getBlockState(pos).getBlock())) {
        return true;
    }
    for (final IBuilding building : colony.getBuildingManager().getBuildings().values()) {
        if (building.isInBuilding(pos)) {
            return false;
        }
    }
    return true;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IChunk(net.minecraft.world.chunk.IChunk) Chunk(net.minecraft.world.chunk.Chunk) IChunk(net.minecraft.world.chunk.IChunk) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 19 with IColonyTagCapability

use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.

the class ColonyManager method claimColonyChunks.

/**
 * Notify all chunks in the range of the colony about the colony.
 * @param world the world of the colony.
 * @param add remove or add
 */
public static void claimColonyChunks(final World world, final boolean add, final int id, final BlockPos center, final int dimension) {
    final Chunk centralChunk = world.getChunkFromBlockCoords(center);
    if (centralChunk.getCapability(CLOSE_COLONY_CAP, null).getOwningColony() == id && add) {
        return;
    }
    final IColonyTagCapability cap = centralChunk.getCapability(CLOSE_COLONY_CAP, null);
    if (add) {
        cap.setOwningColony(id);
        cap.addColony(id);
    } else {
        cap.removeColony(id);
    }
    centralChunk.markDirty();
    MineColonies.getNetwork().sendToAll(new UpdateChunkCapabilityMessage(centralChunk.getCapability(CLOSE_COLONY_CAP, null), centralChunk.x, centralChunk.z));
    final int chunkX = centralChunk.x;
    final int chunkZ = centralChunk.z;
    final int range = Configurations.gameplay.workingRangeTownHallChunks;
    final int buffer = Configurations.gameplay.townHallPaddingChunk;
    claimChunksInRange(id, dimension, add, chunkX, chunkZ, range, buffer);
}
Also used : Chunk(net.minecraft.world.chunk.Chunk) UpdateChunkCapabilityMessage(com.minecolonies.coremod.network.messages.UpdateChunkCapabilityMessage) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 20 with IColonyTagCapability

use of com.minecolonies.api.colony.IColonyTagCapability in project minecolonies by Minecolonies.

the class UpdateChunkCapabilityMessage method onMessage.

@Nullable
@Override
public IMessage onMessage(@NotNull final UpdateChunkCapabilityMessage message, final MessageContext ctx) {
    if (ctx.getClientHandler().world != null) {
        final Chunk chunk = ctx.getClientHandler().world.getChunkFromChunkCoords(message.x, message.z);
        final IColonyTagCapability cap = chunk.getCapability(CLOSE_COLONY_CAP, null);
        cap.reset();
        cap.setOwningColony(message.owningColonyId);
        for (final int id : message.closeColonies) {
            cap.addColony(id);
        }
    }
    return null;
}
Also used : Chunk(net.minecraft.world.chunk.Chunk) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability) Nullable(org.jetbrains.annotations.Nullable)

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