Search in sources :

Example 1 with UpdateChunkCapabilityMessage

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

Example 2 with UpdateChunkCapabilityMessage

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;
}
Also used : 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 3 with UpdateChunkCapabilityMessage

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;
}
Also used : 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 4 with UpdateChunkCapabilityMessage

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);
                        }
                    }
                }
            }
        }
    }
}
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 5 with UpdateChunkCapabilityMessage

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;
}
Also used : IColony(com.minecolonies.api.colony.IColony) 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)

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