Creating command aliases?

Discussion in 'Plugin Development' started by Mattz, Jan 15, 2013.

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

    Mattz

    Is there an easy way to create aliases for commands in my plugin, instead of copying the entire code for each alias?
     
  2. if(commandLabel.equalsIgnoreCase("alias") || commandLabel.equalsIgnoreCase("alias1")
    {
    //Check for args and stuff
    }
    EDIT: The Bukkit plugin.yml setup also supports command aliases (base commands only).
     
  3. Offline

    Mattz

    Thanks.
    I also just discovered the cmd.getAliases() method. What does that do?
     
  4. Stick with the traditional methods. It probably checks from the plugin.yml.
     
  5. Mattz
    Code:
    name: hi
    version: 3264587
    main: this.no.hi.herp
    commands:
      thisisaverylongcommandthatneedsanalias:
        aliases: [thisis, command, hi]
    
    The aliases for "thisisaverylongcommandthatneedsanalias" is now "thisis", "command" and "hi" :p
     
  6. Personally, i would recommend you go with RainoBoy's method of adding an alias.

    Why? well, no extra code on your behalf, just simply adding a word Also because if you are doing commands through a separate executor, i have a feeling it wouldn't work without extra code. Also i don't like the use of commandLabel. Oh and while i think about it, it's easier to read if you have say a dozen aliases in the yml than if you had a dozen commandLabel.equalsIgnoreCase("blah").
     
  7. Offline

    Mattz

    Thanks, it's work all good now.

    Just a quick thing, I'm getting the usage of the command (again), I can't see if I'm missing anything or what.

    Main class:
    Code:
    package net.thepvp.broadcaster;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class AdvancedBroadcaster extends JavaPlugin {
        
        @Override
        public void onEnable() {
            this.saveDefaultConfig();
            getCommand("broadcast").setExecutor(new Commands(this));
            getCommand("bc").setExecutor(new Commands(this));
        }
        
        @Override
        public void onDisable() {
            
        }
    }
    
    Commands.java:
    Code:
    package net.thepvp.broadcaster;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class Commands implements CommandExecutor {
        
        // 50 characters long
        
        AdvancedBroadcaster plugin;
        private String notificationPrefix = plugin.getConfig().getString("notificationPrefix");
        private String version = Integer.toString(plugin.getConfig().getInt("pluginVersion"));
        @SuppressWarnings("unused")
        private String savedMessage;
        private String configVariable;
        private String configValue;
        
        public Commands(AdvancedBroadcaster plugin) {
            this.plugin = plugin;
        }
        
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(plugin.getConfig().getBoolean("enableBroadcaster") == false) {
                sender.sendMessage("Unknown command. Type \"help\" for help.");
            } else {
                if(args.length == 0) {
                    if(cmd.getName().equalsIgnoreCase("broadcast")) {
                        sender.sendMessage(ChatColor.GOLD + notificationPrefix + ChatColor.GREEN + " AdvancedBroadcaster " + version + ChatColor.BLUE + ", created by Enotho");
                        return true;
                    }
                } else {
                    if(args[0].equals("edit")) {
                        if(plugin.getConfig().getBoolean("consoleEditingOnly") == true) {
                            sender.sendMessage(ChatColor.GOLD + notificationPrefix + ChatColor.BLUE + "You can only edit saved messages from the console!");
                        } else {
                            sender.sendMessage(ChatColor.BLUE + "-- SAVED MESSAGES --------------------------------");
                            //for(ConfigurationSection configurationSection : plugin.getConfig().getConfigurationSection("messages")) {
                            //    savedMessage = configurationSection.getString("message").substring(0, 42);
                            //    sender.sendMessage(ChatColor.GREEN + configurationSection.getName() + ". " + ChatColor.WHITE + "\"" + savedMessage + "\"");
                            //}
                        }
                        return true;
                    } else if(args[0].equals("config")) {
                        if(args[1].equals("get")) {
                            configVariable = args[2];
                            try {
                                configValue = plugin.getConfig().get(configVariable).toString();
                                sender.sendMessage(ChatColor.GOLD + notificationPrefix + " CONFIG: " + ChatColor.BLUE + "The value of \'" + configValue + "\' is " + ChatColor.GOLD + configValue);
                            } catch(Exception e) {
                                sender.sendMessage(ChatColor.GOLD + notificationPrefix + ChatColor.BLUE + " Unknown value \'" + configValue + "\'");
                            }
                        }
                        return true;
                    }
                }
                return true;
            }
            sender.sendMessage("test");
            return false;
        }
    }
    
    Plugin.yml has everything it needs.
     
  8. Mattz

    Bukkit will return whatever you enter for "usage:" of the command in your plugin.yml whenever the function returns false. So if you don't want to make that message appear, just make sure your command is returning as true, not letting it reach the false at the end.
     
  9. Offline

    Mattz

    The thing is, I don't even know how it gets down there.

    I set the return false to true, and now essentials took over with its own /broadcast command.
     
  10. Are you purposefully letting it reach the point where it has "sender.sendMessage("test");" ?
     
  11. Offline

    Mattz

    I'm not, actually, it doesn't show the message in chat.

    Normally the code would be...
    Code:
    public onCommand() {
    if(cmd.getName()) {
    } }
    ... but as you can see, it checks in the config file whether or not the plugin is disabled/hidden, then proceeds with the commands.

    I also just noticed this error in the console:
    Code:
    2013-01-15 19:33:39 [SEVERE] Could not load 'plugins\AdvancedBroadcaster.jar' in
     folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ClassNotFoundException: net.
    thepvp.broadcaster.AdvancedBroadcaster
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:184)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.loadPlugins(CraftServer.jav
    a:239)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.reload(CraftServer.java:591
    )
            at org.bukkit.Bukkit.reload(Bukkit.java:184)
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    6)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer
    .java:514)
            at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConn
    ection.java:979)
            at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.ja
    va:897)
            at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:
    852)
            at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
    
            at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:
    112)
            at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30
    )
            at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:59
    8)
            at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:22
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:49
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:
    427)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:84
    9)
    Caused by: java.lang.ClassNotFoundException: net.thepvp.broadcaster.AdvancedBroa
    dcaster
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader
    .java:80)
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:53)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:173)
            ... 21 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  12. Mattz

    That's an error with the whole plugin, it's not loading for some reason. Somewhere your code isn't quite right, it's not necessarily an issue with the code you've been talking about.
     
  13. theguynextdoor
    Finally someone that spells my name correctly!

    Mattz
    I would think its an compiling error, or something in the plugin.yml
     
  14. Offline

    Mattz

    It looks fine to me:
    Code:
    name: AdvancedBroadcaster
    main: net.thepvp.broadcaster.AdvancedBroadcaster
    version: 1.0.0
    
    commands:
      broadcast:
        description: Everything you need in AdvancedBroadcaster
        usage: /broadcast
        permission: AdvancedBroadcaster.broadcast
        permission-message: You don't have permission to use this command!
        aliases: [bc]
     
  15. btw if you dont want to use the plugin.yml,
    just have your command call a method outside oncommand and call that same method on the alias too
     
  16. Offline

    Mattz

    I prefer to have it in the yml, but the issue I'm having is that the command returns the usage.

    Just got a completely different error:
    Code:
    2013-01-16 17:57:27 [INFO] [AdvancedBroadcaster] Enabling AdvancedBroadcaster v1
    .0.0
    2013-01-16 17:57:27 [SEVERE] Error occurred while enabling AdvancedBroadcaster v
    1.0.0 (Is it up to date?)
    java.lang.NullPointerException
            at net.thepvp.broadcaster.Commands.<init>(Commands.java:13)
            at net.thepvp.broadcaster.AdvancedBroadcaster.onEnable(AdvancedBroadcast
    er.java:10)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.loadPlugin(CraftServer.java
    :282)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.enablePlugins(CraftServer.j
    ava:264)
            at net.minecraft.server.v1_4_6.MinecraftServer.j(MinecraftServer.java:32
    1)
            at net.minecraft.server.v1_4_6.MinecraftServer.e(MinecraftServer.java:30
    0)
            at net.minecraft.server.v1_4_6.MinecraftServer.a(MinecraftServer.java:25
    9)
            at net.minecraft.server.v1_4_6.DedicatedServer.init(DedicatedServer.java
    :149)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:
    399)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:84
    9)
    Main class:
    Code:
    package net.thepvp.broadcaster;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class AdvancedBroadcaster extends JavaPlugin {
        
        @Override
        public void onEnable() {
            this.saveDefaultConfig();
            getCommand("broadcast").setExecutor(new Commands(this));
            getCommand("bc").setExecutor(new Commands(this));
        }
        
        @Override
        public void onDisable() {
            
        }
    }
    
    Commands.java:
    Code:
    package net.thepvp.broadcaster;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class Commands implements CommandExecutor {
        
        // 50 characters long
        
        AdvancedBroadcaster plugin;
        private String notificationPrefix = plugin.getConfig().getString("notificationPrefix");
        private String version = Integer.toString(plugin.getConfig().getInt("pluginVersion"));
        @SuppressWarnings("unused")
        private String savedMessage;
        private String configVariable;
        private String configValue;
        
        public Commands(AdvancedBroadcaster plugin) {
            this.plugin = plugin;
        }
        
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(plugin.getConfig().getBoolean("enableBroadcaster") == false) {
                sender.sendMessage("Unknown command. Type \"help\" for help.");
            } else {
                if(args.length == 0) {
                    if(cmd.getName().equalsIgnoreCase("broadcast")) {
                        sender.sendMessage(ChatColor.GOLD + notificationPrefix + ChatColor.GREEN + " AdvancedBroadcaster " + version + ChatColor.BLUE + ", created by Enotho");
                        return true;
                    }
                } else {
                    if(args[0].equals("edit")) {
                        if(plugin.getConfig().getBoolean("consoleEditingOnly") == true) {
                            sender.sendMessage(ChatColor.GOLD + notificationPrefix + ChatColor.BLUE + "You can only edit saved messages from the console!");
                        } else {
                            sender.sendMessage(ChatColor.BLUE + "-- SAVED MESSAGES --------------------------------");
                            //for(ConfigurationSection configurationSection : plugin.getConfig().getConfigurationSection("messages")) {
                            //    savedMessage = configurationSection.getString("message").substring(0, 42);
                            //    sender.sendMessage(ChatColor.GREEN + configurationSection.getName() + ". " + ChatColor.WHITE + "\"" + savedMessage + "\"");
                            //}
                        }
                        return true;
                    } else if(args[0].equals("config")) {
                        if(args[1].equals("get")) {
                            configVariable = args[2];
                            try {
                                configValue = plugin.getConfig().get(configVariable).toString();
                                sender.sendMessage(ChatColor.GOLD + notificationPrefix + " CONFIG: " + ChatColor.BLUE + "The value of \'" + configValue + "\' is " + ChatColor.GOLD + configValue);
                            } catch(Exception e) {
                                sender.sendMessage(ChatColor.GOLD + notificationPrefix + ChatColor.BLUE + " Unknown value \'" + configValue + "\'");
                            }
                        }
                        return true;
                    }
                }
                return true;
            }
            sender.sendMessage("test");
            return false;
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  17. Mattz Don't use plugin before you initialized it
    Code:java
    1. public class Commands implements CommandExecutor {
    2.  
    3. // 50 characters long
    4.  
    5. AdvancedBroadcaster plugin;
    6. private String notificationPrefix;
    7. private String version;
    8. private String savedMessage;
    9. private String configVariable;
    10. private String configValue;
    11.  
    12. public Commands(AdvancedBroadcaster plugin) {
    13. this.plugin = plugin;
    14. notificationPrefix = plugin.getConfig().getString("notificationPrefix");
    15. version = Integer.toString(plugin.getConfig().getInt("pluginVersion"));
    16. }
     
  18. Offline

    Mattz

    This didn't change the error...
     
  19. should change the error
     
  20. Offline

    Mattz

    I had a typo, it fixed the other error, but now I'm getting the same one as earlier:
    Code:
    2013-01-16 19:10:16 [SEVERE] Could not load 'plugins\AdvancedBroadcaster.jar' in
    folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ClassNotFoundException: net.
    thepvp.broadcaster.AdvancedBroadcaster
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:184)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.loadPlugins(CraftServer.jav
    a:239)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.reload(CraftServer.java:591
    )
            at org.bukkit.Bukkit.reload(Bukkit.java:184)
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    6)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer
    .java:514)
            at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConn
    ection.java:979)
            at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.ja
    va:897)
            at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:
    852)
            at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
     
            at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:
    112)
            at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30
    )
            at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:59
    8)
            at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:22
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:49
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:
    427)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:84
    9)
    Caused by: java.lang.ClassNotFoundException: net.thepvp.broadcaster.AdvancedBroa
    dcaster
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader
    .java:80)
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:53)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:173)
            ... 21 more
     
  21. apparently there is a problem with 'main' in plugin.yml. It cannot find the main class ;3
     
  22. Have you put the wrong path?
    Check your plugin.yml
    and it will probably look like this
    main: et.thepvp.broadcaster
     
  23. Offline

    Sagacious_Zed Bukkit Docs

    the value of main must be the fully qualified name of the class which you extend JavaPlugin.
     
  24. Offline

    Mattz

    fireblast709 I can see that, but I have no idea what causes the problem.

    Miner_Fil Looks fine to me (maybe the computer is playing tricks on me!)
    [​IMG]

    Sagacious_Zed What do you mean by that?
     
  25. Mattz, You need to remove @Override annotations maybe thats why it dosent work

    i made a few changes which will make it work
    Code:
    package net.thepvp.broadcaster;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class AdvancedBroadcaster extends JavaPlugin {
     
    public static AdvancedBroadcaster plugin;
     
        public void onEnable() {
            this.saveDefaultConfig();
            getCommand("broadcast").setExecutor(new Commands(this));
            getCommand("bc").setExecutor(new Commands(this));
        }
     
        public void onDisable() {
         
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  26. nope, wouldn't cause the problem
     
    Miner_Fil likes this.
  27. well he has to add the public static AdvancedBroadcaster plugin;
     
  28. Offline

    Mattz

    It doesn't fix it. And what's the point of declaring something I don't even use?
     
Thread Status:
Not open for further replies.

Share This Page