Write a automatic command executor

Discussion in 'Plugin Development' started by _Baxxter, Jun 18, 2020.

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

    _Baxxter

    Hello developer,

    im currently writing a core plugin (like essentials) for my server. This plugin include many commands (25-40) and i try to find a clean and easy to expand way to implement the commands in my plugin.

    I have already read and understand the following documentation about CommandExecutor:(https://bukkit.gamepedia.com/Plugin_Tutorial#Using_a_separate_CommandExecutor_class) .

    But i want to avoid 2 thinks in my plugin:

    1. Write massiv if else cases for (25-40) commands
    2. Write every command with his name like:
    this.getCommand("basic").setExecutor(new MyPluginCommandExecutor(this));

    The second way is not easy to maintaine or expand....

    My idea is now this:

    1 Idea: Every command has his own class. All commands are handled by the command executor. The Main Plugin Class initiate the command executor.

    The main class give the command name to the command executor. The command executor calls the correct command class with the given name.

    Benefit:

    Every logic command is seperate in their own class. The command executor only call the correct class. And its easy to expand.

    My Problem:

    Im not sure how i tell the command executor with class is the correct on (assoziation). For this i need help.

    2 Idea: Do it like the first Idea but using the plugin.yml for the assoziation

    I spend many time on google and youtube to find a really good way.

    What did i need now:

    - feedback if idea 1 or 2 are working and how i can solve the given problems
    - feedback if there are better ways to do it like i want
    - good working code examples xD

    Please dont write a comment with dirty or easy to solve ways :), i really know how to solve this issue quick and dirty. But this is not my style :D.

    Disclaimer: I will share my working code later if i find a good solution. Currently im only have a example like the bukkit documentation
     
  2. Offline

    KarimAKL

    You might not always be able to do it in a clean way.

    Anyway, it sounds like you want to make an abstract super class for your command classes.
     
  3. Offline

    _Baxxter

    I will try it in the cleanest way, maybe you have some good ideas for me :)? Can you explaint it a little bit more how do you think its will work with the super class?

    Current solution

    Example:
    - Class ListCommand implements CommandExecutor and run the logic inside their own onCommand method
    - Class CommandHandler has a method initiateCommands and run for every command following execution: jp.getCommand("list").setExecutor(new ListClassCommand(jp));

    - MainClass create a instance of CommandHandler and run the method initiateCommands

    This works fine and every kind of logic is well seperated in his own class. What i dont like is that i now have to create a setExecutor for every command in my plugin.

    Looking for a solution to handle this dynamic

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 18, 2020
  4. Offline

    KarimAKL

  5. Offline

    The_Spaceman

  6. Offline

    DutchJellyV2

  7. Offline

    Machine Maker

    If you really want to go all in, I use this. It pretty much entirely changes the way you setup commands and makes life SUPER easy.
     
    Strahan likes this.
Thread Status:
Not open for further replies.

Share This Page