Search in sources :

Example 11 with ChunkLoadStorage

use of com.minecolonies.api.util.ChunkLoadStorage in project minecolonies by Minecolonies.

the class ChunkDataHelper method tryClaimBuilding.

/**
 * Add the data to the chunk directly for dynamic claiming.
 * <p>
 * ----- Only for dynamic claiming -----
 *
 * @param world        the world.
 * @param chunkBlockPos          the position.
 * @param add          if add or delete.
 * @param colony           the colony.
 * @param buildingPos  the building pos.
 * @param chunkManager the chunk manager capability.
 * @return true if successful.
 */
public static boolean tryClaimBuilding(final World world, final BlockPos chunkBlockPos, final boolean add, final IColony colony, final BlockPos buildingPos, final IChunkmanagerCapability chunkManager) {
    if (!WorldUtil.isBlockLoaded(world, chunkBlockPos)) {
        final ChunkLoadStorage newStorage = new ChunkLoadStorage(colony.getID(), new ChunkPos(chunkBlockPos).toLong(), world.dimension().location(), buildingPos, add);
        chunkManager.addChunkStorage(SectionPos.blockToSectionCoord(chunkBlockPos.getX()), SectionPos.blockToSectionCoord(chunkBlockPos.getZ()), newStorage);
        return false;
    }
    final Chunk chunk = world.getChunkAt(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.addBuildingClaim(colony.getID(), buildingPos, chunk);
    } else {
        cap.removeBuildingClaim(colony.getID(), buildingPos, chunk);
    }
    Network.getNetwork().sendToTrackingChunk(new UpdateChunkCapabilityMessage(cap, chunk.getPos().x, chunk.getPos().z), chunk);
    return true;
}
Also used : 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)

Example 12 with ChunkLoadStorage

use of com.minecolonies.api.util.ChunkLoadStorage in project minecolonies by ldtteam.

the class ChunkDataHelper method loadChunk.

/**
 * Load the colony info for a certain chunk.
 *
 * @param chunk the chunk.
 * @param world the worldg to.
 */
public static void loadChunk(final Chunk chunk, final World world) {
    // If colony is farther away from a capability then this times the default colony distance it will delete the capability.
    final int distanceToDelete = MineColonies.getConfig().getServer().maxColonySize.get() * BLOCKS_PER_CHUNK * 2 * 5;
    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;
    }
    if (!chunkManager.getAllChunkStorages().isEmpty()) {
        final IColonyManagerCapability cap = world.getCapability(COLONY_MANAGER_CAP, null).resolve().orElse(null);
        if (cap == null) {
            return;
        }
        final ChunkLoadStorage existingStorage = chunkManager.getChunkStorage(chunk.getPos().x, chunk.getPos().z);
        if (existingStorage != null) {
            addStorageToChunk(chunk, existingStorage);
        }
    }
    final int closeColony = chunk.getCapability(CLOSE_COLONY_CAP, null).map(IColonyTagCapability::getOwningColony).orElse(0);
    if (closeColony != 0) {
        final IColony colony = IColonyManager.getInstance().getColonyByDimension(closeColony, world.dimension());
        if (colony != null) {
            colony.addLoadedChunk(ChunkPos.asLong(chunk.getPos().x, chunk.getPos().z), chunk);
        }
    }
}
Also used : IColonyManagerCapability(com.minecolonies.coremod.colony.IColonyManagerCapability) IChunkmanagerCapability(com.minecolonies.api.colony.IChunkmanagerCapability) IColony(com.minecolonies.api.colony.IColony) ChunkLoadStorage(com.minecolonies.api.util.ChunkLoadStorage)

Aggregations

ChunkLoadStorage (com.minecolonies.api.util.ChunkLoadStorage)12 IChunkmanagerCapability (com.minecolonies.api.colony.IChunkmanagerCapability)8 Chunk (net.minecraft.world.chunk.Chunk)8 IColonyTagCapability (com.minecolonies.api.colony.IColonyTagCapability)6 UpdateChunkCapabilityMessage (com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage)4 BlockPos (net.minecraft.util.math.BlockPos)4 NotNull (org.jetbrains.annotations.NotNull)4 IColony (com.minecolonies.api.colony.IColony)3 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 IColonyManagerCapability (com.minecolonies.coremod.colony.IColonyManagerCapability)2 ChunkPos (net.minecraft.util.math.ChunkPos)2 World (net.minecraft.world.World)2