Solved Teleport Here Problem (Help)

Discussion in 'Plugin Development' started by 123ang, Jul 20, 2014.

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

    123ang

    Alright I have some code below that is my code to teleport 1 player to the other. Everything is correct in the plugin.yml because my other commands work fine. I'm getting this error in-game "an internal error occurred while attempting to perform this command." In my console I get an error coming from line 315.

    Line 315 Code:

    Code:java
    1. Player arg1 = Bukkit.getServer().getPlayer(args[1]);


    Whole Command Code:

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("here")){
    2. if (player.hasPermission("teleport.player.here")){
    3. if (args.length == 0){
    4. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.RED + "Please specify 2 players.");
    5. return true;
    6. } else if(args.length == 1){
    7. Player arg0 = Bukkit.getServer().getPlayer(args[0]);
    8. Player arg1 = Bukkit.getServer().getPlayer(args[1]);
    9. if (arg0 == null){
    10. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "0 matches for " + ChatColor.RED + arg0.getName() + ChatColor.GRAY + ".");
    11. return true;
    12. } if(arg1 == null){
    13. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "0 matches for " + ChatColor.RED + arg1.getName() + ChatColor.GRAY + ".");
    14. return true;
    15. } if(arg0 == null || arg1 == null){
    16. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "0 matches for " + ChatColor.RED + arg0.getName() + ChatColor.GRAY + " & " + ChatColor.RED + arg1.getName() + ChatColor.GRAY + ".");
    17. }
    18. arg0.teleport(arg1.getLocation());
    19. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "You teleported " + ChatColor.GREEN + arg0.getName() + ChatColor.GRAY + " to " + ChatColor.GREEN + arg1.getName() + ChatColor.GRAY + ".");
    20. return true;
    21. }
    22. } else{
    23. player.sendMessage(ChatColor.GRAY + "This requires the permission rank " + ChatColor.GRAY + "[" + ChatColor.RED + "ADMIN" + ChatColor.GRAY + "]");
    24. return true;
    25. }
    26. }


    As I said the error comes from line 315, however, I don't know what is wrong. I'll give the console error as well.

    Console Error:

    Code:
    [18:46:59 INFO]: 123ang issued server command: /here 123ang
    [18:46:59 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'here
    ' in plugin Gadgets v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:742) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at me.angtim123.gadgets.GadgetInv.onCommand(GadgetInv.java:315) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Thanks in advance for anyone who can help! :D
     
  2. Offline

    N1T3SLAY3R

    There is only one argument in /here 123ang
    the /here or here is the string or command name and the arguments happen afterwards. so 123ang would be args[0] hence there is nothing afterwards since the arguments length is only 1 making it null which is the error you see
     
  3. Offline

    123ang

    N1T3SLAY3R Okay I did /here 123ang angtim but nothing showed up in chat, no errors, nothing in console, nothing! What is wrong?
     
  4. Offline

    N1T3SLAY3R

    you have an improper teleport command, you need to grab a player not just a string:

    if (Bukkit.getPlayer(args0) != null&&Bukkit.getPlayer(args1) != null) {
    Bukkit.getPlayer(args0).teleport(Bukkit.getPlayer(args1).getLocation);
    } else {
    //1 or both of the players are not online
    }
     
  5. Offline

    123ang

    N1T3SLAY3R Sorry what is the code above doing, and what does it replace??
     
  6. Offline

    N1T3SLAY3R

    Wait now im confused, is the player your trying to get not in the game with you?
    Either way the issue (looking at the code again) is the the else if (args.length==1){
    it needs to be changed to 2 to cover both players or else it will only run if theres one. For example: /home test
    is only 1 args length, it says nothing because the args length is 2 when you enter the second name
     
  7. Offline

    123ang

    N1T3SLAY3R I get an error with getPlayer()

    Error

    "The method getPlayer(String) in the type Bukkit is not applicable for the arguments (Player)"

    N1T3SLAY3R It's a tp here command. So if there is only 1 argument it sends an error message to the players saying you need 2 arguments. Then if there are 2 arguments but the players are offline it sends another error message to the player saying that they need to be online. Finally if there are 2 args, the players are both online, the argument 1 gets tp to the argument 2 player. Make sense?

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

    N1T3SLAY3R

    you did /here 123ang angtim, if the name has a space you need an underscore otherwise its length is 2 arguments

    this should be the code your looking for:

    Code:
    if (cmd.getName().equalsIgnoreCase("here")){
          if (player.hasPermission("teleport.player.here")){
              if (args.length == 0){
                  player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.RED + "Please specify 2 players.");
                  return true;
              } else if(args.length == 1){
                  Player arg0 = Bukkit.getServer().getPlayer(args[0]);
                  Player arg1 = (Player) sender;
                  if (arg0 == null){
                      player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "0 matches for " + ChatColor.RED + arg0.getName() + ChatColor.GRAY + ".");
                      return true;
                  }
                  arg0.teleport(arg1.getLocation());
                  player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "You teleported " + ChatColor.GREEN + arg0.getName() + ChatColor.GRAY + " to " + ChatColor.GREEN + arg1.getName() + ChatColor.GRAY + ".");
                  return true;
              }
          } else{
              player.sendMessage(ChatColor.GRAY + "This requires the permission rank " + ChatColor.GRAY + "[" + ChatColor.RED + "ADMIN" + ChatColor.GRAY + "]");
              return true;
          }
      }
     
  9. Offline

    123ang

    N1T3SLAY3R I want it to have 2 arguments... 123ang is the first, and angtim is the second.
     
  10. Offline

    N1T3SLAY3R

    then as i said above you need to change the arguments length to 2
    an arguments length of 1:
    /here name1
    name1 = args[0]
    an arguments length of 2:
    /here name1 name2
    name1 = args[0], name2 = args[1]
    your else if has an args length check of 1
     
  11. Offline

    123ang

    N1T3SLAY3R Solved It!

    Updated Code:

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("here")){
    2. if (player.hasPermission("teleport.player.here")){
    3.  
    4. if (args.length == 0){
    5. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.RED + "Please specify 2 players.");
    6. return true;
    7. } if (args.length == 1){
    8. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.RED + "Please specify 2 players.");
    9. return true;
    10. } if (args.length == 2){
    11. Player arg0 = Bukkit.getServer().getPlayer(args[0]);
    12. Player arg1 = Bukkit.getServer().getPlayer(args[1]);
    13.  
    14. if (arg0 == null){
    15. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "0 matches for " + ChatColor.RED + args[0] + ChatColor.GRAY + ".");
    16. } else if (arg1 == null){
    17. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "0 matches for " + ChatColor.RED + args[1] + ChatColor.GRAY + ".");
    18. } else if (arg0 == null){
    19. if (arg1 == null){
    20. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "0 matches for " + ChatColor.RED + args[0] + ChatColor.GRAY + " & " + ChatColor.RED + args[1] + ChatColor.GRAY + ".");
    21. }
    22. } else if (arg0 != null && arg1 != null){
    23. arg0.teleport(arg1.getLocation());
    24. player.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "You teleported " + ChatColor.GREEN + args[0] + ChatColor.GRAY + " to " + ChatColor.GREEN + args[1] + ChatColor.GRAY + "!");
    25. arg0.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GRAY + "You were teleported to " + ChatColor.GREEN + args[1] + ChatColor.GRAY + "!");
    26. arg1.sendMessage(ChatColor.GOLD + "Teleport> " + ChatColor.GREEN + args[0] + ChatColor.GRAY + " was teleported to you!");
    27.  
    28. }
    29. }
    30. } else{
    31. player.sendMessage(ChatColor.GRAY + "This requires the permission rank " + ChatColor.GRAY + "[" + ChatColor.RED + "ADMIN" + ChatColor.GRAY + "]");
    32. }
    33.  
    34.  
    35. }
     
Thread Status:
Not open for further replies.

Share This Page