use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class BuildingManager method guardBuildingChangedAt.
@Override
public void guardBuildingChangedAt(final IBuilding guardBuilding, final int newLevel) {
final int claimRadius = guardBuilding.getClaimRadius(Math.max(guardBuilding.getBuildingLevel(), newLevel));
final MutableBoundingBox guardedRegion = BlockPosUtil.getChunkAlignedBB(guardBuilding.getPosition(), claimRadius);
for (final IBuilding building : getBuildings().values()) {
if (guardedRegion.isInside(building.getPosition())) {
building.resetGuardBuildingNear();
}
}
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class BuildingManager method sendFieldPackets.
/**
* Sends packages to update the fields.
*
* @param closeSubscribers the current event subscribers.
* @param newSubscribers the new event subscribers.
*/
private void sendFieldPackets(final Set<ServerPlayerEntity> closeSubscribers, final Set<ServerPlayerEntity> newSubscribers) {
if (isFieldsDirty || !newSubscribers.isEmpty()) {
final Set<ServerPlayerEntity> players = new HashSet<>();
if (isFieldsDirty) {
players.addAll(closeSubscribers);
}
players.addAll(newSubscribers);
for (final IBuilding building : buildings.values()) {
if (building instanceof BuildingFarmer) {
players.forEach(player -> Network.getNetwork().sendToPlayer(new ColonyViewBuildingViewMessage(building), player));
}
}
}
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
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.buildings.IBuilding in project minecolonies by Minecolonies.
the class BuildingEntry method produceBuilding.
public IBuilding produceBuilding(final BlockPos position, final IColony colony) {
final IBuilding building = buildingProducer.apply(colony, position);
for (final Supplier<IBuildingModule> module : buildingModuleProducers) {
building.registerModule(module.get().setBuilding(building));
}
building.setBuildingType(this);
return building;
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class EventHandler method onEnteringChunk.
/**
* Event called when the player enters a new chunk.
*
* @param event the event.
*/
@SubscribeEvent
public static void onEnteringChunk(@NotNull final PlayerEvent.EnteringChunk event) {
final Entity entity = event.getEntity();
final BlockPos pos = new BlockPos(entity.position());
if (event.getOldChunkX() == 0 && event.getOldChunkZ() == 0 && pos.distSqr(BlockPos.ZERO) > 100 * 100) {
return;
}
// Add nearby players
if (entity instanceof ServerPlayerEntity) {
final World world = entity.getCommandSenderWorld();
final Chunk newChunk = world.getChunk(event.getNewChunkX(), event.getNewChunkZ());
ChunkDataHelper.loadChunk(newChunk, entity.level);
Network.getNetwork().sendToPlayer(new UpdateChunkRangeCapabilityMessage(world, event.getNewChunkX(), event.getNewChunkZ(), 8, true), (ServerPlayerEntity) event.getEntity());
final IColonyTagCapability newCloseColonies = newChunk.getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
if (newCloseColonies == null) {
return;
}
Network.getNetwork().sendToPlayer(new UpdateChunkCapabilityMessage(newCloseColonies, newChunk.getPos().x, newChunk.getPos().z), (ServerPlayerEntity) entity);
@NotNull final ServerPlayerEntity player = (ServerPlayerEntity) entity;
final Chunk oldChunk = world.getChunk(event.getOldChunkX(), event.getOldChunkZ());
final IColonyTagCapability oldCloseColonies = oldChunk.getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
if (oldCloseColonies == null) {
return;
}
// Check if we get into a differently claimed chunk
if (newCloseColonies.getOwningColony() != oldCloseColonies.getOwningColony()) {
// Remove visiting/subscriber from old colony
final IColony oldColony = IColonyManager.getInstance().getColonyByWorld(oldCloseColonies.getOwningColony(), world);
if (oldColony != null) {
oldColony.removeVisitingPlayer(player);
oldColony.getPackageManager().removeCloseSubscriber(player);
}
}
// Add visiting/subscriber to new colony
if (newCloseColonies.getOwningColony() != 0) {
final IColony newColony = IColonyManager.getInstance().getColonyByWorld(newCloseColonies.getOwningColony(), world);
if (newColony != null && !newColony.getPackageManager().getCloseSubscribers().contains(player)) {
newColony.addVisitingPlayer(player);
newColony.getPackageManager().addCloseSubscriber(player);
}
}
// Alert nearby buildings of close player
if (newCloseColonies.getOwningColony() != 0) {
for (final Map.Entry<Integer, Set<BlockPos>> entry : newCloseColonies.getAllClaimingBuildings().entrySet()) {
final IColony newColony = IColonyManager.getInstance().getColonyByWorld(entry.getKey(), world);
if (newColony != null) {
for (final BlockPos buildingPos : entry.getValue()) {
IBuilding building = newColony.getBuildingManager().getBuilding(buildingPos);
if (building != null) {
building.onPlayerEnterNearby(player);
}
}
}
}
}
}
}
Aggregations