Solved Start function not looping through all the worlds on my server

Discussion in 'Plugin Development' started by UNC00KED, Oct 24, 2015.

Thread Status:
Not open for further replies.
  1. Having this problem with my Per World Broadcaster plugin that I can't solve. So basically the user runs the command /bm addmessage then they hit the enter key.

    After they run that command, a boolean hashmap with their name as the key, and the value gets set to true. They then type in the broadcast message they would like to be added to the list for whatever world they are in. And the AsyncPlayerChatEvent in the TestListener.java class then grabs their next message and runs the function addMessage in the Test.java main class. If they want to cancel the whole thing, they use the command /bm cancel. For whatever reason I can enter broadcast messages for the world world but not for the world named parkour. No clue why.

    A look at the config when a message is entered in correctly
    Code:
    Messages:
      world:
      - test
    The error in console
    Code:
    [16:15:59 ERROR]: Could not pass event AsyncPlayerChatEvent to Test v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:484) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.PlayerConnection.chat(PlayerConnection.j
    ava:1057) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :995) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat$1.run(PacketPlayInChat.
    java:39) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_40]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_40]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:
    1.8.0_40]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?
    :1.8.0_40]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_40]
    Caused by: java.lang.NullPointerException
            at me.unc00ked.Test.addMessage(Test.java:99) ~[?:?]
            at me.unc00ked.TestListener.getMessageEvent(TestListener.java:38) ~[?:?]
    
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _40]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _40]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_40]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_40]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
            ... 11 more
    The addMessage function
    Code:
    void addMessage(String message, String world) {
     
            List<String>messageList = messages.get(world);
     
            messageList.add(message);
     
            messages.put(world, messageList);
     
            getConfig().set("Messages." + world, messageList);
            saveConfig();
     
            if (messageList.size() != 0) {
         
                toggle.put(world, true);
         
            }
     
        }
    Line 108 is
    messageList.add(message);

    Would appreciate any help, thanks!
     
    Last edited: Oct 24, 2015
  2. Offline

    SuperSniper

    @UNC00KED

    Code:
     at me.unc00ked.Test.addMessage(Test.java:108) ~[?:?]
     at me.unc00ked.TestListener.getMessageEvent(TestListener.java:38) ~[?:?]
    
    Show us line #108 in "Test" class.

    Show us line #38 in "TestListener" class.
     
    Zombie_Striker likes this.
  3. Offline

    Scimiguy

    Better yet, show us both of those classes entirely
    @UNC00KED
     
  4. Test.java
    Code:
    package me.unc00ked;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Test extends JavaPlugin {
       
        Logger logger = Logger.getLogger("Minecraft");
       
        public static Test plugin;
       
        public static Map<String,List<String>> messages = new HashMap<String,List<String>>();
        public static Map<String,Integer> delays = new HashMap<String,Integer>();
        public static Map<String,Boolean> toggle = new HashMap<String,Boolean>();
       
        public static Map<String, Boolean> playerMessageToggle = new HashMap<String, Boolean>();
       
        @Override
        public void onEnable() {
            plugin = this;
            new TestListener(this);
            logger.info("Test Enabled!");
            saveConfig();
            start();
        }
       
        @Override
        public void onDisable() {
            logger.info("Test Disabled!");
            saveConfig();
        }
       
        void start() {
           
            for (World world : Bukkit.getWorlds()) {
               
                List<String> messageList = messages.get(world.getName());
                int delay = 40;
               
                if (getConfig().getStringList("Messages." + world.getName()) != null) {
                   
                    messageList = getConfig().getStringList("Messages." + world.getName());
                   
                    messages.put(world.getName(), messageList);
                   
                }
               
                if (getConfig().get("Delays." + world.getName()) != null) {
                   
                    delay = getConfig().getInt("Delays." + world.getName());
                   
                }
               
                delays.put(world.getName(), delay);
               
                toggle.put(world.getName(), false);
               
                if (messageList.size() != 0) {
                   
                    toggle.put(world.getName(), true);
                   
                }
               
            }
           
        }
       
        public boolean isInt(String s) {
           
            try {
                Integer.parseInt(s);
            } catch (NumberFormatException nfe) {
                return false;
            }
            return true;
           
        }
       
        String addColors(String string) {
           
            return string.replace("&", "\u00a7");
           
        }
       
        void addMessage(String message, String world) {
           
            List<String>messageList = messages.get(world);
           
            messageList.add(message);
           
            messages.put(world, messageList);
           
            getConfig().set("Messages." + world, messageList);
            saveConfig();
           
            if (messageList.size() != 0) {
               
                toggle.put(world, true);
               
            }
           
        }
       
        void delMessage(int number, String world) {
           
            List<String> messageList = messages.get(world);
           
            messageList.remove(number-1);
           
            messages.put(world, messageList);
           
            getConfig().set("Messages." + world, messageList);
            saveConfig();
           
            if (messageList.size() == 0) {
               
                toggle.put(world, false);
               
            }
           
        }
       
        void setDelay(int number, String world) {
           
            delays.put(world, number);
           
            getConfig().set("Delays." + world, number);
            saveConfig();
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;
           
            int length = args.length;
           
            if (cmd.getName().equalsIgnoreCase("broadcastmessage") && sender instanceof Player) {
               
                if ( (length == 1) && (args[0].equalsIgnoreCase("listmessages")) ) {
                   
                    List<String> messageList = messages.get(player.getWorld().getName());
                   
                    if (messageList != null) {
                       
                        if (messageList.size() != 0) {
                           
                            player.sendMessage(ChatColor.GREEN + "Current messages set for world " + ChatColor.YELLOW + player.getWorld().getName() + ChatColor.GREEN + ":");
                           
                            for (int i = 0; i < messageList.size(); i++) {
                               
                                player.sendMessage(ChatColor.GRAY + "Message " + (i+1) + ": " + ChatColor.RESET + addColors(messageList.get(i)));
                               
                            }
                           
                        } else {
                           
                            player.sendMessage(ChatColor.GREEN + "There are currently " + ChatColor.RED + "no" + ChatColor.GREEN + " messages set for world: " + ChatColor.YELLOW + player.getWorld().getName());
                           
                        }
                       
                    }
                   
                } else if ( (length == 1) && (args[0].equalsIgnoreCase("addmessage")) ) {
                   
                    player.sendMessage(ChatColor.GRAY + "Chat the broadcast message you would like to be added to the list for world " + ChatColor.YELLOW + player.getWorld().getName());
                   
                    player.sendMessage(ChatColor.GRAY + "Enter the command " + ChatColor.AQUA + "/bm cancel" + ChatColor.GRAY + " to cancel");
                   
                    playerMessageToggle.put(player.getName(), true);
                   
                } else if ( (length == 1) && (args[0].equalsIgnoreCase("cancel")) ) {
                   
                    player.sendMessage(ChatColor.GREEN + "Cancelled!");
                   
                    playerMessageToggle.put(player.getName(), false);
                   
                } else if ( (length == 2) && (args[0].equalsIgnoreCase("delmessage")) ) {
                   
                    if (isInt(args[1]) == true) {
                       
                        int i = Integer.parseInt(args[1]);
                       
                        List<String> messageList = messages.get(player.getWorld().getName());
                       
                        if (i <= messageList.size()) {
                           
                            delMessage(i, player.getWorld().getName());
                           
                            player.sendMessage(ChatColor.GREEN + "Message " + ChatColor.GOLD + i + ChatColor.GREEN + " for world " + ChatColor.YELLOW + player.getWorld().getName() + ChatColor.GREEN + " has been deleted");
                           
                        } else {
                           
                            player.sendMessage(ChatColor.GREEN + "Message " + ChatColor.GOLD + i + ChatColor.GREEN + " for world " + ChatColor.YELLOW + player.getWorld().getName() + ChatColor.RED + " does not exist!");
                           
                        }
                       
                    }
                   
                } else if ( (length == 2) && (args[0].equalsIgnoreCase("setdelay")) ) {
                   
                    if (isInt(args[1]) == true) {
                       
                        int i = Integer.parseInt(args[1]);
                       
                        player.sendMessage(ChatColor.GREEN + "Message(s) will be broadcasted every " + ChatColor.GOLD + i + ChatColor.GREEN + " seconds for world " + ChatColor.GOLD + player.getWorld().getName());
                       
                        setDelay(i*20, player.getWorld().getName());
                       
                    }
                       
                }
               
                return true;
               
            }
           
            return false;
           
        }
    
    }
    TestListener.java
    Code:
    package me.unc00ked;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class TestListener implements Listener {
       
        public TestListener(Test plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void joinEvent (PlayerJoinEvent event) {
           
            Player player = event.getPlayer();
           
            Test.playerMessageToggle.put(player.getName(), false);
           
        }
       
        @EventHandler
        public void getMessageEvent (AsyncPlayerChatEvent event) {
           
            Player player = event.getPlayer();
           
            if (Test.playerMessageToggle.get(player.getName()) != null) {
               
                if (Test.playerMessageToggle.get(player.getName()) == true) {
                   
                    String message = event.getMessage();
                   
                    String world = player.getWorld().getName();
                   
                    Test.plugin.addMessage(message, world);
                   
                    player.sendMessage(ChatColor.GREEN + "Broadcast message added to the list for world " + ChatColor.YELLOW + player.getWorld().getName());
                   
                    Test.playerMessageToggle.put(player.getName(), false);
                   
                    event.setCancelled(true);
                   
                }
               
            }
           
        }
    
       
    }
     
  5. Offline

    Xerox262

    Why so many statics? You're passing the plugin to the TestListener class, why not just store it and use that instead? Static is so you can have multiple objects of the class but have something be the same between them all (Which you can't be doing with your JavaPlugin class as there should only ever be a single instance of it). And you do not need to save the config everytime you make a single change, reload it on enable and save it on disable, don't save it every time someone decides they want to add or remove a message.

    Also it seems like what you're trying to do is easier managed by the conversation api of bukkit.
     
  6. Offline

    RoboticPlayer

    First, as @Xerox262 you really shouldn't be using static at all in Bukkit. Secondly, use private fields then encapsulate! Thridly, stop stealing Minecraft's logger!
     
  7. Thanks for the suggestions guys, really appreciate it. Changed those variables to make them not static and simply used the single instance of the class.

    Back to the problem at hand. Do you guys have any ideas why I'm getting that org.bukkit.event.EventException exception?
     
  8. Offline

    Scimiguy

    @UNC00KED
    Either your message is null, or your messageList is.
     
  9. Offline

    Xerox262

    It says. Null Pointer, debug to see what is null.

    Like I said, you should use the Conversation API rather than Lists and events.
     
  10. Just did a null test on both and turns out that messageList is null.
     
  11. Offline

    Scimiguy

  12. I have no idea why it is null. Any ideas please?
     
    Last edited: Oct 24, 2015
  13. Offline

    Scimiguy

    @UNC00KED
    At some point you'll have to learn how to debug/solve problems without help ;)

    But so far, we need the rest of the code from your class to help any more
     
  14. I thought that was the point of this forum. So people could help others with problems they couldn't solve...

    And I already gave you the rest of my code...both those classes is all there is...
     
  15. Offline

    Scimiguy

    @UNC00KED
    Oh so you did, my apologies.

    Well you haven't instantiated your List implementation when you initialized your messages variable.
    Don't forget you can't instantiate a List due to it being an interface. You'll need to have a
    new ArrayList();
     
  16. .

    Sorry to reopen this guys but the issue is still prevalent. I thought I fixed it but turns out I didn't. I have it narrowed down to the fact that in the start function, it is not looping through all my worlds for some reason. So variables in the hashmaps do not get defined. I even tried adding a null check: if (world != null) and it didn't help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  17. Offline

    Scimiguy

    @UNC00KED
    Have you set your plugin to
    Load: POSTWORLD

    in your plugin.yml?
     
  18. No, did not even know that was a thing... will look it up now on how to do it.

    That worked perfectly. Thanks @Scimiguy Hopefully this problem does not occur again... ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
Thread Status:
Not open for further replies.

Share This Page