Search in sources :

Example 11 with IChunkmanagerCapability

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

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 12 with IChunkmanagerCapability

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

the class ChunkDataHelper method buildingClaimInRange.

/**
 * 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 buildingClaimInRange(final IColony colony, final boolean add, final int range, final BlockPos center, final boolean force) {
    final World world = colony.getWorld();
    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 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 maxColonySize = getConfig().getServer().maxColonySize.get();
    for (int i = chunkX - range; i <= chunkX + range; i++) {
        for (int j = chunkZ - range; j <= chunkZ + range; j++) {
            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 (tryClaimBuilding(world, pos, add, colony, center, chunkManager)) {
                continue;
            }
        }
    }
    if (add && range > 0) {
        final IBuilding building = colony.getBuildingManager().getBuilding(center);
        MessageUtils.format(COLONY_SIZE_CHANGE, range, building.getSchematicName()).sendTo(colony).forManagers();
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IChunkmanagerCapability(com.minecolonies.api.colony.IChunkmanagerCapability) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World)

Example 13 with IChunkmanagerCapability

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

the class CommandClaimChunks method onExecute.

/**
 * What happens when the command is executed after preConditions are successful.
 *
 * @param context the context of the command execution
 */
@Override
public int onExecute(final CommandContext<CommandSource> context) {
    final Entity sender = context.getSource().getEntity();
    if (!(sender instanceof PlayerEntity)) {
        return 0;
    }
    // Colony
    final int colonyID = IntegerArgumentType.getInteger(context, COLONYID_ARG);
    // Range
    final int range = IntegerArgumentType.getInteger(context, RANGE_ARG);
    if (range > MineColonies.getConfig().getServer().maxColonySize.get()) {
        MessageUtils.format(CommandTranslationConstants.COMMAND_CLAIM_TOO_LARGE, colonyID).sendTo((PlayerEntity) sender);
        return 0;
    }
    // Added/removed
    final boolean add = BoolArgumentType.getBool(context, ADD_ARG);
    final IChunkmanagerCapability chunkManager = sender.level.getCapability(CHUNK_STORAGE_UPDATE_CAP, null).resolve().orElse(null);
    if (chunkManager == null) {
        Log.getLogger().error(UNABLE_TO_FIND_WORLD_CAP_TEXT, new Exception());
        return 0;
    }
    if (chunkManager.getAllChunkStorages().size() > CHUNKS_TO_CLAIM_THRESHOLD) {
        MessageUtils.format(CommandTranslationConstants.COMMAND_CLAIM_MAX_CHUNKS).sendTo((PlayerEntity) sender);
        return 0;
    }
    ChunkDataHelper.staticClaimInRange(colonyID, add, new BlockPos(sender.position()), range, sender.level, true);
    if (add) {
        MessageUtils.format(CommandTranslationConstants.COMMAND_CLAIM_SUCCESS).sendTo((PlayerEntity) sender);
    } else {
        MessageUtils.format(CommandTranslationConstants.COMMAND_CLAIM_REMOVE_CLAIM).sendTo((PlayerEntity) sender);
    }
    return 1;
}
Also used : Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) IChunkmanagerCapability(com.minecolonies.api.colony.IChunkmanagerCapability) BlockPos(net.minecraft.util.math.BlockPos) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 14 with IChunkmanagerCapability

use of com.minecolonies.api.colony.IChunkmanagerCapability 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

IChunkmanagerCapability (com.minecolonies.api.colony.IChunkmanagerCapability)14 BlockPos (net.minecraft.util.math.BlockPos)10 ChunkLoadStorage (com.minecolonies.api.util.ChunkLoadStorage)8 Chunk (net.minecraft.world.chunk.Chunk)7 IColonyTagCapability (com.minecolonies.api.colony.IColonyTagCapability)4 NotNull (org.jetbrains.annotations.NotNull)4 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)3 World (net.minecraft.world.World)3 IColony (com.minecolonies.api.colony.IColony)2 IColonyManagerCapability (com.minecolonies.coremod.colony.IColonyManagerCapability)2 Entity (net.minecraft.entity.Entity)2 PlayerEntity (net.minecraft.entity.player.PlayerEntity)2