Auto Updating Inventory

Discussion in 'Plugin Development' started by NicksTheBoss, Dec 24, 2014.

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

    NicksTheBoss

    So, I want my inventory (that I'm viewing) to update with a running method which I have setup but the code to make it update doesn't seem to work. What can I do for it to work?

    Here's the code:
    Code:
          public static void setupRefreshNavSG() {
                Bukkit.getScheduler().scheduleSyncRepeatingTask(CodexCore.instance, new Runnable() {
                    @SuppressWarnings("deprecation")
                    public void run() {
                       
                        for(Player onlinePlayers : Bukkit.getOnlinePlayers()) {
                            if(onlinePlayers.getOpenInventory().getTitle().equalsIgnoreCase(NavSG.Inv().getTitle())) {
                                for(HumanEntity he : onlinePlayers.getOpenInventory().
                                        getTopInventory().getViewers()) {
                                        if(he instanceof Player) {
                                        Player viewer = (Player) he;
                                        viewer.updateInventory();
                                        }
                                        }
                            }
                        }
                    }
                }, 0L, 20L);
          }
     
  2. Offline

    teej107

    Why? The inventory already updates when you pick up, drop, or move an item.
     
  3. Offline

    NicksTheBoss

    @teej107
    No because the inventory contains an emerald block showing the state of the SG game. So when its in waiting or voting or warmup its an emerald block when it goes in-game it becomes a gold block and when it becomes deathmatch a redstone block. But for you to see these blocks appear you have to reopen the inventory. If the inventory updated itself then they can keep the inventory open to view the games status.
     
  4. Offline

    RainoBoy97

    You need to update the lore of the item every time the game state changes.
     
  5. Offline

    NicksTheBoss

  6. Offline

    WarmakerT

    Code:
        public static void setupRefreshNavSG() {
            new BukkitRunnable() {
                public void run() {
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        if (p.getOpenInventory() == null) {
                            continue;
                        }
    
                        if (p.getOpenInventory().getTitle().equals("A")) {
                            p.updateInventory();
                        }
                    }
                }
            }.runTaskTimer(CodexCore.instance, 0L, 20L);
        }
    If you're already going through every single Player, you don't need to get the viewers of the chests, since they'll already be included in Bukkit.getOnlinePlayers()
    This code should work.
     
Thread Status:
Not open for further replies.

Share This Page