[Solved] /setspawn and /spawn commands

Discussion in 'Spigot Plugin Development' started by Fjpackard, May 12, 2015.

  1. I have been trying to create a plugin that implements a /setspawn and a /spawn command for Spigot 1.8.

    I get this error : An internal error has occurred whilst trying to run this command

    Here is my code :
    Code (Text):
    package me.fjpackard.teleport;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class FjTeleport extends JavaPlugin {

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if (!(sender instanceof Player))
            {
                sender.sendMessage(ChatColor.RED + "[Error] You must be a player to execute this command.");
                return true;
            }
           
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("tp"))
            {
                if (args.length == 0)
                {
                    p.sendMessage(ChatColor.RED + "[Error] You must specify a player.");
                    return true;
                }
            }
            Player target = Bukkit.getServer().getPlayer(args[0]);
            if (target == null)
            {
                p.sendMessage(ChatColor.RED + "[Error] That player could not be found.");
                return true;
            }
            p.teleport(target.getLocation());
           
            if (cmd.getName().equalsIgnoreCase("setspawn")) {
                getConfig().set("spawn.world", p.getLocation().getWorld().getName());
                getConfig().set("spawn.x", p.getLocation().getX());
                getConfig().set("spawn.y", p.getLocation().getY());
                getConfig().set("spawn.z", p.getLocation().getZ());
                saveConfig();
                p.sendMessage(ChatColor.GREEN + "The spawn location has been set.");
                return true;
            }
               
            if (cmd.getName().equalsIgnoreCase("spawn")) {
                if (getConfig().getConfigurationSection("spawn") == null) {
                    p.sendMessage(ChatColor.RED + "[Error] The spawn location has not been set.");
                    return true;
                }
                World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
                double x = getConfig().getDouble("spawn.x");
                double y = getConfig().getDouble("spawn.y");
                double z = getConfig().getDouble("spawn.z");
                p.teleport(new Location(w, x, y, z));
                p.sendMessage(ChatColor.GREEN + "You have been teleported to the spawn location.");
            }
           
            if (cmd.getName().equalsIgnoreCase("testcoords"))
            {
                Location loc = p.getLocation();
            }
           
            return true;
        }
       
    }
     
    And here's what the console prints when I type /setspawn :
    Code (Text):
    [17:54:07 INFO]: Fjpackard issued server command: /setspawn
    [17:54:07 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'setspawn' in plugin FjTeleport v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1133) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:968) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) [?:1.6.0_65]
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) [?:1.6.0_65]
        at java.util.concurrent.FutureTask.run(FutureTask.java:138) [?:1.6.0_65]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:710) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        at java.lang.Thread.run(Thread.java:695) [?:1.6.0_65]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at me.fjpackard.teleport.FjTeleport.onCommand(FjTeleport.java:32) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Spigot-2f787bd-ea28011]
        ... 16 more
    Please help ASAP !

    Thanks,
    Fjpackard.
     
  2. Your tp command is not enclosed within the tp command check.

    So every time you issue a command which is listed below (see bel0w), the code below (see below) is executed. And the index out of bounds exception is generated because there is no args[0].

    Code (Text):

            Player target = Bukkit.getServer().getPlayer(args[0]);
            if (target == null)
            {
                p.sendMessage(ChatColor.RED + "[Error] That player could not be found.");
                return true;
            }
            p.teleport(target.getLocation());
     
    • Winner Winner x 1
  3. That was such a simple error, I can't believe I didn't see that... x) Thank you very much, I spent such a long time trying to figure out why this didn't work ! :D
     
  4. Sometimes an unbiased eye is good :).
     
    • Agree Agree x 1