[HELP] How can I create sub commands in different classes?

Status
This thread has been locked.

TheCrystalStar

Banned
Feedback score
2
Posts
213
Reactions
65
Resources
0
Hey So I'm recoding this large project of mine, and I want to make it cleaner to read and understand in general.

Currently All my sub commands are in my command class like.

PHP:
@Override
public void onCommand(CommandSender sender, Command cmd, String label, String[] args) {

  if(cmd.getName().equalsIgnoreCase("CommandHere")) {
   
    if(args.length == 0) {
 
      //DO STUFF
    }
 
    if(args.length == 1) {
 
      //DO STUFF
    }

  }

}

It gets messy because of all the sub commands in the class, that class alone has 2100 lines its just messy and a pain to navigate through.

I searched google on how to do this and I keep finding that its something called abstraction. I looked at some of the first page google results after searching how to make sub commands in separate classes. No luck (Don't get it) So I looked at this
https://www.tutorialspoint.com/java/java_abstraction.htm I still don't really understand how to go about making a "Command Handler" thingy so I can have my main command in 1 class then all my sub commands in other classes.

Any Help on how I would do this is much appreciated
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Ark9026

Accepting Plugin Orders!
Premium
Feedback score
6
Posts
64
Reactions
60
Resources
0
Hey So I'm recoding this large project of mine, and I want to make it cleaner to read and understand in general.

Currently All my sub commands are in my command class like.

PHP:
@Override
public void onCommand(CommandSender sender, Command cmd, String label, String[] args) {

  if(cmd.getName().equalsIgnoreCase("CommandHere")) {

    if(args.length == 0) {

      //DO STUFF
    }

    if(args.length == 1) {

      //DO STUFF
    }

  }

}

It gets messy because of all the sub commands in the class, that class alone has 2100 lines its just messy and a pain to navigate through.

I searched google on how to do this and I keep finding that its something called abstraction. I looked at some of the first page google results after searching how to make sub commands in separate classes. No luck (Don't get it) So I looked at this
https://www.tutorialspoint.com/java/java_abstraction.htm I still don't really understand how to go about making a "Command Handler" thingy so I can have my main command in 1 class then all my sub commands in other classes.

Any Help on how I would do this is much appreciated
Well, for each subcommand, you could call a method from another class. For example:
PHP:
public void onCommand(CommandSender sender, Command cmd, String label, String[] args) {

  if(cmd.getName().equalsIgnoreCase("CommandHere")) {

    if(args.length == 0) {
          AnotherClassName.someMethod();
    }
  }
}
Since this is more of a utility method, and not so much "each instance", it's okay to use static. There aren't really instances you need to call, so there's no need for abstraction. You use static by putting static into the method, i.e. "public static void".

Think of it like this: you're making a minigame and you have a class named "Game". For every game instance, you could do Game g = new Game(), and then store that instance of Game. Keep in mind that instead of doing Game.method(), you would do g.method(). You would do this for a game because there may be several games going on at once, and you want different settings for each game. You can take a look at this if you want to learn more about calling "each instance".

But in your case, we're just cleaning up your code, and there isn't "multiple instances" of any class. This means that's it okay to use static.

When deciding if static is okay, take a look at this guide on StackOverflow. Hope that made it clear :)
 
Last edited:
Status
This thread has been locked.
Top