Random Drop Item

Discussion in 'Spigot Plugin Development' started by julien0312, Mar 8, 2018.

  1. Hello, I would want to make a random drop of items. Thank you for your help

    Code (Text):
                                List<Material> list = Arrays.asList(Material.SNOW_BALL, Material.DIAMOND_LEGGINGS, Material.CHAINMAIL_CHESTPLATE, Material.IRON_SWORD);
                                Random random = new Random();
                                for(int i = 0; i < 10; i++) {
                                    chest.getWorld().dropItemNaturally(l, list.get(random.nextInt(list.size())));                              
                                }
     
  2. What is the issue? Everything looks correct unless I missed something.
     
  3. I have a problem in this line
    chest.getWorld().dropItemNaturally(l, list.get(random.nextInt(list.size())));
     
  4. Are you only having the problem sometimes?
    The random integer could be zero sometime resulting in a error.

    If this is the case you'd have to check if it equals zero, if so get another random integer.
     
    • Funny Funny x 1
  5. Ok, so add 1 to it.

    Not only that, the latest version of Spigot to my understanding will not
    support the use of item ids.
     
  6. You need add to location:

    l = l.add(0.5, 0.5, 0.5) // Tested in my server
     
  7. What is the actual exception? Stack trace? Exact behaviour?
    It doesn't matter in this case anyway because you are trying to pass a material into that method instead of an item stack. Please look at the documentation of a method before asking why it might not be working.

    In fact, your IDE should have pointed this out clearly unless you decided to ignore it.

    And please, if you want to get help easier attatch the information I stated prior.
     
    • Agree Agree x 1
  8. No, list indexes start at 0.
     
  9. Are nobody seeing that he's trying to drop a "Material". You need to create an ItemStack from this material.

    Code (Java):
    List<ItemStack> drops = new ArrayList<ItemStack>();
    drops.add(new ItemStack(Material.APPLE, 1));
    chest.getWorld().dropItemNaturally(l, drops.get(random.nextInt(list.size() - 1)));
    Pretty sure the IDE should've made this pretty clear to you. Also, when selecting index, you should always subtract one (as I've done above), as if the list has 1 element, then this element will be at index 0 (even though #size() returns 1), and landing at 1 with the random will cause an ArrayOutOfBoundsExcepton.
     
    • Like Like x 1
    • Funny Funny x 1
  10. No need to do it using a Random#nextInt method since the max value is already max - 1.
     
  11. I tried your techniques but nothing works
     
  12. Read your IDE’s error...
     

Share This Page