Commands /tell

Discussion in 'Spigot Plugin Development' started by Carloswasthere, Jun 8, 2015.

  1. I have a this code but not work 100%

    If write: /tell Carloswasthere Hi my good friend!!
    only receive: /tell Carloswasthere Hi

    in the three argument something is wrong...

    and if the player not online give error...

    Code (Text):
    2015-06-08 15:34:33 [INFORMACIÓN] Carloswasthere issued server command: /tell s s
    2015-06-08 15:34:33 [GRAVE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tell' in plugin EsencialPlugin v1.0
    >       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    >       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
    >       at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServer.java:546)
    >       at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerConnection.java:979)
    >       at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.java:895)
    >       at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:840)
    >       at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
    >       at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:224)
    >       at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:109)
    >       at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
    >       at org.spigotmc.MultiplexingServerConnection.b(MultiplexingServerConnection.java:72)
    >       at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:583)
    >       at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:227)
    >       at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:472)
    >       at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:404)
    >       at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    >       at carlos.was.there.gmail.com.Principal.onCommand(Principal.java:119)
    >       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    >       ... 15 more
     

    Code (Text):
        if ((cmd.getName().equalsIgnoreCase("msg"))|| (cmd.getName().equalsIgnoreCase("tell")) || (cmd.getName().equalsIgnoreCase("t")) || (cmd.getName().equalsIgnoreCase("m")))
        {
            if(sender instanceof Player){
             
                if(args.length == 0){
                    player.sendMessage(ChatColor.RED + "Usa los comandos:");
                    player.sendMessage(ChatColor.GREEN + "- /msg <jugador> <mensaje>");
                    player.sendMessage(ChatColor.GREEN + "- /tell <jugador> <mensaje>");
                    player.sendMessage(ChatColor.GREEN + "- /t <jugador> <mensaje>");
                    player.sendMessage(ChatColor.GREEN + "- /m <jugador> <mensaje>");
                } else if(args.length == 1 ){
                    player.sendMessage(ChatColor.RED + "Usa los comandos:");
                    player.sendMessage(ChatColor.GREEN + "- /msg <jugador> <mensaje>");
                    player.sendMessage(ChatColor.GREEN + "- /tell <jugador> <mensaje>");
                    player.sendMessage(ChatColor.GREEN + "- /t <jugador> <mensaje>");
                    player.sendMessage(ChatColor.GREEN + "- /m <jugador> <mensaje>");
                } else if(args.length > 1){
                    StringBuilder str = new StringBuilder();
                    for (int i = 1; i < args.length; i++); {
                        str.append(args[1] + " ");
                    }
                    @SuppressWarnings("deprecation")
                    Player targetplayer = Bukkit.getPlayerExact(args[0]);
                    if(targetplayer.isOnline()){
                        targetplayer.sendMessage(ChatColor.LIGHT_PURPLE + player.getName() + ChatColor.GREEN + " » " + ChatColor.LIGHT_PURPLE + targetplayer.getName() + ChatColor.GRAY + " > " + ChatColor.WHITE + str.toString().trim());
                    } else {
                        sender.sendMessage(ChatColor.RED + targetplayer.getName() + " no está conectado.");
                    }
                }
         
            }
        }
     
  2. Try this:
    Code (Text):

    StringBuilder sb = new StringBuilder();
    for (int i = 1; i < args.length; i++) {
         sb.append(args[i]);
         if(i < args.length) {      
             sb.append(" ");
         }
    }
     
    Send the message:
    Code (Text):

    targetplayer.sendMessage(player.getName() + " §a" + sb.toString());  
     
     
    • Useful Useful x 1
  3. No problem.
     
  4. @Carloswasthere Bukkit.getPlayer(String) will return null when the player isn't online. Hence you will attempt to call isOnline() on null if they aren't online. Use a null check instead.