Solved Changing an item in an openInventory

Discussion in 'Plugin Development' started by Theztc, Jun 3, 2021.

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

    Theztc

    So I'm trying to make crate keys... And I want the items to change while the GUI is open. I've tried numerous ways to do this, and I'm out of ideas. Nothing seems to be working...
    I'm trying to make each item move left once every x ticks. But when I set the variables it just stays the same when it updates.

    Here's my code
    Code:
    Integer timeBetween = 20;
           
        for(int a = 0;a< 20; a++) {
            slotOne = slotTwo;
            slotTwo = slotThree;
            slotThree = slotFour;
            slotFour = mainSlot;
            mainSlot = slotFive;
            slotFive = slotSix;
            slotSix = slotSeven;
            slotSeven = slotEight;
    
            //Constructor and variable are elsewhere. They shouldn't be effecting anything
            randomInt = rand.nextInt(range);
            slotEight = voteKeyReward(randomInt);
            timeBetween++;
    
            i.setItem(9, slotOne);
            i.setItem(10, slotTwo);
            i.setItem(11, slotThree);
                i.setItem(12, slotFour);
            i.setItem(13, mainSlot);
            i.setItem(14, slotFive);
            i.setItem(15, slotSix);
            i.setItem(16, slotSeven);
            i.setItem(17, slotEight);
               
            new BukkitRunnable() {
                @Override
                public void run() {       
                    player.updateInventory();
                }}.runTaskLater(plugin, timeBetween);
        }
    Inventory(i) is already open when this is running. All I'm trying to do is make each item move left and get a new item in the most-right slot.
     
  2. Offline

    KarimAKL

    @Theztc You want the code that moves the items inside of the Runnable.

    Also, I do not think there is a need to update the inventory, but I guess it does not hurt to try.
     
  3. Offline

    Theztc

    updateInventory wasn't my first choice lol. Just trying everything at this point. When I put my setItem code inside of the runnable it gives error "Local variable slotOne defined in an enclosing scope must be final or effectively final".
    But I wouldn't want to make those final correct?
     
  4. Offline

    KarimAKL

    @Theztc I might be misunderstanding something, but I do not see any reason for those variables, you could just use #setItem(slot, #getItem(slot + 1)).
     
  5. Offline

    Theztc

    Ah that's an idea. Let me try that

    Edit. So wait I'm confused. How would I have a variable that I can change the number in it like that? Unless I'm misunderstanding (More than likely the case lol) @KarimAKL
     
    Last edited: Jun 3, 2021
  6. Offline

    KarimAKL

    @Theztc Could you post your current code? I only have the old code to refer to about your current problems.

    An elaboration about your current problem would also help.
     
  7. Offline

    Theztc

    I will in a sec, but here's the issue in depth - I have a constructor that takes a random Int and returns an ItemStack. So I run it 9 times to get 9 random items to put into the middle row of the openInventory. So then I'd like to simulate "rolling" the crate key which I want to do by each item moving to the left and the right most slot being replaced by a new item. When I've moved left x amount of times the Item in the middle would be the item that they won. What I'm having trouble with is, when I open the original inventory, I have trouble making items switch in it (each item moving left). When I setItem to what I want the next instance to be and then either refresh or reopen inventory it never changes from the original. Yeah my code to set the item slot after could use a massive clean up, but that isn't what's going wrong.

    EDIT: Marking as solved, I just made a separate inventory each time using a constructor rather than trying to paste over the same one.
     
    Last edited: Jun 4, 2021
Thread Status:
Not open for further replies.

Share This Page