Solved How to create an array of Itemstacks?

Discussion in 'Plugin Development' started by Azrex, May 15, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Azrex

    So I want to create an array of itemstacks such as a diamond sword, food, armour etc and I want it to pick a random item from the array. How would I do this?
     
  2. Offline

    mythbusterma

    @Azrex

    Well your post pretty much seems like instructions on how to what you want to do.

    Make an array, fill it with itemstacks, pick a random one. Seems pretty straight-forward.
     
  3. Offline

    Azrex

    I was asking how I would do it. Im new to Java.... a little bit of code?
     
  4. Offline

    Reynergodoy

    you read my mind O_O
    i was just trying to do that
     
  5. Offline

    Azrex

  6. Offline

    BagduFagdu

    Code:
    ArrayList<ItemStack[]> items = new ArrayList<ItemStack[]>();
     
  7. @BagduFagdu Thats an ArrayList, not an Array.. :p

    How to define an Array:
    Code:
    ItemStack[] items = new ItemStack[]{item1, item2, item3, item4};
    How to pick a random out of it:
    Code:
    Random r = new Random(); //Define the random object
    int index = r.nextInt(items.length()); //With .nextInt(x) it generates a random between 0 and x-1. (x is the size of the array)
    ItemStack item = items[index]; //Get the item from the array at the index
     
    BagduFagdu likes this.
  8. Offline

    B3N909

    Please mark the thread as solved.
     
  9. Offline

    BagduFagdu

    Oh, that array.
     
  10. Offline

    Tecno_Wizard

    @BagduFagdu, please learn java before you continue.
     
  11. Offline

    Azrex

    Thanks So Much @FisheyLP
    Just wanted to ask, how would I continue with that random item?
    player.SendMessage("You Got (NAME OF ITEM)")
     
  12. I explained this too... Look at my post above again
    To get the name of an ItemStack use
    Code:
    item.getType().name().toLowerCase()
     
  13. Offline

    Azrex

    Thanks <3

    @FisheyLP Im not able to add items to the array. It gives me an error that cannot convert from Material to ItemStack

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  14. Offline

    meguy26

    @Azrex
    1. Learn Java! You cannot convert Material to ItemStack, Material.DIRT is not an ItemStack!

    To create an ItemStack:
    Code:
    ItemStack test = new ItemStack(Material.DIRT); //Creates a dirt ItemStack
    
    ItemStack test2 = new ItemStack(Material.STONE); //Creates a dirt ItemStack
    
    ItemStack[] test3 = {test, test2}; //Do whatever with it
     
  15. Offline

    Azrex

    hmm I tried that before but it didn't work..... now it does, I might have done something wrong.
    Anyways Thanks :D
     
Thread Status:
Not open for further replies.

Share This Page