1.12.2 Inventory is buged when trying to animate it

Discussion in 'Spigot Plugin Development' started by MetalWarrior, Mar 1, 2020.

  1. I honestly did not know what title to put this, my problem goes like this, I have a method to animate the title of the inventory, basically send a new inventory to the player but with a different title to create the animation effect, I do this with a runnable that It runs every 1 second, then I have a method that has InventoryCloseEvent as a parameter, because if the player tries to close the inventory another one will be opened because of the runnable, then when closing the inventory the runnable is going to stop, this works more or less well, since there are times where when closing the inventory the runnable stops, but when it stops for some reason it sends a new inventory again, that would be the problem, I do not seek to be fed with a spoon, I look for a solution and learn at the same time, I will pass code.
    CODE:
    Code (Text):

    public static HashMap<UUID, Integer> taskMap = new HashMap<UUID, Integer>();
        public void animateTitle(Player player, String fileName) {
            File file = new File(plugin.folder(), fileName);
            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
            List<String> namelist = config.getStringList("Inventory.Title");
            int taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
            int i;
            @Override
            public void run() {
                if(config.getConfigurationSection("Inventory").getKeys(false).contains("Title")) {
                    iapi.setTitle(namelist.get(i));
                    ivh.versionHandler(player, iapi.getTitle());
                }else if(config.getString("Inventory.Title").isEmpty()) {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getMenuFile().getString("LobbyBrain.Messages.NullInventoryTitle")));
                    Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getMessages().getString("LobbyBrain.Messages.getFileForError")
                        .replace("%file_name%", fileName)));
                    player.sendMessage(plugin.getMenuFile().getString("LobbbyBrain.Messages.ErrorToOpenInventory"));
                }
                if(i < namelist.size()-1) {
                    i++;
                }else{
                    i = 0;
                }
            }
        }, 0, 20);
            taskMap.put(player.getUniqueId(), taskID);
            }
     
    Code (Text):

    public class InventoryCloseEvents implements Listener{
     
        @EventHandler
        public void onClose(InventoryCloseEvent e){
            if(InventoryManager.taskMap.containsKey(e.getPlayer().getUniqueId())) {
                Bukkit.getScheduler().cancelTask(InventoryManager.taskMap.get(e.getPlayer().getUniqueId()));
            }
        }
    }
     
    Code (Text):

    public void sendInventory(Player player, String title) {
            EntityPlayer cp = ((CraftPlayer) player).getHandle();
            PacketPlayOutOpenWindow window = new PacketPlayOutOpenWindow(cp.activeContainer.windowId, "minecraft:chest", new ChatMessage(ChatColor.translateAlternateColorCodes('&', title)), cp.activeContainer.getBukkitView().getTopInventory().getSize());
            cp.playerConnection.sendPacket(window);
            cp.updateInventory(cp.activeContainer);
        }
     
     
    #1 MetalWarrior, Mar 1, 2020
    Last edited: Mar 1, 2020