Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.bukkit.ChatColor;
- import org.bukkit.Server;
- import org.bukkit.command.CommandSender;
- import org.bukkit.command.SimpleCommandMap;
- import org.bukkit.command.defaults.BukkitCommand;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Type;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class CmdWrapper {
- private final JavaPlugin plugin;
- public CmdWrapper(JavaPlugin plugin) {
- this.plugin = plugin;
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface SCmd {
- String name();
- String description() default "";
- String usage() default "";
- String[] aliases() default {};
- String[] require() default {};
- String noPermMsg() default noPerm;
- boolean playerOnly() default false;
- String playerOnlyMsg() default playerOnly;
- boolean consoleOnly() default false;
- String consoleOnlyMsg() default consoleOnly;
- String noPerm = "&cYou don't have permission to use this command.";
- String playerOnly = "The command is for players only.";
- String consoleOnly = "The command can only be run from console.";
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface Cmd {
- String value();
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface Description {
- String value();
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface Usage {
- String value();
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface Aliases {
- String[] value();
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface Require {
- String[] value();
- String msg() default SCmd.noPerm;
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface PlayerOnly {
- String msg() default SCmd.playerOnly;
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface ConsoleOnly {
- String msg() default SCmd.consoleOnly;
- }
- private static class CmdInfo extends BukkitCommand {
- private Method method;
- private Object object;
- CmdInfo(String name, Object object, Method method) {
- super(name, getDescription(method), getUsage(method), getAliases(method));
- this.method = method;
- this.object = object;
- }
- @Override
- public boolean execute(CommandSender sender, String label, String[] args) {
- if (!method.isAccessible()) method.setAccessible(true);
- try {
- if (sender instanceof Player){
- if (isConsoleOnly(method)) {
- String msg = consoleOnlyMsg(method);
- if (!msg.isEmpty()) sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
- return true;
- }
- Player player = (Player) sender;
- for (String perm : getRequired(method)) {
- if (player.isOp() || player.hasPermission(perm)) continue;
- String msg = getMsg(method);
- if (!msg.isEmpty()) player.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
- return true;
- }
- }
- else {
- if (isPlayerOnly(method)) {
- String msg = playerOnlyMsg(method);
- if (!msg.isEmpty()) sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
- return true;
- }
- }
- return (boolean) method.invoke(object, sender, args);
- } catch (IllegalAccessException | InvocationTargetException e) {
- e.printStackTrace();
- }
- return true;
- }
- static String getCommand(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return scmd.name();
- return method.getAnnotation(Cmd.class).value();
- }
- static String getDescription(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return scmd.description();
- Description desc = method.getAnnotation(Description.class);
- return (desc == null ? "" : desc.value());
- }
- static String getUsage(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return scmd.usage();
- Usage usage = method.getAnnotation(Usage.class);
- return (usage == null ? "" : usage.value());
- }
- static List<String> getAliases(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return Arrays.asList(scmd.aliases());
- Aliases aliases = method.getAnnotation(Aliases.class);
- return (aliases == null ? new ArrayList<>() : Arrays.asList(aliases.value()));
- }
- static List<String> getRequired(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return Arrays.asList(scmd.require());
- Require require = method.getAnnotation(Require.class);
- return (require == null ? new ArrayList<>() : Arrays.asList(require.value()));
- }
- static String getMsg(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return scmd.noPermMsg();
- Require require = method.getAnnotation(Require.class);
- return (require == null ? SCmd.noPerm : require.msg());
- }
- static boolean isPlayerOnly(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return scmd.playerOnly();
- PlayerOnly playerOnly = method.getAnnotation(PlayerOnly.class);
- return (playerOnly != null);
- }
- static String playerOnlyMsg(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return scmd.playerOnlyMsg();
- PlayerOnly playerOnly = method.getAnnotation(PlayerOnly.class);
- return (playerOnly == null ? SCmd.playerOnly : playerOnly.msg());
- }
- static boolean isConsoleOnly(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return scmd.consoleOnly();
- ConsoleOnly consoleOnly = method.getAnnotation(ConsoleOnly.class);
- return (consoleOnly != null);
- }
- static String consoleOnlyMsg(Method method) {
- SCmd scmd = method.getAnnotation(SCmd.class);
- if (scmd != null) return scmd.consoleOnlyMsg();
- ConsoleOnly consoleOnly = method.getAnnotation(ConsoleOnly.class);
- return (consoleOnly == null ? SCmd.consoleOnly : consoleOnly.msg());
- }
- }
- public static void register(JavaPlugin plugin, Object object) {
- SimpleCommandMap map = getCommandMap(plugin.getServer());
- if (map == null) return;
- Arrays.stream(object.getClass().getDeclaredMethods()).filter(CmdWrapper::hasAnnotation).filter(CmdWrapper::correctSignature).forEach(method -> {
- String name = CmdInfo.getCommand(method);
- map.register(name, new CmdInfo(name, object, method));
- });
- }
- public void addCommands(Object object) {
- register(plugin, object);
- }
- private static SimpleCommandMap getCommandMap(Server server) {
- String version;
- try {
- version = server.getClass().getPackage().getName().split("\\.")[3];
- } catch (ArrayIndexOutOfBoundsException e) {
- return null;
- }
- if (version == null) return null;
- try {
- Class<?> clazz = Class.forName("org.bukkit.craftbukkit." + version + ".CraftServer");
- Method method = clazz.getDeclaredMethod("getCommandMap");
- return (SimpleCommandMap) method.invoke(clazz.cast(server));
- } catch (ClassNotFoundException e) {
- return null;
- } catch (NoSuchMethodException e) {
- return null;
- } catch (InvocationTargetException | IllegalAccessException e) {
- return null;
- }
- }
- private static boolean hasAnnotation(Method method) {
- return (method.isAnnotationPresent(SCmd.class) || method.isAnnotationPresent(Cmd.class));
- }
- private static boolean correctSignature(Method method) {
- // boolean method(CommandSender, String[])
- Type[] types = method.getGenericParameterTypes();
- if (types.length != 2) return false;
- if (!CommandSender.class.getCanonicalName().equalsIgnoreCase(types[0].getTypeName())) return false;
- if (!String[].class.getCanonicalName().equalsIgnoreCase(types[1].getTypeName())) return false;
- if (!boolean.class.getCanonicalName().equalsIgnoreCase(method.getGenericReturnType().getTypeName())) return false;
- return true;
- }
- }
Add Comment
Please, Sign In to add comment