use of com.minecolonies.api.entity.ai.citizen.guards.GuardGear in project minecolonies by Minecolonies.
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) {
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);
}
}
use of com.minecolonies.api.entity.ai.citizen.guards.GuardGear in project minecolonies by Minecolonies.
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() {
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());
}
}
}
}
use of com.minecolonies.api.entity.ai.citizen.guards.GuardGear in project minecolonies by Minecolonies.
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() {
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();
}
Aggregations