Tutorial [Tutorial] Creating Configurable Commands (without plugin.yml)

Discussion in 'Resources' started by AoH_Ruthless, Mar 8, 2014.

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

    AoH_Ruthless

    For my server, I was trying to create a plugin in which I can configure commands from a config, without having to register each one in the plugin.yml. After about 30 minutes of googling and thinking, I have put together the following tutorial.

    This has many practical uses. You can create your own plugin to send players messages with ease and without having to create a new plugin or hard code each one. This tutorial will show you how to store the command in the file and then get it's string value to send the command sender a message. I am not teaching you how to use any other application than messages but it should be the same setup.

    Whilst many people recommend using reflection (just an observation during my googling), I found that reflection is completely unnecessary.

    So, let's get started.


    1) Create the class you need. We will be utilizing the PlayerCommandPreprocessEvent, so if you want a separate class for your listener, go for it. I am just going to be using the class that extends JavaPlugin.

    Code:
    public class Links extends JavaPlugin implements Listener
    {
        public void onEnable() {
          getServer().getPluginManager().registerEvents(this, this);
        }
    }
    2) Setup the Config file. For this tutorial, I will be utilizing the Bukkit Configuration API. Specifically, we are using Configuration Sections.

    Code:
    public class Links extends JavaPlugin implements Listener
    {
        public void onEnable() {
          loadConfigFile();
     
          getServer().getPluginManager().registerEvents(this, this);
        }
     
        private void loadConfigFile() {
            // Load the config file.
            saveDefaultConfig();
     
            // If the Configuration Section doesn't exist, create it and save the config.
            if (getConfig().getConfigurationSection("commands") == null) {
                getConfig().createSection("commands");
                // I am going to be adding some example commands.
                getConfig().set("commands.example", "&4This is an example command");
                saveConfig();
            }
        }
    }
    3) Now we need to setup our Event. We need to do this in such a way that we don't get the "Unknown command" message with our commands but we also need to allow commands from other plugins.

    Code:
        @EventHandler
        public void onPreprocess(PlayerCommandPreprocessEvent e) {
            ConfigurationSection s = getConfig().getConfigurationSection("commands");
           
                    // Loop through all the commands.
            for (String command : s.getKeys(false)) {
                            // If the command equals the message. We need to specify "/" because that isn't in our config.
                if (e.getMessage().equalsIgnoreCase("/" + command)) {
                    e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("commands." + command)));
                                    // Cancelling the event avoids the "Unknown Command" message.
                    e.setCancelled(true);
                }
            }
           
                    // If the section doesn't contain the message sent, we don't want to cancel. Otherwise, the command won't go through.
            if (!s.getKeys(false).contains(e.getMessage().replace("/", "")))
                e.setCancelled(false);
        }

    Now, to put it all together.

    Code:java
    1. public class Links extends JavaPlugin implements Listener
    2. {
    3. public void onEnable() {
    4. loadConfigFile();
    5.  
    6. getServer().getPluginManager().registerEvents(this, this);
    7. }
    8.  
    9. private void loadConfigFile() {
    10. // Load the config file.
    11. saveDefaultConfig();
    12.  
    13. // If the Configuration Section doesn't exist, create it and save the config.
    14. if (getConfig().getConfigurationSection("commands") == null) {
    15. getConfig().createSection("commands");
    16. // I am going to be adding some example commands.
    17. getConfig().set("commands.example", "&4This is an example command");
    18. saveConfig();
    19. }
    20. }
    21.  
    22. @EventHandler
    23. public void onPreprocess(PlayerCommandPreprocessEvent e) {
    24. ConfigurationSection s = getConfig().getConfigurationSection("commands");
    25.  
    26. // Loop through all the commands.
    27. for (String command : s.getKeys(false)) {
    28. // If the command equals the message. We need to specify "/" because that isn't in our config.
    29. if (e.getMessage().equalsIgnoreCase("/" + command)) {
    30. e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("commands." + command)));
    31. // Cancelling the event avoids the "Unknown Command" message.
    32. e.setCancelled(true);
    33. }
    34. }
    35.  
    36. // If the section doesn't contain the message sent, we don't want to cancel. Otherwise, the command won't go through.
    37. if (!s.getKeys(false).contains(e.getMessage().replace("/", "")))
    38. e.setCancelled(false);
    39. }
    40. }


    I hope this thread was useful! If you have found any errors please notify me! This was tested in-game and should work fine.

    Reserved.

    Note: As I forgot to put it in the first post, the config.yml is embedded in the code and must be present in the project tree!

    Since it is a configuration section, you can have unlimited commands! The config.yml should look something like this:

    Code:
    commands:
      example: '&4This is an example command.'
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page