Commands Not Executing?

Discussion in 'Plugin Development' started by LegitJava, Sep 29, 2013.

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

    LegitJava

    Hi there,

    I recently got done developing a mini-game plugin no sooner to find out the commands weren't executing. I type the name of the command but doesn't bother to actually execute the command, all it does is display the usage information that I defined in my plugin.yml. Can anyone help me with this? Thanks in advance!

    CommandManager:
    Code:java
    1. public class CommandManager implements CommandExecutor {
    2.  
    3. private ArrayList<SubCommand> commands = new ArrayList<SubCommand>();
    4.  
    5. public void setup() {
    6. commands.add(new Create());
    7. commands.add(new Delete());
    8. commands.add(new ForceStart());
    9. commands.add(new ForceStop());
    10. commands.add(new Join());
    11. commands.add(new Leave());
    12. commands.add(new Reload());
    13. commands.add(new SetLocation());
    14. }
    15.  
    16. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    17.  
    18. if (!(sender instanceof Player)) {
    19. MessageManager.getInstance().msg(sender, MessageType.BAD, "You cannot use Dodgeball from the console!");
    20. return true;
    21. }
    22.  
    23. Player p = (Player) sender;
    24.  
    25. if (cmd.getName().equalsIgnoreCase("dodgeball")) {
    26. if (args.length == 0) {
    27. for (SubCommand c : commands) {
    28. MessageManager.getInstance().msg(p, MessageType.INFO, "/dodgeball " + c.name() + "(" + aliases(c) + ")" + " - " + c.info());
    29. }
    30. return true;
    31. }
    32.  
    33. SubCommand target = get(args[0]);
    34.  
    35. if (target == null) {
    36. MessageManager.getInstance().msg(p, MessageType.BAD, "/dodgeball " + args[0] + " is not a valid subcommand!");
    37. return true;
    38. }
    39.  
    40. ArrayList<String> a = new ArrayList<String>();
    41. a.addAll(Arrays.asList(args));
    42. a.remove(0);
    43. args = a.toArray(new String[a.size()]);
    44.  
    45. try {
    46. target.onCommand(p, args);
    47. }
    48.  
    49. catch (Exception e) {
    50. MessageManager.getInstance().msg(p, MessageType.BAD, "An error has occured: " + e.getCause());
    51. e.printStackTrace();
    52. }
    53. }
    54. return true;
    55. }
    56.  
    57. private String aliases(SubCommand cmd) {
    58. String fin = "";
    59.  
    60. for (String a : cmd.aliases()) {
    61. fin += a + " | ";
    62. }
    63.  
    64. return fin.substring(0, fin.lastIndexOf(" | "));
    65. }
    66.  
    67. private SubCommand get(String name) {
    68. for (SubCommand cmd : commands) {
    69. if (cmd.name().equalsIgnoreCase(name)) return cmd;
    70. for (String alias : cmd.aliases()) if (name.equalsIgnoreCase(alias)) return cmd;
    71. }
    72. return null;
    73. }
    74. }

    Main class (setting up CommandManager):
    Code:java
    1. CommandManager cm = new CommandManager();
    2. cm.setup();
    3. getCommand("dodgeball").setExecutor(cm);

    SubCommands Class:
    Code:java
    1. public abstract class SubCommands {
    2.  
    3. public abstract void onCommand(Player p, String[] args);
    4.  
    5. public abstract String name();
    6.  
    7. public abstract String info();
    8.  
    9. public abstract String[] aliases();
    10.  
    11. }
     
  2. Offline

    1Rogue Retired Staff

    Instead of using getCommand within the main class and .setup(), just use it in the constructor:

    Code:java
    1. CommandManager cm = new CommandManager(this);
    2.  
    3. /* CommandManager.java */
    4.  
    5. public CommandManager(YourPlugin plugin) {
    6. SubCommand commands = new SubCommand[] {
    7. new Create(),
    8. new Delete(),
    9. new ForceStart(),
    10. new ForceStop(),
    11. new Join(),
    12. new Leave(),
    13. new Reload(),
    14. new SetLocation()
    15. };
    16.  
    17. for (SubCommand cmd : commands) {
    18. this.commands.add(cmd);
    19. plugin.getCommand(cmd.getName()).setExecutor(this);
    20. }
    21. }


    Also, you are using "SubCommand" but your class is named "SubCommands"
     
  3. Offline

    nrs23

    Also make sure that all the commands are registered in Plugin.yml
     
Thread Status:
Not open for further replies.

Share This Page