Command Interpreter 0.0.2-SNAPSHOT

Simplifies adding commands to a plugin

  1. Stuperfied
    Tested Minecraft Versions:
    • 1.8
    • 1.9
    • 1.10
    • 1.11
    Source Code:
    https://github.com/Stuperfied/Util
    https://oss.sonatype.org/service/lo...0.2-SNAPSHOT/Util-0.0.2-20170413.115523-1.jar

    SHA1: 9241e8d5fb0f327d95f912c7bcff4c6284163624
    MD5: 5be0dbe487b0657fb9b546aaaf1c4cd6

    Code (Text):

    <repository>
      <id>oss-sonatype</id>
      <name>oss-sonatype</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>

    <dependency>
      <groupId>au.com.msh-craft</groupId>
      <artifactId>Util</artifactId>
      <version>0.0.2-SNAPSHOT</version>
    </dependency>

    Feel free to contribute to this project on gitHub at the link provided as the source.

    Help keep me sitting on the couch and bringing you cool stuff!

    [​IMG]


    What it does:

    Remember this? https://bukkit.org/threads/tutorial-command-arguments.186497/

    If command = this, if command = that.... Blah, blah, blah...
    Ever wished there was a way to avoid it and have it just be automatic?

    Included in my collection of utilities is a command interpreter. This interpreter greatly simplifies the process of adding commands to a plugin. In fact, it does the whole onCommand method for you.

    The plugin takes a map of your commands, their classes and methods allong with any constructor arguments required by your class responsible for executing the command. It will declare and instantiate your executing classes on the fly and hand over control to the method responsible for executing your command.

    Code (Text):
    public CommandInterpreter(Plugin instance, Map<String, Map<String, String>> commandMap, Object[] constructorArgs) {
    This probably looks rather daunting but don't worry, the complimentary example code accompanying this plugin, shown down further on this page is designed to do all that hard work for you. It will generate a file for you on first run called commands.yml, this file will appear inside your plugins root folder. It will also conveniently generate a config.yml for your plugin which you are free to disable.

    After you have entered your commands into the commands.yml file and your own plugin.yml file, restart the server and the complimentary code will build a map of any commands you set. After your map is built, It will then iterate over the commands you listed in your plugin.yml, automatically set the executing class and pass the map of commands to the plugin.

    Using reflection, the plugin will forward any commands listed in the commands.yml and their arguments to the function (method) you want.


    Step 1.
    Download the plugin, include it as an external jar in your project

    Step 2.
    Add the repository oss-sonatype and the plugin as a dependency in your pom.xml
    Code (Text):

    <repositories>
      <repository>
        <id>spigot-repo</id>
        <url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
      </repository>
      <repository>
        <id>oss-sonatype</id>
        <name>oss-sonatype</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        <snapshots>
          <enabled>true</enabled>
        </snapshots>
      </repository>
    </repositories>
    <dependencies>
      <dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.11.2-R0.1-SNAPSHOT</version>
        <type>jar</type>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>au.com.msh-craft</groupId>
        <artifactId>Util</artifactId>
        <version>0.0.2-SNAPSHOT</version>
      </dependency>
    </dependencies>
     

    Step 3. (Optional)
    Add the command interpreter plugin as a dependency in your plugin.yml.


    Code (Text):
    public class Main extends JavaPlugin {

      private Plugin plugin;
      private FileConfiguration config, commands;
      private File configf, commandsf;

      @Override
      public void onEnable() {
        createFiles();
        this.plugin = this;

        Object[] constructorArgs;
        PluginDescriptionFile pluginInfo;
        Map<String, Map<String, Object>> pluginCommands = new HashMap<>();
        Map<String, Map<String, String>> comMap = new HashMap<>();

        pluginInfo = getDescription();
        pluginCommands = pluginInfo.getCommands();

        for (String com : pluginCommands.keySet()) {
          if (commands.isConfigurationSection(com)) {  // added this check to prevent errors being thrown
            comMap = getData(pluginInfo, com, comMap);  // moved and added com as a parameter
            constructorArgs = new Object[] {this.plugin, com};
            getCommand(com).setExecutor(new CommandInterpreter(plugin, comMap, constructorArgs));
          }
        }

        getLogger().info("Example has been enabled");
      }


      private void createFiles() {

        String regex = ".";
        String path;
        Path comPath;


        // Load the configuration file
        String configFile = "config.yml";

        path = getDataFolder().toString();
        comPath = Paths.get(path);
        configf = new File(comPath.toString(), configFile);
        if (!configf.exists()) {
          configf.getParentFile().mkdirs();
          if (getResource(configFile) != null) {
            saveResource(configFile, false);
          } else {
            try {
              configf.createNewFile();
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        }

        config = new YamlConfiguration();
        try {
          config.load(configf);
        } catch (IOException | InvalidConfigurationException e) {
          e.printStackTrace();
        }


        // Commands and methods are dynamic and polymorphic
        // Load the file which maps the commands to methods
        String commandsKey = "commands.commandsFile";
        String commandsFile = "commands";

        if (config.isSet(commandsKey)) {
          commandsFile = config.getString(commandsKey);
        }

        // add extension

        String[] splitC = commandsFile.split(".");
        if (splitC.length > 0) {
          int lengthC = splitC.length-1;
          String lastC = splitC[lengthC];
          if (!lastC.equalsIgnoreCase("yml")) {
            commandsFile += ".yml";
          }
        } else {
          commandsFile += ".yml";
        }

        // load the commands file which maps the methods
        path = getDataFolder().toString();
        comPath = Paths.get(path, commandsFile);
        commandsf = new File(comPath.toString());
        if (!commandsf.exists()) {
          commandsf.getParentFile().mkdirs();
          if (getResource(commandsFile) != null) {
            saveResource(commandsFile, false);
          } else {
            try {
              commandsf.createNewFile();
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        }

         commands = new YamlConfiguration();
        try {
          commands.load(commandsf);
        } catch (IOException | InvalidConfigurationException e) {
          e.printStackTrace();
        }
      }


        // added com as a parameter
        private Map<String, Map<String, String>> getData(PluginDescriptionFile pluginInfo, String com, Map<String, Map<String, String>> comMap) {

        String pluginName = pluginInfo.getName();
        String pluginMain = pluginInfo.getMain();

        int end = pluginMain.length() - pluginName.length();
        String pluginPackage = pluginMain.substring(0, end);

        Map<String, String> dataMap;


        String subComPath;
        String itemPath;
        String itemData;
        String data;
        // for (String com : commands.getKeys(false)) {  // this is now performed by the onEnable method
          for (String subCom : commands.getConfigurationSection(com).getKeys(false)) {
            subComPath = com + "." + subCom;
            System.out.println("SubCommand" + subCom);
            dataMap = new HashMap<>();
            for (String itemKey : commands.getConfigurationSection(subComPath).getKeys(false)) {
              itemPath = subComPath + "." + itemKey;
              itemData = commands.getString(itemPath);

              data = itemKey.equalsIgnoreCase("class") ? pluginPackage + itemData : itemData;
              dataMap.put(itemKey, data);

              System.out.println(itemKey + ": " + dataMap.get(itemKey));

            }
            comMap.put(subCom, dataMap);
          }
        // }
        return comMap;
      }
    }
    As you can see, this code creates 2 files in your plugins directory.
    1. config.yml - for your convenience.
    2. commands.yml - to hold your plugins commands.

    Then all you have to do is enter your plugins commands into the commands.yml file which should now be located in your plugins root.

    team:
    default:
    class: CommandHandler
    method: giveHelp​
    help:
    class: CommandHandler
    method: giveHelp​
    arena:
    help:
    class: ArenaCommandHandler
    method: giveHelp​
    list:
    class: ArenaCommandHandler
    method: list

    Now if a player types "/team help", it will look in your class file called CommandHandler for a method called giveHelp. If they type "/arena list", it will look in your ArenaCommandHandler for a method called list. If they type "/team" by its self, it will use the default method if defined.

    If you like the plugin, please leave a review. If you are experiencing any problems, require help or have something you wish to discuss, please feel free to comment in the discussion section.

Recent Updates

  1. Bug Fixes, Code Refinements