Search in sources :

Example 1 with GuardGear

use of com.minecolonies.api.entity.ai.citizen.guards.GuardGear in project minecolonies by ldtteam.

the class AbstractEntityAIFight method atBuildingActions.

/**
 * Task to do when at the own building, as guards only go there on requests and on dump
 */
protected void atBuildingActions() {
    final IGuardBuilding building = getOwnBuilding();
    for (final List<GuardGear> itemList : itemsNeeded) {
        for (final GuardGear item : itemList) {
            if (!(building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired())) {
                continue;
            }
            if (item.getItemNeeded() == ToolType.SHIELD && worker.getCitizenColonyHandler().getColony().getResearchManager().getResearchEffects().getEffectStrength(SHIELD_USAGE) <= 0) {
                continue;
            }
            int bestSlot = -1;
            int bestLevel = -1;
            IItemHandler bestHandler = null;
            if (!ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType()))) {
                bestLevel = ItemStackUtils.getMiningLevel(worker.getItemBySlot(item.getType()), item.getItemNeeded());
            }
            final Map<IItemHandler, List<Integer>> items = InventoryUtils.findAllSlotsInProviderWith(building, item::test);
            if (items.isEmpty()) {
                // None found, check for equipped
                if (ItemStackUtils.isEmpty(worker.getItemBySlot(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(worker.getItemBySlot(item.getType()))) {
                    final int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), stack -> stack == worker.getItemBySlot(item.getType()));
                    if (slot > -1) {
                        InventoryUtils.transferItemStackIntoNextFreeSlotInProvider(worker.getInventoryCitizen(), slot, building);
                    }
                }
                // Used for further comparisons, set to the right inventory slot afterwards
                worker.setItemSlot(item.getType(), bestHandler.getStackInSlot(bestSlot));
                InventoryUtils.transferItemStackIntoNextFreeSlotInItemHandler(bestHandler, bestSlot, worker.getInventoryCitizen());
            }
        }
    }
    equipInventoryArmor();
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) IItemHandler(net.minecraftforge.items.IItemHandler) GuardGear(com.minecolonies.api.entity.ai.citizen.guards.GuardGear) ArrayList(java.util.ArrayList) List(java.util.List) ItemStack(net.minecraft.item.ItemStack) Map(java.util.Map)

Example 2 with GuardGear

use of com.minecolonies.api.entity.ai.citizen.guards.GuardGear in project minecolonies by ldtteam.

the class AbstractEntityAIFight method equipInventoryArmor.

/**
 * Equips armor existing in inventory
 */
public void equipInventoryArmor() {
    cleanArmor();
    final IGuardBuilding building = getOwnBuilding();
    for (final List<GuardGear> itemList : itemsNeeded) {
        for (final GuardGear item : itemList) {
            if (ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType())) && building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired()) {
                int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), item::test);
                if (slot > -1) {
                    worker.setItemSlot(item.getType(), worker.getInventoryCitizen().getStackInSlot(slot));
                }
            }
        }
    }
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) GuardGear(com.minecolonies.api.entity.ai.citizen.guards.GuardGear)

Example 3 with GuardGear

use of com.minecolonies.api.entity.ai.citizen.guards.GuardGear 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 4 with GuardGear

use of com.minecolonies.api.entity.ai.citizen.guards.GuardGear 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)

Example 5 with GuardGear

use of com.minecolonies.api.entity.ai.citizen.guards.GuardGear in project minecolonies by Minecolonies.

the class AbstractEntityAIFight method equipInventoryArmor.

/**
 * Equips armor existing in inventory
 */
public void equipInventoryArmor() {
    cleanArmor();
    final IGuardBuilding building = getOwnBuilding();
    for (final List<GuardGear> itemList : itemsNeeded) {
        for (final GuardGear item : itemList) {
            if (ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType())) && building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired()) {
                int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), item::test);
                if (slot > -1) {
                    worker.setItemSlot(item.getType(), worker.getInventoryCitizen().getStackInSlot(slot));
                }
            }
        }
    }
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) GuardGear(com.minecolonies.api.entity.ai.citizen.guards.GuardGear)

Aggregations

GuardGear (com.minecolonies.api.entity.ai.citizen.guards.GuardGear)8 net.minecraft.item (net.minecraft.item)4 IItemHandler (net.minecraftforge.items.IItemHandler)4 IGuardBuilding (com.minecolonies.api.colony.buildings.IGuardBuilding)3 ImmutableList (com.google.common.collect.ImmutableList)2 StackList (com.minecolonies.api.colony.requestsystem.requestable.StackList)2 BuildingNetherWorker (com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingNetherWorker)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 ItemStack (net.minecraft.item.ItemStack)2