Search in sources :

Example 1 with UpdateChunkRangeCapabilityMessage

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);
                        }
                    }
                }
            }
        }
    }
}
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)

Example 2 with UpdateChunkRangeCapabilityMessage

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);
                        }
                    }
                }
            }
        }
    }
}
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

IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 UpdateChunkCapabilityMessage (com.minecolonies.coremod.network.messages.client.UpdateChunkCapabilityMessage)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 World (net.minecraft.world.World)2 Chunk (net.minecraft.world.chunk.Chunk)2 ServerWorld (net.minecraft.world.server.ServerWorld)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2