Search in sources :

Example 86 with IBuilding

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

the class RaidManager method getRandomBuilding.

@Override
@NotNull
public BlockPos getRandomBuilding() {
    buildingPosUsage++;
    if (buildingPosUsage > 3 || lastBuilding == null) {
        buildingPosUsage = 0;
        final Collection<IBuilding> buildingList = colony.getBuildingManager().getBuildings().values();
        final Object[] buildingArray = buildingList.toArray();
        if (buildingArray.length != 0) {
            final int rand = colony.getWorld().random.nextInt(buildingArray.length);
            final IBuilding building = (IBuilding) buildingArray[rand];
            lastBuilding = building.getPosition();
        } else {
            lastBuilding = colony.getCenter();
        }
    }
    return lastBuilding;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with IBuilding

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

the class RegisteredStructureManager method removeBuilding.

@Override
public void removeBuilding(@NotNull final IBuilding building, final Set<ServerPlayerEntity> subscribers) {
    if (buildings.containsKey(building.getID())) {
        final ImmutableMap.Builder<BlockPos, IBuilding> builder = new ImmutableMap.Builder<>();
        for (final IBuilding tbuilding : buildings.values()) {
            if (tbuilding != building) {
                builder.put(tbuilding.getID(), tbuilding);
            }
        }
        buildings = builder.build();
        for (final ServerPlayerEntity player : subscribers) {
            Network.getNetwork().sendToPlayer(new ColonyViewRemoveBuildingMessage(colony, building.getID()), player);
        }
        Log.getLogger().info(String.format("Colony %d - removed AbstractBuilding %s of type %s", colony.getID(), building.getID(), building.getSchematicName()));
    }
    if (building instanceof BuildingTownHall) {
        townHall = null;
    } else if (building instanceof BuildingWareHouse) {
        wareHouses.remove(building);
    } else if (building instanceof BuildingMysticalSite) {
        mysticalSites.remove(building);
    }
    // Allow Citizens to fix up any data that wasn't fixed up by the AbstractBuilding's own onDestroyed
    for (@NotNull final ICitizenData citizen : colony.getCitizenManager().getCitizens()) {
        citizen.onRemoveBuilding(building);
        building.cancelAllRequestsOfCitizen(citizen);
    }
    colony.getRequestManager().onProviderRemovedFromColony(building);
    colony.getRequestManager().onRequesterRemovedFromColony(building.getRequester());
    colony.getCitizenManager().calculateMaxCitizens();
}
Also used : BuildingMysticalSite(com.minecolonies.coremod.colony.buildings.BuildingMysticalSite) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ICitizenData(com.minecolonies.api.colony.ICitizenData) BlockPos(net.minecraft.util.math.BlockPos) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ColonyViewRemoveBuildingMessage(com.minecolonies.coremod.network.messages.client.colony.ColonyViewRemoveBuildingMessage) NotNull(org.jetbrains.annotations.NotNull) ImmutableMap(com.google.common.collect.ImmutableMap) BuildingTownHall(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingTownHall) BuildingWareHouse(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingWareHouse)

Example 88 with IBuilding

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

the class RegisteredStructureManager method getBestBuilding.

@Override
public BlockPos getBestBuilding(final BlockPos citizen, final Class<? extends IBuilding> clazz) {
    double distance = Double.MAX_VALUE;
    BlockPos goodCook = null;
    for (final IBuilding building : buildings.values()) {
        if (clazz.isInstance(building) && building.getBuildingLevel() > 0) {
            final double localDistance = building.getPosition().distSqr(citizen);
            if (localDistance < distance) {
                distance = localDistance;
                goodCook = building.getPosition();
            }
        }
    }
    return goodCook;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) BlockPos(net.minecraft.util.math.BlockPos)

Example 89 with IBuilding

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

the class RegisteredStructureManager 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 90 with IBuilding

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

the class RegisteredStructureManager method cleanUpBuildings.

@Override
public void cleanUpBuildings(@NotNull final IColony colony) {
    @Nullable final List<IBuilding> removedBuildings = new ArrayList<>();
    // Need this list, we may enter here while we add a building in the real world.
    final List<IBuilding> tempBuildings = new ArrayList<>(buildings.values());
    for (@NotNull final IBuilding building : tempBuildings) {
        final BlockPos loc = building.getPosition();
        if (WorldUtil.isBlockLoaded(colony.getWorld(), loc) && !building.isMatchingBlock(colony.getWorld().getBlockState(loc).getBlock())) {
            // Sanity cleanup
            removedBuildings.add(building);
        }
    }
    @NotNull final ArrayList<BlockPos> tempFields = new ArrayList<>(fields);
    for (@NotNull final BlockPos pos : tempFields) {
        if (WorldUtil.isBlockLoaded(colony.getWorld(), pos)) {
            if (!(colony.getWorld().getBlockEntity(pos) instanceof ScarecrowTileEntity)) {
                removeField(pos);
            }
        }
    }
    for (@NotNull final BlockPos pos : leisureSites) {
        if (WorldUtil.isBlockLoaded(colony.getWorld(), pos)) {
            if (!(colony.getWorld().getBlockEntity(pos) instanceof TileEntityDecorationController)) {
                removeLeisureSite(pos);
            }
        }
    }
    if (!removedBuildings.isEmpty() && removedBuildings.size() >= buildings.values().size()) {
        Log.getLogger().warn("Colony:" + colony.getID() + " is removing all buildings at once. Did you just load a backup? If not there is a chance that colony data got corrupted and you want to restore a backup.");
    }
    removedBuildings.forEach(IBuilding::destroy);
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) TileEntityDecorationController(com.minecolonies.coremod.tileentities.TileEntityDecorationController) BlockPos(net.minecraft.util.math.BlockPos) ScarecrowTileEntity(com.minecolonies.coremod.tileentities.ScarecrowTileEntity) AbstractScarecrowTileEntity(com.minecolonies.api.tileentities.AbstractScarecrowTileEntity) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

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