Items adding a Stacksize to a Arraylist

Discussion in 'Spigot Plugin Development' started by GoRoK, Jun 14, 2016.

  1. Code (Text):

    public void onPlayerClick(PlayerInteractEvent e)
            {
                items.add(Material.DIAMOND);
                Inventory inv = Bukkit.createInventory(null, InventoryType.CHEST, ChatColor.AQUA + "                [SG]Chest");
                Random rnd = new Random();
                int n = rnd.nextInt(6);
               
               
               
               
               
               
                itemslist.add(new ItemStack(Material.GOLDEN_APPLE));
                itemslist.add(new ItemStack(Material.APPLE));
                itemslist.add(new ItemStack(Material.WOOD));
                itemslist.add(new ItemStack(Material.STONE));
                itemslist.add(new ItemStack(Material.BRICK));
                itemslist.add(new ItemStack(Material.DIRT));
                itemslist.add(new ItemStack(Material.ANVIL));
                itemslist.add(14, new ItemStack(Material.ARROW));
                itemslist.add(14, new ItemStack(Material.ARROW));
                itemslist.add(14, new ItemStack(Material.ARROW));
                itemslist.add(14, new ItemStack(Material.ARROW));
                itemslist.add(14, new ItemStack(Material.ARROW));
     
    Hey! I want to add to the ItemStack a Stacksize, so short explanation of the Plugin.

    It generate a virtual Inventory, in that Inventory i want to put XX Items from the above, im using a Arraylist<ItemStack>.
    If i explained that you dont understand:

    I want to add to:
    itemslist.add(STACKSIZE, new ItemStack(Material.ARROW));

    and i dont know how to.

    Thank you for help.
     
  2. You mean you just want to set the amount of the items? Just use:
    Code (Text):
    new ItemStack(Material.ARROW).setAmount(5);
    Obviously replacing 5 with the actual stack size
     
    • Agree Agree x 2
  3. Oh man, didnt thought about "behind", thank you very much!
     
  4. btw, is there a way to add a "chance" how often the item gets in the chest?


    And its not working on "itemslist.add(new ItemStack(Material.ARROW).setAmount(5));"
    is:
    Code (Text):
    itemslist.add(new ItemStack(Material.ARROW).setAmount(5));
    The method add(ItemStack) in the type ArrayList<ItemStack> is not applicable for the arguments (void)
     
  5. Yes, in my envoys plugin I use this:

    Code (Text):
    Chest chest = (Chest) b.getState();

    Inventory i = chest.getInventory();int random = r.nextInt(100);
    if(chance >= random) {
        ItemStack stack = new ItemStack(Material.getMaterial(id));
        i.addItem(stack);
    }
     
  6. Thanks you for that, any solution for the problem with the stacks?
     
  7. Create the stack before you add it to the array, like this:

    Code (Text):
    ItemStack stack = new ItemStack(Material.ARROW).setAmount(5);
    i.addItem(stack);
     
  8. Just copied your code, error:
    Type mismatch: cannot convert from void to ItemStack
     
  9. new ItemStack(Material.ARROW, 5);
     
    • Winner Winner x 1