Solved Getting random items in inventory

Discussion in 'Plugin Development' started by CONTREKEE, Jul 12, 2016.

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

    CONTREKEE

    So what I am trying to do is get a random item every time in a inventory, and every time, its a different item.

    Code:
                            Random object2 = new Random();
                            final int number2;
                                number2 = 1+object2.nextInt(26);
                           
                                boxtime.put(player, 27);
                               
                               
                            boxtask.put(player, new BukkitRunnable() {
                                public void run() {
                                   
                                    boxtime.put(player, boxtime.get(player) -1);
                                   
                                    player.getWorld().playSound(player.getLocation(), Sound.ZOMBIE_WOODBREAK, 1, 1);
                                   
    
                                    Random object3 = new Random();
                                    int number3 = 0;
                                    for(int counter = 1; counter<=1; counter++) {
                                        number3 = 1+object3.nextInt(26);
                                       
                                        if(number3 == number2 || commonBoxe.getItem(number3).equals(XXX)) {
                                            number3 = 1+object3.nextInt(26);
                                           
                                        }
                                       
                                       
                                    }
                                   
                                   
                                   
                                   
                                    for(ItemStack items : commonBoxe.getContents()) {
                                       
                                            if(!items.equals(XXX)) {
                                                if(number3 != number2) {
                                                    if(!commonBoxe.getItem(number3).equals(XXX)) {
                                                        commonBoxe.setItem(number3, XXX);
                                                    }
                                                       
                                                    }
                                                }
                                               
                                               
                                            }
    
                                   
                                   
                                   
    
                                   
                                   
                                   
                                   
                                    if(boxtime.get(player) == 1) {
                                        player.getInventory().addItem(commonBoxe.getItem(number2));
                                        player.sendMessage(prefix1 + gray + "You have won " + green + commonBoxe.getItem(number2).getItemMeta().getDisplayName().toString());
                                       
                                       
                                    }
                                   
                                   
                                    if(boxtime.get(player)  == 0) {
                                        player.closeInventory();
                                        boxtime.remove(player);
                                        boxtask.remove(player);
                                        cancel();
                                    }
                                   
                                   
                                }
                        });
                            boxtask.get(player).runTaskTimer(this, 10, 10);
                      
                        return;
     
  2. Offline

    mine-care

    Ok nice.

    So what can we help you with here?
     
  3. Offline

    CONTREKEE

    Okay, so what I am trying to do is get a different item in an inventory, every time it goes through the BukkitRunnable, except for one item that is selected before the BukkitRunnable starts.

    So every item, except for the item selected before the BukkitRunnable, will eventually be set to bedrock for example.

    Every time it loops through the BukkitRunnable, one item, at random, will be set to bedrock.

    I'm not sure if I explained this well enough.
     
  4. @CONTREKEE Why are you making multiple randoms? Why not use the same one?
     
  5. Offline

    CONTREKEE

    The first random number gets a random item in this inventory
    http://prntscr.com/bsau2s

    The second random is different every time, and sets the item in the inventory to bedrock.
     
  6. @CONTREKEE You can still use the same one, they don't generate the same number, it will always be random.
     
  7. Offline

    CONTREKEE

    Do you understand what I am trying to do?
    Because right now, I am confused and don't know how to fix it.
     
  8. Offline

    mine-care

    @CONTREKEE Still i am not sure i understand.
    Are you trying to randomize all itemsin an inventory except from one that is selected beforehand?
     
  9. Offline

    CONTREKEE

    Yes, I am trying to get a random item in the inventory except for the item selected beforehand.

    After the random items are selected, I'm trying to make it so they cannot be selected again.
     
  10. Offline

    mine-care

    Thats not exactly what i said, but anyways. What i understand now is that you are trying to put a new item in some slot of the inventory that is not the same as an item chosen beforehand.
    Right, for that you will have to:
    1. create the runnable
    2. when the task runs you will create a random itemstack (get a random material, ammount etc.)
    3. then add the item to the inventory. Repeat the task as many times as you want ;)
     
  11. Offline

    CONTREKEE


    I'm not trying to do that.
    This is what I am trying to do.
    https://giphy.com/gifs/LnCZkn4NVTK4o
     
  12. Offline

    mine-care

    Aha! now i understand! :D
    Right, so you will make a bukkit runnable that repeats every 2 ticks per say. outside the runnable there will be an int indicating the slot of the item that will not be overwritten. Lets call that 'excludeslot' for now. The code will look sometihng like this:
    Code:
    int excludeSlot = //the slot of the item that will not be changed
    
    onRun{
          int randomslot = //get a random int between 0 and 27
          while( randomslot equals excludeSlot OR the item in that slot is red glass pane)then:
              randomslot = //new random integer so that the excludeSlot will not be overwritten.
          end loop;
           //here you set the item in the inventory at slot 'randomslot' to red glass pane ;)
           //you need to stop this task from executing when the onRun() method has executed 26 times.
    }
    
     
  13. Offline

    CONTREKEE

    Okay, thanks!
    I'll test it out right now. :)

    So the problem here is, is that the random number has the chance of being the same number that it was before.
    I'm not sure how I could store the numbers to detect if that number was already used before.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 13, 2016
  14. Make a list of numbers you have used, then check against that before you select a new one.
     
  15. Offline

    CONTREKEE

    A while loop tends to lag out the server even before it gets the chance to get to the break.
    Any suggestions for that?

    Nevermind, I got it working!
    Thanks @mine-care & @AlvinB
     
    Last edited: Jul 13, 2016
  16. Use List.contains()
     
  17. Offline

    mine-care

    @AlvinB @CONTREKEE
    Thats what the loop is there for.
    If the randomSlot == excludeSlot, OR the item in the slot with ID randomSlot is the cover item (In the gif sent by the OP it was glass pane stained red) then find a new random ;)

    You are right it does indeed. I need to think of a better way without the while (i used a similar arrangement in a cmd aplication but servers are a whole differnt storry) in the mean time you can run asynchronously but yet again this is not the most efficient method on earth :/ the reason the while is there is to make sure there wont be overlaps so essentially you wont cover the same slot 2 or more times .
     
  18. Offline

    CONTREKEE


    Yeah, the while loop will work for now. :)
    Thanks again!
     
  19. Offline

    mine-care

Thread Status:
Not open for further replies.

Share This Page