[Resource] AbstractCommand - Add commands without accessing your plugin.yml

Discussion in 'Resources' started by Goblom, Nov 17, 2013.

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

    Goblom

    Class is located here https://github.com/Goblom/Bukkit-Libraries/blob/master/src/main/java/command/AbstractCommand.java

    To Use
    Code:java
    1. public class MyPlugin extends JavaPlugin {
    2. public void onEnable() {
    3. AbstractCommand myCommand = new MyCommand("mycmd", "/<command> [args]", "This is my command");
    4. myCommand.register();
    5. }
    6. }
    Code:java
    1. public class MyCommand extends AbstractCommand {
    2.  
    3. public MyCommand(String command, String usage, String description) {
    4. super(command, usage, description);
    5. }
    6.  
    7. @Override
    8. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    9. //do command stuff
    10. }
    11. }
     
  2. Offline

    Goblom

    spoljo666 I Found the error but its not with your code, Its with mine.
    Line 58 is
    Code:java
    1. plgCMD = Bukkit.getServer().getPluginCommand(this.command);

    That will return null if the command does not yet exist. And getPluginCommand() gets a command from the plugin.yml (and the plugin.yml should not have any commands in it ;)). I am currently fixing this problem by using Reflection and i'm sure its not the best method, but i am 95% sure it will work.

    If you do not wish to use reflection you can un-comment all the plgCMD stuff and comment out all the reflection stuff. I forgot to set the executor for plgCMD before but its in their now..

    Edit: Updated AbstractCommand pushed
     
    spoljo666 likes this.
  3. Offline

    viper_monster

    Goblom, so if I add the command to plugin.yml it should work or?
     
  4. Offline

    Goblom

    spoljo666 With the new code it is not needed. The point of this resource is not having to touch the plugin.yml ;)

    spoljo666 Im guessing by the lack of comment that this is working ? :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    spoljo666 likes this.
  5. Offline

    viper_monster

    Goblom, oh yeah, completely forgot to answer... So far everything works :) There are no errors on startup and commands work properly.

    Goblom, actually, aliases for commands doesn't work.

    Code:java
    1. new CommandClearChat("clearchat", "clearchat [player]", "clears the chat", Arrays.asList("cc"));


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    Goblom likes this.
  6. Offline

    Goblom

    spoljo666 I moved the register around a little, its now after all data gets put into the command. Hopefully it works.

    Update has been pushed to the repo
     
    spoljo666 likes this.
  7. Offline

    viper_monster

  8. Offline

    r0llingthund3r

    This is pretty sick thanks for the share. I will definitely be using this in my plugins.
     
  9. Offline

    viper_monster

    Goblom, Hmmm, commands that are added with this util doesn't show up when you type /help or /help <yourPluginName>
     
  10. Offline

    Goblom

    spoljo666 Ill have to take a look at that later. I need to study the HelpMap for a bit to see how this is done. But, it is not a huge priority for me because you can add help messages to the command itself (in the onCommand()).
     
    spoljo666 likes this.
  11. Offline

    Debels

    Nice resource, this will solve so many issues my users have with my plugin, like other plugins having the same commands.

    I notice your using:
    Code:
    CraftServer.class.getDeclaredField("commandMap");
    you can change that for:
    Code:
    Bukkit.getServer().getClass().getDeclaredField("commandMap");
    so it works on all or almost all the versions of bukkit.

    edit: by the way, why do you have this?
    Code:
    private boolean isPlayer(CommandSender sender) { return (sender instanceof Player); }
        private boolean isAuthorized(CommandSender sender, String permission) { return sender.hasPermission(permission); }
        private boolean isAuthorized(Player player, String permission) { return player.hasPermission(permission); }
        private boolean isAuthorized(CommandSender sender, Permission perm) { return sender.hasPermission(perm); }
        private boolean isAuthorized(Player player, Permission perm) { return player.hasPermission(perm); }
    if you don't use it in that class?
     
  12. Offline

    Goblom

    Debels Its for anyone wanting to use it... This was originally made for me but after i realized that it is so much more then just easy command creation, so i gave it to you guys without any stripped code.

    That code is simple ways to check if player has a permission or if sender is player. If you do not want to use it you dont have to, if you do not like it in their you may remove it, there is no one stopping you or forcing you to do anything with that.

    Code:java
    1. Bukkit.getServer().getClass().getDeclaredField("commandMap");
    I do not know if that works or not, so i did not use it. And PS, this should work on any version of CraftBukkit all you would need to do is change "v1_6_R2" in line 38 to whatever version you want to use.

    If you want to test it with Bukkit.getServer().getClass() be my guest and post back the results of some testing. If not i could just get around to using reflection to dynamically import the CraftServer class.
     
  13. Offline

    Debels

    Well I use that code to replace "commandMap" and other classes on one of my WIP plugins and it works perfectly.

    What I mean by working on all Craftbukkit version I meant without having to change the version for each release.

    I'll put you on my plugin credits for this awesome resource, its really going to help me.
     
  14. Offline

    Goblom

    Debels So you are saying that Bukkit.getServer().getClass().getDeclaredField("commandMap") does work with adding commands using this class ?
     
  15. Offline

    Debels

    Goblom Sorry for the late answer, yes it does work with Bukkit.getServer().getClass().getDeclaredField("commandMap");
     
  16. Offline

    Goblom

    Updated AbstractCommand.

    March 13, 2014
    • Command no longer automatically registers
      • AbstractCommand cmd = new AbstractCommand();
      • cmd.register();
    • onTabComplete is now optional (must be overidden)
      • @Override
      • public List<String> onTablComplete(/*stuff*/) { }
    • Removed excess comments
     
  17. Offline

    Strat

    Hey, about your tabComplete.
    I tried using it, but no matter what I did, I could not get AbstractCommand to use tabComplete.
    After some code and javadoc digging, I figured out what was wrong, and decided to patch it.
    You can see my changes here; take note of the changes here, here, and here.
     
    Goblom likes this.
  18. Offline

    Goblom

Thread Status:
Not open for further replies.

Share This Page