How to get a Random Itemstack

Discussion in 'Spigot Plugin Development' started by Computerdude3, Jun 1, 2017.

Thread Status:
Not open for further replies.
  1. My plugin needs to get a random itemstack from every one in the game (including Damages). Is there a clean way to do it without hard coding each item to an arraylist?
  2. for (Material material : Material.values()) {
    player.getInventory().addItem(new ItemStack(material));
  3. That won't do what op wants.

    OP, outside of keeping your own list, you could potentially just download one and use that. And that data doesn't need to be in your code, it could be a config file.
  4. Okay, I'll just do that then. Thanks for replying.
  5. EDIT
    Sorry, didn't read it to good, so this answer is not you are looking for!

    You need to use Random from java:

    Code (Text):

    Random random = new Random();
    int material = random.nextInt(Material.values().length);
    int amount = random.nextInt(maxAmount);
    new ItemStack(Material.values()[material], Math.min(Material.values()[material].getMaxStackSize(), amount), damage);
    I don't know if Materials.values() returns an array or a list, so maybe it is Material.values().size() and Material.values().get(material)
    To get a random damage is not easy because you don't know what damage values that item can have! So you need to hardcode that.
    #5 Remceau, Jun 1, 2017
    Last edited: Jun 1, 2017
  6. Obviously not. The main point of my post was to show that he can just use the 'values()' method from the Material enum. He should be able to figure out randomization from there.
  7. Should this work?

    Code (Text):
    ArrayList<ItemStack> itms = new ArrayList<ItemStack>();
            for(Material m : Material.values()) {
                if(m.getMaxDurability() > 1) {
                    for(int i = 0; i < m.getMaxDurability(); i++) {
                        itms.add(new ItemStack(m, 1, (byte) i));
                } else {
                    itms.add(new ItemStack(m, 1));
  8. - Max durability only applies to items that can be damaged (like swords/pickaxes/flintAndSteel/etc)
    - Some Materials are not actually obtainable as an item (cropsBlocks/brewingStandBlock/etc), so you can't just randomly select from all materials in the enum
    - Some items that are obtainable have data values that only apply in block-form (leafDecay/logRotation)
  9. Is there a way to get the highest data value of an item? Also, I can filter out nonobtainable items and block data values.
  10. I don't think there is an easy way... there are just to many variables. You have to hardcode it.
  11. Alright, thanks anyway!
  12. You fucked my mind with that code...
    What the hell are you doing writing the var material into an array where any array exists? Why the hell do you define vars instead of getting it directly? Why the hell you don't add one to the random due to 0 is not a valued value (it's like clean inventory and there's a specific method for that). Your code has a lot of mistakes and it's only 4 lines; won't read any plugin of yours.

    Oh, and please respect conventions and mark "random" as a final value as you're not longer updating it.
    • Funny Funny x 1
  13. Holy crap... didn't expect that

    It is just an example
    Arrays start at 0, not at 1
    final is just weird if you use it in a methode scope
    the vars make it more easy to look at
    I have only got one resource and that is an api
    Do you have your own programming language??? i do
    Do you make 3D games???? i do
    It was quick and dirty, not something i would write normally
  14. Bruh. You had some proud moment or something?
    • Friendly Friendly x 1
  15. Someones having a hard day ;)
    • Agree Agree x 1
  16. Thread least if I know what I am doing
Thread Status:
Not open for further replies.