Animation does not work

Discussion in 'Plugin Development' started by TheUncleBarto, Feb 19, 2020.

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

    TheUncleBarto

    Hello! I have a problem that I have been dealing with for a long time, it happens that I have a code to animate the items in an inventory, that is to say that the item in the inventory will change when prompted
    Example:
    SLOT 1: IRON_SWORD
    wait 3 seconds ...
    SLOT 1: DIAMOND_SWORD
    As my code works "well", it is capable of animating the items but a problem occurs, it happens that I have Material.valueOf (itemList.get (itemIndex));
    and I also have an if to know when the itemIndex has already toured the entire ArrayList of materials
    if (itemIndex <itemList.size () - 1) {
    i ++;
    } else {
    i = 0;
    }
    This goes through the list again.
    The list is as follows:
    https://pastebin.com/bLcq8xfE
    In this list you can see that the size of the lists are not equal, there are 2 lists whose size is 1 and the last list is of size 0. Then it happens that the itemIndex returns to 0 because it has already crossed the list that has size 0, then the animation does not occur because it does not go through the other lists.
    I will leave my code for greater understanding :D.
    Code:
    Code:
    package me.LobbyBrain.Inventory;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitScheduler;
    import org.bukkit.scheduler.BukkitTask;
    
    import me.LobbyBrain.Main;
    
    public class Inv{
     
        private Main plugin;
        public Inv(Main plugin) {
            this.plugin = plugin;
        }
     
        InventoryVersionHandler ivh = new InventoryVersionHandler();
        public static HashMap<UUID, Integer> taskMap = new HashMap<UUID, Integer>();
        public void inventoryContents(Player player, String fileName) {
            File file = new File(plugin.folder(), fileName);
            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
            Inventory inv = Bukkit.createInventory(null, 54, "");
            BukkitScheduler sc = Bukkit.getServer().getScheduler();
            sc.scheduleSyncRepeatingTask(plugin, new Runnable() {
                List<String> items;
                List<String> displayname;
                int itemIndex = 0;
                int    displayNameIndex = 0;
                int X;
                int Y;
                ItemStack stack;
                ItemMeta meta;
                @Override
                public void run() {
                    for(String itemkey : config.getConfigurationSection("Inventory.Items").getKeys(false)) {
                        items = new ArrayList<String>(config.getStringList("Inventory.Items."+itemkey+".ID"));
                        displayname = config.getStringList("Inventory.Items."+itemkey+".DisplayName");
                        //meta = stack.getItemMeta();
                        X = config.getInt("Inventory.Items."+itemkey+".X");
                        Y = config.getInt("Inventory.Items."+itemkey+".Y");
                        if(config.getConfigurationSection("Inventory.Items."+itemkey).contains("Lore")) {
                            List<String> listlore = config.getStringList("Inventory.Items."+itemkey+".Lore");
                            List<String> list2 = new ArrayList<String>();
                            for(int i = 0;i < listlore.size();i++) {
                                list2.add(listlore.get(i).replace("&", "ยง"));
                                meta.setLore(list2);
                            }
                        }
                            stack.setItemMeta(meta);
                            stack = new ItemStack(Material.valueOf(ChatColor.translateAlternateColorCodes('&', items.get(itemIndex))));
                            inv.setItem(Y*9-1-9+X, stack);
                            player.sendMessage(""+items.get(itemIndex));
                            if(itemIndex < items.size()-1) {
                                itemIndex++;
                            }else {
                                itemIndex = 0;
                            }
                    }
                 
                }
             
            }, 0, 20);
            player.openInventory(inv);
            //taskID(player, fileName);
        }
        public int taskID(Player player, String fileName) {
                int task = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                File file = new File(plugin.folder(), fileName);
                FileConfiguration config = YamlConfiguration.loadConfiguration(file);
                List<String> titleName = config.getStringList("Inventory.Title");
                int i;
                @Override
                public void run() {
                            if(i < titleName.size()-1) {
                                i++;
                            }else {
                                i = 0;
                            }
                            ivh.versionHandler(player, titleName.get(i));
                }
            },0, 20);
             
                taskMap.put(player.getUniqueId(), task);
                return task;
        }
    }
    
    
     
Thread Status:
Not open for further replies.

Share This Page