Player messaging event error

Discussion in 'Spigot Plugin Development' started by DustinThePlugin, Apr 13, 2019.

  1. I am challenging myself, to learn Bukkit and accell myself in Java; I am creating a server in all custom plugins. I got stuck trying to being using Listeners instead of having a ton of code for each command execution.

    I am having an error, below i will list the command, event, and stacktrace!
    Thank You!

    CommandListener
    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player)sender;
           
            if(cmd.getName().equalsIgnoreCase("tell")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lERROR &8&l| &7You must specify a reciever and a message!"));
                    return true;
                }
               
                if (args.length == 1) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lERROR &8&l| &7You must specify a message!"));
                    return true;
                }
               
                if (args.length <= 2) {
                    Player target = Bukkit.getPlayer(args[0]);
                   
                    StringBuilder builder = new StringBuilder();
                   
                    for (int i = 1; i < args.length; i++) {
                        builder.append(args[i]).append(" ");
                    }
                   
                    String outputMessage = builder.toString();
                   
                    listeners.sendMessage(player.getName(), target.getName(), outputMessage);
                }
            }
            return true;
        }
    EventListeners
    Code (Java):
    Map<String, String> replyList = new HashMap<String, String>();
       
        public void sendMessage(String sender, String reciever, String message) {
            if (!Bukkit.getPlayer(sender).isOnline()) {
                return;
            }
           
            if (!Bukkit.getPlayer(reciever).isOnline()) {
                Bukkit.getPlayer(sender).sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lERROR &8&l| &7" + reciever.toString() + " is not online!"));
                return;
            }
           
            Bukkit.getPlayer(sender).sendMessage(ChatColor.translateAlternateColorCodes('&', "&aMe -> " + reciever.toString() + " &8&l| &7" + message));
            Bukkit.getPlayer(reciever).sendMessage(ChatColor.translateAlternateColorCodes('&', "&a" + sender.toString() + " -> Me &8&l| &7" + message));
           
            replyList.put(sender, reciever);
            replyList.put(reciever, sender);
            return;
        }
    StackTrace
    Code (Text):
    [12:31:17 INFO]: Reheight issued server command: /tell Reheight t
    [12:31:17 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tell' in plugin Excell v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:139) ~[spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchCommand(CraftServer.java:702) ~[spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PlayerConnection.a(PlayerConnection.java:1476) ~[spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) ~[spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PlayerConnectionUtils.a(SourceFile:10) ~[spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_201]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_201]
            at net.minecraft.server.v1_13_R2.SystemUtils.a(SourceFile:199) [spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:895) [spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:417) [spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:830) [spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:728) [spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
    Caused by: java.lang.NullPointerException
            at me.reheight.excell.CommandListener.onCommand(CommandListener.java:53) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.13.2.jar:git-Spigot-518206a-a5b9c7b]
            ... 15 more
    I appreciate any pointers, tips, and support!
     
  2. SteelPhoenix

    Moderator

    #getPlayer() is null if the player is not online. Also you should not assume the sender of the command is a player.
     
  3. I have already have a
    Code (Java):
    if (!(sender instanceof Player)) {
                plugin.getLogger().info("The Excell plugin has not yet implemented console integration yet!");
                return true;
            }
    I actually just added it.
     
  4. Instead of using player#isOnline(), check if player equals null

    Code (Text):
      if (!Bukkit.getPlayer(sender).isOnline()) {
            if (!Bukkit.getPlayer(reciever).isOnline()) {

     
    Code (Text):

    if (Bukkit.getPlayer(sender) == null) {
    if (Bukkit.getPlayer(reciever) == null) {
     
     
  5. It still comes out the same, further inspection shows this...

    I wanted to make sure my event was even working so I did this
    Code (Java):
    Map<String, String> replyList = new HashMap<String, String>();
       
        public void sendMessage(String sender, String reciever, String message) {
           
            Bukkit.broadcastMessage(sender + " " + reciever + " " + message);
           
            /*if (Bukkit.getPlayer(sender) == null) {
                return;
            }
           
            if (Bukkit.getPlayer(reciever) == null) {
                Bukkit.getPlayer(sender).sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lERROR &8&l| &7" + reciever.toString() + " is not online!"));
                return;
            }
           
            Player recieverPlayer = Bukkit.getPlayer(reciever);
           
            Bukkit.getPlayer(sender).sendMessage(ChatColor.translateAlternateColorCodes('&', "&aMe -> " + reciever.toString() + " &8&l| &7" + message));
            recieverPlayer.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a" + sender.toString() + " -> Me &8&l| &7" + message));
           
            replyList.put(sender, recieverPlayer.getName());
            replyList.put(recieverPlayer.getName(), sender);*/

        }
    And it still shows me the same error I listed
     
  6. Code (Text):
            at me.reheight.excell.CommandListener.onCommand(CommandListener.java:53) ~[?:?]
     
    What is in this line?
     
  7. It was me initiating the event...

    I was not being smart, turns out I never registered the event in my main class...

    Whoops...