Update Inventory in repeating task?

Discussion in 'Spigot Plugin Development' started by Deitog, Jul 1, 2019.

  1. Hi, I'm going to update a plugin and is there a method where the menu is updated in a repeating task?
    Because what I want to do is that when the player opens the menu there will be an item that I want to update as long as the player menu is open.
    Can you offer your help?
    I use version 1.8 but I test my plugins in other versions too

    Code (Text):
    Sorry, I Speak Spanish
     
  2. I think Player#openInventory(inv); will overwrite the already opened inventory.
     
  3. SteelPhoenix

    Junior Mod

    There's no need in opening different inventories each iteration and it will cause flicker. Just get the opened inventory and change its contents, it should show up for the viewing player.
     
    • Agree Agree x 1
  4. Hi, if you want to update inventory, here I made quite a bit long post with complete steps and description on similar topic, you can look and maybe like it, if it will help you :) : https://www.spigotmc.org/threads/refresh-gui.367074/#post-3370167
     
    • Agree Agree x 1
  5. Then I have to put a "p.openInventory(inv);" in the repeating task in order to update the inventory?
     
  6. SteelPhoenix

    Junior Mod

    No the viewers should just see the updates instantly
     
  7. Code (Text):
    package deitog.simpleping;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scheduler.BukkitRunnable;

    public class PingMenu {
        private Principal pl;public PingMenu(Principal pl){
            this.pl = pl;
            }
       
        Inventory inv = Bukkit.createInventory(null, 45, "Ping");
       
        public void openInventory(Player p){
            p.openInventory(inv);
            updateInventory(p);
        }
       
        public void updateInventory(final Player p){
            new BukkitRunnable(){
               
                @Override
                public void run(){
                    Inventory inv = ((HumanEntity) p).getOpenInventory().getTopInventory();
                   
                    if(!inv.getName().equalsIgnoreCase("LATENCIA") || !inv.getName().equalsIgnoreCase("PING")){
                        cancel();
                        return;
                    }
                    ItemStack item = new ItemStack(Material.IRON_BLOCK,Principal.getPing(p));
                    ItemMeta meta = item.getItemMeta();
                    if(pl.getConfig().getString("language")=="en"){
                        meta.setDisplayName("PING");
                        meta.setLore(Arrays.asList(Principal.getPing(p)+""));
                    }
                    if(pl.getConfig().getString("language")=="es"){
                        meta.setDisplayName("LATENCIA");
                        meta.setLore(Arrays.asList(Principal.getPing(p)+""));
                    }
                   
                    item.setItemMeta(meta);
                    inv.setItem(1, item);
                }  
            }.runTaskTimer(pl, 20L, 20L);
        }
    }
     
    Thanks for the help
     
  8. SteelPhoenix

    Junior Mod

    There's no need to cast a Player to HumanEntity and you should compare Strings using #equals() (or #equalsIgnoreCase())
     
  9. Code (Text):
    Inventory inv = p.getOpenInventory().getTopInventory();
    I leave it like this?
     
  10. No problem, I am glad I helped.

    Yes.
     
    • Winner Winner x 1