Switch arguments

Discussion in 'Plugin Development' started by Shzylo, Jul 10, 2013.

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

    Shzylo

    I was having a problem in which I could not get my commands to work correctly, having 2 different classes and both have the same actual command name, but not the same sub commands. I was referenced to the McMMO github (link 1, link 2) since I was doing basically the same thing as them.

    I am having a kind-of different set up as they had for it. The error is coming from my enumeration class 'SubcommandType'.

    Here is the code of my main class (plugin name replaced with 'MyPlugin'):

    Code:java
    1. public final class MyPlugin extends JavaPlugin {
    2. private final Logger logger = Logger.getLogger("Minecraft");
    3. private CommandExecutor wandCommand = new CmdWand(this);
    4.  
    5. private static final List<String> subcommandList;
    6.  
    7. static {
    8. ArrayList<String> subcommands = new ArrayList<String>();
    9. for(SubcommandType subcommand : SubcommandType.values()) {
    10. subcommands.add(subcommand.toString());
    11. }
    12. Collections.sort(subcommands);
    13. subcommandList = ImmutableList.copyOf(subcommands);
    14. }
    15.  
    16. public void onEnable() {
    17. PluginDescriptionFile pdf = this.getDescription();
    18. this.logger.info("MyPlugin v" + pdf.getVersion() + " Successfully Booted Up.");
    19. }
    20.  
    21. public void onDisable() {
    22. PluginDescriptionFile pdf = this.getDescription();
    23. this.logger.info("MyPlugin v" + pdf.getVersion() + " Successfully Shut Down.");
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    27. String cmd = command.getName();
    28. SubcommandType subcommand = SubcommandType.getSubcommandType(args[0]);
    29.  
    30. if (cmd.equalsIgnoreCase("mycommand")) {
    31. sender.sendMessage("It Worked.");
    32. }
    33.  
    34. if(args.length > 0) {
    35. switch (subcommand) {
    36. case WAND:
    37. return wandCommand.onCommand(sender, command, label, args);
    38. default:
    39. break;
    40. }
    41. }
    42. return false;
    43. }
    44. }


    the case WAND works fine, gives me the message, but if I were to have another case, and I type "/mycommand <command>" it will do both the first case and the second.

    and here is the code for SubcommandType:
    Code:java
    1. public enum SubcommandType {
    2. RELOAD, WAND;
    3.  
    4. public static SubcommandType getSubcommandType(String cmdName) {
    5. for (SubcommandType cmd : values()) {
    6. if (cmd.name().equalsIgnoreCase(cmdName)) {
    7. return cmd;
    8. }
    9. }
    10. if (cmdName.equalsIgnoreCase("reload")) {
    11. return RELOAD;
    12. } else if (cmdName.equalsIgnoreCase("wand")) {
    13. return WAND;
    14. }
    15. return null;
    16. }
    17. }


    Just an example of a class that is returned:
    Code:java
    1. public class CmdWand implements CommandExecutor {
    2. MyPlugin plugin;
    3.  
    4. public CmdWand(MyPlugin instance) {
    5. this.plugin = instance;
    6. }
    7.  
    8. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    9. switch (args.length) {
    10. case 1:
    11. sender.sendMessage("It Worked.");
    12. }
    13.  
    14. if(args.length > 1) {
    15. sender.sendMessage("Too many arguments!");
    16. }
    17. return false;
    18. }
    19. }


    Here is my error:
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'mycommand'
    in plugin MyPlugin v0.0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerCon
    nection.java:964)
            at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.j
    ava:882)
            at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :839)
            at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
            at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :118)
            at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.shzylo.myplugin.MyPlugin.onCommand(MyPlugin.java:46)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
     
  2. Offline

    LinearLogic

    Confirm that args.length > 0 before calling
    Code:java
    1. SubcommandType subcommand = SubcommandType.getSubcommandType(args[0]);
     
  3. Offline

    Shzylo

    I did:
    Code:java
    1. if (cmd.equalsIgnoreCase("mycommand")) {
    2. sender.sendMessage("It Worked.");
    3. }
    4.  
    5. if(args.length > 0) {
    6. switch (subcommand) {
    7. case WAND:
    8. return wandCommand.onCommand(sender, command, label, args);
    9. default:
    10. break;
    11. }
    12. }
     
  4. Offline

    LinearLogic

    That code is executed after you call the line in question...
     
  5. Offline

    SnipsRevival

    Shzylo Yes, but you assigned that variable before checking how many args you have.
     
Thread Status:
Not open for further replies.

Share This Page