Random item

Discussion in 'Spigot Help' started by Inspectiver, Feb 9, 2020.

  1. As part of a personal project I need a way to pick a random item from minecrafts full list of items/blocks.

    Previously I was using someone elses plugin, however it only supported up to 1.14.1, so it wasn't giving us any of the latest items.

    How can I achieve this in code?

  2. I think this should work, please correct me if I am wrong, couldn't test it yet.

    Code (Text):
    Random random = new Random();
    Player player = ...
    int randomIndex = random.nextInt(Material.values().length);
    ItemStack randomItem = new ItemStack(Material.values()[randomIndex]);
    • Like Like x 1
    • Winner Winner x 1
  3. Strahan


    Just remember to make that Random object outside the scope of the function so it isn't being created each time, that or use ThreadLocalRandom.
    • Winner Winner x 1