Teleportation

Discussion in 'Spigot Plugin Development' started by Sunique, Jun 14, 2016.

  1. Are you checking if the command length is at least 1? If not, the code is basically saying:

    You: If the first argument is equal to "hub"
    Code: How do I know what the first argument is? Nothing was specified.

    So basically, it's telling you that you're trying to access an argument that isn't there, hence why it throws an ArrayIndexOutOfBoundsException.

    The description of an ArrayIndexOutOfBoundsException from the Oracle documents:

    Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

    Your issue described right there. You're trying to access an index index that isn't the size of your arguments.
     
  2. So what do I do to fix it?
     
  3. Check if the args length is greater than or equal to 1.
     
  4. On line 242?
     
  5. Before line 242 to make sure it actually has the required amount of arguments before accessing them.
     
  6. Like this?
    Code (Text):
    if(args.length >= 1)
            if(p.getWorld().getName().equals("HUB")&&args[0].equalsIgnoreCase("hub")){
                p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "You are already connected to" + ChatColor.GREEN + " Hub");
                return false;
            }
    EDIT This command doesn't have any errors tho. It's /spawn and /prison that have errors.
     
  7. Paste the errors here and we can try and help! :)
     
  8. I don't get a error with /server hub but I get errors with /spawn and /prison here is my full code.
    http://pastebin.com/UZhaT9xW


    I get this error with /spawn and /prison


    Code (Text):
    [15:18:12 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'prison' in plugin ApexNation v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-d97e08b-5e85956]
            at me.incomprehendable.dev.pwp.FakeSimpleCommandMap.dispatch(FakeSimpleCommandMap.java:98) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:640) ~[spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_72]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_72]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.sunique.ApexNation.Main.onCommand(Main.java:242) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-d97e08b-5e85956]
            ... 15 more
    [15:18:13 INFO]: UniqueUsername issued server command: /spawn
    [15:18:13 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'spawn' in plugin ApexNation v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-d97e08b-5e85956]
            at me.incomprehendable.dev.pwp.FakeSimpleCommandMap.dispatch(FakeSimpleCommandMap.java:98) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:640) ~[spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_72]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_72]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot.jar:git-Spigot-d97e08b-5e85956]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.sunique.ApexNation.Main.onCommand(Main.java:242) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-d97e08b-5e85956]
            ... 15 more
     
  9. Okay? You have the exact same issues with both of your commands? Just check if the arguments length is greater than or equal to 1, and add the other arguments as well.

    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at me.sunique.ApexNation.Main.onCommand(Main.java:242) ~[?:?]

    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at me.sunique.ApexNation.Main.onCommand(Main.java:242) ~[?:?]


    By the way, I recommend reading this guide:
    https://bukkit.org/threads/how-to-r...ubleshoot-your-own-plugins-by-yourself.32457/
     
  10. I added that to line 242?
     
  11. You're checking if args[0] equals hub.
    If you're typing /spawn, and that's it, then there is no args[0].
    Add a check for the length of the command.
    Code (Text):
    if (args.length == 1) {
      if (p.getWorld().getName().equals("HUB") && args[0].equalsIgnoreCase("hub")) {
        //Do stuff
      }
    } else {
      p.sendMessage("Not enough/too many args");
    }
     
    You have to check if there is an args[0] before you check what it is.
    NOTE: args[0] is the word after the command
    If the command is /spawn hub, hub is args[0]

    EDIT: Don't know if my information is still relevant, didn't even know that @GamerzKing already told him this.
     
    • Agree Agree x 1
  12. Omw, just show us what is on line 242.
     
  13. Your kidding right? I have said it at least fifty times..

     
  14.  
  15. While YoungCerly has pointed out the issue, I would personally have the code more like this:
    http://pastebin.com/fk6TFvYi

    It's easier to check argument length and avoid running code that doesn't need to be.

    EDIT: Also format the code to java next time. http://pastebin.com/UWFX8kQa
     
    • Useful Useful x 1
  16. Anyway, since you won't show us your full code, I am going to show you how I setup my commands... And they work.

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    final Player p = (Player) sender;

    if (args.length == 0) {
    if (cmd.getName().equalsIgnoreCase("hub") {
    if (p.getWorld().getName().equals("HUB")) {
    // teleport
    return true;
    } else {
    p.sendMessage("You must be in world HUB");
    return true;
    }
    } else if (cmd.getName().equalsIgnoreCase("spawn") {
    // teleport
    return true;
    }
    } else {
    if (args.length == 1) {
    if (args[0].equalsIgnoreCase("test") {
    //blah
    return true;
    }
    }

    }
     
  17. Why are you checking for the length before you check for the command name?
    Also don't cast player to sender before you know the sender is actually a player.
    Will cause a lot of issues for the people who use consoles.

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args {
     
      if (cmd.getName().equalsIgnoreCase("spawn") {
        if (sender instanceof player {
          Player p = (Player) sender;
          if (args.length == 0) {
            Location loc = new Location(w, x, y, z);
            p.teleport(loc);
            p.sendMessage(Chatcolor.translateAlternateColorCodes(&, "&a&lPlugin &8&l> &7You have been teleported to spawn!"));
          }
          if (args.length > 1) {
            p.sendMessage(Chatcolor.translateAlternateColorCodes(&, "&a&lPlugin &8&l> &7Too many arguments!"));
          }
        } else {
          sender.sendMessage("You must be a player to teleport to spawn!");
        }
      }
    }
    That's how I do it, however I'm sure there are some issues with my code.
     
  18. Here is my class.. It is super glitching I took your advice but it's not working out.
    http://pastebin.com/BeVaDirx

    Code (Text):
            if (cmd.getName().equalsIgnoreCase("server")) {
                if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("hub") && !p.getWorld().getName().equals("HUB")) {
                        Location loc = new Location(Bukkit.getWorld("HUB"), -587.445, 30, -522.442);
                        loc.setYaw((float) 0.9);
                        loc.setPitch((float) -28.2);
                        p.teleport(loc);
                        p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "Connected to" + ChatColor.GREEN + " Hub");
                        ItemStack g = (ItemStack) new ItemStack(Material.DRAGON_EGG);
                        ItemMeta gm = (ItemMeta) g.getItemMeta();
                        gm.setDisplayName(ChatColor.RED+ "Gadgets " + ChatColor.GRAY + "(Right Click)");
                        g.setItemMeta(gm);

                        ItemStack t = (ItemStack) new ItemStack(Material.COMPASS);
                        ItemMeta tm = (ItemMeta) t.getItemMeta();
                        tm.setDisplayName(ChatColor.RED+ "Teleporter " + ChatColor.GRAY + "(Right Click)");
                        t.setItemMeta(tm);

                        ItemStack sb = (ItemStack) new ItemStack(Material.SUGAR);
                        ItemMeta sbm = (ItemMeta) sb.getItemMeta();
                        sbm.setDisplayName(ChatColor.RED+ "Speed Buff " + ChatColor.GRAY + "(Right Click)");
                        sb.setItemMeta(sbm);

                        p.getInventory().clear();
                        p.getInventory().setItem(4, t);
                        p.getInventory().setItem(0, g);
                        p.getInventory().setItem(8, sb);
                        return false;
                 
                    } else if (args[0].equalsIgnoreCase("pvp")) {
                        p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "This server is not ready yet!");
                 
                    } else if (args[0].equalsIgnoreCase("minigames")) {
                        p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "This server is not ready yet!");
                 
                    } else if (args[0].equalsIgnoreCase("prison")) {
                        p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "This server is not ready yet!");
                        Location loc = new Location(Bukkit.getWorld("Prison"), -71.518, 67.0, -840.548);
                        loc.setYaw((float) 0.9);
                        loc.setPitch((float) -28.2);
                        p.teleport(loc);
                        p.setAllowFlight(true);
                        p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "Connected to" + ChatColor.GREEN + " Prison");
                 
                    } else {
                        p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "That server does not exist!");
                    }
             
                } else {
                    p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "You are currently on server " + ChatColor.GREEN + p.getWorld().getName());
                }



             
                  if (cmd.getName().equalsIgnoreCase("hub")) {
                        if (sender instanceof Player) {
                          if (args.length == 0) {
                              Location loc = new Location(Bukkit.getWorld("HUB"), -587.445, 30, -522.442);
                                loc.setYaw((float) 0.9);
                                loc.setPitch((float) -28.2);
                                p.teleport(loc);
                                p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "Connected to" + ChatColor.GREEN + " Hub");
                                ItemStack g = (ItemStack) new ItemStack(Material.DRAGON_EGG);
                                ItemMeta gm = (ItemMeta) g.getItemMeta();
                                gm.setDisplayName(ChatColor.RED+ "Gadgets " + ChatColor.GRAY + "(Right Click)");
                                g.setItemMeta(gm);
                 
                                ItemStack t = (ItemStack) new ItemStack(Material.COMPASS);
                                ItemMeta tm = (ItemMeta) t.getItemMeta();
                                tm.setDisplayName(ChatColor.RED+ "Teleporter " + ChatColor.GRAY + "(Right Click)");
                                t.setItemMeta(tm);
                 
                                ItemStack sb = (ItemStack) new ItemStack(Material.SUGAR);
                                ItemMeta sbm = (ItemMeta) sb.getItemMeta();
                                sbm.setDisplayName(ChatColor.RED+ "Speed Buff " + ChatColor.GRAY + "(Right Click)");
                                sb.setItemMeta(sbm);
                 
                                p.getInventory().clear();
                                p.getInventory().setItem(4, t);
                                p.getInventory().setItem(0, g);
                                p.getInventory().setItem(8, sb);
                          }
                          if (args.length > 1) {
                            p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " +  ChatColor.GRAY + "You have too many arguements! Use " + ChatColor.GREEN + "/hub" + ChatColor.GRAY + ".");
                          }
                        } else {
                          sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "You must be a player to use this command.");
                        }
                      }
                    }
         

         
                  if (cmd.getName().equalsIgnoreCase("spawn")) {
                        if (sender instanceof Player) {
                            if(p.getWorld().getName().equalsIgnoreCase("Prison") || p.getWorld().getName().equalsIgnoreCase("Prison-1")){
                          if (args.length == 0) {
                                p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "Teleporting...");
                                Location loc = new Location(Bukkit.getWorld("Prison"), -71.518, 67.0, -840.548);
                                loc.setYaw((float) 0.9);
                                loc.setPitch((float) -28.2);
                                p.teleport(loc);
                             
                                if(!p.getWorld().getName().equalsIgnoreCase("Prison") || !p.getWorld().getName().equalsIgnoreCase("Prison-1")){
                                    p.sendMessage("Sorry but, thats not a command!");              
                                }
                          }
                          if (args.length > 1) {
                              p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " +  ChatColor.GRAY + "You have too many arguements! Use " + ChatColor.GREEN + "/spawn" + ChatColor.GRAY + ".");
                          }
                        } else {
                             sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "You must be a player to use this command.");
                        }
                      }

                if(cmd.getName().equalsIgnoreCase("servers")){
                    p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "You can connect to");
                    p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "Hub, Prison, Factions, PvP, or Minigames.");
                    return false;
                }
                  }
                return false;
            }
        }
     
  19. Ok Here is your code:
    Code (Text):
    if (cmd.getName().equalsIgnoreCase("server")) {
                if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("hub") && !p.getWorld().getName().equals("HUB")) {
                        Location loc = new Location(Bukkit.getWorld("HUB"), -587.445, 30, -522.442);
                        loc.setYaw((float) 0.9);
                        loc.setPitch((float) -28.2);
                        p.teleport(loc);
                        p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "APEXNATION" + ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + " >> " + ChatColor.GRAY + "Connected to" + ChatColor.GREEN + " Hub");
                        ItemStack g = (ItemStack) new ItemStack(Material.DRAGON_EGG);
                        ItemMeta gm = (ItemMeta) g.getItemMeta();
                        gm.setDisplayName(ChatColor.RED+ "Gadgets " + ChatColor.GRAY + "(Right Click)");
                        g.setItemMeta(gm);
                        ItemStack t = (ItemStack) new ItemStack(Material.COMPASS);
                        ItemMeta tm = (ItemMeta) t.getItemMeta();
                        tm.setDisplayName(ChatColor.RED+ "Teleporter " + ChatColor.GRAY + "(Right Click)");
                        t.setItemMeta(tm);
                        ItemStack sb = (ItemStack) new ItemStack(Material.SUGAR);
                        ItemMeta sbm = (ItemMeta) sb.getItemMeta();
                        sbm.setDisplayName(ChatColor.RED+ "Speed Buff " + ChatColor.GRAY + "(Right Click)");
                        sb.setItemMeta(sbm);
                        p.getInventory().clear();
                        p.getInventory().setItem(4, t);
                        p.getInventory().setItem(0, g);
                        p.getInventory().setItem(8, sb);
                        return false;
                   
                    }
    What you want to do is make it so the code is formatted like this:
    Code (Text):
    if (cmd...) {

    if (args..) {

    // Only if they type /server

    } else if (args.length) {

    // Check for arguments of Hub

    }

    }
     
  20. Yeah but how would I make it so it sends them a message if they are already in the world hub?