Skip to content

Instantly share code, notes, and snippets.

@HMP5K
Last active December 16, 2015 23:29
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 HMP5K/5514182 to your computer and use it in GitHub Desktop.
Save HMP5K/5514182 to your computer and use it in GitHub Desktop.
Bukkit Command Library (include-version)
package de.mineorea.lib.bukkit.command;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Simple CommandLibary with Provides Runtime Command Registration
*
* @author MP5K (HMP5K)
* @version 1.0.1 (include-version)
* @category Bukkit CommandLibrary
*/
public abstract class ICommand {
public Boolean onConsoleCommand(ConsoleCommandSender sender , String command , String[] args , List<ISubCommand> subcommands , int lenght){return null;}
public Boolean onPlayerCommand(Player sender , String command , String[] args , List<ISubCommand> subcommands , int lenght){ return null;}
public Boolean onCommand(CommandSender sender ,String Command , String[] args , List<ISubCommand> subcommands , int lenght){ sender.sendMessage("null"); return null;}
private String name;
private String usage;
private String Permission;
private String Description;
private String PermMessage;
private List<String> alias = new ArrayList<String>();
private List<ISubCommand> subcommands = new ArrayList<ISubCommand>();
private PluginCommand instance;
private final ICommand me = this;
private final JavaPlugin plugin;
private static CommandMap map = Reflection.getCommandMap();
public ICommand(JavaPlugin plugin){
this.plugin = plugin;
}
/**
* Returns the Command Map with is used to Register the Commands
* @return The Command map (never null)
*/
public static final CommandMap getCommandMap(){
return map;
}
/**
* <b>! Needs to be called after <code>register()</code> ! </b> <br>
* Changes the Current Command Usaged with is displayed if one of the Command Methods <br>
* <code>onConsoleCommand(...); onPlayerCommand(...); onCommand(...)</code> <br>
* returns <code>false</code>
* @param value the new Usage
* @throws java.lang.NullPointerException if <code>value</code> is null
*/
protected final void setUsage(String value){
if(value == null) throw new NullPointerException();
this.usage = value;
}
/**
* <b>! Needs to be called after <code>register()</code> ! </b> <br>
* Changes the Permission with is required to execute the Command <br>
* Set to "<code>?</code>" or "<code>null</code>" if no permission is required.
* @param value the new Permission (or null if none)
*/
protected final void setPermission(String value){
if(value == null){
this.Permission = "?";
return;
}
this.Permission = value;
}
/**
* <b>! Needs to be called after <code>register()</code> ! </b> <br>
* Changes the Current No Permission Message (<code>null</code> if none)
* @param value the New No Permission Message
*/
protected final void setPermissionsMessage(String value){
this.PermMessage = value;
}
/**
* Returns the current Usage (never null)
* @return the current Usage
*/
public final String getUsage(){
return this.usage;
}
/**
* Returns the current Command Name
* @return the Command Name
*/
public final String getName(){
return this.name;
}
/**
* Returns the current Command Description (never Null)
* @return the Command Description
*/
public final String getDescription(){
return this.Description;
}
/**
* Returns the current Command Permission ("<code>?</code>" if no Permission)
* @return the Command Permission
*/
public final String getPermission(){
return this.Permission;
}
/**
* Returns the current Message with is Displayed if the Player leaks the Permissions to run the Command <br>
* "<code>null</code>" if no Message
* @return the Message (may be null)
*/
public final String getPermissionsMessage(){
return this.PermMessage;
}
/**
* Returns the current Command Aliases
* @return the Aliases as a <code>List< java.lang.String ></code>
*/
public final List<String> getAlias(){
return this.alias;
}
/**
* Internal Method
*/
final void fill(ICommand command){
if(command.getClass().isAnnotationPresent(CommandManager.class) == false) throw new java.lang.RuntimeException("Annotation is not Present!");
CommandManager manager = command.getClass().getAnnotation(CommandManager.class);
this.name = manager.name();
this.Description = manager.description();
this.usage = manager.usage().replace("<command>", name);
this.Permission = manager.permission();
this.PermMessage = manager.noPermMessage().replace("<command>", name);
alias.add(name);
for(String s : manager.aliases())
alias.add(s);
for(ISubCommand sub : this.subcommands){
if(sub.getClass().isAnnotationPresent(CommandManager.class) == false) throw new java.lang.RuntimeException("Annotation is not Present!");
CommandManager smanager = sub.getClass().getAnnotation(CommandManager.class);
sub.name = smanager.name();
sub.Description = smanager.description();
sub.Permission = smanager.permission();
sub.usage = smanager.usage().replace("<command>", name+ " " + sub.name);
sub.PermMessage = smanager.noPermMessage().replace("<command>", name + " " + sub.name);
List<String> temp_a = new ArrayList<String>();
for(String str : smanager.aliases()){
temp_a.add(str);
}
sub.aliases = temp_a;
}
}
/**
* Internal Method
*/
final void create(){
this.instance = Reflection.createCommand(this.plugin, this.name);
instance.setDescription(Description);
instance.setUsage(usage);
instance.setAliases(alias);
instance.setExecutor(new CommandExecutor(){
@Override
public boolean onCommand(CommandSender arg0, Command arg3, String arg1, String[] arg2) {
try{
me.dispatch(arg0, arg2, arg1, me.subcommands);
}catch(Exception e){
e.printStackTrace();return false;
}
return false;
}
});
map.register(this.name, instance);
}
/**
* Unregisters the Command <br>
* Does nothing if the Command is not found / registered
*/
public final void unregister(){
Command command = map.getCommand("ICommand<" + this.name + ">");
if(command == null) return;
command.unregister(map);
}
/**
* Registers the Command (using the CommandMap retrieved by <code> ICommand.getCommandMap()</code>;
*/
public final void register(){
this.fill(this);
this.create();
}
/**
* Registers a Sub Command
* @param sub the Sub Command to Register
* @throws java.lang.NullPointerException if the argument is null
*/
public final void addSubCommand(ISubCommand sub){
if(sub == null) throw new NullPointerException();
this.subcommands.add(sub);
}
/**
* Internal Method
*/
final void dispatch(CommandSender sender , String[] args , String lable, List<ISubCommand> subs){
for(ISubCommand sub : subs){
if(args.length >= 1)
if(args[0].equalsIgnoreCase(sub.name) || sub.aliases.contains(args[0])){
String[] subargs = new String[args.length - 1];
for(int i = 1 ; i < args.length ; i++) {
subargs[i - 1] = args[i];
}
if(sender.hasPermission(sub.Permission) == false & sub.Permission.trim().equalsIgnoreCase("?") == false){
if(sub.PermMessage != null){
sender.sendMessage(ChatColor.translateAlternateColorCodes('&' ,sub.PermMessage));
}
}else{
Boolean runByPlayer = null;
Boolean runByConsole = null;
Boolean runByAll = null;
if(sender instanceof Player){
Player player = (Player) sender;
runByPlayer = sub.onPlayerCommand(player, lable, subargs, this , subargs.length );
}else{
ConsoleCommandSender console = (ConsoleCommandSender) sender;
runByConsole = sub.onConsoleCommand(console, lable, subargs, this, subargs.length);
}
if(runByPlayer == null && runByConsole == null){
runByAll = sub.onCommand(sender, lable, subargs, this, subargs.length);
}
if(runByPlayer != null && runByPlayer == false) sender.sendMessage(ChatColor.translateAlternateColorCodes('&' ,usage));
if(runByConsole != null && runByConsole == false) sender.sendMessage(ChatColor.translateAlternateColorCodes('&' ,usage));
if(runByAll != null && runByAll == false) sender.sendMessage(ChatColor.translateAlternateColorCodes('&' ,usage));
}
return;
}
}
Boolean runByPlayer = null;
Boolean runByConsole = null;
Boolean runByAll = null;
if(sender instanceof Player){
Player player = (Player) sender;
if(sender.hasPermission(Permission) == false & Permission.trim().equalsIgnoreCase("?") == false){
if(PermMessage != null){
sender.sendMessage(ChatColor.translateAlternateColorCodes('&' ,PermMessage));
}
runByPlayer = true;
}else{
runByPlayer = this.onPlayerCommand(player, lable, args, this.subcommands , args.length );
}
}else{
ConsoleCommandSender console = (ConsoleCommandSender) sender;
runByConsole = this.onConsoleCommand(console, lable, args, this.subcommands, args.length);
}
if(runByPlayer == null && runByConsole == null){
runByAll = this.onCommand(sender, lable, args, this.subcommands, args.length);
}
if(runByPlayer != null && runByPlayer == false) sender.sendMessage(ChatColor.translateAlternateColorCodes('&' ,usage));
if(runByConsole != null && runByConsole == false) sender.sendMessage(ChatColor.translateAlternateColorCodes('&' ,usage));
if(runByAll != null && runByAll == false) sender.sendMessage(ChatColor.translateAlternateColorCodes('&' ,usage));
}
/**
* The SubCommand class
*
* @author MP5K (HMP5K)
* @version 1.0.1 (include-version)
* @category Bukkit CommandLibrary SubCommand
*/
public static class ISubCommand {
private String name;
private String usage;
private String Permission;
private String Description;
private String PermMessage;
private List<String> aliases;
/**
* Returns the current Sub Command Name (never null)
* @return the current Sub Command Name
*/
public final String getName(){
return this.name;
}
/**
* Returns the current Sub Command Usage (never null)
* @return the current Sub Command Usage
*/
public final String getUsage(){
return this.usage;
}
/**
* Returns the current Sub Command Description
* @return the current Sub Command Description
*/
public final String getDescription(){
return this.Description;
}
/**
* Returns the current Command Permission ("<code>?</code>" if no Permission)
* @return the Command Permission
*/
public final String getPermissionsMessage(){
return this.Permission;
}
/**
* Returns the current Message with is Displayed if the Player leaks the Permissions to run the Command <br>
* "<code>null</code>" if no Message
* @return the Message (may be null)
*/
public final String getPermission(){
return this.Permission;
}
/**
* Returns the current Command Aliases
* @return the Aliases as a <code>List< java.lang.String ></code>
*/
public final List<String> getAlias(){
return this.aliases;
}
public Boolean onConsoleCommand(ConsoleCommandSender sender , String command , String[] args , ICommand main, int lenght){return null;}
public Boolean onPlayerCommand(Player sender , String command , String[] args , ICommand main , int lenght){return null;}
public Boolean onCommand(CommandSender sender ,String Command , String[] args , ICommand main , int lenght){return null;}
}
/**
* The CommandManager Annotation
*
* @author MP5K (HMP5K)
* @version 1.0.1 (include-version)
* @category Bukkit CommandLibrary SubCommand
*/
@Retention(RetentionPolicy.RUNTIME)
public static @interface CommandManager {
public String name();
public String description();
public String usage() default "/<command> [...]";
public String permission() default "?";
public String noPermMessage() default "&4You don't have Permission to use /<command>";
public String[] aliases() default {};
}
static final class Reflection {
private static CommandMap map;
public static CommandMap getCommandMap(){
if(map == null) load();
return map;
}
public static PluginCommand createCommand(Plugin instance , String name){
PluginCommand com = null;
try {
Constructor<PluginCommand> c = PluginCommand.class.getDeclaredConstructor(String.class , Plugin.class);
c.setAccessible(true);
com = c.newInstance(name , instance);
} catch (Exception e) {
e.printStackTrace();
}
return com;
}
private static void load(){
try{
Field f = Bukkit.getServer().getPluginManager().getClass().getDeclaredField("commandMap");
f.setAccessible(true);
map = (CommandMap) f.get(Bukkit.getServer().getPluginManager());
} catch (Exception e) {
e.printStackTrace(); map = new SimpleCommandMap(Bukkit.getServer());
}
}
}
}
//Test Command
package de.mineorea.lib.commands;
import java.util.List;
import org.bukkit.entity.Player;
import de.mineorea.lib.commands.ICommand.CommandManager;
@CommandManager(description = "Prints <true>", name = "true")
public final class TestCommand extends ICommand {
public TestCommand(){
this.register();
this.setPermission("my.plugin.permission");
this.setPermissionsMessage(null);
}
@Override
public Boolean onPlayerCommand(Player sender, String Command, String[] args, List<ISubCommand> subcommands, int lenght) {
sender.sendMessage("true");
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment