Skip to content

Instantly share code, notes, and snippets.

@markhughes
Created July 10, 2016 03:42
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 markhughes/cc32b18792c6557032ccac6711237dc7 to your computer and use it in GitHub Desktop.
Save markhughes/cc32b18792c6557032ccac6711237dc7 to your computer and use it in GitHub Desktop.
FactionsFramework command example
/**
* This adds a command `/f example` with a required argument
*/
public class CmdExample extends FactionsCommand {
// This is a simple Singleton method, however you can store this instance
// anywhere. This just allows getting the instance with CmdExample.get()
// you could for example have a CommandManager class, and store CmdExample in
// there.
private static CmdExample i = new CmdExample();
public static CmdExample get() { return i; }
// Use the constructor to define options and settings for the command
public CmdExample() {
// You don't have to pass the second argument. But you can pass as many arguments
// as you want to add more aliases. This adds `/f example` but also adds `/f eg` as
// an additional alias
this.addAlias("example", "eg");
// This is the description - it will show in `/f help`
this.setDescription("example command!");
// Optional - but you can add a permission here. If you want to you can
// add your own way of managing permissions
this.setPermission("your.permission");
// Optional - adds a required argument, add these in order!
// you can also add optional arguments. Optional arguments are
// always at the end of the command, but they are still in the
// order that you add them
this.addRequiredArgument("name");
// Optional - requirements allow you to use preset common requirements
// you can find more here:
// https://github.com/MarkehMe/FactionsFramework/tree/master/framework/src/main/java/me/markeh/factionsframework/command/requirements
this.addRequirement(ReqInFaction.get(this)); // all requirements require passing the commands class
this.addRequirement(ReqRankAtLeast.get(this, Config.get().minimumManage)); // some require passing additional arguments
// Optional - allow overflows, so they can pass as many arguments as they want
// this.allowOverflow(true);
}
public void run() throws Exception {
String name = this.getArg(0); // grab arguments in index order!
// send messages with msg, you can add colours using tags like <red> or <gold>
msg("<gold>This text is gold!<red> This text is red!");
// you should pass variables using this method, it not only stops players injecting colours
// but allows you to easily add in variables without a messy string
// GOOD:
msg("<red>hello {someone}!",
"someone", name);
// BAD:
msg("<red>hello " + name + "!");
// when this class is called you can access several other methods
FPlayer fPlayer = this.getFPlayer(); // get a faction player object
// you can also use:
// CommandSender sender = this.getSender()
// String universe = this.getUniverse() // returns default for non-universe installations
// Boolean isPlayer = this.isPlayer()
// List<String> args = this.getArgs()
// String arg = this.getArg(0)
// this.getArgAs(0, Faction.class, null); // get an argument as a faction, null is the default value
// this.getArgAs(0, FPlayer.class, null); // get an argument as an FPlayer, null is the default value
// this.getArgsConcated(2); // get argumets concated from the 2nd index (3rd passed variable)
}
}
// for more information see this class:
// https://github.com/MarkehMe/FactionsFramework/blob/master/framework/src/main/java/me/markeh/factionsframework/command/FactionsCommand.java
public class PluginExample extends JavaPlugin {
@Override
public void onEnable() {
// You can register the command here
FactionsCommandManager.get().add(CmdExample.get());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment