Solved CommandException

Discussion in 'Plugin Development' started by badboysteee98, Apr 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    badboysteee98

    Hello Bukkit,

    So I'm making a Watch plugin where if people with a permission can watch another player but when the player unwatchs the player I want to teleport them back to the spawn location anyway of doing this so they run the command?
     
  2. Offline

    TrollTaylor

    You can always
    Code:
      player.teleport(player.getWorld().getSpawnLocation());
    
     
    badboysteee98 likes this.
  3. Offline

    badboysteee98

    TrollTaylor Thanks

    Update to this post:

    When I do this
    Code:java
    1. Player target = Bukkit.getServer().getPlayer(args[0]);


    For some reason it throws me this when I try to put a player's name in

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    TrollTaylor

    badboysteee98 Is this on line 24? That is where the error is.
    Code:
     Player target = Bukkit.getServer().getPlayer(args[0]);
     
  5. Offline

    mazentheamazin

    badboysteee98
    You did not input any arguments, you must check the argument length, and make sure it is enough to run your command properly, if not, send them a message saying they're using the incorrect syntax.
     
  6. Offline

    badboysteee98

    TrollTaylor Yep

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  7. Offline

    badboysteee98

    Bump*

    Not going to lie not trying to be meaning to bump this because I bumped 17 mins ago but I could do with some help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  8. Offline

    badboysteee98

    I still getting an error on this line

    Code:java
    1. Player target = p.getServer().getPlayer(args[0]);


    Stack Trace

    PHP:
    [15:01:59 ERROR]: null
    org
    .bukkit.command.CommandExceptionUnhandled exception executing command 'watc
    h' 
    in plugin Watch v1.0
            at org
    .bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit
    -1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0
    ) ~[craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r
    .java:696) ~[craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
     
            
    at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection
    .java:953) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050
    jnks
    ]
            
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :815) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:148
    ) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit
    -1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67
    ) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60
    ) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58
    ) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28
    ) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
    Caused byjava.lang.ArrayIndexOutOfBoundsException0
            at com
    .ste.Watch.Watch.onCommand(Watch.java:24) ~[?:?]
            
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit
    -1.7.9.jar:git-Bukkit-1.7.2-R0.3-21-g1ab090e-b3050jnks]
            ... 
    13 more
     
  9. Offline

    tryy3

    Not enough arguments when you run the command, check if the "args" length is long enough
    example
    Code:java
    1. if (args.length > 1)
    2. {
    3. ...
    4. }
     
  10. Offline

    badboysteee98

    tryy3 already do that, that is not the problem this is
    Code:java
    1. Player target = p.getServer().getPlayer(args[0]);


    Full Code
    Code:java
    1. package com.ste.Watch;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Watch extends JavaPlugin {
    11.  
    12. public void onEnable() {
    13. Bukkit.getServer().getLogger().info("[Watched] Enabled!");
    14. }
    15.  
    16. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    17.  
    18. if (!(sender instanceof Player)) {
    19. sender.sendMessage(ChatColor.RED + "You must be a player to do that command");
    20. return true;
    21. }
    22.  
    23. Player p = (Player) sender;
    24. Player target = p.getServer().getPlayer(args[0]);
    25.  
    26. if(p.hasPermission("watch.player")) {
    27. if(args.length < 1) {
    28. if(cmd.getName().equalsIgnoreCase("watch")) {
    29. p.sendMessage(ChatColor.YELLOW + "/watch <player>");
    30. return true;
    31. }
    32. }
    33.  
    34. if(target == null) {
    35. p.sendMessage(ChatColor.RED + "Could not find player " + ChatColor.YELLOW + target);
    36. return true;
    37. }
    38. else {
    39. if(args.length == 1) {
    40. p.teleport(target.getLocation());
    41. p.hidePlayer(p);
    42. return true;
    43. }
    44. }
    45. }
    46.  
    47. if(p.hasPermission("unwatch.player")) {
    48. if(cmd.getName().equalsIgnoreCase("unwatch")) {
    49. p.showPlayer(p);
    50. p.teleport(p.getWorld().getSpawnLocation());
    51. return true;
    52. }
    53. }
    54. return true;
    55.  
    56. }
    57.  
    58. }
    59.  
     
  11. Offline

    tryy3

    its giving you an error because args[0] doesn't exists, put the if(args.length < 1) before you make the variable
    Code:java
    1. Player target = p.getServer().getPlayer(args[0]);
     
  12. Offline

    badboysteee98

    tryy3 Where would I put this? Example if you don't mind? Sorry for asking :)
     
  13. Offline

    tryy3

    Code:
    if(args.length > 1)
    {
        Player target = p.getServer().getPlayer(args[0]);
        ...
    }
    else
    {
        ...
    }
     
    badboysteee98 likes this.
  14. Offline

    badboysteee98

    tryy3 Thank you it works
     
Thread Status:
Not open for further replies.

Share This Page