Search in sources :

Example 11 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project Almura by AlmuraDev.

the class CapabilityDualItemHandlerGuiHandler method getClientGuiElement.

@SideOnly(Side.CLIENT)
@Nullable
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
    final BlockPos pos = new BlockPos(x, y, z);
    final Block block = world.getBlockState(pos).getBlock();
    final TileEntity te = world.getTileEntity(pos);
    if (te == null || !(te instanceof MultiSlotTileEntity)) {
        return null;
    }
    final IItemHandler teItemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    if (teItemHandler == null) {
        return null;
    }
    final IItemHandler playerItemHandler = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    if (playerItemHandler == null) {
        return null;
    }
    return new DualItemHandlerGui(teItemHandler, playerItemHandler, new TextComponentString(block.getLocalizedName()), new TextComponentTranslation("container.inventory"));
}
Also used : MultiSlotTileEntity(com.almuradev.almura.shared.tileentity.MultiSlotTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) DualItemHandlerGui(com.almuradev.almura.shared.network.handler.DualItemHandlerGui) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IItemHandler(net.minecraftforge.items.IItemHandler) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) MultiSlotTileEntity(com.almuradev.almura.shared.tileentity.MultiSlotTileEntity) TextComponentString(net.minecraft.util.text.TextComponentString) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) Nullable(javax.annotation.Nullable)

Example 12 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project ConvenientAdditions by Necr0.

the class EventHandlerSoulbound method onPlayerDrops.

@SubscribeEvent
public void onPlayerDrops(PlayerDropsEvent e) {
    if (e.getEntityPlayer().getEntityWorld().isRemote || e.getEntityPlayer().getEntityWorld().getGameRules().getBoolean("keepInventory"))
        return;
    Iterator<EntityItem> i = e.getDrops().iterator();
    while (i.hasNext()) {
        EntityItem ent = i.next();
        ItemStack stack = ent.getEntityItem();
        if (!stack.isEmpty() && stack.getItem() instanceof ISoulbound && ((ISoulbound) stack.getItem()).isSoulbound(stack, e.getEntityPlayer())) {
            IItemHandler h = e.getEntityPlayer().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN);
            ItemStack out = tryInsert(stack, h);
            if (out == null)
                i.remove();
            else
                stack.setCount(out.getCount());
        }
    }
}
Also used : ISoulbound(convenientadditions.api.item.ISoulbound) IItemHandler(net.minecraftforge.items.IItemHandler) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 13 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project MinecraftForge by MinecraftForge.

the class FluidUtil method interactWithFluidHandler.

/**
     * Used to handle the common case of a player holding a fluid item and right-clicking on a fluid handler.
     * First it tries to fill the container item from the fluid handler,
     * if that action fails then it tries to drain the container item into the fluid handler.
     *
     * @param stack        The filled or empty fluid container.
     *                     Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
     * @param fluidHandler The fluid handler to interact with.
     * @param player       The player doing the interaction between the item and fluid handler.
     * @return a {@link FluidActionResult} holding the result and resulting container.
     */
@Nonnull
public static FluidActionResult interactWithFluidHandler(@Nonnull ItemStack stack, IFluidHandler fluidHandler, EntityPlayer player) {
    if (stack.isEmpty() || fluidHandler == null || player == null) {
        return FluidActionResult.FAILURE;
    }
    IItemHandler playerInventory = new InvWrapper(player.inventory);
    FluidActionResult fillResult = tryFillContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
    if (fillResult.isSuccess()) {
        return fillResult;
    } else {
        return tryEmptyContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
    }
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) Nonnull(javax.annotation.Nonnull)

Example 14 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project ImmersiveEngineering by BluSunrize.

the class Utils method insertStackIntoInventory.

public static ItemStack insertStackIntoInventory(TileEntity inventory, ItemStack stack, EnumFacing side) {
    if (stack != null && inventory != null && inventory.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)) {
        IItemHandler handler = inventory.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
        ItemStack temp = ItemHandlerHelper.insertItem(handler, stack.copy(), true);
        if (temp == null || temp.stackSize < stack.stackSize)
            return ItemHandlerHelper.insertItem(handler, stack, false);
    }
    return stack;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) ItemStack(net.minecraft.item.ItemStack)

Example 15 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project minecolonies by Minecolonies.

the class InventoryUtils method forceItemStackToProvider.

/**
     * Adapted from {@link net.minecraft.entity.player.InventoryPlayer#addItemStackToInventory(ItemStack)}.
     *
     * @param provider                 {@link ICapabilityProvider} to add
     *                                 itemstack to.
     * @param itemStack                ItemStack to add.
     * @param itemStackToKeepPredicate The {@link Predicate} that determines
     *                                 which ItemStacks to keep in the
     *                                 inventory. Return false to replace.
     * @return itemStack which has been replaced.
     */
@Nullable
public static ItemStack forceItemStackToProvider(@NotNull final ICapabilityProvider provider, @NotNull final ItemStack itemStack, @NotNull final Predicate<ItemStack> itemStackToKeepPredicate) {
    final ItemStack standardInsertionResult = addItemStackToProviderWithResult(provider, itemStack);
    if (!isItemStackEmpty(standardInsertionResult)) {
        ItemStack resultStack = standardInsertionResult.copy();
        final Iterator<IItemHandler> iterator = getItemHandlersFromProvider(provider).iterator();
        while (iterator.hasNext() && !isItemStackEmpty(resultStack)) {
            resultStack = forceItemStackToItemHandler(iterator.next(), resultStack, itemStackToKeepPredicate);
        }
        return resultStack;
    }
    return EMPTY;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IItemHandler (net.minecraftforge.items.IItemHandler)22 ItemStack (net.minecraft.item.ItemStack)17 TileEntity (net.minecraft.tileentity.TileEntity)7 BlockPos (net.minecraft.util.math.BlockPos)4 IBlockState (net.minecraft.block.state.IBlockState)3 MultiSlotTileEntity (com.almuradev.almura.shared.tileentity.MultiSlotTileEntity)2 ISoulbound (convenientadditions.api.item.ISoulbound)2 Nullable (javax.annotation.Nullable)2 ItemDoll (mdc.voodoocraft.items.ItemDoll)2 TileDollPedestal (mdc.voodoocraft.tile.TileDollPedestal)2 TileEntityTeleporter (net.dyeo.teleporter.tileentity.TileEntityTeleporter)2 Block (net.minecraft.block.Block)2 EntityItem (net.minecraft.entity.item.EntityItem)2 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)2 ISpecialMeasuringBehavior (betterwithaddons.util.ISpecialMeasuringBehavior)1 TileEntityFilteredHopper (betterwithmods.common.blocks.mechanical.tile.TileEntityFilteredHopper)1 IImmersiveConnectable (blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable)1 IEInventoryHandler (blusunrize.immersiveengineering.common.util.inventory.IEInventoryHandler)1 IIEInventory (blusunrize.immersiveengineering.common.util.inventory.IIEInventory)1 DualItemHandlerContainer (com.almuradev.almura.shared.inventory.DualItemHandlerContainer)1