use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class AbstractEntityAIGuard method updateArmor.
/**
* Updates the equipment. Always take the first item of each type and set it.
*/
protected void updateArmor() {
worker.setItemStackToSlot(EntityEquipmentSlot.CHEST, ItemStack.EMPTY);
worker.setItemStackToSlot(EntityEquipmentSlot.FEET, ItemStack.EMPTY);
worker.setItemStackToSlot(EntityEquipmentSlot.HEAD, ItemStack.EMPTY);
worker.setItemStackToSlot(EntityEquipmentSlot.LEGS, ItemStack.EMPTY);
for (int i = 0; i < new InvWrapper(worker.getInventoryCitizen()).getSlots(); i++) {
final ItemStack stack = worker.getInventoryCitizen().getStackInSlot(i);
if (InventoryUtils.isItemStackEmpty(stack)) {
new InvWrapper(worker.getInventoryCitizen()).extractItem(i, Integer.MAX_VALUE, false);
continue;
}
if (stack.getItem() instanceof ItemArmor && worker.getItemStackFromSlot(((ItemArmor) stack.getItem()).armorType) == ItemStack.EMPTY) {
worker.setItemStackToSlot(((ItemArmor) stack.getItem()).armorType, stack);
}
}
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class EntityAIWorkFarmer method plantCrop.
/**
* Plants the crop at a given location.
*
* @param item the crop.
* @param position the location.
*/
private boolean plantCrop(final ItemStack item, @NotNull final BlockPos position) {
final int slot = worker.findFirstSlotInInventoryWith(item.getItem(), item.getItemDamage());
if (slot == -1) {
return false;
} else {
@NotNull final IPlantable seed = (IPlantable) item.getItem();
world.setBlockState(position.up(), seed.getPlant(world, position));
new InvWrapper(getInventory()).extractItem(slot, 1, false);
requestSeeds = false;
//Flag 1+2 is needed for updates
return true;
}
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class EntityAIWorkDeliveryman method deliver.
/**
* Deliver the items to the hut.
*
* @return the next state.
*/
private AIState deliver() {
final BuildingDeliveryman deliveryHut = (getOwnBuilding() instanceof BuildingDeliveryman) ? (BuildingDeliveryman) getOwnBuilding() : null;
final AbstractBuilding buildingToDeliver = deliveryHut == null ? null : deliveryHut.getBuildingToDeliver();
if (deliveryHut == null || buildingToDeliver == null) {
return START_WORKING;
}
if (!worker.isWorkerAtSiteWithMove(buildingToDeliver.getLocation(), MIN_DISTANCE_TO_WAREHOUSE)) {
return DELIVERY;
}
final InvWrapper workerInventory = new InvWrapper(worker.getInventoryCitizen());
for (int i = 0; i < new InvWrapper(worker.getInventoryCitizen()).getSlots(); i++) {
final ItemStack stack = workerInventory.extractItem(i, Integer.MAX_VALUE, false);
if (InventoryUtils.isItemStackEmpty(stack)) {
continue;
}
final ItemStack insertionResultStack = buildingToDeliver.forceTransferStack(stack, world);
if (!InventoryUtils.isItemStackEmpty(insertionResultStack)) {
if (ItemStack.areItemStacksEqual(insertionResultStack, stack)) {
//same stack, we could not deliver ?
if (buildingToDeliver instanceof AbstractBuildingWorker) {
chatSpamFilter.talkWithoutSpam(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_NAMEDCHESTFULL, ((AbstractBuildingWorker) buildingToDeliver).getWorker().getName());
} else {
chatSpamFilter.talkWithoutSpam(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL, new TextComponentString(" :" + buildingToDeliver.getSchematicName()));
}
}
//Insert the result back into the inventory so we do not loose it.
workerInventory.insertItem(i, insertionResultStack, false);
}
}
worker.addExperience(1.0D);
worker.setHeldItem(SLOT_HAND);
buildingToDeliver.setOnGoingDelivery(false);
deliveryHut.setBuildingToDeliver(null);
if (buildingToDeliver instanceof BuildingHome) {
((BuildingHome) buildingToDeliver).setFoodNeeded(false);
}
gatherTarget = buildingToDeliver.getLocation();
return GATHERING;
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class AbstractEntityAIStructure method decreaseInventory.
private boolean decreaseInventory(@NotNull final BlockPos pos, final Block block, @NotNull final IBlockState state) {
@NotNull final IBlockState stateToPlace = state;
//Move out of the way when placing blocks
if (MathHelper.floor(worker.posX) == pos.getX() && MathHelper.abs(pos.getY() - (int) worker.posY) <= 1 && MathHelper.floor(worker.posZ) == pos.getZ() && worker.getNavigator().noPath()) {
worker.getNavigator().moveAwayFromXYZ(pos, RUN_AWAY_SPEED, 1.0);
}
@NotNull final Block blockToPlace = block;
connectBlockToBuildingIfNecessary(blockToPlace, pos);
//It will crash at blocks like water which is actually free, we don't have to decrease the stacks we have.
if (isBlockFree(blockToPlace, blockToPlace.getMetaFromState(stateToPlace))) {
return true;
}
@Nullable final ItemStack stack = BlockUtils.getItemStackFromBlockState(stateToPlace);
if (InventoryUtils.isItemStackEmpty(stack)) {
Log.getLogger().error("Block causes NPE: " + stateToPlace.getBlock());
return false;
}
final List<ItemStack> itemList = new ArrayList<>();
itemList.add(stack);
itemList.addAll(getItemsFromTileEntity());
for (final ItemStack tempStack : itemList) {
if (!InventoryUtils.isItemStackEmpty(tempStack)) {
final int slot = worker.findFirstSlotInInventoryWith(tempStack.getItem(), tempStack.getItemDamage());
if (slot != -1) {
new InvWrapper(getInventory()).extractItem(slot, 1, false);
reduceNeededResources(tempStack);
}
}
}
if (Configurations.builderBuildBlockDelay > 0 && blockToPlace != Blocks.AIR) {
setDelay(Configurations.builderBuildBlockDelay);
}
return true;
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class AbstractEntityAIBasic method shouldDumpItem.
/**
* Checks if an item should be kept and deposits the rest into his chest.
*
* @param alreadyKept already kept items.
* @param shouldKeep items that should be kept.
* @param buildingWorker the building of the worker.
* @param stack the stack being analyzed.
* @param slot the iteration inside the inventory.
* @return true if should be dumped.
*/
private boolean shouldDumpItem(@NotNull final Map<ItemStorage, Integer> alreadyKept, @NotNull final Map<ItemStorage, Integer> shouldKeep, @NotNull final AbstractBuildingWorker buildingWorker, @NotNull final ItemStack stack, final int slot) {
@Nullable final ItemStack returnStack;
int amountToKeep = 0;
if (keptEnough(alreadyKept, shouldKeep, stack)) {
returnStack = InventoryUtils.addItemStackToProviderWithResult(buildingWorker.getTileEntity(), stack.copy());
} else {
final ItemStorage tempStorage = new ItemStorage(stack.getItem(), stack.getItemDamage(), stack.getCount(), false);
final ItemStack tempStack = handleKeepX(alreadyKept, shouldKeep, tempStorage);
if (InventoryUtils.isItemStackEmpty(tempStack)) {
return false;
}
amountToKeep = stack.getCount() - tempStorage.getAmount();
returnStack = InventoryUtils.addItemStackToProviderWithResult(buildingWorker.getTileEntity(), tempStack);
}
if (InventoryUtils.isItemStackEmpty(returnStack)) {
new InvWrapper(worker.getInventoryCitizen()).extractItem(slot, stack.getCount() - amountToKeep, false);
return amountToKeep == 0;
}
new InvWrapper(worker.getInventoryCitizen()).extractItem(slot, stack.getCount() - returnStack.getCount() - amountToKeep, false);
//Check that we are not inserting into a full inventory.
return stack.getCount() != returnStack.getCount();
}
Aggregations