Problem with arguments

Discussion in 'Spigot Plugin Development' started by AJKproductions, Jun 20, 2015.

  1. Hello,
    i have made an extremely basic heal command that takes in 2 arguments: the amount and the player. Problem is that if the player does not enter any arguments i want it to default to the sender and heal him to his max health.
    However i am getting this error:
    [11:22:05] [Server thread/INFO]: Starting minecraft server version 1.8
    [11:22:05] [Server thread/INFO]: Loading properties
    [11:22:05] [Server thread/INFO]: Default game type: SURVIVAL
    [11:22:05] [Server thread/INFO]: This server is running CraftBukkit version git-Spigot-d0d1d87-15e81cf (MC: 1.8) (Implementing API version 1.8-R0.1-SNAPSHOT)
    [11:22:05] [Server thread/INFO]: Debug logging is enabled
    [11:22:05] [Server thread/INFO]: Server Ping Player Sample Count: 12
    [11:22:05] [Server thread/INFO]: Using 4 threads for Netty based IO
    [11:22:05] [Server thread/INFO]: Generating keypair
    [11:22:05] [Server thread/INFO]: Starting Minecraft server on *:25565
    [11:22:05] [Server thread/INFO]: Set PluginClassLoader as parallel capable
    [11:22:05] [Server thread/INFO]: [FirstPlugin] Loading FirstPlugin v0.0.1
    [11:22:05] [Server thread/INFO]: **** Beginning UUID conversion, this may take A LONG time ****
    [11:22:05] [Server thread/INFO]: Preparing level "world"
    [11:22:05] [Server thread/INFO]: -------- World Settings For [world] --------
    [11:22:05] [Server thread/INFO]: Arrow Despawn Rate: 1200
    [11:22:05] [Server thread/INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [11:22:05] [Server thread/INFO]: Item Merge Radius: 2.5
    [11:22:05] [Server thread/INFO]: Item Despawn Rate: 6000
    [11:22:05] [Server thread/INFO]: View Distance: 10
    [11:22:05] [Server thread/INFO]: Zombie Aggressive Towards Villager: true
    [11:22:05] [Server thread/INFO]: Nerfing mobs spawned from spawners: false
    [11:22:05] [Server thread/INFO]: Cactus Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Cane Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Melon Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Mushroom Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Pumpkin Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Sapling Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Wheat Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Mob Spawn Range: 4
    [11:22:05] [Server thread/INFO]: Anti X-Ray: true
    [11:22:05] [Server thread/INFO]: Engine Mode: 1
    [11:22:05] [Server thread/INFO]: Hidden Blocks: [14, 15, 16, 21, 48, 49, 54, 56, 73, 74, 82, 129, 130]
    [11:22:05] [Server thread/INFO]: Replace Blocks: [1, 5]
    [11:22:05] [Server thread/INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [11:22:05] [Server thread/INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
    [11:22:05] [Server thread/INFO]: Alternative Hopper Ticking: false
    [11:22:05] [Server thread/INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [11:22:05] [Server thread/INFO]: Random Lighting Updates: false
    [11:22:05] [Server thread/INFO]: Structure Info Saving: true
    [11:22:05] [Server thread/INFO]: Sending up to 10 chunks per packet
    [11:22:05] [Server thread/INFO]: Max TNT Explosions: 100
    [11:22:05] [Server thread/INFO]: Chunks to Grow per Tick: 650
    [11:22:05] [Server thread/INFO]: Clear tick list: false
    [11:22:05] [Server thread/INFO]: Experience Merge Radius: 3.0
    [11:22:05] [Server thread/INFO]: Max Entity Collisions: 8
    [11:22:05] [Server thread/INFO]: Custom Map Seeds: Village: 10387312 Feature: 14357617
    [11:22:05] [Server thread/INFO]: -------- World Settings For [world_nether] --------
    [11:22:05] [Server thread/INFO]: Arrow Despawn Rate: 1200
    [11:22:05] [Server thread/INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [11:22:05] [Server thread/INFO]: Item Merge Radius: 2.5
    [11:22:05] [Server thread/INFO]: Item Despawn Rate: 6000
    [11:22:05] [Server thread/INFO]: View Distance: 10
    [11:22:05] [Server thread/INFO]: Zombie Aggressive Towards Villager: true
    [11:22:05] [Server thread/INFO]: Nerfing mobs spawned from spawners: false
    [11:22:05] [Server thread/INFO]: Cactus Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Cane Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Melon Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Mushroom Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Pumpkin Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Sapling Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Wheat Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Mob Spawn Range: 4
    [11:22:05] [Server thread/INFO]: Anti X-Ray: true
    [11:22:05] [Server thread/INFO]: Engine Mode: 1
    [11:22:05] [Server thread/INFO]: Hidden Blocks: [14, 15, 16, 21, 48, 49, 54, 56, 73, 74, 82, 129, 130]
    [11:22:05] [Server thread/INFO]: Replace Blocks: [1, 5]
    [11:22:05] [Server thread/INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [11:22:05] [Server thread/INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
    [11:22:05] [Server thread/INFO]: Alternative Hopper Ticking: false
    [11:22:05] [Server thread/INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [11:22:05] [Server thread/INFO]: Random Lighting Updates: false
    [11:22:05] [Server thread/INFO]: Structure Info Saving: true
    [11:22:05] [Server thread/INFO]: Sending up to 10 chunks per packet
    [11:22:05] [Server thread/INFO]: Max TNT Explosions: 100
    [11:22:05] [Server thread/INFO]: Chunks to Grow per Tick: 650
    [11:22:05] [Server thread/INFO]: Clear tick list: false
    [11:22:05] [Server thread/INFO]: Experience Merge Radius: 3.0
    [11:22:05] [Server thread/INFO]: Max Entity Collisions: 8
    [11:22:05] [Server thread/INFO]: Custom Map Seeds: Village: 10387312 Feature: 14357617
    [11:22:05] [Server thread/INFO]: -------- World Settings For [world_the_end] --------
    [11:22:05] [Server thread/INFO]: Arrow Despawn Rate: 1200
    [11:22:05] [Server thread/INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [11:22:05] [Server thread/INFO]: Item Merge Radius: 2.5
    [11:22:05] [Server thread/INFO]: Item Despawn Rate: 6000
    [11:22:05] [Server thread/INFO]: View Distance: 10
    [11:22:05] [Server thread/INFO]: Zombie Aggressive Towards Villager: true
    [11:22:05] [Server thread/INFO]: Nerfing mobs spawned from spawners: false
    [11:22:05] [Server thread/INFO]: Cactus Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Cane Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Melon Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Mushroom Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Pumpkin Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Sapling Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Wheat Growth Modifier: 100%
    [11:22:05] [Server thread/INFO]: Mob Spawn Range: 4
    [11:22:05] [Server thread/INFO]: Anti X-Ray: true
    [11:22:05] [Server thread/INFO]: Engine Mode: 1
    [11:22:05] [Server thread/INFO]: Hidden Blocks: [14, 15, 16, 21, 48, 49, 54, 56, 73, 74, 82, 129, 130]
    [11:22:05] [Server thread/INFO]: Replace Blocks: [1, 5]
    [11:22:05] [Server thread/INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [11:22:05] [Server thread/INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
    [11:22:05] [Server thread/INFO]: Alternative Hopper Ticking: false
    [11:22:05] [Server thread/INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [11:22:05] [Server thread/INFO]: Random Lighting Updates: false
    [11:22:05] [Server thread/INFO]: Structure Info Saving: true
    [11:22:05] [Server thread/INFO]: Sending up to 10 chunks per packet
    [11:22:05] [Server thread/INFO]: Max TNT Explosions: 100
    [11:22:05] [Server thread/INFO]: Chunks to Grow per Tick: 650
    [11:22:05] [Server thread/INFO]: Clear tick list: false
    [11:22:05] [Server thread/INFO]: Experience Merge Radius: 3.0
    [11:22:05] [Server thread/INFO]: Max Entity Collisions: 8
    [11:22:05] [Server thread/INFO]: Custom Map Seeds: Village: 10387312 Feature: 14357617
    [11:22:05] [Server thread/INFO]: Preparing start region for level 0 (Seed: 101180657721616679)
    [11:22:06] [Server thread/INFO]: Preparing start region for level 1 (Seed: 101180657721616679)
    [11:22:06] [Server thread/INFO]: Preparing start region for level 2 (Seed: 101180657721616679)
    [11:22:07] [Server thread/INFO]: [FirstPlugin] Enabling FirstPlugin v0.0.1
    [11:22:07] [Server thread/INFO]: [0;32;1m[[0;30;1mFirstPlugin[0;32;1m] [0;33;22mWarning! [0;33;1mThis is just a test warning[m
    [11:22:07] [Server thread/INFO]: [0;32;1m[[0;30;1mFirstPlugin[0;32;1m] [0;34;1mInfo: [0;36;22mThis is just test info[m
    [11:22:07] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [11:22:07] [Server thread/INFO]: Done (1.267s)! For help, type "help" or "?"
    [11:22:09] [Netty Server IO #1/DEBUG]: Set listener of [email protected] to [email protected]
    [11:22:09] [Netty Server IO #1/DEBUG]: Enabled auto read
    [11:22:09] [Netty Server IO #1/DEBUG]: Enabled auto read
    [11:22:09] [Netty Server IO #1/DEBUG]: Set listener of [email protected] to [email protected]
    [11:22:10] [Netty Server IO #2/DEBUG]: Set listener of [email protected] to [email protected]
    [11:22:10] [Netty Server IO #2/DEBUG]: Enabled auto read
    [11:22:10] [Netty Server IO #2/DEBUG]: Enabled auto read
    [11:22:10] [Netty Server IO #2/DEBUG]: Set listener of [email protected] to [email protected]
    [11:22:11] [User Authenticator #1/DEBUG]: Opening connection to https://sessionserver.mojang.com/se...0cdf63ae62fe187494cab&username=AJKproductions
    [11:22:11] [User Authenticator #1/DEBUG]: Reading data from https://sessionserver.mojang.com/se...0cdf63ae62fe187494cab&username=AJKproductions
    [11:22:12] [User Authenticator #1/DEBUG]: Successful read, server response was 200
    [11:22:12] [User Authenticator #1/DEBUG]: Response: {"id":"7f628058c12140768efb68594e332c02","name":"AJKproductions","properties":[{"name":"textures","value":"eyJ0aW1lc3RhbXAiOjE0MzQ3OTU3MjQ0MTAsInByb2ZpbGVJZCI6IjdmNjI4MDU4YzEyMTQwNzY4ZWZiNjg1OTRlMzMyYzAyIiwicHJvZmlsZU5hbWUiOiJBSktwcm9kdWN0aW9ucyIsInRleHR1cmVzIjp7IlNLSU4iOnsidXJsIjoiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS9hYjMyZDBjZGVhZDc5MjBkYjZkYzMyNGNiMWVmMGY4OGZjZmY1MzUxMTg0OGNjNDkyZmI4ZDY5M2RhZTZlNjgifX19","signature":"jKt02RhDak7NB13X82IBoMyXagwMwtUDU6ju5mf0gFEOIzFDV6rKMnlAFxGxLqoMuhoNaNWE7bG9pM8nOnm4MSf2Z8gG6wJZYRZk9oHppUSoUAaVEBsufwWSF+Vgg+E8bFEXS3srFA5BZdqRKpV8IABzWA2ukVxG4y96p4PpbYZLYderT8aT0SyUL7teqNXca5yQOwU2BEIVszzJLUO+3M6+Aw53/LA9VDR7DU1/qnxBRFdgUaBK4C+jB808LRPJN+lwQuXuJFfbeWkhySI4AX60vG2xgyQ+Oymwg81QGHwlgtVF3Mfi2wpmYAcjxt0h+3zrlEgsD+9mm/yCnOkg+F7UTaaFPe7lut25ULVGDHVicuChJt4hJ825MWPg8r1fwiwzwHxL8Jxb4+Js5acOQYHfycTHtLP71vMqq7+mgrdsB5FkHZVsAJSBMVnPypgH7Jx9dE/MVtpasH0cAsF4FneD9pNr0adJAGve+7ySnnika0leBVUE0ngx0nUn92lCzX+h8MxyP12Baj62qQQkuOqw/4QC/1PHtU2rRVAreUB3y7L1P92sX+tnTQdlanlhJaJPjlDoU15BEjgCn95Lqb+pg0JAwoBPZMOm5fX54PDQ+VAqinnKnewD2yxwyQhgFiBsHoou30HRa8xd/1v4iEmVdPumDBJnlbnQ+wsZ1PM="}]}
    [11:22:12] [User Authenticator #1/INFO]: UUID of player AJKproductions is 7f628058-c121-4076-8efb-68594e332c02
    [11:22:12] [Server thread/DEBUG]: Set listener of [email protected] to [email protected]
    [11:22:12] [Server thread/DEBUG]: Disabled auto read
    [11:22:12] [Netty Server IO #2/DEBUG]: Enabled auto read
    [11:22:12] [Server thread/INFO]: AJKproductions[/127.0.0.1:49185] logged in with entity id 25 at ([world] -65.30000001192093, 67.0, 44.69999998807907)
    [11:22:19] [Server thread/INFO]: AJKproductions issued server command: /gamemode 1
    [11:22:19] [Server thread/INFO]: [AJKproductions: Set own game mode to Creative Mode]
    [11:22:29] [Server thread/INFO]: AJKproductions issued server command: /time set -
    [11:22:31] [Server thread/INFO]: AJKproductions issued server command: /time set 0
    [11:22:31] [Server thread/INFO]: [AJKproductions: Set the time to 0]
    [11:22:47] [Server thread/INFO]: AJKproductions issued server command: /gamemode 0
    [11:22:47] [Server thread/INFO]: [AJKproductions: Set own game mode to Survival Mode]
    [11:22:50] [Server thread/INFO]: AJKproductions issued server command: /heal
    [11:22:50] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'heal' in plugin FirstPlugin v0.0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:683) [spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at com.enjin.thewarofworlds.Commands.TESTHeal.onCommand(TESTHeal.java:25) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.jar:git-Spigot-d0d1d87-15e81cf]
    ... 14 more
    [11:22:55] [Server thread/INFO]: AJKproductions issued server command: /heal 20
    [11:23:12] [Server thread/INFO]: AJKproductions lost connection: Disconnected
    [11:23:12] [Server thread/INFO]: AJKproductions left the game.
    [12:05:32] [Server thread/INFO]: Stopping the server
    [12:05:32] [Server thread/INFO]: Stopping server
    [12:05:32] [Server thread/INFO]: [FirstPlugin] Disabling FirstPlugin v0.0.1
    [12:05:32] [Server thread/INFO]: [FirstPlugin] Plugin has been disabled, successfully
    [12:05:32] [Server thread/INFO]: Saving players
    [12:05:32] [Server thread/INFO]: Saving worlds
    [12:05:32] [Server thread/INFO]: Saving chunks for level 'world'/Overworld
    [12:05:32] [Server thread/INFO]: Saving chunks for level 'world_nether'/Nether
    [12:05:32] [Server thread/INFO]: Saving chunks for level 'world_the_end'/The End
    [12:05:32] [Thread-4/INFO]: Stopping server

    and here is the code from my main class and the commands class:

    (command class)
    Code (Text):
    package com.enjin.thewarofworlds.Commands;

    import com.enjin.thewarofworlds.TESTMessenger;
    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    /**
    * Created by AJKproductions on 14/06/2015.
    */
    public class TESTHeal implements CommandExecutor {


        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("heal"))
            {
                if(sender instanceof Player)
                {
                    int length = args.length;
                    double healAmount = Double.parseDouble(args[0]);

                    Player player = (Player) sender;

                    if(length == 2)
                    {
                        Player target = Bukkit.getPlayer(args[1]); //Cast the string as a player
                        target.setHealth(healAmount);
                        sender.sendMessage(ChatColor.GREEN + "You healed " + ChatColor.RESET + args[1] + ChatColor.GREEN + " by " + ChatColor.GRAY + args[0]);
                        return true;

                    } if(length == 1)
                    {
                        player.setHealth(healAmount);
                        player.sendMessage(ChatColor.GREEN + "You healed yourself by " + ChatColor.GRAY + args[0]);
                        return true;
                    } if(args.toString() == ""){
                        player.setHealth(20);
                        player.sendMessage(ChatColor.GREEN + "You healed yourself by " + ChatColor.GRAY + 20);
                        return true;

                    }
                }else
                {
                    TESTMessenger.ConsoleWarn("Unable to execute a player command");
                }

            }
            return false;
        }
    }
     

    Code (Text):
    package com.enjin.thewarofworlds;


    import com.enjin.thewarofworlds.Commands.GetOnlinePlayers;
    import com.enjin.thewarofworlds.Commands.TESTHeal;
    import com.enjin.thewarofworlds.Commands.TestCommand;
    import com.enjin.thewarofworlds.Events.OnPlayerJoin;
    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    /**
    * Created by AJKproductions on 10/06/2015.
    */

    public class FirstPlugin extends JavaPlugin implements Listener
    {

        private static Plugin plugin;


        @Override
        public void onEnable()
        {
            plugin = this;
            registerEvents(this, new OnPlayerJoin());

            this.getCommand("test").setExecutor(new TestCommand());
            this.getCommand("heal").setExecutor(new TESTHeal());
            this.getCommand("getOnlinePlayers").setExecutor(new GetOnlinePlayers());


            TESTMessenger.ConsoleWarn("This is just a test warning");
            TESTMessenger.ConsoleInfo("This is just test info");

        }

        @Override
        public void onDisable()
        {
            plugin = null;
            getLogger().info("Plugin has been disabled, successfully");


        }


        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for (Listener listener : listeners) {
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
        }

        public static Plugin getPlugin() {
            return plugin;
        }
    }
     

    Hope you can help, i know its a really simple mistake but i can't seem to recognise what i have done wrong.
    All help is appreciated. :D
     
  2. double healAmount = Double.parseDouble(args[0]);
    You are trying to grab the arg 0 without checking if it has at least 1 argument.
     
    • Useful Useful x 1
  3. Thank You SubSide! I just used the Double.parseDouble(args[0]) where it is needed instead of defining a variable...
     
  4. Make sure you Try {} catch it, just imagine if they use a text instead of a number!