List of usage examples for net.minecraftforge.items.wrapper InvWrapper InvWrapper
public InvWrapper(IInventory inv)
From source file:com.teambr.bookshelf.util.InventoryUtils.java
License:Creative Commons License
/** * Used to move items from one inventory to another. You can wrap it if you have to * but since you'll be calling from us, there shouldn't be an issue * * @param source The source inventory, can be IInventory, ISideInventory, or preferably IItemHandler * @param fromSlot The from slot, -1 for any * @param target The target inventory, can be IInventory, ISideInventory, or preferably IItemHandler * @param intoSlot The slot to move into the target, -1 for any * @param maxAmount The max amount to move/extract * @param dir The direction moving into, so the face of the fromInventory * @param doMove True to actually do the move, false to simulate * @return True if something was moved//ww w .ja v a 2 s . c o m */ public static boolean moveItemInto(Object source, int fromSlot, Object target, int intoSlot, int maxAmount, EnumFacing dir, boolean doMove, boolean checkSidedSource, boolean checkSidedTarget) { // Null Checks if (source == null || target == null) return false; // Object to hold source IItemHandler fromInventory; // If source is not an item handler, attempt to cast if (!(source instanceof IItemHandler)) { if (source instanceof IInventory) { if (!(source instanceof ISidedInventory)) fromInventory = new InvWrapper((IInventory) source); // Wrap vanilla inventory else fromInventory = new SidedInvWrapper((ISidedInventory) source, dir.getOpposite()); // Wrap sided } else return false; // Not valid } else { // Cast item handlers fromInventory = (IItemHandler) source; } // If required, check for sidedness on tiles if (checkSidedSource) { if (source instanceof TileEntity) { TileEntity tile = (TileEntity) source; if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir)) fromInventory = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir); else return false; // Source does not want to expose access } } IItemHandler targetInventory; // If sink is not an item handler, attempt to cast if (!(target instanceof IItemHandler)) { if (target instanceof IInventory) { if (!(target instanceof ISidedInventory)) targetInventory = new InvWrapper((IInventory) target); // Wrap vanilla inventory else targetInventory = new SidedInvWrapper((ISidedInventory) target, dir); // Wrap sided } else return false; // Not valid } else { // Cast item handlers targetInventory = (IItemHandler) target; } // If required, check for sidedness on tiles if (checkSidedTarget) { if (target instanceof TileEntity) { TileEntity tile = (TileEntity) target; if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir.getOpposite())) targetInventory = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir.getOpposite()); else return false; // Target does not want to expose access } } // Load slots List<Integer> fromSlots = new ArrayList<>(); List<Integer> toSlots = new ArrayList<>(); // Add From Slots if (fromSlot != -1) fromSlots.add(fromSlot); else for (int x = 0; x < fromInventory.getSlots(); x++) fromSlots.add(x); // Add to slots if (intoSlot != -1) toSlots.add(intoSlot); else for (int x = 0; x < targetInventory.getSlots(); x++) toSlots.add(x); // Do actual movement for (Integer fromSlot1 : fromSlots) { // Cycle fromInventory if (fromInventory.getStackInSlot(fromSlot1) != null) { // If we have something ItemStack fromStack = fromInventory.extractItem(fromSlot1, maxAmount, true); // Simulate to get stack if (!fromStack.isEmpty()) { // Make sure we got something for (Integer toSlot : toSlots) { // Cycle to inventory int slotID = toSlot; // Grab slot ItemStack movedStack = targetInventory.insertItem(slotID, fromStack.copy(), !doMove); // Try insert if (!ItemStack.areItemStacksEqual(fromStack, movedStack)) { // If a change was made to the stack fromInventory.extractItem(fromSlot1, (!movedStack.isEmpty()) ? fromStack.getCount() - movedStack.getCount() : maxAmount, !doMove); // Extract from original return true; // Exit } } } } } return false; // Failed to move something }
From source file:de.ellpeck.actuallyadditions.mod.items.ItemChestToCrateUpgrade.java
@Override public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float par8, float par9, float par10) { ItemStack heldStack = player.getHeldItem(hand); if (player.isSneaking()) { TileEntity tileHit = world.getTileEntity(pos); if (tileHit.getClass() == this.start) { if (!world.isRemote) { //Copy Slots IItemHandlerModifiable chest = null; if (tileHit instanceof IInventory) { chest = new InvWrapper((IInventory) tileHit); } else if (tileHit instanceof TileEntityInventoryBase) { chest = ((TileEntityInventoryBase) tileHit).slots; }/*from ww w . ja v a 2 s . co m*/ if (chest != null) { ItemStack[] stacks = new ItemStack[chest.getSlots()]; for (int i = 0; i < stacks.length; i++) { ItemStack aStack = chest.getStackInSlot(i); if (StackUtil.isValid(aStack)) { stacks[i] = aStack.copy(); chest.setStackInSlot(i, StackUtil.getNull()); } } //Set New Block world.playEvent(2001, pos, Block.getStateId(world.getBlockState(pos))); world.setBlockState(pos, this.end, 2); //Copy Items into new Chest TileEntity newTileHit = world.getTileEntity(pos); if (newTileHit instanceof TileEntityInventoryBase) { IItemHandlerModifiable newChest = ((TileEntityInventoryBase) newTileHit).slots; for (int i = 0; i < stacks.length; i++) { if (StackUtil.isValid(stacks[i])) { if (newChest.getSlots() > i) { newChest.setStackInSlot(i, stacks[i].copy()); } } } } if (!player.capabilities.isCreativeMode) { player.setHeldItem(hand, StackUtil.addStackSize(heldStack, -1)); } } } return EnumActionResult.SUCCESS; } } return super.onItemUse(player, world, pos, hand, facing, par8, par9, par10); }
From source file:hellfirepvp.astralsorcery.common.block.network.BlockWell.java
License:Open Source License
@Override public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { if (!worldIn.isRemote) { ItemStack heldItem = playerIn.getHeldItem(hand); if (!heldItem.isEmpty() && playerIn instanceof EntityPlayerMP) { TileWell tw = MiscUtils.getTileAt(worldIn, pos, TileWell.class, false); if (tw == null) return false; WellLiquefaction.LiquefactionEntry entry = WellLiquefaction.getLiquefactionEntry(heldItem); if (entry != null) { ItemStackHandler handle = tw.getInventoryHandler(); if (!handle.getStackInSlot(0).isEmpty()) return false; if (!worldIn.isAirBlock(pos.up())) { return false; }/*from ww w .j a v a 2 s. c o m*/ handle.setStackInSlot(0, ItemUtils.copyStackWithSize(heldItem, 1)); worldIn.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ((worldIn.rand.nextFloat() - worldIn.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F); if (!MiscUtils.isPlayerFakeMP((EntityPlayerMP) playerIn) && entry.producing instanceof FluidLiquidStarlight) { //Lets assume it starts collecting right away... playerIn.addStat(RegistryAchievements.achvLiqStarlight); } if (!playerIn.isCreative()) { heldItem.shrink(1); } if (heldItem.getCount() <= 0) { playerIn.setHeldItem(hand, ItemStack.EMPTY); } } FluidActionResult far = FluidUtil.tryFillContainerAndStow(heldItem, tw.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.DOWN), new InvWrapper(playerIn.inventory), 1000, playerIn); if (far.isSuccess()) { playerIn.setHeldItem(hand, far.getResult()); SoundHelper.playSoundAround(SoundEvents.ITEM_BUCKET_FILL, worldIn, pos, 1F, 1F); tw.markForUpdate(); } } } return true; }
From source file:hellfirepvp.astralsorcery.common.container.ContainerJournal.java
License:Open Source License
public ContainerJournal(InventoryPlayer playerInv, ItemStack journal, int journalIndex) { this.parentJournal = journal; this.journalIndex = journalIndex; buildPlayerSlots(playerInv);//from ww w. ja v a 2 s.c om buildSlots(new InvWrapper(ItemJournal.getJournalStorage(journal))); }
From source file:hellfirepvp.astralsorcery.common.integrations.ModIntegrationBotania.java
License:Open Source License
public static int getItemCount(EntityPlayer requestingPlayer, ItemStack requestingStack, IBlockState stateSearch) {// w w w . j a va 2 s.c o m Block block = stateSearch.getBlock(); int meta; try { meta = block.damageDropped(stateSearch); } catch (Exception e) { meta = 0; } ItemStack blockStackStored = ItemUtils.createBlockStack(stateSearch); IItemHandler inv = requestingPlayer.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); if (inv == null) { return 0; } int amtFound = 0; for (int i = inv.getSlots() - 1; i >= 0; i--) { ItemStack invStack = inv.getStackInSlot(i); if (!invStack.isEmpty()) { Item item = invStack.getItem(); if ((item instanceof IBlockProvider)) { int res = ((IBlockProvider) item).getBlockCount(requestingPlayer, requestingStack, invStack, block, meta); if (res == -1) { return -1; } else { amtFound += res; } } } } Collection<ItemStack> stacks = ItemUtils.scanInventoryForMatching( new InvWrapper(Minecraft.getMinecraft().player.inventory), blockStackStored, false); for (ItemStack stack : stacks) { amtFound += stack.getCount(); } return amtFound; }
From source file:hellfirepvp.astralsorcery.common.item.wand.ItemArchitectWand.java
License:Open Source License
@Override @SideOnly(Side.CLIENT)/* ww w.java2 s .co m*/ public void onRenderInHandHUD(ItemStack lastCacheInstance, float fadeAlpha, float pTicks) { ItemStack blockStackStored = getStoredStateAsStack(lastCacheInstance); if (blockStackStored.isEmpty()) return; int amtFound = 0; if (Mods.BOTANIA.isPresent()) { amtFound = ModIntegrationBotania.getItemCount(Minecraft.getMinecraft().player, lastCacheInstance, ItemUtils.createBlockState(blockStackStored)); } else { Collection<ItemStack> stacks = ItemUtils.scanInventoryForMatching( new InvWrapper(Minecraft.getMinecraft().player.inventory), blockStackStored, false); for (ItemStack stack : stacks) { amtFound += stack.getCount(); } } int height = 26; int width = 26; int offsetX = 30; int offsetY = 15; GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS); GL11.glPushMatrix(); GL11.glDisable(GL11.GL_ALPHA_TEST); GL11.glEnable(GL11.GL_BLEND); Blending.DEFAULT.apply(); ClientRenderEventHandler.texHUDItemFrame.bind(); GL11.glColor4f(1F, 1F, 1F, fadeAlpha * 0.9F); Tessellator tes = Tessellator.getInstance(); VertexBuffer vb = tes.getBuffer(); vb.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX); vb.pos(offsetX, offsetY + height, 10).tex(0, 1).endVertex(); vb.pos(offsetX + width, offsetY + height, 10).tex(1, 1).endVertex(); vb.pos(offsetX + width, offsetY, 10).tex(1, 0).endVertex(); vb.pos(offsetX, offsetY, 10).tex(0, 0).endVertex(); tes.draw(); TextureHelper.refreshTextureBindState(); TextureHelper.setActiveTextureToAtlasSprite(); RenderHelper.enableGUIStandardItemLighting(); RenderItem ri = Minecraft.getMinecraft().getRenderItem(); ri.renderItemAndEffectIntoGUI(Minecraft.getMinecraft().player, blockStackStored, offsetX + 5, offsetY + 5); RenderHelper.disableStandardItemLighting(); GlStateManager.enableAlpha(); //Because Mc item rendering.. GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glPushMatrix(); GL11.glTranslated(offsetX + 14, offsetY + 16, 0); String amtString = String.valueOf(amtFound); if (amtFound == -1) { amtString = ""; } GL11.glTranslated(-Minecraft.getMinecraft().fontRenderer.getStringWidth(amtString) / 3, 0, 0); GL11.glScaled(0.7, 0.7, 0.7); if (amtString.length() > 3) { GL11.glScaled(0.9, 0.9, 0.9); } int c = 0x00DDDDDD; Minecraft.getMinecraft().fontRenderer.drawStringWithShadow(amtString, 0, 0, c); GlStateManager.color(1F, 1F, 1F, 1F); TextureHelper.refreshTextureBindState(); GL11.glPopMatrix(); GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glEnable(GL11.GL_ALPHA_TEST); GL11.glPopMatrix(); GL11.glPopAttrib(); }
From source file:net.teamio.taam.util.InventoryUtils.java
License:Open Source License
/** * Gets an {@link IItemHandler} from the given tile entity. * <p>//from w ww.j av a 2 s . c o m * If enabled in the config, {@link ISidedInventory} and {@link IInventory} * will be wrapped & returned accordingly. * * @param tileEntity * @param side * @return An {@link IItemHandler} for the given tileEntity, or null if no * inventory was found or tileEntity was null. * @author Oliver Kahrmann */ public static IItemHandler getInventory(TileEntity tileEntity, EnumFacing side) { if (tileEntity == null) { return null; } IItemHandler itemHandler = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side); if (itemHandler == null && Config.use_iinventory_compat) { if (tileEntity instanceof ISidedInventory) { itemHandler = new SidedInvWrapper((ISidedInventory) tileEntity, side); } else if (tileEntity instanceof IInventory) { itemHandler = new InvWrapper((IInventory) tileEntity); } } return itemHandler; }
From source file:vazkii.botania.common.core.handler.InternalMethodHandler.java
License:Open Source License
@Override public IItemHandlerModifiable getBaublesInventoryWrapped(EntityPlayer player) { IInventory inv = getBaublesInventory(player); return inv == null ? null : new InvWrapper(inv); }
From source file:vazkii.botania.common.integration.corporea.WrappedDeepStorage.java
License:Open Source License
@Override public InvWithLocation getWrappedObject() { return new InvWithLocation(new InvWrapper((IInventory) invRaw), spark.getSparkInventory().world, spark.getSparkInventory().pos); }
From source file:vazkii.botania.common.item.ItemEnderHand.java
License:Open Source License
@Override public boolean provideBlock(EntityPlayer player, ItemStack requestor, ItemStack stack, Block block, int meta, boolean doit) { if (requestor != null && requestor.getItem() == this) return false; ItemStack istack = ItemExchangeRod.removeFromInventory(player, new InvWrapper(player.getInventoryEnderChest()), stack, block, meta, false); if (!istack.isEmpty()) { boolean mana = ManaItemHandler.requestManaExact(stack, player, COST_PROVIDE, false); if (mana) { if (doit) { ManaItemHandler.requestManaExact(stack, player, COST_PROVIDE, true); ItemExchangeRod.removeFromInventory(player, new InvWrapper(player.getInventoryEnderChest()), stack, block, meta, true); }/*from www .j a va 2 s . c o m*/ return true; } } return false; }