Solved Could not pass ServerListPingEvent? Need help :D

Discussion in 'Plugin Development' started by Ultracrepadarian, Jul 15, 2015.

Thread Status:
Not open for further replies.
  1. I am in the process of making my plugin, and I am trying to add an in game feature to set MOTD in game. My problem is when I ping the server, it pops up a big error saying "Could not pass ServerListPingEvent to KrazyFunStuff", or to be precise:
    Error Message (open)

    [07:31:16 ERROR]: Could not pass event ServerListPingEvent to KrazyFunStuff v0.2

    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:305) ~[spigot.jar:git-Spigot-870264a-0a645a2]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-870264a-0a645a2]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-870264a-0a645a2]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.PacketStatusListener.a(PacketStatusListe
    ner.java:106) [spigot.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.PacketStatusInStart.a(SourceFile:23) [sp
    igot.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.PacketStatusInStart.a(SourceFile:8) [spi
    got.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.NetworkManager.a(NetworkManager.java:124
    ) [spigot.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.NetworkManager.channelRead0(NetworkManag
    er.java:325) [spigot.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.NetworkManager.channelRead0(NetworkManag
    er.java:1) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChanne
    lInboundHandler.java:105) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
    ractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(Abstra
    ctChannelHandlerContext.java:319) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessage
    Decoder.java:163) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
    ractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(Abstra
    ctChannelHandlerContext.java:319) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessage
    Decoder.java:163) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
    ractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(Abstra
    ctChannelHandlerContext.java:319) [spigot.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.LegacyPingHandler.channelRead(SourceFile
    :94) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
    ractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(Abstra
    ctChannelHandlerContext.java:319) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.handler.timeout.ReadTimeoutHandler.channelRead(ReadTimeoutHa
    ndler.java:150) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
    ractChannelHandlerContext.java:333) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(Abstra
    ctChannelHandlerContext.java:319) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChanne
    lPipeline.java:787) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(Abstra
    ctNioByteChannel.java:130) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.jav
    a:511) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEve
    ntLoop.java:468) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.ja
    va:382) [spigot.jar:git-Spigot-870264a-0a645a2]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) [spigot.
    jar:git-Spigot-870264a-0a645a2]
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThread
    EventExecutor.java:116) [spigot.jar:git-Spigot-870264a-0a645a2]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
    at com.krazytar.plugins.MainFile.ServerPing(MainFile.java:145) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_45]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301) ~[spigot.jar:git-Spigot-870264a-0a645a2]
    ... 32 more


    I have registered my events, And done everything I can. I have looked around the forums for answers on this, but cant find out what I'm doing wrong! Please help! :D Thanks in advance!

    Code:
    Full Code (open)

    Its long xD Alot of it is Useless and in development
    Code:
    package com.krazytar.plugins;
    
    import java.util.Random;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.event.server.ServerListPingEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MainFile extends JavaPlugin implements Listener
    {
    
        String chatSenderName;
        String chatTargetName;
        boolean onCommand;
        MainFile configGetter;
        String motd;
    
        public final Logger logger = Logger.getLogger("Minecraft");
    
        @Override
        public void onEnable()
        {
            this.getServer().getPluginManager().registerEvents(this, this);
            onCommand = false;
            this.getConfig().addDefault("welcome-message", "Welcome to the server!");
            this.getConfig().addDefault("motd", "A Minecraft Server");
            this.getConfig().options().copyDefaults(true);
            saveConfig();
            this.logger.info("Plugin enabled! :D");
        }
    
        @Override
        public void onDisable()
        {
        
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("setmotdmy") && sender instanceof Player)
            {
                if(!args[0].equals(null))
                {
                    motd = args[0];
                    this.getConfig().set("motd", motd);
                }
            }
            if(cmd.getName().equalsIgnoreCase("chat") && sender instanceof Player)
            {
            
            
                Player player = (Player) sender;
                Location loc = player.getLocation();
                if(args.length == 1)
                {
                    for(Player target : Bukkit.getServer().getOnlinePlayers())
                    {
                        if(target.getName().equalsIgnoreCase(args[0]))
                                {
                            onCommand = true;
                            chatSenderName = player.getName();
                            chatTargetName = args[0];
                            player.sendMessage("You are now speaking for " + chatTargetName);
                                }
                    }
                }
            
            }
            if(cmd.getName().equalsIgnoreCase("welcome") && sender instanceof Player)
            {
                String welcome = this.getConfig().getString("welcome-message");
                Player player = (Player) sender;
                player.chat(welcome);
            }
            if(cmd.getName().equalsIgnoreCase("sethearts") && sender instanceof Player)
            {
            
                if(new Float(args[1]) > 1000)
                {
                    Player player = (Player) sender;
                    player.sendMessage(ChatColor.RED + "The max amount of hearts must be under 1000");
                
                } else {
                    Player player = (Player) sender;
                
                    if(args.length == 0)
                    {
                        player.sendMessage(ChatColor.RED + "Usage: /sethearts <Player> <Amount>");
                    }
                
                    for(Player target : Bukkit.getServer().getOnlinePlayers())
                    {
                        if(target.getName().equalsIgnoreCase(args[0]))
                                {
                                    target.setMaxHealth(new Float(args[1]) * 2);
                                    target.setHealth(new Float(args[1]) * 2);
                                }
                    }
                }
            
                return true;
            }
            if(cmd.getName().equalsIgnoreCase("kfs") || cmd.getName().equalsIgnoreCase("kfs"))
            {
                Player player = (Player) sender;
                Location loc = player.getLocation();
                    if(args.length == 0)
                    {
                    
                    }
                }
            return false;
        }
    
        @EventHandler
        public void PlayerChat(AsyncPlayerChatEvent e)
        {
            if(onCommand == true)
            {
                if(e.getPlayer().equals(chatSenderName))
                {
                    e.setCancelled(true);
                    Player target = Bukkit.getPlayer(chatTargetName);
                    String message = e.getMessage();
                    target.chat(message);
                }
            }
        }
    
        @EventHandler
        public void ServerPing(ServerListPingEvent e)
        {
            motd.replaceAll("_", " ");
            motd.replaceAll("&", "\u00A7");
            this.getConfig().set("motd", motd);
            e.setMotd(getConfig().getString("motd"));
        }
    }


    ServerListPingEvent (open)

    Code:
        @EventHandler
        public void ServerPing(ServerListPingEvent e)
        {
            motd.replaceAll("_", " ");
            motd.replaceAll("&", "\u00A7");
            this.getConfig().set("motd", motd);
            e.setMotd(getConfig().getString("motd"));
        }

    Command (open)

    Code:
            if(cmd.getName().equalsIgnoreCase("setmotd") && sender instanceof Player)
            {
                if(!args[0].equals(null))
                {
                    motd = args[0];
                    this.getConfig().set("motd", motd);
                }
            }

    OnEnable (open)

    Code:
        @Override
        public void onEnable()
        {
            this.getServer().getPluginManager().registerEvents(this, this);
            onCommand = false;
            this.getConfig().addDefault("welcome-message", "Welcome to the server!");
            this.getConfig().addDefault("motd", "A Minecraft Server");
            this.getConfig().options().copyDefaults(true);
            saveConfig();
            this.logger.info("Plugin enabled! :D");
        }


    Thats it! Please help! :D
     
    Last edited: Jul 18, 2015
  2. Offline

    Azen0xCore

    You don't say what is the problem but after line 58, you don't save config:
    Code:
    this.getConfig().set("motd", motd);
    saveconfig();
     
  3. Offline

    schwabfl

    I don't know what to say about this..

    1) String#replace() returns a copy and does not change the string (in this case motd)
    2) Like @Azen0xCore said, save your config, after you set a value (or in onDisable(), which is blank in your plugin, I don't know why)
    3) Follow Java naming conventions
    4)
    Code:
    if(!args[0].equals(null))
    if the player does not specify any arguments, this will throw an ArrayIndexOutOfBoundsException because args does not have a position 0
    check the length of the array instead.
     
  4. Thanks for the info schwabfl
    :D

    Sorry for the lack of info! Post updated

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jul 15, 2015
  5. Offline

    guitargun

    I have not looked into the event but you only initialize the string motd when you call the command for it. Since this is a nullpointer and it is pointing to an empty String I would say your problem is there. Also try initializing that string in the onenable so it's not null to start with
     
  6. Since I made that post I've made quite a few changes, the new file is:
    Code:
    package com.krazytar.plugins;
    
    import java.util.Random;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.event.server.ServerListPingEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MainFile extends JavaPlugin implements Listener
    {
      
       String chatSenderName;
       String chatTargetName;
       boolean onCommand;
       MainFile configGetter;
       String motd;
       String message;
      
       public final Logger logger = Logger.getLogger("Minecraft");
      
       @Override
       public void onEnable()
       {
         loadConfiguration();
         this.getServer().getPluginManager().registerEvents(this, this);
         onCommand = false;
         this.logger.info(ChatColor.GOLD + "[KrazyFunStuff] " + ChatColor.BLUE + "KrazyFunStuff Has been enabled!");
       }
      
       @Override
       public void onDisable()
       {
         this.logger.info(ChatColor.GOLD + "[KrazyFunStuff] " + ChatColor.BLUE + "KrazyFunStuff Has been disabled! :(");
         this.logger.info(ChatColor.GOLD + "[KrazyFunStuff] " + ChatColor.BLUE + "I... I hope it was for the greater cause... :(");
    
       }
      
       public void loadConfiguration()
       {
         this.getConfig().addDefault("welcome-message", "Welcome to the server!");
         this.getConfig().addDefault("motd", null);
         motd = this.getConfig().getString("motd");
         this.getConfig().options().copyDefaults(true);
         saveConfig();
       }
      
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
       {
         Player player = (Player) sender;
         if(cmd.getName().equalsIgnoreCase("setmotd") && sender instanceof Player)
         {
           if(args.length == 0)
           {
             this.getConfig().set("motd", args[0]);
             saveConfig();
             motd = this.getConfig().getString("motd");
           } else if (args.length > 0)
           {
             player.sendMessage("Usage: /setmotd <Motd>");
           }
         }
         if(cmd.getName().equalsIgnoreCase("chat") && sender instanceof Player)
         {
           Location loc = player.getLocation();
           if(args.length == 1)
           {
             for(Player target : Bukkit.getServer().getOnlinePlayers())
             {
               if(target.getName().equalsIgnoreCase(args[0]))
                   {
                 onCommand = true;
                 chatSenderName = player.getName();
                 chatTargetName = args[0];
                 player.sendMessage("You are now speaking for " + chatTargetName);
                   }
             }
           }
          
         }
         if(cmd.getName().equalsIgnoreCase("welcome") && sender instanceof Player)
         {
           String welcome = this.getConfig().getString("welcome-message");
           player.chat(welcome);
         }
         if(cmd.getName().equalsIgnoreCase("sethearts") && sender instanceof Player)
         {
          
           if(new Float(args[1]) > 1000)
           {
             player.sendMessage(ChatColor.RED + "The max amount of hearts must be under 1000");
            
           } else {
            
             if(args.length == 0)
             {
               player.sendMessage(ChatColor.RED + "Usage: /sethearts <Player> <Amount>");
             }
            
             for(Player target : Bukkit.getServer().getOnlinePlayers())
             {
               if(target.getName().equalsIgnoreCase(args[0]))
                   {
                     target.setMaxHealth(new Float(args[1]) * 2);
                     target.setHealth(new Float(args[1]) * 2);
                   }
             }
           }
          
           return true;
         }
         if(cmd.getName().equalsIgnoreCase("kfs") || cmd.getName().equalsIgnoreCase("kfs"))
         {
           Location loc = player.getLocation();
             if(args.length == 0)
             {
              
             }
           }
         return false;
       }
      
       @EventHandler
       public void PlayerChat(AsyncPlayerChatEvent e)
       {
         if(onCommand == true)
         {
           if(e.getPlayer().equals(chatSenderName))
           {
             e.setCancelled(true);
             Player target = Bukkit.getPlayer(chatTargetName);
             message = e.getMessage();
             target.chat(message);
           }
         }
       }
      
       @EventHandler
       public void ServerPing(ServerListPingEvent e)
       {
         motd = motd.replaceAll("_", " ");
         this.getConfig().set("motd", motd);
         saveConfig();
         e.setMotd(motd);
       }
    }
    

    So @guitargun

    Do you mind telling me the line your talking about?
     
  7. Offline

    timtower Administrator Administrator Moderator

    @Ultracrepadarian Please use this.getLogger instead of getLogger("Minecraft"), that was the way years ago.
    The enabled and disabled messages are done by Bukkit itself already.
     
  8. Thanks @timtower :D And i already knew that :D I was just testing :D

    Thank you all for the great tips! Although I'm afraid my problem still isn't solved :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  9. I found out my problem! So I was doing two things wrong, First, I was seeing if the args.length was 0, When it should have been one. Second, I was trying to get my motd sting from config, but earlier in code, I assigned it to Args[0], But since the event was outside the onCommand function, It couldn't find args[0]. Just wanted to write that to help someone else if they had the same problem :D Thanks everyone!
     
Thread Status:
Not open for further replies.

Share This Page