Example #1
0
 public static ItemStack insertItem(
     IInventory inventory, ItemStack itemStack, boolean test, boolean force) {
   if (test) {
     itemStack = itemStack.copy();
   }
   int size = inventory.getSizeInventory();
   for (int i = 0; i < size; i++) {
     if (inventory.isItemValidForSlot(i, itemStack) || force) {
       ItemStack storedItem = inventory.getStackInSlot(i);
       if (storedItem != null) {
         if (equalItemAndNBT(storedItem, itemStack)) {
           int maxStackSize =
               Math.min(itemStack.getMaxStackSize(), inventory.getInventoryStackLimit());
           int add = Math.min(itemStack.stackSize, maxStackSize - storedItem.stackSize);
           if (!test) {
             storedItem.stackSize += add;
           }
           itemStack.stackSize -= add;
           inventory.setInventorySlotContents(i, storedItem);
         }
       } else {
         storedItem = itemStack.copy();
         storedItem.stackSize = Math.min(itemStack.stackSize, itemStack.getMaxStackSize());
         if (!test) {
           inventory.setInventorySlotContents(i, storedItem);
         }
         itemStack.stackSize -= storedItem.stackSize;
       }
       if (itemStack.stackSize <= 0) {
         return null;
       }
     }
   }
   return itemStack;
 }
  @Override
  public ItemStack removeItem(int maxRemove, ItemStack type) {

    if (maxRemove <= 0) {
      return null;
    }

    int[] slots = getSlots();
    if (slots == null) {
      return null;
    }

    for (int i : slots) {
      ItemStack s = getSlotContents(i);
      if (ItemHelper.itemsEqualWithMetadata(s, type) && canRemoveItem(s, i)) {
        int toRemove = Math.min(s.stackSize, maxRemove);
        s.stackSize -= toRemove;
        ItemStack removed = s.copy();
        removed.stackSize = toRemove;
        if (s.stackSize > 0) {
          _inv.setInventorySlotContents(i, s);
        } else {
          _inv.setInventorySlotContents(i, null);
        }
        return removed;
      }
    }
    return null;
  }
  public static boolean requestElements(
      EntityPlayer player, ElementList list, int purityRequired, boolean doit) {
    if (player.capabilities.isCreativeMode) return true;

    IInventory mainInv = player.inventory;

    int invSize = mainInv.getSizeInventory();

    for (int i = 0; i < invSize; i++) {
      ItemStack stackInSlot = mainInv.getStackInSlot(i);

      if (stackInSlot != null && stackInSlot.getItem() instanceof IElementProvider) {
        IElementProvider elementItem = (IElementProvider) stackInSlot.getItem();
        if (elementItem.getPurity(player, stackInSlot) < purityRequired) continue;
        ElementList els = elementItem.getElements(player, stackInSlot);
        if (els.size() != 0) {
          ItemStack newStack = elementItem.consumeElements(player, stackInSlot, list, doit);
          if (newStack.stackSize <= 0) mainInv.setInventorySlotContents(i, null);
          else mainInv.setInventorySlotContents(i, newStack);

          if (list.size() == 0) return true;
        }
      }
    }
    return false;
  }
  @Override
  public ItemStack removeItem(int maxRemove) {

    if (maxRemove <= 0) {
      return null;
    }

    int[] slots = getSlots();
    if (slots == null) {
      return null;
    }

    for (int i : slots) {
      ItemStack s = getSlotContents(i);
      if (s != null && canRemoveItem(s, i)) {
        int toRemove = Math.min(s.stackSize, maxRemove);
        s.stackSize -= toRemove;
        ItemStack removed = s.copy();
        removed.stackSize = toRemove;
        if (s.stackSize > 0) {
          _inv.setInventorySlotContents(i, s);
        } else {
          _inv.setInventorySlotContents(i, null);
        }
        _inv.markDirty();
        return removed;
      }
    }
    return null;
  }
 // Sets the result in the output slot depending on if there's a pattern in the input and on which
 // pattern was selected
 public void updateResult() {
   // no pattern :(
   if (craftMatrix.getStackInSlot(0) == null || output == null) {
     craftResult.setInventorySlotContents(0, null);
   } else {
     // set pattern from selection (or null if no selection)
     craftResult.setInventorySlotContents(0, output.copy());
   }
 }
 public static ItemStack addToOccupiedSlot(
     IInventory inventory, int slot, ItemStack stack, ItemStack existingStack) {
   int stackLimit = Math.min(inventory.getInventoryStackLimit(), stack.getMaxStackSize());
   if (stack.stackSize + existingStack.stackSize > stackLimit) {
     int stackDiff = stackLimit - existingStack.stackSize;
     existingStack.stackSize = stackLimit;
     stack.stackSize -= stackDiff;
     inventory.setInventorySlotContents(slot, existingStack);
     return stack;
   }
   existingStack.stackSize += Math.min(stack.stackSize, stackLimit);
   inventory.setInventorySlotContents(slot, existingStack);
   return stackLimit >= stack.stackSize ? null : stack.splitStack(stack.stackSize - stackLimit);
 }
  @Override
  public ItemStack addItem(ItemStack stack) {

    if (stack == null) {
      return null;
    }

    int quantitytoadd = stack.stackSize;
    ItemStack remaining = stack.copy();
    int[] slots = getSlots();
    if (slots == null) {
      return remaining;
    }

    for (int i : slots) {
      int maxStackSize = Math.min(_inv.getInventoryStackLimit(), stack.getMaxStackSize());
      ItemStack s = getSlotContents(i);
      if (s == null) {
        ItemStack add = stack.copy();
        add.stackSize = Math.min(quantitytoadd, maxStackSize);

        if (canAddItem(add, i)) {
          quantitytoadd -= add.stackSize;
          _inv.setInventorySlotContents(i, add);
          _inv.markDirty();
        }
      } else if (ItemHelper.itemsEqualWithMetadata(s, stack)) {
        ItemStack add = stack.copy();
        add.stackSize = Math.min(quantitytoadd, maxStackSize - s.stackSize);

        if (add.stackSize > 0 && canAddItem(add, i)) {
          s.stackSize += add.stackSize;
          quantitytoadd -= add.stackSize;
          _inv.setInventorySlotContents(i, s);
          _inv.markDirty();
        }
      }
      if (quantitytoadd == 0) {
        break;
      }
    }

    remaining.stackSize = quantitytoadd;
    if (remaining.stackSize == 0) {
      return null;
    } else {
      return remaining;
    }
  }
  @Override
  public void setInventorySlotContents(int i, ItemStack itemstack) {

    // TODO: add gregcipies
    if (i == 0) glassPanel = itemstack;
    else {

      ForgeDirection front = RotatableBlock.getFront(this.getBlockMetadata());

      for (ForgeDirection f : VALID_INVENTORY_DIRECTIONS) {
        if (f == front) continue;

        TileEntity e =
            this.worldObj.getTileEntity(
                this.xCoord + f.offsetX, this.yCoord + f.offsetY, this.zCoord + f.offsetZ);

        // TODO: may cause inf loop
        if (e != null && e instanceof IInventory)
          if (i < ((IInventory) e).getSizeInventory()) {
            ((IInventory) e).setInventorySlotContents(i, itemstack);
            break;
          } else i -= ((IInventory) e).getSizeInventory();
      }

      this.checkCanRun();
    }
  }
  /**
   * Adds an ItemStack to the first available slot in the provided IInventory, continuing to
   * distribute the stack as necessary until the stacksize reaches 0, if possible
   *
   * @return the number of items remaining in the stack, zero if all were added
   */
  public static int addItemToInventory(ItemStack stack, IInventory inv) {
    int remaining = stack.stackSize;
    for (int i = 0; i < inv.getSizeInventory() && remaining > 0; ++i) {
      ItemStack slotstack = inv.getStackInSlot(i);
      if (slotstack == null && inv.isItemValidForSlot(i, stack)) {
        remaining -= inv.getInventoryStackLimit();
        stack.stackSize = (remaining > 0 ? inv.getInventoryStackLimit() : stack.stackSize);
        inv.setInventorySlotContents(i, stack);
        inv.markDirty();
      } else if (slotstack != null && stack.isStackable() && inv.isItemValidForSlot(i, stack)) {
        if (slotstack.getItem() == stack.getItem()
            && (!stack.getHasSubtypes() || stack.getItemDamage() == slotstack.getItemDamage())
            && ItemStack.areItemStackTagsEqual(stack, slotstack)) {
          int l = slotstack.stackSize + remaining;
          if (l <= stack.getMaxStackSize() && l <= inv.getInventoryStackLimit()) {
            remaining = 0;
            slotstack.stackSize = l;
            inv.markDirty();
          } else if (slotstack.stackSize < stack.getMaxStackSize()
              && stack.getMaxStackSize() <= inv.getInventoryStackLimit()) {
            remaining -= stack.getMaxStackSize() - slotstack.stackSize;
            slotstack.stackSize = stack.getMaxStackSize();
            inv.markDirty();
          }
        }
      }
    }

    return remaining;
  }
  public static StorageChunk cutWorldBB(World worldObj, AxisAlignedBB bb) {
    StorageChunk chunk = StorageChunk.copyWorldBB(worldObj, bb);
    for (int x = (int) bb.minX; x <= bb.maxX; x++) {
      for (int z = (int) bb.minZ; z <= bb.maxZ; z++) {
        for (int y = (int) bb.minY; y <= bb.maxY; y++) {

          // Workaround for dupe
          TileEntity tile = worldObj.getTileEntity(x, y, z);
          if (tile instanceof IInventory) {
            IInventory inv = (IInventory) tile;
            for (int i = 0; i < inv.getSizeInventory(); i++) {
              inv.setInventorySlotContents(i, null);
            }
          }

          worldObj.setBlock(x, y, z, Blocks.air, 0, 2);
        }
      }
    }

    // Carpenter's block's dupe
    for (Object entity : worldObj.getEntitiesWithinAABB(EntityItem.class, bb.expand(5, 5, 5))) {
      ((Entity) entity).setDead();
    }

    return chunk;
  }
Example #11
0
  @Override
  public void onCrafting(EntityPlayer player, ItemStack item, IInventory inv) {
    if (item.itemID == ModItems.Marshmallow.itemID) {
      if (item.getItemDamage() == 0) {
        ItemStack bowl = new ItemStack(Item.bowlEmpty);
        player.inventory.addItemStackToInventory(bowl);
      }
    }

    for (int i = 0; i < inv.getSizeInventory(); i++) {
      if (inv.getStackInSlot(i) != null) {
        ItemStack j = inv.getStackInSlot(i);
        if (j.getItem() != null && ToolHelper.isTool(j) && !ToolHelper.isTool(item)) {
          ItemStack k = ToolHelper.addDamage(j, player);
          if (k != null) k.stackSize++;
          inv.setInventorySlotContents(i, k);
        }
      }
    }

    if (item.itemID == ModItems.CampingBag.itemID) {
      for (int i = 0; i < inv.getSizeInventory(); i++) {
        if (inv.getStackInSlot(i) != null) {
          ItemStack j = inv.getStackInSlot(i);
          if (j.getItem() != null && j.itemID == ModItems.CampingBag.itemID) {
            if (j.hasTagCompound()) {
              item.setTagCompound((NBTTagCompound) j.getTagCompound().copy());
            }
          }
        }
      }
    }

    if (item.itemID == ModBlocks.campfire.blockID) {
      if (item.getItemDamage() == campfire[0].getItemDamage())
        player.addStat(ModAchievements.Campfire, 1);
      if (item.getItemDamage() == campfire[1].getItemDamage())
        player.addStat(ModAchievements.CampfireMultiCook, 1);
      if (item.getItemDamage() == campfire[2].getItemDamage())
        player.addStat(ModAchievements.CampfireFastCook, 1);
      if (item.getItemDamage() == campfire[3].getItemDamage())
        player.addStat(ModAchievements.CampfireCheapCook, 1);
      if (item.getItemDamage() == campfire[4].getItemDamage())
        player.addStat(ModAchievements.CampfireInstaCook, 1);
    }

    if (item.itemID == ModItems.CampingBag.itemID) {
      if (item.getItemDamage() == bag[0].getItemDamage())
        player.addStat(ModAchievements.CampingBagSmall, 1);
      if (item.getItemDamage() == bag[1].getItemDamage())
        player.addStat(ModAchievements.CampingBagNormal, 1);
      if (item.getItemDamage() == bag[2].getItemDamage())
        player.addStat(ModAchievements.CampingBagLarge, 1);
    }

    if (item.itemID == ModItems.CampTool.itemID) player.addStat(ModAchievements.CampersTool, 1);
    if (item.itemID == ModItems.TentParts.itemID) player.addStat(ModAchievements.TentParts, 1);
    if (item.itemID == tent.itemID) player.addStat(ModAchievements.Tent, 1);
  }
  /**
   * @param t - the TileEntity
   * @param loadTag - the tag
   */
  public static void loadInventory(TileEntity t, NBTTagCompound loadTag) {
    if (t instanceof IInventory) {
      IInventory tile = (IInventory) t;
      for (int i = 0; i < tile.getSizeInventory(); ++i) {
        tile.setInventorySlotContents(i, null);
      }
      NBTTagList nbttaglist = loadTag.getTagList("Items", 10);
      for (int i = 0; i < nbttaglist.tagCount(); ++i) {
        NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
        byte b0 = nbttagcompound1.getByte("Slot");

        if (b0 >= 0 && b0 < tile.getSizeInventory()) {
          tile.setInventorySlotContents(b0, ItemStack.loadItemStackFromNBT(nbttagcompound1));
        }
      }
    }
  }
Example #13
0
  /**
   * Universally discharges an item, and updates the TileEntity's energy level.
   *
   * @param slotID - ID of the slot of which to charge
   * @param storer - TileEntity the item is being charged in
   */
  public static void discharge(int slotID, IStrictEnergyStorage storer) {
    IInventory inv = (TileEntityContainerBlock) storer;

    if (inv.getStackInSlot(slotID) != null && storer.getEnergy() < storer.getMaxEnergy()) {
      if (inv.getStackInSlot(slotID).getItem() instanceof IEnergizedItem) {
        storer.setEnergy(
            storer.getEnergy()
                + EnergizedItemManager.discharge(
                    inv.getStackInSlot(slotID), storer.getMaxEnergy() - storer.getEnergy()));
      } else if (MekanismUtils.useIC2()
          && inv.getStackInSlot(slotID).getItem() instanceof IElectricItem) {
        IElectricItem item = (IElectricItem) inv.getStackInSlot(slotID).getItem();

        if (item.canProvideEnergy(inv.getStackInSlot(slotID))) {
          double gain =
              ElectricItem.manager.discharge(
                      inv.getStackInSlot(slotID),
                      (int) ((storer.getMaxEnergy() - storer.getEnergy()) * general.TO_IC2),
                      4,
                      true,
                      true,
                      false)
                  * general.FROM_IC2;
          storer.setEnergy(storer.getEnergy() + gain);
        }
      } else if (MekanismUtils.useRF()
          && inv.getStackInSlot(slotID).getItem() instanceof IEnergyContainerItem) {
        ItemStack itemStack = inv.getStackInSlot(slotID);
        IEnergyContainerItem item = (IEnergyContainerItem) inv.getStackInSlot(slotID).getItem();

        int itemEnergy =
            (int)
                Math.round(
                    Math.min(
                        Math.sqrt(item.getMaxEnergyStored(itemStack)),
                        item.getEnergyStored(itemStack)));
        int toTransfer =
            (int)
                Math.round(
                    Math.min(
                        itemEnergy,
                        ((storer.getMaxEnergy() - storer.getEnergy()) * general.TO_RF)));

        storer.setEnergy(
            storer.getEnergy()
                + (item.extractEnergy(itemStack, toTransfer, false) * general.FROM_RF));
      } else if (inv.getStackInSlot(slotID).getItem() == Items.REDSTONE
          && storer.getEnergy() + general.ENERGY_PER_REDSTONE <= storer.getMaxEnergy()) {
        storer.setEnergy(storer.getEnergy() + general.ENERGY_PER_REDSTONE);
        inv.getStackInSlot(slotID).stackSize--;

        if (inv.getStackInSlot(slotID).stackSize <= 0) {
          inv.setInventorySlotContents(slotID, null);
        }
      }
    }
  }
 public static ItemStack addToEmptyInventorySlot(IInventory inventory, int slot, ItemStack stack) {
   if (!inventory.isItemValidForSlot(slot, stack)) {
     return stack;
   }
   int stackLimit = inventory.getInventoryStackLimit();
   inventory.setInventorySlotContents(
       slot, copyStackWithAmount(stack, Math.min(stack.stackSize, stackLimit)));
   return stackLimit >= stack.stackSize ? null : stack.splitStack(stack.stackSize - stackLimit);
 }
  protected void handleMouseClick(
      Slot p_146984_1_, int p_146984_2_, int p_146984_3_, int p_146984_4_) {
    if (p_146984_1_ != null && p_146984_2_ != -999) {
      Debug.println("p_146984_1_", p_146984_1_.slotNumber + "");
      Debug.println("p_146984_2_", p_146984_2_ + "");
      Debug.println("p_146984_3_", p_146984_3_ + "");
      Debug.println("p_146984_4_", p_146984_4_ + "");

      if (p_146984_1_ != null && p_146984_1_.getHasStack()) {

        ItemStack itemstack = p_146984_1_.getStack();
        ItemStack itemstack2;

        List recipes = CustomCraftingManager.getInstance().getRecipeList();

        for (int j = 0; j < recipes.size(); ++j) {
          IRecipe irecipe = (IRecipe) recipes.get(j);
          itemstack2 = irecipe.getRecipeOutput();
          if (itemstack2 != null) {
            if (itemstack2.getItem() == itemstack.getItem()
                && itemstack.getItemDamage() == itemstack2.getItemDamage()) {

              ItemStack[][] RezeptAr = irecipe.getRecipe(itemstack2);

              Debug.println(
                  irecipe.getClass().getName(),
                  irecipe.getClass().getName() + " " + itemstack2.getItemDamage());

              if (irecipe.getRecipe(itemstack2) != null) {
                craftResult.setInventorySlotContents(0, itemstack2);
                // irecipe.
                for (int i = 0; i < RezeptAr.length; i++) {
                  for (int ii = 0; ii < RezeptAr[i].length; ii++) {
                    recipefield.setInventorySlotContents((i * 3) + ii, null);
                    ItemStack Rezeptitem = RezeptAr[i][ii];
                    if (Rezeptitem != null) {
                      Debug.println(
                          i + "/" + ii,
                          Rezeptitem.getDisplayName() + " " + Rezeptitem.getItemDamage());
                      Rezeptitem.stackSize = 1;
                      recipefield.setInventorySlotContents((i * 3) + ii, Rezeptitem);
                    }
                  }
                }

                Debug.println("itemstack", itemstack.getDisplayName() + "");

                break;
              }
            }
          }
        }
      }
      ((CreativeInv.ContainerCreative) this.inventorySlots).scrollTo(this.currentScroll);
      // this.scrollTo(0.0F);
    }
  }
Example #16
0
  private void handleLeftoverItems(IInventory items) {
    for (int i = 0; i < items.getSizeInventory(); i++) {
      if (items.getStackInSlot(i) != null) {
        ItemStack output = items.getStackInSlot(i);

        if (output.stackSize <= 0) {
          items.setInventorySlotContents(i, null);
          continue;
        }

        boolean inserted = false;

        for (int j = 2; j <= 4; j++) {
          ItemStack target = getStackInSlot(j);

          if (target == null || target.stackSize <= 0) {
            setInventorySlotContents(j, output);
            inserted = true;
            break;
          } else {
            output.stackSize -= StackHelper.mergeStacks(output, target, true);
            if (output.stackSize == 0) {
              inserted = true;
              break;
            }
          }
        }

        if (!inserted) {
          if (output.stackSize > 0) {
            output.stackSize -=
                Utils.addToRandomInventoryAround(worldObj, xCoord, yCoord, zCoord, output);

            if (output.stackSize > 0) {
              InvUtils.dropItems(worldObj, output, xCoord, yCoord + 1, zCoord);
            }
          }
        }

        items.setInventorySlotContents(i, null);
      }
    }
  }
  @Override
  public void buildBlock(BptSlotInfo slot, IBptContext context) {
    super.buildBlock(slot, context);

    IInventory inv = (IInventory) context.world().getTileEntity(slot.x, slot.y, slot.z);

    for (int i = 0; i < inv.getSizeInventory(); ++i) {
      inv.setInventorySlotContents(i, null);
    }
  }
Example #18
0
 /**
  * We can assume that if null is passed for the container that the container was consumed by the
  * process and we should just remove the input container.
  *
  * @param inv
  * @param inputSlot
  * @param outputSlot
  * @param container
  */
 private static void storeContainer(
     IInventory inv, int inputSlot, int outputSlot, ItemStack container) {
   if (container == null) {
     inv.decrStackSize(inputSlot, 1);
     return;
   }
   ItemStack output = inv.getStackInSlot(outputSlot);
   if (output == null) inv.setInventorySlotContents(outputSlot, container);
   else output.stackSize++;
   inv.decrStackSize(inputSlot, 1);
 }
  public static ArrayList<ItemStack> dumpToIInventory(
      ArrayList<ItemStack> stacks, IInventory inventory) {

    boolean debug = false;
    ArrayList<ItemStack> remaining = new ArrayList<ItemStack>(); // returns the remainder

    ItemStack chestStack;

    for (ItemStack current : stacks) {
      if (current == null) {
        continue;
      }

      for (int i = 0; i < inventory.getSizeInventory(); i++) {

        if (current == null) {
          continue;
        }

        chestStack = inventory.getStackInSlot(i);

        if (chestStack == null) {
          if (debug) System.out.println("DUMP " + i);

          inventory.setInventorySlotContents(i, current);
          // and dont add current ot remainder at all ! sweet!
          current = null;
        } else if (chestStack.isItemEqual(current)) {

          int space = chestStack.getMaxStackSize() - chestStack.stackSize;

          int toDeposit = Math.min(space, current.stackSize);

          if (toDeposit > 0) {

            if (debug) System.out.println("merge " + i + " ; toDeposit =  " + toDeposit);
            current.stackSize -= toDeposit;
            chestStack.stackSize += toDeposit;

            if (current.stackSize == 0) {
              current = null;
            }
          }
        }
      } // finished current pass over inventory
      if (current != null) {
        if (debug) System.out.println("remaining.add : stackSize = " + current.stackSize);
        remaining.add(current);
      }
    }

    if (debug) System.out.println("remaining" + remaining.size());
    return remaining;
  }
 /**
  * Attempts to add the itemstack to a random slot in the inventory; failing that, it will add to
  * the first available slot
  *
  * @param numAttempts number of times to attempt random placement
  * @return the number of items remaining in the stack, zero if all were added
  */
 public static int addItemToInventoryAtRandom(
     Random rand, ItemStack stack, IInventory inv, int numAttempts) {
   for (int i = 0; i < numAttempts; ++i) {
     int slot = rand.nextInt(inv.getSizeInventory());
     if (inv.getStackInSlot(slot) == null) {
       inv.setInventorySlotContents(slot, stack);
       return 0;
     }
   }
   return addItemToInventory(stack, inv);
 }
 @Override
 public void clearGrid(EntityPlayer entityPlayer, Container container, int id) {
   try {
     IInventory craftMatrix = (IInventory) crafterField.get(container);
     for (int i = 0; i < 9; i++) {
       craftMatrix.setInventorySlotContents(i, null);
     }
     container.detectAndSendChanges();
   } catch (IllegalAccessException e) {
     e.printStackTrace();
   }
 }
  @Override
  public void onContainerClosed(EntityPlayer player) {
    super.onContainerClosed(player);

    for (int i = 0; i < 4; i++) {
      ItemStack stack = craftMatrix.removeStackFromSlot(i);

      if (stack != null) player.dropItem(stack, false);
    }

    craftResult.setInventorySlotContents(0, ItemStack.field_190927_a);
  }
  private static ItemStack func_102014_c(
      IInventory par0IInventory, ItemStack par1ItemStack, int par2, int par3) {
    ItemStack itemstack1 = par0IInventory.getStackInSlot(par2);

    if (canInsertItemToInventory(par0IInventory, par1ItemStack, par2, par3)) {
      boolean flag = false;

      if (itemstack1 == null) {
        int max =
            Math.min(par1ItemStack.getMaxStackSize(), par0IInventory.getInventoryStackLimit());
        if (max >= par1ItemStack.stackSize) {
          par0IInventory.setInventorySlotContents(par2, par1ItemStack);
          par1ItemStack = null;
        } else {
          par0IInventory.setInventorySlotContents(par2, par1ItemStack.splitStack(max));
        }
        flag = true;
      } else if (areItemStacksEqualItem(itemstack1, par1ItemStack)) {
        int max =
            Math.min(par1ItemStack.getMaxStackSize(), par0IInventory.getInventoryStackLimit());
        if (max > itemstack1.stackSize) {
          int l = Math.min(par1ItemStack.stackSize, max - itemstack1.stackSize);
          par1ItemStack.stackSize -= l;
          itemstack1.stackSize += l;
          flag = l > 0;
        }
      }

      if (flag) {
        if (par0IInventory instanceof TileEntityHopper) {
          ((TileEntityHopper) par0IInventory).setTransferCooldown(8);
          par0IInventory.onInventoryChanged();
        }

        par0IInventory.onInventoryChanged();
      }
    }

    return par1ItemStack;
  }
Example #24
0
 private static void fill(TileEntity tile, List<ItemStack> stacks, double factor) {
   if (tile instanceof IInventory) {
     IInventory chest = (IInventory) tile;
     Random rand = new Random();
     int num;
     for (int slot = 0; slot < chest.getSizeInventory(); slot++) {
       num = rand.nextInt((int) (stacks.size() * factor));
       if (num < stacks.size()) {
         chest.setInventorySlotContents(slot, stacks.get(num));
       }
     }
   }
 }
  @LuaMethod(
      returnType = LuaType.BOOLEAN,
      description = "Swap two slots in the inventory",
      args = {
        @Arg(type = LuaType.NUMBER, name = "from", description = "The first slot"),
        @Arg(type = LuaType.NUMBER, name = "to", description = "The other slot")
      })
  public boolean swapStacks(IComputerAccess computer, IInventory target, int from, int to)
      throws Exception {
    from--;
    to--;
    IInventory inventory = InventoryUtils.getInventory(target);
    if (inventory == null && target != null) {
      System.out.println(
          "OpenPeripheral Warning: (swapStacks) getInventory for the same inventory failed hard. That's a bug!!!");
      inventory = target;
    }
    if (from >= 0
        && from < inventory.getSizeInventory()
        && to >= 0
        && to < inventory.getSizeInventory()) {

      ItemStack stack1 = inventory.getStackInSlot(from);
      ItemStack stack2 = inventory.getStackInSlot(to);

      if (stack1 != null) {
        stack1 = stack1.copy();
      }
      if (stack2 != null) {
        stack2 = stack2.copy();
      }

      inventory.setInventorySlotContents(from, stack2);
      inventory.setInventorySlotContents(to, stack1);
      return true;
    }
    return false;
  }
 @LuaMethod(
     returnType = LuaType.VOID,
     description = "Destroy a stack",
     args = {
       @Arg(
           type = LuaType.NUMBER,
           name = "slotNumber",
           description = "The slot number, from 1 to the max amount of slots")
     })
 public void destroyStack(IComputerAccess computer, IInventory target, int slot) throws Exception {
   IInventory invent = InventoryUtils.getInventory(target);
   slot--;
   if (slot < 0 || slot >= invent.getSizeInventory()) {
     throw new Exception("Invalid slot number");
   }
   invent.setInventorySlotContents(slot, null);
 }
Example #27
0
  public static void DropItems(TileEntity tileEntity, int min, int max) {
    if (!(tileEntity instanceof IInventory)) {
      return;
    }

    IInventory inventory = (IInventory) tileEntity;
    World world = tileEntity.getWorld();
    BlockPos blockPos = tileEntity.getPos();

    for (int i = min; i <= max; i++) {
      ItemStack itemStack = inventory.getStackInSlot(i);

      if (itemStack != null && itemStack.stackSize > 0) {
        Random rand = new Random();

        float dX = rand.nextFloat() * 0.8F + 0.1F;
        float dY = rand.nextFloat() * 0.8F + 0.1F;
        float dZ = rand.nextFloat() * 0.8F + 0.1F;

        EntityItem entityItem =
            new EntityItem(
                world,
                blockPos.getX() + dX,
                blockPos.getY() + dY,
                blockPos.getZ() + dZ,
                itemStack.copy());

        if (itemStack.hasTagCompound()) {
          entityItem
              .getEntityItem()
              .setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy());
        }

        float factor = 0.05F;
        entityItem.motionX = rand.nextGaussian() * factor;
        entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
        entityItem.motionZ = rand.nextGaussian() * factor;
        world.spawnEntityInWorld(entityItem);
        itemStack.stackSize = 0;
        inventory.setInventorySlotContents(i, null);
      }
    }

    inventory.markDirty();
  }
  private static boolean insertStackFromInventory(
      Hopper par0Hopper, IInventory par1IInventory, int par2, int par3) {
    ItemStack itemstack = par1IInventory.getStackInSlot(par2);

    if (itemstack != null && canExtractItemFromInventory(par1IInventory, itemstack, par2, par3)) {
      ItemStack itemstack1 = itemstack.copy();
      ItemStack itemstack2 = insertStack(par0Hopper, par1IInventory.decrStackSize(par2, 1), -1);

      if (itemstack2 == null || itemstack2.stackSize == 0) {
        par1IInventory.onInventoryChanged();
        return true;
      }

      par1IInventory.setInventorySlotContents(par2, itemstack1);
    }

    return false;
  }
  @Override
  public ArrayList<ItemStack> getDrops(
      World world, int x, int y, int z, int metadata, int fortune) {
    ArrayList<ItemStack> dropList = super.getDrops(world, x, y, z, metadata, fortune);
    TileEntity te = world.getTileEntity(x, y, z);
    if (te instanceof IInventory) {
      IInventory iinv = (IInventory) te;
      for (int index = 0; index < iinv.getSizeInventory(); ++index) {
        ItemStack itemstack = iinv.getStackInSlot(index);
        if (itemstack != null) {
          dropList.add(itemstack);
          iinv.setInventorySlotContents(index, (ItemStack) null);
        }
      }
    }

    return dropList;
  }
  /** Generates the Chest contents. */
  public static void generateChestContents(
      Random par0Random,
      WeightedRandomChestContent[] par1ArrayOfWeightedRandomChestContent,
      IInventory par2IInventory,
      int par3) {
    for (int j = 0; j < par3; ++j) {
      WeightedRandomChestContent weightedrandomchestcontent =
          (WeightedRandomChestContent)
              WeightedRandom.getRandomItem(par0Random, par1ArrayOfWeightedRandomChestContent);
      ItemStack[] stacks =
          weightedrandomchestcontent.generateChestContent(par0Random, par2IInventory);

      for (ItemStack item : stacks) {
        par2IInventory.setInventorySlotContents(
            par0Random.nextInt(par2IInventory.getSizeInventory()), item);
      }
    }
  }