Search in sources :

Example 51 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.

the class BuildingManager method guardBuildingChangedAt.

@Override
public void guardBuildingChangedAt(final IBuilding guardBuilding, final int newLevel) {
    final int claimRadius = guardBuilding.getClaimRadius(Math.max(guardBuilding.getBuildingLevel(), newLevel));
    final MutableBoundingBox guardedRegion = BlockPosUtil.getChunkAlignedBB(guardBuilding.getPosition(), claimRadius);
    for (final IBuilding building : getBuildings().values()) {
        if (guardedRegion.isInside(building.getPosition())) {
            building.resetGuardBuildingNear();
        }
    }
}
Also used : MutableBoundingBox(net.minecraft.util.math.MutableBoundingBox) IBuilding(com.minecolonies.api.colony.buildings.IBuilding)

Example 52 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.

the class BuildingManager method sendFieldPackets.

/**
 * Sends packages to update the fields.
 *
 * @param closeSubscribers the current event subscribers.
 * @param newSubscribers   the new event subscribers.
 */
private void sendFieldPackets(final Set<ServerPlayerEntity> closeSubscribers, final Set<ServerPlayerEntity> newSubscribers) {
    if (isFieldsDirty || !newSubscribers.isEmpty()) {
        final Set<ServerPlayerEntity> players = new HashSet<>();
        if (isFieldsDirty) {
            players.addAll(closeSubscribers);
        }
        players.addAll(newSubscribers);
        for (final IBuilding building : buildings.values()) {
            if (building instanceof BuildingFarmer) {
                players.forEach(player -> Network.getNetwork().sendToPlayer(new ColonyViewBuildingViewMessage(building), player));
            }
        }
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) BuildingFarmer(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingFarmer) ColonyViewBuildingViewMessage(com.minecolonies.coremod.network.messages.client.colony.ColonyViewBuildingViewMessage)

Example 53 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.

the class ChunkDataHelper method claimChunksInRange.

/**
 * Claim a number of chunks in a certain range around a position. Prevents the initial chunkradius from beeing unclaimed, unless forced.
 *
 * @param colony the colony to claim for
 * @param add    if claim or unclaim.
 * @param range  the range.
 * @param center the center position to be claimed.
 * @param force  whether to ignore restrictions.
 */
public static void claimChunksInRange(final IColony colony, final boolean add, final int range, final BlockPos center, final boolean force) {
    final World world = colony.getWorld();
    final int colonyId = colony.getID();
    final RegistryKey<World> dimension = colony.getDimension();
    boolean areAllChunksAdded = true;
    final IChunkmanagerCapability chunkManager = world.getCapability(CHUNK_STORAGE_UPDATE_CAP, null).resolve().orElse(null);
    if (chunkManager == null) {
        Log.getLogger().error(UNABLE_TO_FIND_WORLD_CAP_TEXT, new Exception());
        return;
    }
    final int chunkColonyCenterX = colony.getCenter().getX() >> 4;
    final int chunkColonyCenterZ = colony.getCenter().getZ() >> 4;
    final BlockPos colonyCenterCompare = new BlockPos(colony.getCenter().getX(), 0, colony.getCenter().getZ());
    final int chunkX = center.getX() >> 4;
    final int chunkZ = center.getZ() >> 4;
    final int initialColonySize = getConfig().getServer().initialColonySize.get();
    final int maxColonySize = getConfig().getServer().maxColonySize.get();
    for (int i = chunkX - range; i <= chunkX + range; i++) {
        for (int j = chunkZ - range; j <= chunkZ + range; j++) {
            // TODO: move out from for loops
            if (!force && !add && (Math.abs(chunkColonyCenterX - i) <= initialColonySize + 1 && Math.abs(chunkColonyCenterZ - j) <= initialColonySize + 1)) {
                Log.getLogger().debug("Unclaim of initial chunk prevented");
                continue;
            }
            final BlockPos pos = new BlockPos(i * BLOCKS_PER_CHUNK, 0, j * BLOCKS_PER_CHUNK);
            if (!force && maxColonySize != 0 && pos.distSqr(colonyCenterCompare) > Math.pow(maxColonySize * BLOCKS_PER_CHUNK, 2)) {
                Log.getLogger().debug("Tried to claim chunk at pos X:" + pos.getX() + " Z:" + pos.getZ() + " too far away from the colony:" + colony.getID() + " center:" + colony.getCenter() + " max is config workingRangeTownHall ^2");
                continue;
            }
            if (loadChunkAndAddData(world, pos, add, colonyId, center, chunkManager)) {
                continue;
            }
            areAllChunksAdded = false;
            @NotNull final ChunkLoadStorage newStorage = new ChunkLoadStorage(colonyId, ChunkPos.asLong(i, j), dimension.location(), center);
            chunkManager.addChunkStorage(i, j, newStorage);
        }
    }
    if (areAllChunksAdded && add && range > 0) {
        final IBuilding building = colony.getBuildingManager().getBuilding(center);
        sendPlayersMessage(colony.getImportantMessageEntityPlayers(), COLONY_SIZE_CHANGE, range, building.getSchematicName());
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IChunkmanagerCapability(com.minecolonies.api.colony.IChunkmanagerCapability) BlockPos(net.minecraft.util.math.BlockPos) ChunkLoadStorage(com.minecolonies.api.util.ChunkLoadStorage) World(net.minecraft.world.World) NotNull(org.jetbrains.annotations.NotNull)

Example 54 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.

the class BuildingEntry method produceBuilding.

public IBuilding produceBuilding(final BlockPos position, final IColony colony) {
    final IBuilding building = buildingProducer.apply(colony, position);
    for (final Supplier<IBuildingModule> module : buildingModuleProducers) {
        building.registerModule(module.get().setBuilding(building));
    }
    building.setBuildingType(this);
    return building;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IBuildingModule(com.minecolonies.api.colony.buildings.modules.IBuildingModule)

Example 55 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding 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)

Aggregations

IBuilding (com.minecolonies.api.colony.buildings.IBuilding)187 BlockPos (net.minecraft.util.math.BlockPos)71 NotNull (org.jetbrains.annotations.NotNull)45 IColony (com.minecolonies.api.colony.IColony)37 Nullable (org.jetbrains.annotations.Nullable)26 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)24 ICitizenData (com.minecolonies.api.colony.ICitizenData)22 World (net.minecraft.world.World)20 ItemStack (net.minecraft.item.ItemStack)19 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)19 TileEntity (net.minecraft.tileentity.TileEntity)17 CompoundNBT (net.minecraft.nbt.CompoundNBT)15 ArrayList (java.util.ArrayList)14 AbstractBuildingGuards (com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards)10 ItemStorage (com.minecolonies.api.crafting.ItemStorage)9 ResourceLocation (net.minecraft.util.ResourceLocation)9 IItemHandler (net.minecraftforge.items.IItemHandler)9 InventoryUtils (com.minecolonies.api.util.InventoryUtils)8 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)8 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)8