Search in sources :

Example 1 with BuildingNetherWorker

use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingNetherWorker in project minecolonies by ldtteam.

the class EntityAIWorkNether method checkAndRequestArmor.

/**
 * Make sure we have all the needed adventuring supplies
 * This is very similar to the AbstractEntityAiFight "atBuildingActions"
 * But doesn't handle shields, and doesn't equip or leave equipped armor.
 */
protected void checkAndRequestArmor() {
    final BuildingNetherWorker building = getOwnBuilding();
    for (final List<GuardGear> itemList : itemsNeeded) {
        for (final GuardGear item : itemList) {
            if (!(building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired())) {
                continue;
            }
            int bestSlot = -1;
            int bestLevel = -1;
            IItemHandler bestHandler = null;
            if (virtualEquipmentSlots.containsKey(item.getType()) && !ItemStackUtils.isEmpty(virtualEquipmentSlots.get(item.getType()))) {
                bestLevel = ItemStackUtils.getMiningLevel(virtualEquipmentSlots.get(item.getType()), item.getItemNeeded());
            } else {
                int equipSlot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getItemHandlerCitizen(), item::test);
                if (equipSlot > -1) {
                    ItemStack invItem = worker.getItemHandlerCitizen().getStackInSlot(equipSlot);
                    if (!virtualEquipmentSlots.containsKey(item.getType()) || ItemStackUtils.isEmpty(virtualEquipmentSlots.get(item.getType()))) {
                        virtualEquipmentSlots.put(item.getType(), invItem);
                        bestLevel = ItemStackUtils.getMiningLevel(invItem, item.getItemNeeded());
                    }
                } else {
                    virtualEquipmentSlots.put(item.getType(), ItemStack.EMPTY);
                }
            }
            final Map<IItemHandler, List<Integer>> items = InventoryUtils.findAllSlotsInProviderWith(building, item::test);
            if (items.isEmpty()) {
                // None found, check for equipped
                if (ItemStackUtils.isEmpty(virtualEquipmentSlots.get(item.getType()))) {
                    // create request
                    checkForToolorWeaponASync(item.getItemNeeded(), item.getMinArmorLevel(), item.getMaxArmorLevel());
                }
            } else {
                // Compare levels
                for (Map.Entry<IItemHandler, List<Integer>> entry : items.entrySet()) {
                    for (final Integer slot : entry.getValue()) {
                        final ItemStack stack = entry.getKey().getStackInSlot(slot);
                        if (ItemStackUtils.isEmpty(stack)) {
                            continue;
                        }
                        int currentLevel = ItemStackUtils.getMiningLevel(stack, item.getItemNeeded());
                        if (currentLevel > bestLevel) {
                            bestLevel = currentLevel;
                            bestSlot = slot;
                            bestHandler = entry.getKey();
                        }
                    }
                }
            }
            // Transfer if needed
            if (bestHandler != null) {
                if (!ItemStackUtils.isEmpty(virtualEquipmentSlots.get(item.getType()))) {
                    final int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), stack -> stack == virtualEquipmentSlots.get(item.getType()));
                    if (slot > -1) {
                        InventoryUtils.transferItemStackIntoNextFreeSlotInProvider(worker.getInventoryCitizen(), slot, building);
                    }
                }
                // Used for further comparisons, set to the right inventory slot afterwards
                virtualEquipmentSlots.put(item.getType(), bestHandler.getStackInSlot(bestSlot));
                InventoryUtils.transferItemStackIntoNextFreeSlotInItemHandler(bestHandler, bestSlot, worker.getInventoryCitizen());
            }
        }
    }
}
Also used : BuildingNetherWorker(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingNetherWorker) net.minecraft.item(net.minecraft.item) IItemHandler(net.minecraftforge.items.IItemHandler) GuardGear(com.minecolonies.api.entity.ai.citizen.guards.GuardGear) StackList(com.minecolonies.api.colony.requestsystem.requestable.StackList) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with BuildingNetherWorker

use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingNetherWorker in project minecolonies by ldtteam.

the class EntityAIWorkNether method setEquipSlot.

/**
 * Equip or Un-equip armor etc.
 * @param equipSlot Slot to attempt to modify
 * @param equip true if equipping, false if clearing
 */
private void setEquipSlot(EquipmentSlotType equipSlot, boolean equip) {
    if (equip) {
        final BuildingNetherWorker building = getOwnBuilding();
        for (final List<GuardGear> itemList : itemsNeeded) {
            for (final GuardGear item : itemList) {
                if (ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType())) && item.getType().equals(equipSlot) && building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired()) {
                    final int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), item::test);
                    if (slot > -1) {
                        final ItemStack toBeEquipped = worker.getInventoryCitizen().getStackInSlot(slot);
                        worker.setItemSlot(item.getType(), toBeEquipped);
                        virtualEquipmentSlots.put(item.getType(), toBeEquipped);
                    }
                }
            }
        }
    } else {
        worker.setItemSlot(equipSlot, ItemStack.EMPTY);
        virtualEquipmentSlots.put(equipSlot, ItemStack.EMPTY);
    }
}
Also used : BuildingNetherWorker(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingNetherWorker) net.minecraft.item(net.minecraft.item) GuardGear(com.minecolonies.api.entity.ai.citizen.guards.GuardGear)

Aggregations

GuardGear (com.minecolonies.api.entity.ai.citizen.guards.GuardGear)2 BuildingNetherWorker (com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingNetherWorker)2 net.minecraft.item (net.minecraft.item)2 ImmutableList (com.google.common.collect.ImmutableList)1 StackList (com.minecolonies.api.colony.requestsystem.requestable.StackList)1 IItemHandler (net.minecraftforge.items.IItemHandler)1