use of com.minecolonies.coremod.network.messages.client.UpdateChunkRangeCapabilityMessage 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);
}
}
}
}
}
}
}
use of com.minecolonies.coremod.network.messages.client.UpdateChunkRangeCapabilityMessage 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