Search in sources :

Example 1 with ChunkLoadStorage

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

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 2 with ChunkLoadStorage

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

the class ChunkDataHelper method claimChunksInRange.

/**
 * Claim a number of chunks in a certain range around a position. Prevents the initial chunkradius from beeing unclaimed, unless forced.
 *
 * @param colony the colony to claim for
 * @param add    if claim or unclaim.
 * @param range  the range.
 * @param center the center position to be claimed.
 * @param force  whether to ignore restrictions.
 */
public static void claimChunksInRange(final IColony colony, final boolean add, final int range, final BlockPos center, final boolean force) {
    final World world = colony.getWorld();
    final int colonyId = colony.getID();
    final RegistryKey<World> dimension = colony.getDimension();
    boolean areAllChunksAdded = true;
    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 chunkColonyCenterX = colony.getCenter().getX() >> 4;
    final int chunkColonyCenterZ = colony.getCenter().getZ() >> 4;
    final BlockPos colonyCenterCompare = new BlockPos(colony.getCenter().getX(), 0, colony.getCenter().getZ());
    final int chunkX = center.getX() >> 4;
    final int chunkZ = center.getZ() >> 4;
    final int initialColonySize = getConfig().getServer().initialColonySize.get();
    final int maxColonySize = getConfig().getServer().maxColonySize.get();
    for (int i = chunkX - range; i <= chunkX + range; i++) {
        for (int j = chunkZ - range; j <= chunkZ + range; j++) {
            // TODO: move out from for loops
            if (!force && !add && (Math.abs(chunkColonyCenterX - i) <= initialColonySize + 1 && Math.abs(chunkColonyCenterZ - j) <= initialColonySize + 1)) {
                Log.getLogger().debug("Unclaim of initial chunk prevented");
                continue;
            }
            final BlockPos pos = new BlockPos(i * BLOCKS_PER_CHUNK, 0, j * BLOCKS_PER_CHUNK);
            if (!force && maxColonySize != 0 && pos.distSqr(colonyCenterCompare) > Math.pow(maxColonySize * BLOCKS_PER_CHUNK, 2)) {
                Log.getLogger().debug("Tried to claim chunk at pos X:" + pos.getX() + " Z:" + pos.getZ() + " too far away from the colony:" + colony.getID() + " center:" + colony.getCenter() + " max is config workingRangeTownHall ^2");
                continue;
            }
            if (loadChunkAndAddData(world, pos, add, colonyId, center, chunkManager)) {
                continue;
            }
            areAllChunksAdded = false;
            @NotNull final ChunkLoadStorage newStorage = new ChunkLoadStorage(colonyId, ChunkPos.asLong(i, j), dimension.location(), center);
            chunkManager.addChunkStorage(i, j, newStorage);
        }
    }
    if (areAllChunksAdded && add && range > 0) {
        final IBuilding building = colony.getBuildingManager().getBuilding(center);
        sendPlayersMessage(colony.getImportantMessageEntityPlayers(), COLONY_SIZE_CHANGE, range, building.getSchematicName());
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IChunkmanagerCapability(com.minecolonies.api.colony.IChunkmanagerCapability) BlockPos(net.minecraft.util.math.BlockPos) ChunkLoadStorage(com.minecolonies.api.util.ChunkLoadStorage) World(net.minecraft.world.World) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ChunkLoadStorage

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

the class ChunkDataHelper method claimChunksInRange.

/**
 * Claim a number of chunks in a certain range around a position.
 *
 * @param colonyId  the colony id.
 * @param dimension the dimension.
 * @param add       if claim or unclaim.
 * @param center    the center position to be claimed.
 * @param range     the range.
 * @param buffer    the buffer.
 * @param world     the world.
 */
public static void claimChunksInRange(final int colonyId, final RegistryKey<World> dimension, final boolean add, final BlockPos center, 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 Chunk centralChunk = world.getChunkAt(center);
    loadChunkAndAddData(world, center, add, colonyId, chunkManager);
    final int chunkX = centralChunk.getPos().x;
    final int chunkZ = centralChunk.getPos().z;
    final int maxRange = range * 2 + buffer;
    for (int i = chunkX - maxRange; i <= chunkX + maxRange; i++) {
        for (int j = chunkZ - maxRange; j <= chunkZ + maxRange; j++) {
            if (i == chunkX && j == chunkZ) {
                continue;
            }
            if (i >= chunkX - DISTANCE_TO_LOAD_IMMEDIATELY && j >= chunkZ - DISTANCE_TO_LOAD_IMMEDIATELY && i <= chunkX + DISTANCE_TO_LOAD_IMMEDIATELY && j <= chunkZ + DISTANCE_TO_LOAD_IMMEDIATELY && loadChunkAndAddData(world, new BlockPos(i * BLOCKS_PER_CHUNK, 0, j * BLOCKS_PER_CHUNK), add, colonyId, chunkManager)) {
                continue;
            }
            final boolean owning = i >= chunkX - range && j >= chunkZ - range && i <= chunkX + range && j <= chunkZ + range;
            @NotNull final ChunkLoadStorage newStorage = new ChunkLoadStorage(colonyId, ChunkPos.asLong(i, j), add, dimension.location(), owning);
            chunkManager.addChunkStorage(i, j, newStorage);
        }
    }
}
Also used : IChunkmanagerCapability(com.minecolonies.api.colony.IChunkmanagerCapability) BlockPos(net.minecraft.util.math.BlockPos) ChunkLoadStorage(com.minecolonies.api.util.ChunkLoadStorage) Chunk(net.minecraft.world.chunk.Chunk) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ChunkLoadStorage

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

the class ChunkDataHelper method loadChunkAndAddData.

/**
 * Add the data to the chunk directly for dynamic claiming.
 * <p>
 * ----- Only for dynamic claiming -----
 *
 * @param world        the world.
 * @param pos          the position.
 * @param add          if add or delete.
 * @param id           the id.
 * @param buildingPos  the building pos.
 * @param chunkManager the chunk manager capability.
 * @return true if successful.
 */
public static boolean loadChunkAndAddData(final World world, final BlockPos pos, final boolean add, final int id, final BlockPos buildingPos, final IChunkmanagerCapability chunkManager) {
    if (!WorldUtil.isBlockLoaded(world, pos)) {
        return false;
    }
    final Chunk chunk = world.getChunkAt(pos);
    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(id, buildingPos, chunk);
    } else {
        cap.removeBuildingClaim(id, buildingPos, chunk);
    }
    Network.getNetwork().sendToEveryone(new UpdateChunkCapabilityMessage(cap, chunk.getPos().x, chunk.getPos().z));
    return true;
}
Also used : 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 5 with ChunkLoadStorage

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

the class ChunkDataHelper method loadChunkAndAddData.

/**
 * Add the data to the chunk directly for dynamic claiming.
 * <p>
 * ----- Only for dynamic claiming -----
 *
 * @param world        the world.
 * @param pos          the position.
 * @param add          if add or delete.
 * @param id           the id.
 * @param buildingPos  the building pos.
 * @param chunkManager the chunk manager capability.
 * @return true if successful.
 */
public static boolean loadChunkAndAddData(final World world, final BlockPos pos, final boolean add, final int id, final BlockPos buildingPos, final IChunkmanagerCapability chunkManager) {
    if (!WorldUtil.isBlockLoaded(world, pos)) {
        return false;
    }
    final Chunk chunk = world.getChunkAt(pos);
    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(id, buildingPos, chunk);
    } else {
        cap.removeBuildingClaim(id, buildingPos, chunk);
    }
    Network.getNetwork().sendToEveryone(new UpdateChunkCapabilityMessage(cap, chunk.getPos().x, chunk.getPos().z));
    return true;
}
Also used : 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

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