Solved Hashmap itemstack

Discussion in 'Plugin Development' started by xepisolonxx, Feb 28, 2015.

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

    xepisolonxx

    My promblem is to get the all the items in a players hashmap and get a random number within it
    Lists class:
    Code:
        public static HashMap<String, ItemStack> pi = new HashMap<String, ItemStack>();
    
    Example of how i put items in hashmap
    Code:
                                    Lists.pi.put(player.getName(), horsetamer);
    
    Broken items:

    How im trying to get the size of the player hashmap
    Code:
                       int size = Lists.pi.get(p.getName()).getAmount();
    
    This is how im trying to get random itemstack from player hashmap
    Code:
                         I
    temStack mat = Lists.items.get(p.getName().indexOf(index));
    
     
  2. Is this what you want ? i think i didn't understand your problem
    Code:java
    1.  
    2. public Map<String, ItemStack> items = new HashMap<>();
    3.  
    4. //This should work
    5. public ItemStack getRandomItem() {
    6. List<String> list = new ArrayList<>();
    7. for(String name : items.keySet()) {
    8. list.add(name);
    9. }
    10. Integer random = new Random().nexInt(list.size());
    11. return items.get(list.get(random));
    12. }
    13.  

    If you wanna store something use player UUID instead of his Name
     
  3. Offline

    xepisolonxx

    Everytime i add something to the items hashmap it only shows up the last one i put in i want it so that i can put multiple itemstacks without using ItemStack[] and getting a random item by number in hashmap
     
  4. Offline

    Zombie_Striker

    You can't then. There can only be one ItemStack that get paired to each String. I would really suggest using a ItemStack[], which would make this much easier. If you are completely against ItemStack[]s, add to the end of your string a number (e.g. List.put(p.getName()+"1",item);)
     
  5. Offline

    mythbusterma

    @xepisolonxx

    Use an ArrayList, that's your best bet (or LinkedList). Add all the items to it and chose a random index to get from. Quite simple.
     
  6. Offline

    sebcio98

    @xepisolonxx Just wanted to say that you don't have to use "Lists.pi.put...", just "pi.put...". Same with get.
     
  7. @sebcio98 He's getting it from another class probably
     
  8. Offline

    Henzz

    @xepisolonxx
    As what mythbusterma said, replace itemstack with an arraylist of itemstack in your map. Since you've implied that you want to get random item out of their stored items.
    Then create a getRandomItem(Player p, Map<String , ArrayList<ItemStack> map) method (If you're actually gonna store this to a file or something, you might want to store their UUIDs instead of their names).
    1. Check if the map contains player name given from local variable, otherwise return null.
    2. Then create a list of itemstack and assign map.get(p.getName()) to the variable.
    3. Then to get a random item, list.get(new Random().nextInt(list.size())).
     
  9. Oh man that's something different
    Code:java
    1.  
    2. //You only need a list for that
    3. List<ItemStack> items = new ArrayList<ItemStack>();
    4.  
    5. //You must check if the list ain't empty
    6. public ItemStack getRandomItem() {
    7. Integer random = new Random().nexInt(items.getSize());
    8. return items.get(random);
    9. }
    10.  
     
  10. Offline

    Henzz

    @MaTaMoR_
    What you mean, thats something different?
    Besides that let's try not to spoon feed him some code so he can try this out himself. :)
     
  11. Something different from what i thought was his problem, is not a big spoon feed .
     
  12. Offline

    xepisolonxx

    Figured it out
     
    Last edited: Mar 7, 2015
Thread Status:
Not open for further replies.

Share This Page