Error in dispatching command

Discussion in 'BungeeCord Plugin Development' started by BiastGaming, May 17, 2016.

  1. My /report plugin is not working, i have provided the dstacktrace and the source code. I bet I did something real stupid thanks,
    Code (Text):
    17.05 11:05:02 [Server] INFO Login by BiastGaming
    17.05 11:05:09 [Server] WARNING Error in dispatching command
    17.05 11:05:09 [Server] INFO java.lang.ArrayIndexOutOfBoundsException: 0
    17.05 11:05:09 [Server] INFO at me.BiastGaming.SyntaxNetwork.Commands.ReportCommand.execute(ReportCommand.java:17)
    17.05 11:05:09 [Server] INFO at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:162)
    17.05 11:05:09 [Server] INFO at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:116)
    17.05 11:05:09 [Server] INFO at net.md_5.bungee.connection.UpstreamBridge.handle(UpstreamBridge.java:112)
    17.05 11:05:09 [Server] INFO at net.md_5.bungee.protocol.packet.Chat.handle(Chat.java:50)
    17.05 11:05:09 [Server] INFO at net.md_5.bungee.netty.HandlerBoss.channelRead(HandlerBoss.java:75)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:05:09 [Server] INFO at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:05:09 [Server] INFO at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:05:09 [Server] INFO at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:05:09 [Server] INFO at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:05:09 [Server] INFO at io.netty.handler.timeout.ReadTimeoutHandler.channelRead(ReadTimeoutHandler.java:150)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:05:09 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:05:09 [Server] INFO at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787)
    17.05 11:05:09 [Server] INFO at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
    17.05 11:05:09 [Server] INFO at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
    17.05 11:05:09 [Server] INFO at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
    Source Code For ReportCommand Class:

    Code (Text):
    package me.BiastGaming.SyntaxNetwork.Commands;

    import net.md_5.bungee.api.ChatColor;
    import net.md_5.bungee.api.CommandSender;
    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.plugin.Command;

    public class ReportCommand extends Command {
       
        public ReportCommand() {
            super ("report", "Biast.default", "rpt");
        }
        public void execute(CommandSender sender, String[] args) {
       
            ProxiedPlayer target = ProxyServer.getInstance().getPlayer(args[0]);
           
            if (args.length == 0) {
                sender.sendMessage(new TextComponent(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "Error" + ChatColor.DARK_GRAY + "]: " + ChatColor.RED + "You are trying to report player with no reason " + ChatColor.YELLOW + args[0] ));
            }
            if (target == null) {
                sender.sendMessage(new TextComponent (ChatColor.DARK_GRAY + "[" + ChatColor.RED + "Error" + ChatColor.DARK_GRAY + "]: " + ChatColor.RED + "The player is not online, if you have evidence report them on the forums!"));
            }
            String player = sender.getName();

             StringBuilder str = new StringBuilder();
             for(int i=1;i<args.length;i++)
             {
                 str.append(args[i] + " ");
             }
             String message1 = str.toString();

       
               
            for(ProxiedPlayer all : ProxyServer.getInstance().getPlayers()){
                if(all.hasPermission("Biast.staff")) {
                    all.sendMessage(new TextComponent(ChatColor.GRAY + "[" + ChatColor.RED + "" + ChatColor.BOLD + "STAFF" + ChatColor.GRAY + "]: " +  ChatColor.YELLOW + player + ChatColor.GRAY + " has just reported " + ChatColor.YELLOW + target + " with reason " + message1 + ChatColor.DARK_AQUA + " " + target.getServer().getInfo().getName()));
                }
               
            }
        }

    }
     
     
    #1 BiastGaming, May 17, 2016
    Last edited: May 17, 2016
  2. When you type /report the arguments length equals 0.

    Initialize the target only if the argument not equals 0.

    Code (Text):
    ProxiedPlayer target = null;
           
    if (args.length == 0) {
        sender.sendMessage(new TextComponent(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "Error" + ChatColor.DARK_GRAY + "]: " + ChatColor.RED + "You are trying to report player with no reason " + ChatColor.YELLOW + args[0] ));
    } else {
        target = ProxyServer.getInstance().getPlayer(args[0])
    }
    You will got an error with the if(target == null) ... Check it ;D
     
  3. Stacktrace still the same with updated code.
    Code (Text):
    package me.BiastGaming.SyntaxNetwork.Commands;

    import net.md_5.bungee.api.ChatColor;
    import net.md_5.bungee.api.CommandSender;
    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.plugin.Command;

    public class ReportCommand extends Command {
       
        public ReportCommand() {
            super ("report", "Biast.default", "rpt");
        }
        public void execute(CommandSender sender, String[] args) {
           
            ProxiedPlayer target = null;

           
            if (args.length == 0) {
                sender.sendMessage(new TextComponent(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "Error" + ChatColor.DARK_GRAY + "]: " + ChatColor.RED + "You are trying to report player with no reason " + ChatColor.YELLOW + args[0] ));
            }else {
               
                target = ProxyServer.getInstance().getPlayer(args[0]);
                String player = sender.getName();

                 StringBuilder str = new StringBuilder();
                 for(int i=1;i<args.length;i++)
                 {
                     str.append(args[i] + " ");
                 }
                 String message1 = str.toString();

           
                   
                for(ProxiedPlayer all : ProxyServer.getInstance().getPlayers()){
                    if(all.hasPermission("Biast.staff")) {
                        all.sendMessage(new TextComponent(ChatColor.GRAY + "[" + ChatColor.RED + "" + ChatColor.BOLD + "STAFF" + ChatColor.GRAY + "]: " +  ChatColor.YELLOW + player + ChatColor.GRAY + " has just reported " + ChatColor.YELLOW + target + " with reason " + message1 + ChatColor.DARK_AQUA + " " + target.getServer().getInfo().getName()));
               
            }
           
                }
               
            }
        }

    }
     
    StackTrace:
    Code (Text):
    17.05 11:16:24 [Server] WARNING Error in dispatching command
    17.05 11:16:24 [Server] INFO java.lang.ArrayIndexOutOfBoundsException: 0
    17.05 11:16:24 [Server] INFO at me.BiastGaming.SyntaxNetwork.Commands.ReportCommand.execute(ReportCommand.java:21)
    17.05 11:16:24 [Server] INFO at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:162)
    17.05 11:16:24 [Server] INFO at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:116)
    17.05 11:16:24 [Server] INFO at net.md_5.bungee.connection.UpstreamBridge.handle(UpstreamBridge.java:112)
    17.05 11:16:24 [Server] INFO at net.md_5.bungee.protocol.packet.Chat.handle(Chat.java:50)
    17.05 11:16:24 [Server] INFO at net.md_5.bungee.netty.HandlerBoss.channelRead(HandlerBoss.java:75)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:16:24 [Server] INFO at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:16:24 [Server] INFO at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:16:24 [Server] INFO at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:16:24 [Server] INFO at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:16:24 [Server] INFO at io.netty.handler.timeout.ReadTimeoutHandler.channelRead(ReadTimeoutHandler.java:150)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    17.05 11:16:24 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    17.05 11:16:24 [Server] INFO at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787)
    17.05 11:16:24 [Server] INFO at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
    17.05 11:16:24 [Server] INFO at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
    17.05 11:16:24 [Server] INFO at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
     
  4. You try to send a value not existing. I can't do the code for you. Search a little bit.
     
  5. But I'm not getting an NPE in stacktrace? And the error is to do with [0] in the get player method, but target already exists with the first line of code ProxiedPlayer target = null; do I have to recheck in }else {
     
  6. Doc

    Doc

    You can check again the "length".
    if i execute "/report Doc" you command not work...


    you can try this :3
    Code (Text):

    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.plugin.Command;

    public class ReportCommand extends Command {
     
      public ReportCommand() {
      super ("report", "Biast.default", "rpt");
      }
      public void execute(CommandSender sender, String[] args) {
     
      ProxiedPlayer target = null;

     
      if (args.length == 0) { // "/command"
      sender.sendMessage(new TextComponent(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "Error" + ChatColor.DARK_GRAY + "]: " + ChatColor.RED + "You are trying to report player with no reason " + ChatColor.YELLOW + args[0] ));
      } else if(args.length == 1) { // "/command PLAYER"
           //NEED MORE ARGS
         } else { //"/command PLAYER ARG2 ARG3"
     
     
           String playersh = "";
      StringBuilder str = new StringBuilder();
         
            int cont = 0;
      for (String txt : args) { //FOR into all values in args
         if (cont == 0) {
               playersh = txt;
      cont++;
      } else {
               str.append(txt + " ");
      }
      }

            target = ProxyServer.getInstance().getPlayer(playersh); //YOU NEED CHECK IF TARGET IS INSTANCE...
      String player = sender.getName();

      String message1 = str.toString();

     
     
      for(ProxiedPlayer all : ProxyServer.getInstance().getPlayers()){
      if(all.hasPermission("Biast.staff")) {
      all.sendMessage(new TextComponent(ChatColor.GRAY + "[" + ChatColor.RED + "" + ChatColor.BOLD + "STAFF" + ChatColor.GRAY + "]: " +  ChatColor.YELLOW + player + ChatColor.GRAY + " has just reported " + ChatColor.YELLOW + target + " with reason " + message1 + ChatColor.DARK_AQUA + " " + target.getServer().getInfo().getName()));
     
      }
     
      }
     
      }
      }

    }
     
    i dont try this code...
    I hope you find it useful.
     
  7. Ok, so you issue command:

    Command --- Args[0]
    VVV VVV
    /Report NOTHING

    It's an indexoutofboundsexception because you never sent in any arguments into the
    command that you are issuing.

    either handle the error, or use a try catch to let the user know
    that the command was not issued correctly.