Randomizing a players inventory

Discussion in 'Plugin Development' started by kameronn, Aug 24, 2016.

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

    kameronn

    how would i randomize a players inventory, ive spent hours on this and its still not working its honestly frustrating heres my code.

    Code:
                          List<ItemStack> list = new ArrayList<ItemStack>();
                         
                        
                       int n = Math.round(r.nextInt(35) + 1);
    
                       for (ItemStack item : entity.getInventory().getContents()) {  
                           if(item != null) {
                               if(!list.contains(item)) {
                                   list.add(item);
                                 entity.getInventory().remove(item);
                               }
                           }
                         }
    
                       for(int i = 0; i < list.size();i++) {
                           if(entity.getInventory().getItem(n).getType() == Material.AIR || entity.getInventory().getItem(n) == null) {
                           entity.getInventory().setItem(n, list.get(i));
                           }
                       }
     
  2. Offline

    aztk

    If you want to add random items to player inventory, you should have the list of all Materials, then code checks if boolean chance (for example 1%) is true on every of these Materials, create ItemStack and add it to player's inventory.
     
    Last edited: Aug 24, 2016
  3. Offline

    kameronn

    @aztk
    lol.... im randomizing the players inventory not adding items to it
     
  4. Offline

    Lordloss

    You should better explain what exactly isnt working.

    I see multiple things in your code that maybe make some problems:
    - Why do you round an Integer?
    - You check first if the list allready contains a specific item, maybe to prevent adding it twice. 1. This would be the job of a Set, not ArrayList 2. What if there are multiple same stacks of items in the inv? They will all be lost but one.
    - Next you want to add them back in a random order. So you iterate over your list, and check if the slots are empty. They must be empty, because you cleared the whole inv out in the first loop.
    - You try to set every stack to the slot n, which is the same number all the time.

    my approach would be:
    - save the inventory contents to a List, no looping needed.
    - call Collections.shuffle on the List.
    - clear out the Inventory.
    - Iterate over the list, call Inventory.addItem for each stack.
     
  5. Offline

    kameronn

    Well I tried to do what you suggested but it didnt work
    Here's my code
    Code:
                         List<ItemStack[]> list = new ArrayList<ItemStack[]>();                     
                        
                         list.add(entity.getInventory().getContents());
                        
                         Collections.shuffle(list);
                           
                         entity.getInventory().clear();
                        
                        
                         for(int i=0;i < list.size();i++) {
                         for(ItemStack item : list.get(i)) {
                             if(item != null) {
                             entity.getInventory().addItem(item);
                            
                         }
                         }
     
  6. Offline

    Zombie_Striker

    @kameronn
    That is because you are storing an itemstack ARRAY inside the list. Remove the brackets "[]" from the List parameters. After that, add the individual itemstack instances to the collection instead of adding the whole itemstack array instance.
     
  7. Offline

    kameronn

    @Zombie_Striker
    Well, I decided to go about it another way and its still not working. I'm doing exactly what you said but instead im using a different kind of loop. It's passing through the iterator after doing some bebugging but I feel like it's not passing through the check to see if the slot its trying to set the item to is open.
    Code:
                                    List<ItemStack> list = new ArrayList<ItemStack>();
    
                                    for (ItemStack items : entity.getInventory().getContents()) {
                                        list.add(items);
    
                                    }
                                    entity.getInventory().clear();
    
                                    Random random = new Random();
                                    Iterator<ItemStack> items = list.iterator();
    
                                    while (items.hasNext()) {
                                        if (items.next() != null) {
                                            int r = random.nextInt(35);
                                            if (entity.getInventory().getItem(r) == null
                                                    || entity.getInventory().getItem(r).getType() == Material.AIR) {
                                                entity.getInventory().setItem(r, items.next());
                                               
                                                items.remove();
                                            }
    
                                        }
                                    }
                                }
     
  8. Offline

    Lordloss

    Well you are just overcomplicating things. Once again, if you clear out an Inventory completely, why do you check if the slots are empty after this? Of course they are.
    Also you dont need an Iterator for such a simple thing.

    Create New ArrayList
    Call List.addAll(getInventory().getContents())
    shuffle the thing up like i said before
    Clear inventory
    And the adding is a simple one-liner: for (ItemStack i : list) inventory.addItem(i);

    Only 5 lines needed. I hope you got it to work now, that felt very much like spoonfeeding, but i think it was the only way to show you how easy it is.
     
  9. Offline

    kameronn

    Well thats how I had it before and I did get it working, but what I'm trying to do now is get a random slot in the players inventory and set the item there
     
  10. Offline

    Lordloss

    Hm then i must got those posts wrong if you say its working :p
    An Iterator is still not needed for this, i give you my suggestion in pseudocode.

    While List Is Not Empty:
    get a random slot number
    check if item at this position is null
    if so, add the item with list.get(0)
    and remove the index 0 from your list

    be careful with whiles, if you have some logical issue it is gonna break your main thread.
     
  11. Offline

    kameronn

    @Lordloss
    I'm doing that now and I'm getting a current modification error, most likely because I'm removing it
    Code:
                                    List<ItemStack> list = new ArrayList<ItemStack>();
    
                                    for (ItemStack items : entity.getInventory().getContents()) {
                                        list.add(items);
    
                                    }
                                    entity.getInventory().clear();
    
                                    Random random = new Random();
                                    Collections.shuffle(list);
    
                                        for(ItemStack item : list) {
                                            int r = random.nextInt(35);
                                            if (entity.getInventory().getItem(r) == null || entity.getInventory().getItem(r).getType() == Material.AIR) {
                                                entity.getInventory().setItem(r, item);
                                               
                                                list.remove(entity.getInventory().getItem(r));
                                           
    
                                        }
                                    }
                                }
    
                            }
     
  12. Offline

    kameronn

  13. Well, of course you're getting the error. Going through a list and editing it while going through it will run you into some problems. To solve this, just use an iterator (pseudo code below).
    Iterator variable;
    while the iterator has a next value
    set slot in entity's inventory to item
    iterator.remove
     
  14. Offline

    Lordloss

    Thats because youre again not doing what i suggested you to do. And of course youre lacking basic java. My approach is very simple, you shouldnt have any problems with it.
     
Thread Status:
Not open for further replies.

Share This Page