Solved Updating items

Discussion in 'Spigot Plugin Development' started by SmaxoS, Jun 22, 2018.

Thread Status:
Not open for further replies.
  1. Hi, I have this code:
    Code (Java):
        private static int r;

        @EventHandler
        public void onOpen(InventoryOpenEvent e) {
            r = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable() {

                @Override
                public void run() {        
                    addItem(itemMethod, e.getInventory());
                    addItem(itemMethod, e.getInventory());
                    addItem(itemMethod, e.getInventory());
                }          

            }, 0, 10);
        }
     
        @EventHandler
        public void onClose(InventoryCloseEvent e) {      
            Bukkit.getScheduler().cancelTask(r);    
        }
    But when many players have openned inventory, than cannot cancel all tasks. How can I fix it?
     
  2. Because when multiple players open the inventory, you're constantly assigning "r". E.g if player1 opened an inventory, that would assign the task run to r. But when player2 opens an inventory, it would repeat what happened with player1, only player1's value of "r" is completely lost. (If all that made sense) I'd personally use a static hashmap for this instead of a static int, the HashMap being assigned like this:

    Code (Text):
    HashMap<Player, Integer> players = new HashMap<>();
    And just use r as a local variable instead of a global static one.
     
    • Informative Informative x 1
  3. Ou, thank you, but me and HashMaps, that's not good. :D

    Code?
    Code (Java):
        private static HashMap<Player, Integer> players = new HashMap<>();
        private static int r;

        @EventHandler
        public void onOpen(InventoryOpenEvent e) {

            r = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable() {

                @Override
                public void run() {              
                    addItem(itemMethod, e.getInventory());
                    addItem(itemMethod, e.getInventory());
                    addItem(itemMethod, e.getInventory());
                }          

            }, 0, 10);
           
            players.put((Player) e.getPlayer(), r);

        }
       
        @EventHandler
        public void onClose(InventoryCloseEvent e) {
            players.remove(e.getPlayer());
            Bukkit.getScheduler().cancelTask(r);  
        }
    Sorry, but I don't know how to use this HashMap. :D :/
     
  4. Hm, close. Remove this line at the top:

    Code (Text):
    private static int r;
    And swap these two lines:

    Code (Text):
    Bukkit.getScheduler().cancelTask(players.get(e.getPlayer());
    players.remove(e.getPlayer());
     
    But it's absolutely fine. HashMaps are pretty easy to use when you get the hang of them. ;)

    EDIT: Oh yeah. And change this line as well so Java knows "r" is an int (it's now a local variable):

    Code (Text):
    int r = Bukkit.getScheduler().scheduleSyncRepeatingTask... // Ongoing code
     
    • Friendly Friendly x 1
  5. Thank you very much. It's working. Have a nice day!
     
    • Friendly Friendly x 1
Thread Status:
Not open for further replies.