Solved Problems with getting list

Discussion in 'Spigot Plugin Development' started by SickSkillz, Apr 21, 2017.

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

    Supporter

    I'm currently working on some sort of "security" plugin which includes a 2FA system.
    When a player has a password I add them on join to a list like this:
    Code (Text):
        @EventHandler
        public void onJoinEvent(PlayerJoinEvent event) {
            if (config.isSet("Players." + event.getPlayer().getUniqueId().toString() + ".Password")) {
                getLoginList().add(event.getPlayer().getName());
                event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&',
                        config.getString("Messages.LoginNeeded")));
            }
        }
    And I have a getList like this:
    Code (Text):
       public List<String> getLoginList() {
            return new ArrayList<String>();
        }
    Everything is working when I am using this in the class, but when I use it in another class it doesn't work.
    Code (Text):
        @EventHandler
        public void onChatEvent(AsyncPlayerChatEvent event) {
            if (config.getBoolean("Settings.BlockChat")) {
                if (jl.getLoginList().contains(event.getPlayer().getName())) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&',
                            config.getString("Messages.LoginNeeded")));
                }
            }
        }
    I have tested with System.out.println where it goes wrong and it goes wrong with the getLoginList.
    After trying a lot of things I don't know what is going wrong. Maybe it's because I am a bit tired but not sure :p.

    If you need the whole source take a look at this: https://github.com/Ghostriderdev/ProServerSecurity
    If you need any more info I would be happy to provide :).

    Edit: It worked a few hours before, I tried reverting my changes but that didn't work too.(also had to go away so I could not ctrl+z everything).
     
  2. Ehhh you now that you request at the getLoginList everytime its beeing called a new arraylist right?
     
  3. SickSkillz

    Supporter

    Oh, wait... Lol didn't notice.
     
  4. SickSkillz

    Supporter

    Okay my class is now this:
    Code (Text):
    package me.ghostrider.proserversecurity.listeners;

    import me.ghostrider.proserversecurity.ProServerSecurity;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;

    import java.util.ArrayList;

    public class JoinListener implements Listener {

        private Plugin plugin = ProServerSecurity.getPlugin();
        private FileConfiguration config = plugin.getConfig();

        private ArrayList<String> loginList = new ArrayList<String>();

        @EventHandler
        public void onJoinEvent(PlayerJoinEvent event) {
            if (config.isSet("Players." + event.getPlayer().getUniqueId().toString() + ".Password")) {
                getLoginList().add(event.getPlayer().getName());
                event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&',
                        config.getString("Messages.LoginNeeded")));
            }
        }

        public ArrayList<String> getLoginList() {
            return loginList;
        }
    }

     
    I don't know why it worked before and what I did to let it work... I've also googled some things but no result :/
     
  5. So you still receive an empty list right?
     
  6. SickSkillz

    Supporter

    I think, getLoginList().contains() is always false
     
  7. Is your chatEvent still in the same class, because you only posted the code without the chat ?
     
  8. SickSkillz

    Supporter

    Yes it is(didn't post all classes as it happens in some more classes too.).
     
  9. Hmm okay wondering how do you access the loginList in the chatEvent, do you always request a new class instance like jl.getLoginList() ? because you request a public value from a private one
     
  10. SickSkillz

    Supporter

  11. SickSkillz

    Supporter

    Hmm, will try tomorrow.
     
  12. SickSkillz

    Supporter

    This fixed it. Thanks!
     
Thread Status:
Not open for further replies.