Solved ArrayLists automatically clear

Discussion in 'Spigot Plugin Development' started by Christophe6, Apr 28, 2017.

  1. Hello

    In my onEnable() I call the setupPortkeys() method and it fills the portkeys arraylist.
    When I call the arraylist in my events, it's empty.
    This is my class:
    Code (Java):
    package com.christophe6.harrypottereconomy.portkeys;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    import com.christophe6.harrypottereconomy.SettingsManager;
    import com.connorlinfoot.titleapi.TitleAPI;

    public class PortkeyEvents implements Listener {
       
        public static PortkeyEvents instance = new PortkeyEvents();

        public static PortkeyEvents getInstance() {
            return instance;
        }
       
        public List<Location> portkeys = new ArrayList<Location>();
       
        public void setupPortkeys() {
            int slot = 0;
            while(SettingsManager.getInstance().getPortkeys().getString("portkey.slot." + slot) != null) {
                Location loc = new Location(Bukkit.getServer().getWorld(SettingsManager.getInstance().getPortkeys().getString("portkey.slot." + slot + ".location.portkey.world")), SettingsManager.getInstance().getPortkeys().getDouble("portkey.slot." + slot + ".location.portkey.x"), SettingsManager.getInstance().getPortkeys().getDouble("portkey.slot." + slot + ".location.portkey.y"), SettingsManager.getInstance().getPortkeys().getDouble("portkey.slot." + slot + ".location.portkey.z"));
                portkeys.add(loc);
                System.out.print(loc);
                System.out.print(portkeys);
                slot++;
            }
        }
       
        @EventHandler
        public void onRightClick(PlayerInteractEvent e) {
            if(e.getAction() != Action.RIGHT_CLICK_BLOCK) {
                return;
            }
            System.out.print(portkeys);
            for(Location loc : portkeys) {
                //Do something
            }
        }
    }
     
  2. Show us your onEnable() to be sure that you call setupPortkeys() on the same instance that you register as Listener.
     
    • Like Like x 1
  3. @Nikl
    Code (Java):
    @Override
        public void onEnable() {
            if (!VaultEconomyHandler.getInstance().setupEconomy() ) {
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
           
            if (Bukkit.getServer().getPluginManager().getPlugin("TitleAPI") == null) {
                getLogger().severe(String.format("[%s] - You can't use titles while using this plugin as you didn't install TitleAPI.", getDescription().getName()));
            }
           
            SettingsManager.getInstance().setup(this);
            PortkeyEvents.getInstance().setupPortkeys();
           
            Bukkit.getServer().getPluginManager().registerEvents(new FlooPowderEvents(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new FlooPowderSignListener(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new GringotsEvents(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new HouseEvents(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new PortkeyEvents(), this);
           
            //HarryPotterEconomy
            getCommand("harrypottereconomy").setExecutor(new CommandManager());
           
            //Gringots
            getCommand("gringotseconomy").setExecutor(new GECommandManager());
            getCommand("galleons").setExecutor(new GalleonCommand());
           
            //Floo Powder
            getCommand("floopowder").setExecutor(new FlooPowderCommand());
            getCommand("floopowdereconomy").setExecutor(new FPECommandManager());
           
            //House
            getCommand("house").setExecutor(new HouseCommandManager());
           
            //Portkey
            getCommand("portkey").setExecutor(new PKCommandManager());
        }
    I removed the addDefaults from the onEnable()
     
  4. I added my portkeys arraylist to my main class and that fixed it. But what was the problem?
     
  5. You have one portkeys arraylist per instance.

    Here you call setupPortkeys() on your static instance:
    But here:
    You register a new instance (with empty portkeys) as Listener. This is your problem.

    Use this:
    Code (Java):
    Bukkit.getServer().getPluginManager().registerEvents(PortkeyEvents.getInstance(), this);
    Then you use the same instance as Listener for which you setup the portkeys before.
     
    • Useful Useful x 1
  6. Thank you for helping me out!
     
    • Friendly Friendly x 1