Search in sources :

Example 21 with IItemHandlerModifiable

use of net.minecraftforge.items.IItemHandlerModifiable in project Tropicraft by Tropicraft.

the class BambooDoubleChestItemHandler method setStackInSlot.

@Override
public void setStackInSlot(int slot, @Nonnull ItemStack stack) {
    boolean accessingUpperChest = slot < 27;
    int targetSlot = accessingUpperChest ? slot : slot - 27;
    TileEntityBambooChest chest = getChest(accessingUpperChest);
    if (chest != null) {
        IItemHandler singleHandler = chest.getSingleChestHandler();
        if (singleHandler instanceof IItemHandlerModifiable) {
            ((IItemHandlerModifiable) singleHandler).setStackInSlot(targetSlot, stack);
        }
    }
    chest = getChest(!accessingUpperChest);
    if (chest != null)
        chest.markDirty();
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) IItemHandler(net.minecraftforge.items.IItemHandler) TileEntityBambooChest(net.tropicraft.core.common.block.tileentity.TileEntityBambooChest)

Example 22 with IItemHandlerModifiable

use of net.minecraftforge.items.IItemHandlerModifiable in project artisan-worktables by codetaylor.

the class ArtisanRecipe method onCraftReduceIngredients.

protected void onCraftReduceIngredients(ICraftingContext context) {
    IItemHandlerModifiable matrixHandler = context.getCraftingMatrixHandler();
    IFluidHandler fluidHandler = context.getFluidHandler();
    List<ItemStack> remainingItems = this.getRemainingItems(context, NonNullList.withSize(matrixHandler.getSlots(), ItemStack.EMPTY));
    for (int i = 0; i < remainingItems.size(); i++) {
        ItemStack itemStack = matrixHandler.getStackInSlot(i);
        ItemStack remainingItemStack = remainingItems.get(i);
        if (!itemStack.isEmpty()) {
            matrixHandler.setStackInSlot(i, Util.decrease(itemStack.copy(), 1, false));
            itemStack = matrixHandler.getStackInSlot(i);
        }
        if (!remainingItemStack.isEmpty()) {
            if (itemStack.isEmpty()) {
                matrixHandler.setStackInSlot(i, remainingItemStack);
            } else if (ItemStack.areItemsEqual(itemStack, remainingItemStack) && ItemStack.areItemStackTagsEqual(itemStack, remainingItemStack)) {
                int combinedStackSize = remainingItemStack.getCount() + itemStack.getCount();
                if (combinedStackSize > itemStack.getMaxStackSize()) {
                    // Special handling if combined stack size exceeds max stack size.
                    // First, set the existing slot to the maximum stack size. Next,
                    // loop while the combined stack size is larger than the max stack
                    // size and put the items into the player's inventory or on the
                    // ground. Finally, put the remaining items in the player's
                    // inventory or on the ground.
                    ItemStack copy = remainingItemStack.copy();
                    copy.setCount(itemStack.getMaxStackSize());
                    matrixHandler.setStackInSlot(i, copy);
                    combinedStackSize -= itemStack.getMaxStackSize();
                    while (combinedStackSize > itemStack.getMaxStackSize()) {
                        copy = remainingItemStack.copy();
                        copy.setCount(itemStack.getMaxStackSize());
                        if (!context.getPlayer().addItemStackToInventory(copy)) {
                            context.getPlayer().dropItem(copy, false);
                        }
                        combinedStackSize -= itemStack.getMaxStackSize();
                    }
                    remainingItemStack.grow(itemStack.getCount());
                    if (!context.getPlayer().addItemStackToInventory(remainingItemStack)) {
                        context.getPlayer().dropItem(remainingItemStack, false);
                    }
                } else {
                    remainingItemStack.grow(itemStack.getCount());
                    matrixHandler.setStackInSlot(i, remainingItemStack);
                }
            } else if (!context.getPlayer().addItemStackToInventory(remainingItemStack)) {
                context.getPlayer().dropItem(remainingItemStack, false);
            }
        }
    }
    FluidStack fluidIngredient = this.getFluidIngredient();
    if (fluidIngredient != null) {
        fluidHandler.drain(fluidIngredient, true);
    }
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.item.ItemStack) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 23 with IItemHandlerModifiable

use of net.minecraftforge.items.IItemHandlerModifiable in project artisan-worktables by codetaylor.

the class ArtisanRecipe method onCraftDamageTool.

protected void onCraftDamageTool(ICraftingContext context, int toolIndex) {
    World world = context.getWorld();
    EntityPlayer player = context.getPlayer();
    IItemHandlerModifiable toolHandler = context.getToolHandler();
    ItemStack itemStack = toolHandler.getStackInSlot(toolIndex);
    if (!itemStack.isEmpty() && this.isValidTool(itemStack, toolIndex)) {
        int itemDamage = itemStack.getMetadata() + this.getToolDamage(toolIndex);
        if (itemDamage >= itemStack.getItem().getMaxDamage(itemStack)) {
            toolHandler.setStackInSlot(toolIndex, ItemStack.EMPTY);
            if (!world.isRemote) {
                world.playSound(player, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ITEM_BREAK, SoundCategory.PLAYERS, 1.0f, 1.0f);
            }
        } else {
            ItemStack copy = itemStack.copy();
            copy.setItemDamage(itemDamage);
            toolHandler.setStackInSlot(toolIndex, copy);
        }
    }
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) EntityPlayer(net.minecraft.entity.player.EntityPlayer) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack)

Example 24 with IItemHandlerModifiable

use of net.minecraftforge.items.IItemHandlerModifiable in project artisan-worktables by codetaylor.

the class ArtisanRecipe method onCraftReduceSecondaryIngredients.

protected void onCraftReduceSecondaryIngredients(ICraftingContext context) {
    IItemHandlerModifiable secondaryIngredientHandler = context.getSecondaryIngredientHandler();
    if (!this.consumeSecondaryIngredients() || secondaryIngredientHandler == null) {
        return;
    }
    List<IArtisanIngredient> secondaryIngredients = this.secondaryIngredients;
    if (!secondaryIngredients.isEmpty()) {
        for (IArtisanIngredient requiredIngredient : secondaryIngredients) {
            int requiredAmount = requiredIngredient.getAmount();
            int slotCount = secondaryIngredientHandler.getSlots();
            for (int i = 0; i < slotCount; i++) {
                ItemStack stackInSlot = secondaryIngredientHandler.getStackInSlot(i);
                if (stackInSlot.isEmpty()) {
                    continue;
                }
                if (requiredIngredient.matchesIgnoreAmount(stackInSlot)) {
                    // get the remaining secondary item
                    ItemStack remainingItemStack = this.getRemainingSecondaryItem(context, requiredIngredient, stackInSlot);
                    int existingStackCount = stackInSlot.getCount();
                    if (existingStackCount <= requiredAmount) {
                        if (remainingItemStack.isEmpty()) {
                            // If the remaining item is empty, empty the slot.
                            secondaryIngredientHandler.setStackInSlot(i, ItemStack.EMPTY);
                        } else {
                            // First, empty the slot. Next, increase the remaining item count to match
                            // the slot's previous stack count. Finally, attempt to insert the remaining
                            // item into the secondary ingredient slots spilling any overflow into the
                            // player's inventory or onto the ground.
                            secondaryIngredientHandler.setStackInSlot(i, ItemStack.EMPTY);
                            remainingItemStack.setCount(existingStackCount);
                            ItemStack itemStack = secondaryIngredientHandler.insertItem(i, remainingItemStack, false);
                            for (int j = 0; !itemStack.isEmpty() && j < secondaryIngredientHandler.getSlots() && j != i; j++) {
                                itemStack = secondaryIngredientHandler.insertItem(j, itemStack, false);
                            }
                            if (!itemStack.isEmpty() && !context.getPlayer().addItemStackToInventory(itemStack)) {
                                context.getPlayer().dropItem(itemStack, false);
                            }
                        }
                        requiredAmount -= existingStackCount;
                    } else if (existingStackCount > requiredAmount) {
                        // Replace partial stack in slot with remaining item.
                        // First, decrease the existing stack size. Next, since we know that the
                        // existing stack has been decreased by the required amount, we increase
                        // the remaining item stack count by the required amount. Finally, attempt
                        // to insert the remaining item stack into the secondary ingredient slots
                        // spilling any overflow into the player's inventory or onto the ground.
                        ItemStack decreasedStack = Util.decrease(stackInSlot.copy(), requiredAmount, false);
                        secondaryIngredientHandler.setStackInSlot(i, decreasedStack);
                        if (!remainingItemStack.isEmpty()) {
                            remainingItemStack.setCount(requiredAmount);
                            ItemStack itemStack = secondaryIngredientHandler.insertItem(i, remainingItemStack, false);
                            for (int j = 0; !itemStack.isEmpty() && j < secondaryIngredientHandler.getSlots() && j != i; j++) {
                                itemStack = secondaryIngredientHandler.insertItem(j, itemStack, false);
                            }
                            if (!itemStack.isEmpty() && !context.getPlayer().addItemStackToInventory(itemStack)) {
                                context.getPlayer().dropItem(itemStack, false);
                            }
                        }
                        requiredAmount = 0;
                    }
                    if (requiredAmount == 0) {
                        break;
                    }
                }
            }
            if (requiredAmount > 0) {
            // TODO: failed to find all required ingredients... shouldn't happen if the matching code is correct
            }
        }
    }
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) ItemStack(net.minecraft.item.ItemStack)

Example 25 with IItemHandlerModifiable

use of net.minecraftforge.items.IItemHandlerModifiable in project artisan-worktables by codetaylor.

the class ArtisanRecipe method onCraftCheckAndReplaceTool.

protected void onCraftCheckAndReplaceTool(ICraftingContext context, int toolIndex) {
    IItemHandlerModifiable toolHandler = context.getToolHandler();
    ItemStack itemStack = toolHandler.getStackInSlot(toolIndex);
    if (!this.hasSufficientToolDurability(itemStack, toolIndex)) {
        // Tool needs to be replaced
        IItemHandler capability = context.getToolReplacementHandler();
        if (capability == null) {
            return;
        }
        int slotCount = capability.getSlots();
        for (int i = 0; i < slotCount; i++) {
            ItemStack potentialTool = capability.getStackInSlot(i);
            if (potentialTool.isEmpty()) {
                continue;
            }
            if (this.isValidTool(potentialTool, toolIndex) && this.hasSufficientToolDurability(potentialTool, toolIndex)) {
                // Found an acceptable tool
                potentialTool = capability.extractItem(i, 1, false);
                capability.insertItem(i, toolHandler.getStackInSlot(toolIndex), false);
                toolHandler.setStackInSlot(toolIndex, potentialTool);
            }
        }
    }
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) IItemHandler(net.minecraftforge.items.IItemHandler) ItemStack(net.minecraft.item.ItemStack)

Aggregations

IItemHandlerModifiable (net.minecraftforge.items.IItemHandlerModifiable)39 ItemStack (net.minecraft.item.ItemStack)15 IItemHandler (net.minecraftforge.items.IItemHandler)9 IElectricItem (gregtech.api.capability.IElectricItem)6 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)5 NBTTagList (net.minecraft.nbt.NBTTagList)5 Nonnull (javax.annotation.Nonnull)4 TileEntity (net.minecraft.tileentity.TileEntity)4 INBTSerializable (net.minecraftforge.common.util.INBTSerializable)4 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)3 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 NBTTagInt (net.minecraft.nbt.NBTTagInt)2 EnumFacing (net.minecraft.util.EnumFacing)2 World (net.minecraft.world.World)2 EnumPipePart (buildcraft.api.core.EnumPipePart)1 IItemHandlerFiltered (buildcraft.api.inventory.IItemHandlerFiltered)1 IPhantomSlot (buildcraft.lib.gui.slot.IPhantomSlot)1 SlotPhantom (buildcraft.lib.gui.slot.SlotPhantom)1 IItemHandlerAdv (buildcraft.lib.tile.item.IItemHandlerAdv)1 ImmutableCollection (com.google.common.collect.ImmutableCollection)1