Search in sources :

Example 1 with ItemInventoryBackpack

use of forestry.storage.inventory.ItemInventoryBackpack in project ForestryMC by ForestryMC.

the class ItemBackpack method tryStowing.

public static void tryStowing(EntityPlayer player, ItemStack backpackStack, ItemStack stack) {
    if (getMode(backpackStack) == BackpackMode.LOCKED) {
        return;
    }
    ItemBackpack backpack = (ItemBackpack) backpackStack.getItem();
    ItemInventory inventory = new ItemInventoryBackpack(player, backpack.getBackpackSize(), backpackStack);
    if (MinecraftForge.EVENT_BUS.post(new BackpackStowEvent(player, backpack.getDefinition(), inventory, stack))) {
        return;
    }
    if (stack.isEmpty()) {
        return;
    }
    IItemHandler itemHandler = inventory.getItemHandler();
    ItemHandlerInventoryManipulator manipulator = new ItemHandlerInventoryManipulator(itemHandler);
    ItemStack remainder = manipulator.addStack(stack);
    stack.setCount(remainder == null ? 0 : remainder.getCount());
}
Also used : BackpackStowEvent(forestry.api.storage.BackpackStowEvent) ItemInventory(forestry.core.inventory.ItemInventory) IItemHandler(net.minecraftforge.items.IItemHandler) ItemInventoryBackpack(forestry.storage.inventory.ItemInventoryBackpack) ItemStack(net.minecraft.item.ItemStack) ItemHandlerInventoryManipulator(forestry.core.inventory.ItemHandlerInventoryManipulator)

Example 2 with ItemInventoryBackpack

use of forestry.storage.inventory.ItemInventoryBackpack in project ForestryMC by ForestryMC.

the class ItemBackpack method evaluateTileHit.

private boolean evaluateTileHit(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side) {
    // Shift right-clicking on an inventory tile will attempt to transfer
    // items contained in the backpack
    IItemHandler inventory = getInventoryHit(world, pos, side);
    // Process only inventories
    if (inventory != null) {
        // Must have inventory slots
        if (inventory.getSlots() <= 0) {
            return true;
        }
        if (!world.isRemote) {
            // Create our own backpack inventory
            ItemInventoryBackpack backpackInventory = new ItemInventoryBackpack(player, getBackpackSize(), stack);
            BackpackMode mode = getMode(stack);
            if (mode == BackpackMode.RECEIVE) {
                receiveFromChest(backpackInventory, inventory);
            } else {
                transferToChest(backpackInventory, inventory);
            }
        }
        return true;
    }
    return false;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) ItemInventoryBackpack(forestry.storage.inventory.ItemInventoryBackpack) BackpackMode(forestry.storage.BackpackMode)

Example 3 with ItemInventoryBackpack

use of forestry.storage.inventory.ItemInventoryBackpack in project ForestryMC by ForestryMC.

the class ResupplyHandler method resupply.

@Override
public void resupply(EntityPlayer player) {
    // Do not attempt resupplying if this backpack is already opened.
    if (!(player.openContainer instanceof ContainerPlayer)) {
        return;
    }
    for (ItemStack backpack : getBackpacks(player.inventory)) {
        if (ItemBackpack.getMode(backpack) == BackpackMode.RESUPPLY) {
            // Load their inventory
            ItemBackpack backpackItem = (ItemBackpack) backpack.getItem();
            ItemInventory backpackInventory = new ItemInventoryBackpack(player, backpackItem.getBackpackSize(), backpack);
            Event event = new BackpackResupplyEvent(player, backpackItem.getDefinition(), backpackInventory);
            if (!MinecraftForge.EVENT_BUS.post(event)) {
                for (int i = 0; i < backpackInventory.getSizeInventory(); i++) {
                    ItemStack itemStack = backpackInventory.getStackInSlot(i);
                    if (topOffPlayerInventory(player, itemStack)) {
                        backpackInventory.setInventorySlotContents(i, itemStack);
                        break;
                    }
                }
            }
        }
    }
}
Also used : ContainerPlayer(net.minecraft.inventory.ContainerPlayer) ItemInventory(forestry.core.inventory.ItemInventory) ItemInventoryBackpack(forestry.storage.inventory.ItemInventoryBackpack) BackpackResupplyEvent(forestry.api.storage.BackpackResupplyEvent) Event(net.minecraftforge.fml.common.eventhandler.Event) BackpackResupplyEvent(forestry.api.storage.BackpackResupplyEvent) ItemBackpack(forestry.storage.items.ItemBackpack) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ItemInventoryBackpack (forestry.storage.inventory.ItemInventoryBackpack)3 ItemInventory (forestry.core.inventory.ItemInventory)2 ItemStack (net.minecraft.item.ItemStack)2 IItemHandler (net.minecraftforge.items.IItemHandler)2 BackpackResupplyEvent (forestry.api.storage.BackpackResupplyEvent)1 BackpackStowEvent (forestry.api.storage.BackpackStowEvent)1 ItemHandlerInventoryManipulator (forestry.core.inventory.ItemHandlerInventoryManipulator)1 BackpackMode (forestry.storage.BackpackMode)1 ItemBackpack (forestry.storage.items.ItemBackpack)1 ContainerPlayer (net.minecraft.inventory.ContainerPlayer)1 Event (net.minecraftforge.fml.common.eventhandler.Event)1