Loop is not working properly.

Discussion in 'Plugin Development' started by jasonjuniorgray, Oct 4, 2018.

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

    jasonjuniorgray

    Hey guys, have a quick problem here and I'm not sure why.

    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
    
            p.sendMessage("test"); // this works.
            int countapps = 0;
    
            for (Application a : applications) {
                if (a.getApplicationStatus() == ApplicationStatus.NOT_CHECKED) { // this same loop is used above and works.
                    countapps ++;
                }
            }
            if(countapps > 0) {
                p.sendMessage("§7test 2"); // this does not.
            }
    
        }
    So, when a player joins it will send the first test message. Pretty simple stuff. However, what I want it to do is loop through the applications and for every application that is NOT_CHECKED it will add 1 to countapps. In the end, if countapps is greater than 0 I want it to send the second test message, however it's not exactly doing that. No matter how I set it. If I set the statement to countapps = 0 it still will not send the message. != 0, will not send the message. Not exactly sure what's going on here.
     
  2. Online

    timtower Administrator Administrator Moderator

    @jasonjuniorgray And what if you print countapps, add a logging message there, have the sendMessage without the color?
     
  3. Offline

    jasonjuniorgray

    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
    
            p.sendMessage("test"); // this works.
            int countapps = 0;
    
            p.sendMessage(" - " + countapps); // now this works
    
            for (Application a : applications) {
                if (a.getApplicationStatus() == ApplicationStatus.NOT_CHECKED) { // this same loop is used above and works.
                    p.sendMessage("2 - " + countapps);
                    countapps ++;
                }
            }
    
            p.sendMessage("2.5 - " + countapps); // this message doesnt even show???
    
            if(countapps > 0) {
                p.sendMessage("3 - " + countapps);
            }
    
        }
    Only the first message shows. I even added a sendMessage inbetween the two if statements for further testing, even that one does not show. Something is wrong with the loop and I'm not sure what it is. I also again changed the statement to if(countapps == 0) (because the first sendMessage told me it was 0), and nope, still nothing.

    Essentially, the code doesn't make it after the loop - it stops.
     
    Last edited: Oct 5, 2018
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    jasonjuniorgray

    Ahaaaa

    Code:
    [11:10:16] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to GreyList v1.0-SNAPSHOT
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:308) ~[spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.PlayerList.onPlayerJoin(PlayerList.java:343) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.PlayerList.a(PlayerList.java:163) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.LoginListener.b(LoginListener.java:150) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.LoginListener.Y_(LoginListener.java:54) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.NetworkManager.a(NetworkManager.java:231) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.ServerConnection.c(ServerConnection.java:120) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:994) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:411) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:831) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:729) [spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
    Caused by: java.lang.NullPointerException
        at sk.jasonjuniorgray.greylist.managers.ApplicationManager.onPlayerJoin(ApplicationManager.java:184) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_181]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_181]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_181]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_181]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304) ~[spigot-1.13.1.jar:git-Spigot-2440e18-4228a56]
        ... 14 more
    [11:10:17] [Server thread/INFO]: jasonjuniorgray[/127.0.0.1:63531] logged in with entity id 357 at ([MineS3]-13.377718332127872, 85.80521704757686, -4733.41833526403)
     
    Last edited: Oct 5, 2018
  6. Online

    timtower Administrator Administrator Moderator

    @jasonjuniorgray Your list is probably null.
    And always check the console when something is not working.
     
  7. Offline

    jasonjuniorgray

    Yes I didn't even think of that. I'm just not sure why it is null

    So I printed "applications" the variable and its coming back as null, which is what would cause this console error. However, I'm not entirely sure why because in a very similar piece of code it is working just fine

    Code:
    public void showAllApplications(CommandSender sender) {
            if (sender.hasPermission("gl.appmanage")) {
                sender.sendMessage("§7------------------------------------------------------------------------");
                for (Application a : applications) {
                    if (a.getApplicationStatus() == ApplicationStatus.NOT_CHECKED)
                        sender.sendMessage(" - §e" + a.getOfflinePlayer().getName());
                }
                sender.sendMessage("§7------------------------------------------------------------------------");
            } else {
                sender.sendMessage(Message.NO_PERM.getMessage());
            }
        }
    That code works perfectly fine.
     
  8. Online

    timtower Administrator Administrator Moderator

  9. Offline

    jasonjuniorgray

    They're both in the same class, which furthers the oddness but here is the whole class

    Code:
    public class ApplicationManager implements Listener {
    
        private static ApplicationManager ourInstance = new ApplicationManager();
        private PlayerJoinEvent e;
    
        public static ApplicationManager getInstance() {
            return ourInstance;
        }
    
        private List<String> questions;
        private List<Application> applications;
        private List<String> acceptCommands;
        private List<String> denyCommands;
    
        public void load() {
            questions = new ArrayList<>();
            applications = new ArrayList<>();
            acceptCommands = GreyList.getInstance().getConfig().getStringList("accept-commands");
            denyCommands = GreyList.getInstance().getConfig().getStringList("deny-commands");
    
            for (String s : GreyList.getInstance().getConfig().getStringList("questions")) {
                questions.add(ChatColor.translateAlternateColorCodes('&', s));
            }
    
            for (String s : GreyList.getFileManager().getConfig("applications.yml").get().getKeys(false)) {
                UUID uuid = UUID.fromString(s);
                Application a = new Application(uuid, true);
                applications.add(a);
            }
        }
    
        public List<String> getQuestions() {
            return questions;
        }
    
        public List<Application> getApplications() {
            return applications;
        }
    
        public void newApplication(Player player, CommandSender sender) {
            if (player.hasPermission("gl.apply")) {
                Application a = new Application(player.getUniqueId(), false);
                applications.add(a);
                sender.sendMessage("§9You have started the application process. You will be prompted with a question, answer by typing in the in-game chat.");
                a.firstQuestion();
            } else {
                player.sendMessage(Message.NO_PERM.getMessage());
            }
        }
    
        public void showPlayerApplication(CommandSender sender, OfflinePlayer target) {
            if (sender.hasPermission("gl.appmanage")) {
                Application application = getPlayerApplication(target);
                if (application != null) {
                    application.displayTo(sender);
                }
            } else {
                sender.sendMessage(Message.NO_PERM.getMessage());
            }
        }
    
        private Application getPlayerApplication(OfflinePlayer target) {
            for (Application a : applications) {
                if (target.getUniqueId().equals(a.getUUID())) {
                    return a;
                }
            }
            return null;
        }
    
        public Application getPlayerApplication(Player p) {
            for (Application a : applications) {
                if (p.getUniqueId().equals(a.getUUID())) {
                    return a;
                }
            }
            return null;
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            int countapps = 0;
    
            if (p.hasPermission("gl.appmanage")) {
    
                for (Application a : applications) {
                    if (a.getApplicationStatus() == ApplicationStatus.NOT_CHECKED) {
                        countapps++;
                    }
                }
                p.sendMessage("§7------------------------------------------------------------------------");
            } else {
                p.sendMessage(Message.NO_PERM.getMessage());
            }
    
        }
    
        public void showAllApplications(CommandSender sender) {
            if (sender.hasPermission("gl.appmanage")) {
                sender.sendMessage("§7------------------------------------------------------------------------");
                for (Application a : applications) {
                    if (a.getApplicationStatus() == ApplicationStatus.NOT_CHECKED)
                        sender.sendMessage(" - §e" + a.getOfflinePlayer().getName());
                }
                sender.sendMessage("§7------------------------------------------------------------------------");
            } else {
                sender.sendMessage(Message.NO_PERM.getMessage());
            }
        }
    
        public void manageApplication(CommandSender manager, OfflinePlayer applicant, boolean accept) {
            if (manager.hasPermission("gl.appmanage")) {
                Application a = getPlayerApplication(applicant);
                if (a != null && a.getApplicationStatus() == ApplicationStatus.NOT_CHECKED) {
                    a.setAccepted(manager, accept);
                }
            } else {
                manager.sendMessage(Message.NO_PERM.getMessage());
            }
        }
    
        public void deleteApplication(CommandSender sender, OfflinePlayer target) {
            if (sender.hasPermission("gl.appdelete")) {
                Application a = getPlayerApplication(target);
                if (a != null) {
                    a.delete();
                    applications.remove(a);
                    a = null;
                    sender.sendMessage("§aApplication deleted.");
                }
            } else {
                sender.sendMessage(Message.NO_PERM.getMessage());
            }
        }
    
        public void runDenyCommands(OfflinePlayer p) {
            for (String s : denyCommands) {
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), s.replaceAll("%player%", p.getName()));
            }
        }
    
        public void runAcceptCommands(OfflinePlayer p) {
            for (String s : acceptCommands) {
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), s.replaceAll("%player%", p.getName()));
            }
        }
    
        public boolean isAccepted(UUID uuid) {
            for (Application a : applications) {
                if (a.getUUID().equals(uuid) && a.getApplicationStatus() == ApplicationStatus.ACCEPTED) {
                    return true;
                }
    
            }
            return false;
        }
    
        public boolean isPending(UUID uuid) {
            for (Application a : applications) {
                if (a.getUUID().equals(uuid) && a.getApplicationStatus() == ApplicationStatus.NOT_CHECKED) {
                    return true;
                }
    
            }
            return false;
        }
    
        public void deleteApplication(Application a) {
            a.delete();
            applications.remove(a);
            a = null;
        }
    }
     
  10. Online

    timtower Administrator Administrator Moderator

  11. Offline

    jasonjuniorgray

    In Main, I'll show you the whole class. Specifically in onEnable

    Code:
    public final class GreyList extends JavaPlugin {
    
    
        private static GreyList instance;
        private static FileManager fileManager;
    
        @Override
        public void onEnable() {
            instance = this;
            fileManager = new FileManager(this);
            saveDefaultConfig();
            fileManager.getConfig("applications.yml").saveDefaultConfig();
            loadCommands();
            getServer().getPluginManager().registerEvents(new ApplicationManager(), this);
            getServer().getPluginManager().registerEvents(new ApplicationListener(), this);
            ApplicationManager.getInstance().load();
        }
    
        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }
    
        public static GreyList getInstance() {
            return instance;
        }
    
        private void loadCommands() {
            getCommand("greylist").setExecutor(new GreyListCommand());
            getCommand("apply").setExecutor(new ApplyCommand());
            getCommand("applications").setExecutor(new ApplicationsCommand());
        }
    
        public void reloadPlugin(CommandSender sender) {
            ApplicationManager.getInstance().load();
            Message.reloadMessages();
            sender.sendMessage("§7[Greylist] §9Successfully reloaded the Greylist plugin.");
        }
    
        public static FileManager getFileManager() {
            return fileManager;
        }
    }
     
  12. Online

    timtower Administrator Administrator Moderator

  13. Offline

    jasonjuniorgray

    And removing which one would "unnull" applications?
     
  14. Online

    timtower Administrator Administrator Moderator

    You can use the getInstance() instead of the "new ApplicationManager()"
     
  15. Offline

    jasonjuniorgray

    You're a genius that worked, thank you! I don't even know how I missed that.
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page