Search in sources :

Example 6 with UpdateChunkCapabilityMessage

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));
    }
}
Also used : UpdateChunkCapabilityMessage(com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 7 with UpdateChunkCapabilityMessage

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;
}
Also used : ChunkPos(net.minecraft.util.math.ChunkPos) ChunkLoadStorage(com.minecolonies.api.util.ChunkLoadStorage) Chunk(net.minecraft.world.chunk.Chunk) UpdateChunkCapabilityMessage(com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 8 with UpdateChunkCapabilityMessage

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);
                        }
                    }
                }
            }
        }
    }
}
Also used : MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) LivingEntity(net.minecraft.entity.LivingEntity) TileEntity(net.minecraft.tileentity.TileEntity) ZombieVillagerEntity(net.minecraft.entity.monster.ZombieVillagerEntity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ClientPlayerEntity(net.minecraft.client.entity.player.ClientPlayerEntity) MobEntity(net.minecraft.entity.MobEntity) Entity(net.minecraft.entity.Entity) Set(java.util.Set) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) World(net.minecraft.world.World) ClientWorld(net.minecraft.client.world.ClientWorld) ServerWorld(net.minecraft.world.server.ServerWorld) Chunk(net.minecraft.world.chunk.Chunk) NotNull(org.jetbrains.annotations.NotNull) UpdateChunkRangeCapabilityMessage(com.minecolonies.coremod.network.messages.client.UpdateChunkRangeCapabilityMessage) BlockPos(net.minecraft.util.math.BlockPos) UpdateChunkCapabilityMessage(com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage) Map(java.util.Map) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

UpdateChunkCapabilityMessage (com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage)8 IColonyTagCapability (com.minecolonies.api.colony.IColonyTagCapability)6 Chunk (net.minecraft.world.chunk.Chunk)6 ChunkLoadStorage (com.minecolonies.api.util.ChunkLoadStorage)4 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 UpdateChunkRangeCapabilityMessage (com.minecolonies.coremod.network.messages.client.UpdateChunkRangeCapabilityMessage)2 Map (java.util.Map)2 Set (java.util.Set)2 ClientPlayerEntity (net.minecraft.client.entity.player.ClientPlayerEntity)2 ClientWorld (net.minecraft.client.world.ClientWorld)2 Entity (net.minecraft.entity.Entity)2 LivingEntity (net.minecraft.entity.LivingEntity)2 MobEntity (net.minecraft.entity.MobEntity)2 ZombieVillagerEntity (net.minecraft.entity.monster.ZombieVillagerEntity)2 PlayerEntity (net.minecraft.entity.player.PlayerEntity)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 MobSpawnerTileEntity (net.minecraft.tileentity.MobSpawnerTileEntity)2 TileEntity (net.minecraft.tileentity.TileEntity)2 BlockPos (net.minecraft.util.math.BlockPos)2 ChunkPos (net.minecraft.util.math.ChunkPos)2