Search in sources :

Example 16 with InvWrapper

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);
        }
    }
}
Also used : InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) ItemArmor(net.minecraft.item.ItemArmor) ItemStack(net.minecraft.item.ItemStack)

Example 17 with InvWrapper

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;
    }
}
Also used : InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) IPlantable(net.minecraftforge.common.IPlantable) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with InvWrapper

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;
}
Also used : InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) ItemStack(net.minecraft.item.ItemStack) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 19 with InvWrapper

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;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with InvWrapper

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();
}
Also used : InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable) ItemStorage(com.minecolonies.coremod.entity.ai.item.handling.ItemStorage)

Aggregations

InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)22 ItemStack (net.minecraft.item.ItemStack)15 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 InventoryCitizen (com.minecolonies.coremod.inventory.InventoryCitizen)4 TileEntity (net.minecraft.tileentity.TileEntity)4 TileEntityChest (net.minecraft.tileentity.TileEntityChest)4 BlockPos (net.minecraft.util.math.BlockPos)4 AbstractBuilding (com.minecolonies.coremod.colony.buildings.AbstractBuilding)3 Block (net.minecraft.block.Block)3 ItemFood (net.minecraft.item.ItemFood)3 AbstractBuildingWorker (com.minecolonies.coremod.colony.buildings.AbstractBuildingWorker)2 BuildingBuilderResource (com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource)2 ItemStorage (com.minecolonies.coremod.entity.ai.item.handling.ItemStorage)2 TileEntityColonyBuilding (com.minecolonies.coremod.tileentities.TileEntityColonyBuilding)2 ArrayList (java.util.ArrayList)2 IBlockState (net.minecraft.block.state.IBlockState)2 ItemArmor (net.minecraft.item.ItemArmor)2 com.minecolonies.api.util (com.minecolonies.api.util)1 InventoryUtils (com.minecolonies.api.util.InventoryUtils)1