use of com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage in project minecolonies by Minecolonies.
the class ChunkDataHelper method addStorageToChunk.
/**
* Add a chunk storage to a chunk.
*
* @param chunk the chunk to add it to.
* @param storage the said storage.
*/
public static void addStorageToChunk(final Chunk chunk, final ChunkLoadStorage storage) {
final IColonyTagCapability cap = chunk.getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
storage.applyToCap(cap, chunk);
if (cap != null) {
Network.getNetwork().sendToEveryone(new UpdateChunkCapabilityMessage(cap, chunk.getPos().x, chunk.getPos().z));
}
}
use of com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage 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;
}
use of com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage in project minecolonies by ldtteam.
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