use of com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage in project minecolonies by ldtteam.
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 ldtteam.
the class ChunkDataHelper method loadChunkAndAddData.
/**
* Add the data to the chunk directly for dynamic claiming.
* <p>
* ----- Only for dynamic claiming -----
*
* @param world the world.
* @param pos the position.
* @param add if add or delete.
* @param id the id.
* @param buildingPos the building pos.
* @param chunkManager the chunk manager capability.
* @return true if successful.
*/
public static boolean loadChunkAndAddData(final World world, final BlockPos pos, final boolean add, final int id, final BlockPos buildingPos, final IChunkmanagerCapability chunkManager) {
if (!WorldUtil.isBlockLoaded(world, pos)) {
return false;
}
final Chunk chunk = world.getChunkAt(pos);
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(id, buildingPos, chunk);
} else {
cap.removeBuildingClaim(id, buildingPos, chunk);
}
Network.getNetwork().sendToEveryone(new UpdateChunkCapabilityMessage(cap, chunk.getPos().x, chunk.getPos().z));
return true;
}
use of com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage in project minecolonies by Minecolonies.
the class ChunkDataHelper method loadChunkAndAddData.
/**
* Add the data to the chunk directly for dynamic claiming.
* <p>
* ----- Only for dynamic claiming -----
*
* @param world the world.
* @param pos the position.
* @param add if add or delete.
* @param id the id.
* @param buildingPos the building pos.
* @param chunkManager the chunk manager capability.
* @return true if successful.
*/
public static boolean loadChunkAndAddData(final World world, final BlockPos pos, final boolean add, final int id, final BlockPos buildingPos, final IChunkmanagerCapability chunkManager) {
if (!WorldUtil.isBlockLoaded(world, pos)) {
return false;
}
final Chunk chunk = world.getChunkAt(pos);
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(id, buildingPos, chunk);
} else {
cap.removeBuildingClaim(id, buildingPos, chunk);
}
Network.getNetwork().sendToEveryone(new UpdateChunkCapabilityMessage(cap, chunk.getPos().x, chunk.getPos().z));
return true;
}
use of com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage 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.UpdateChunkCapabilityMessage in project minecolonies by Minecolonies.
the class ChunkDataHelper method tryClaim.
/**
* Add the data to the chunk directly.
*
* @param world the world.
* @param chunkBlockPos the position.
* @param add if add or delete.
* @param id the id.
* @param chunkManager the chunk manager capability.
* @return true if successful.
*/
public static boolean tryClaim(final World world, final BlockPos chunkBlockPos, final boolean add, final int id, final IChunkmanagerCapability chunkManager, boolean forceOwnerChange) {
if (!WorldUtil.isBlockLoaded(world, chunkBlockPos)) {
final ChunkLoadStorage newStorage = new ChunkLoadStorage(id, new ChunkPos(chunkBlockPos).toLong(), add, world.dimension().location(), forceOwnerChange);
chunkManager.addChunkStorage(SectionPos.blockToSectionCoord(chunkBlockPos.getX()), SectionPos.blockToSectionCoord(chunkBlockPos.getX()), newStorage);
return false;
}
final Chunk chunk = (Chunk) world.getChunk(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.addColony(id, chunk);
if (forceOwnerChange) {
cap.setOwningColony(id, chunk);
final IColony colony = IColonyManager.getInstance().getColonyByDimension(id, world.dimension());
if (colony != null) {
colony.addLoadedChunk(ChunkPos.asLong(chunk.getPos().x, chunk.getPos().z), chunk);
}
}
} else {
cap.removeColony(id, chunk);
}
Network.getNetwork().sendToTrackingChunk(new UpdateChunkCapabilityMessage(cap, chunk.getPos().x, chunk.getPos().z), chunk);
return true;
}
Aggregations