Search in sources :

Example 21 with IBuilding

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

the class AbstractEntityAIBasic method dumpOneMoreSlot.

/**
 * Dumps one inventory slot into the building chest.
 *
 * @return true if is has to dump more.
 */
@SuppressWarnings("PMD.PrematureDeclaration")
private boolean dumpOneMoreSlot() {
    if (walkToBlock(getBuildingToDump().getPosition())) {
        return true;
    }
    @Nullable final IBuilding buildingWorker = getOwnBuilding();
    ItemStack stackToDump = worker.getInventoryCitizen().getStackInSlot(slotAt);
    final int totalSize = worker.getInventoryCitizen().getSlots();
    while (stackToDump.isEmpty()) {
        if (slotAt >= totalSize) {
            return false;
        }
        slotAt++;
        stackToDump = worker.getInventoryCitizen().getStackInSlot(slotAt);
    }
    boolean dumpAnyway = false;
    if (slotAt + MIN_OPEN_SLOTS * 2 >= totalSize) {
        final long openSlots = InventoryUtils.openSlotCount(worker.getInventoryCitizen());
        if (openSlots < MIN_OPEN_SLOTS * 2) {
            if (stackToDump.getCount() < CHANCE_TO_DUMP_50) {
                dumpAnyway = worker.getRandom().nextBoolean();
            } else {
                dumpAnyway = worker.getRandom().nextInt(stackToDump.getCount()) < CHANCE_TO_DUMP;
            }
        }
    }
    if (buildingWorker != null && !ItemStackUtils.isEmpty(stackToDump)) {
        final int amount = dumpAnyway ? stackToDump.getCount() : buildingWorker.buildingRequiresCertainAmountOfItem(stackToDump, alreadyKept, true);
        if (amount > 0) {
            final ItemStack activeStack = getInventory().extractItem(slotAt, amount, false);
            InventoryUtils.transferItemStackIntoNextBestSlotInItemHandler(activeStack, getBuildingToDump().getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null));
            hasDumpedItems = true;
        }
    }
    slotAt++;
    return slotAt < totalSize;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with IBuilding

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

the class BuildingBarracks method registerBlockPosition.

@Override
public void registerBlockPosition(@NotNull final BlockState block, @NotNull final BlockPos pos, @NotNull final World world) {
    super.registerBlockPosition(block, pos, world);
    if (block.getBlock() == ModBlocks.blockHutBarracksTower) {
        final IBuilding building = getColony().getBuildingManager().getBuilding(pos);
        if (building instanceof BuildingBarracksTower) {
            building.setStyle(this.getStyle());
            ((BuildingBarracksTower) building).addBarracks(getPosition());
            if (!towers.contains(pos)) {
                towers.add(pos);
            }
        }
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding)

Example 23 with IBuilding

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

the class BuildingBarracksTower method onUpgradeComplete.

@Override
public void onUpgradeComplete(final int newLevel) {
    super.onUpgradeComplete(newLevel);
    final IBuilding barrack = colony.getBuildingManager().getBuilding(barracks);
    if (barrack == null) {
        return;
    }
    ChunkDataHelper.claimColonyChunks(colony, true, barracks, barrack.getClaimRadius(newLevel));
    if (newLevel == barrack.getMaxBuildingLevel()) {
        boolean allUpgraded = true;
        for (BlockPos tower : ((BuildingBarracks) barrack).getTowers()) {
            Log.getLogger().info("Upgraded: " + allUpgraded);
            if (colony.getBuildingManager().getBuilding(tower).getBuildingLevel() != barrack.getMaxBuildingLevel())
                allUpgraded = false;
        }
        if (allUpgraded)
            AdvancementUtils.TriggerAdvancementPlayersForColony(colony, AdvancementTriggers.ALL_TOWERS::trigger);
    }
}
Also used : AdvancementTriggers(com.minecolonies.api.advancements.AdvancementTriggers) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) BlockPos(net.minecraft.util.math.BlockPos)

Example 24 with IBuilding

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

the class AbstractEntityAIStructureWithWorkOrder method executeSpecificCompleteActions.

@Override
public void executeSpecificCompleteActions() {
    if (job.getBlueprint() == null && job.hasWorkOrder()) {
        // fix for bad structures
        job.complete();
    }
    if (job.getBlueprint() == null) {
        return;
    }
    final String structureName = job.getBlueprint().getName();
    final WorkOrderBuildDecoration wo = job.getWorkOrder();
    if (wo instanceof WorkOrderBuildBuilding) {
        sendCompletionMessage(wo);
        WorkOrderBuild wob = (WorkOrderBuild) wo;
        String buildingName = wo.getStructureName();
        buildingName = buildingName.substring(buildingName.indexOf('/') + 1, buildingName.lastIndexOf('/')) + " " + buildingName.substring(buildingName.lastIndexOf('/') + 1, buildingName.lastIndexOf(String.valueOf(wob.getUpgradeLevel())));
        job.getColony().getEventDescriptionManager().addEventDescription(wob.getUpgradeLevel() > 1 ? new BuildingUpgradedEvent(wo.getSchematicLocation(), buildingName, wob.getUpgradeLevel()) : new BuildingBuiltEvent(wo.getSchematicLocation(), buildingName, wob.getUpgradeLevel()));
    } else if (wo instanceof WorkOrderBuildRemoval) {
        worker.getCitizenChatHandler().sendLocalizedChat(COM_MINECOLONIES_COREMOD_ENTITY_BUILDER_DECONSTRUCTION_COMPLETE, structureName);
        WorkOrderBuild wob = (WorkOrderBuild) wo;
        String buildingName = wo.getStructureName();
        buildingName = buildingName.substring(buildingName.indexOf('/') + 1, buildingName.lastIndexOf('/')) + " " + buildingName.substring(buildingName.lastIndexOf('/') + 1, buildingName.indexOf(String.valueOf(wob.getUpgradeLevel())));
        job.getColony().getEventDescriptionManager().addEventDescription(new BuildingDeconstructedEvent(wo.getSchematicLocation(), buildingName, wob.getUpgradeLevel()));
    } else {
        sendCompletionMessage(wo);
    }
    if (wo == null) {
        Log.getLogger().error(String.format("Worker (%d:%d) ERROR - Finished, but missing work order(%d)", worker.getCitizenColonyHandler().getColony().getID(), worker.getCitizenData().getId(), job.getWorkOrderId()));
    } else {
        job.complete();
        if (wo instanceof WorkOrderBuildBuilding) {
            final IBuilding building = job.getColony().getBuildingManager().getBuilding(wo.getSchematicLocation());
            if (building == null) {
                Log.getLogger().error(String.format("Builder (%d:%d) ERROR - Finished, but missing building(%s)", worker.getCitizenColonyHandler().getColony().getID(), worker.getCitizenData().getId(), wo.getSchematicLocation()));
            } else {
                // Normally levels are done through the schematic data, but incase it is missing we do it manually here.
                final TileEntity te = worker.level.getBlockEntity(building.getID());
                if (te instanceof AbstractTileEntityColonyBuilding && ((IBlueprintDataProvider) te).getSchematicName().isEmpty()) {
                    building.onUpgradeComplete(((WorkOrderBuildBuilding) wo).getUpgradeLevel());
                    building.setBuildingLevel(((WorkOrderBuildBuilding) wo).getUpgradeLevel());
                }
            }
        } else if (wo instanceof WorkOrderBuildRemoval) {
            final IBuilding building = job.getColony().getBuildingManager().getBuilding(wo.getSchematicLocation());
            if (building == null) {
                Log.getLogger().error(String.format("Builder (%d:%d) ERROR - Finished, but missing building(%s)", worker.getCitizenColonyHandler().getColony().getID(), worker.getCitizenData().getId(), wo.getSchematicLocation()));
            } else {
                building.setDeconstructed();
            }
        }
    }
    getOwnBuilding().resetNeededResources();
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) BuildingDeconstructedEvent(com.minecolonies.coremod.colony.colonyEvents.buildingEvents.BuildingDeconstructedEvent) BuildingBuiltEvent(com.minecolonies.coremod.colony.colonyEvents.buildingEvents.BuildingBuiltEvent) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IBlueprintDataProvider(com.ldtteam.structurize.blocks.interfaces.IBlueprintDataProvider) BuildingUpgradedEvent(com.minecolonies.coremod.colony.colonyEvents.buildingEvents.BuildingUpgradedEvent)

Example 25 with IBuilding

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

the class AbstractEntityAIStructureWithWorkOrder method loadRequirements.

/**
 * Takes the existing workorder, loads the structure and tests the worker order if it is valid.
 */
@Override
public IAIState loadRequirements() {
    if (!job.hasBlueprint() || structurePlacer == null) {
        loadStructure();
        final WorkOrderBuildDecoration wo = job.getWorkOrder();
        if (wo == null) {
            Log.getLogger().error(String.format("Worker (%d:%d) ERROR - Starting and missing work order(%d)", worker.getCitizenColonyHandler().getColony().getID(), worker.getCitizenData().getId(), job.getWorkOrderId()), new Exception());
            job.setWorkOrder(null);
            return IDLE;
        }
        if (wo instanceof WorkOrderBuildBuilding) {
            final IBuilding building = job.getColony().getBuildingManager().getBuilding(wo.getSchematicLocation());
            if (building == null) {
                Log.getLogger().error(String.format("Worker (%d:%d) ERROR - Starting and missing building(%s)", worker.getCitizenColonyHandler().getColony().getID(), worker.getCitizenData().getId(), wo.getSchematicLocation()), new Exception());
                return IDLE;
            }
            worker.getCitizenChatHandler().sendLocalizedChat(COM_MINECOLONIES_COREMOD_ENTITY_BUILDER_BUILDSTART, job.getWorkOrder().getDisplayName());
            // Don't go through the CLEAR stage for repairs and upgrades
            if (building.getBuildingLevel() > 0) {
                wo.setCleared(true);
            }
        } else if (!(wo instanceof WorkOrderBuildMiner)) {
            worker.getCitizenChatHandler().sendLocalizedChat(COM_MINECOLONIES_COREMOD_ENTITY_BUILDER_BUILDSTART, wo.getDisplayName());
        }
        return getState();
    }
    if (job.getWorkOrder().isRequested()) {
        return afterStructureLoading();
    }
    // We need to deal with materials
    requestMaterialsState();
    return getState();
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding)

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