Skip to content

Instantly share code, notes, and snippets.

@ducc
Created February 7, 2016 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducc/07721c53bfa5452278b9 to your computer and use it in GitHub Desktop.
Save ducc/07721c53bfa5452278b9 to your computer and use it in GitHub Desktop.

Nice job, I like your system. Here are some improvements:

#####1) You should use Map instead of HashMap for your maps when you do not need the extended features of a HashMap.

Map<Something, SomethingElse> map = new HashMap<>();

This is known as the Liskov substitution principle.

#####2) Try to make variables final when possible. http://stackoverflow.com/questions/137868/using-final-modifier-whenever-applicable-in-java

#####3) Take a look at using abstract constructors. This will make it much cleaner & harder to make mistakes when adding new commands. See my example below.

There are other small things I could pick up on, but these are the main ones.

public abstract class CommandExecutor {
private final String command, permission; // you can group variables of the same type together like this
private final boolean console, player;
private final int length;
/*
Instantiating the variables inside of the constructor instead of using a setter. If you do not want all of these
to be set (e.g. if some are optional) you can have multiple constructors.
*/
public CommandExecutor(String command, String permission, boolean console, boolean player, int length) {
this.command = command;
this.permission = permission;
this.console = console;
this.player = player;
this.length = length;
}
// you know what this does
public abstract void onCommand(CommandSender sender, Command cmd, String label, String[] args);
public String getCommand() {
return this.command; // using the "this" operater in getters is just a personal preference of mine.
}
public String getPermission() {
return this.permission;
}
public boolean isConsole() {
return this.console;
}
public boolean isPlayer() {
return this.player;
}
public int getLength() {
return this.length;
}
}
public class ExampleCommand extends CommandExecutor {
// A constructor for our example command
public ExampleCommand() {
/*
Here is the "super" operator. What it is doing is injecting values to our super class' constructor,
which is CommandExecutor. Nice and clean.
*/
super("example", "lms.example", true, false, 0);
}
@Override
public void onCommand(CommandSender sender, Command cmd, String label, String[] args) {
sender.sendMessage("Testing, 1 3 3!");
}
}
@juIio
Copy link

juIio commented Feb 8, 2016

Thanks for the suggestions, however for the abstract constructor, I don't like it how it's setup that way (personal preference) IMO it looks a bit unorganized as it's all just one line and such. I like the way I have it setup where I can just put in what I need and I feel like it looks better. I will definitely keep in mind everything else though, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment