use of com.minecolonies.api.colony.IChunkmanagerCapability in project minecolonies by ldtteam.
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()) {
LanguageHandler.sendPlayerMessage((PlayerEntity) sender, "com.minecolonies.command.claim.toolarge", colonyID);
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) {
LanguageHandler.sendPlayerMessage((PlayerEntity) sender, "com.minecolonies.command.claim.maxchunks");
return 0;
}
ChunkDataHelper.claimChunksInRange(colonyID, context.getSource().getLevel().dimension(), add, new BlockPos(sender.position()), range, 0, sender.level);
if (add) {
LanguageHandler.sendPlayerMessage((PlayerEntity) sender, "com.minecolonies.command.claim.success");
} else {
LanguageHandler.sendPlayerMessage((PlayerEntity) sender, "com.minecolonies.command.claim.unclaim");
}
return 1;
}
use of com.minecolonies.api.colony.IChunkmanagerCapability 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;
}
use of com.minecolonies.api.colony.IChunkmanagerCapability 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());
}
}
use of com.minecolonies.api.colony.IChunkmanagerCapability 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);
}
}
}
use of com.minecolonies.api.colony.IChunkmanagerCapability in project minecolonies by ldtteam.
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());
}
}
}
}
Aggregations