Last active
June 17, 2021 09:01
-
-
Save 6dc/9643750 to your computer and use it in GitHub Desktop.
DcAPI - This API makes registering commands & Listeners simple: Download the jar here: http://6dc.eu/DcAPI/DcAPI.jar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package eu.dc.lib; | |
| import java.util.Arrays; | |
| import org.bukkit.entity.Player; | |
| public abstract class Command { | |
| protected org.bukkit.command.Command cmd; | |
| protected String[] args; | |
| protected Player p; | |
| protected Command(org.bukkit.command.Command cmd, String[] args, Player p) { | |
| setCmd(cmd); | |
| setArgs(args); | |
| setP(p); | |
| } | |
| protected Command(){} | |
| protected abstract boolean execute(); | |
| protected org.bukkit.command.Command getCmd() { | |
| return cmd; | |
| } | |
| protected String[] getArgs() { | |
| return args; | |
| } | |
| protected Player getP() { | |
| return p; | |
| } | |
| protected void setCmd(org.bukkit.command.Command cmd) { | |
| this.cmd = cmd; | |
| } | |
| protected void setArgs(String[] args) { | |
| this.args = args; | |
| } | |
| protected void setP(Player p) { | |
| this.p = p; | |
| } | |
| protected void debugCommand(){ | |
| System.out.println("�c"+this.toString()); | |
| } | |
| protected void sendSenderMessage(String msg){ | |
| if(p!=null){ | |
| if(p.isOnline()) | |
| { | |
| p.sendMessage(msg); | |
| } | |
| } | |
| } | |
| @Override | |
| public String toString() { | |
| return "Command [cmd=" + cmd + ", args=" + Arrays.toString(args) | |
| + ", p=" + p + "]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package eu.dc.lib; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.Collections; | |
| import java.util.Enumeration; | |
| import java.util.HashMap; | |
| import org.bukkit.entity.Player; | |
| import org.bukkit.event.Listener; | |
| import org.bukkit.plugin.Plugin; | |
| import org.bukkit.plugin.PluginManager; | |
| import java.lang.reflect.Constructor; | |
| import java.lang.reflect.Field; | |
| import java.lang.reflect.InvocationTargetException; | |
| import java.net.URLDecoder; | |
| import java.util.List; | |
| import java.util.zip.ZipEntry; | |
| import java.util.zip.ZipFile; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.command.Command; | |
| import org.bukkit.command.CommandMap; | |
| import org.bukkit.command.CommandSender; | |
| import org.bukkit.command.PluginCommand; | |
| import org.bukkit.plugin.SimplePluginManager; | |
| public class DcAPI { | |
| Plugin plugin; | |
| HashMap<String,Object> commandMap = new HashMap<String,Object>(); | |
| HashMap<String,String[]> permissionMap = new HashMap<String,String[]>(); | |
| public DcAPI(Plugin plugin){ | |
| this.plugin=plugin; | |
| } | |
| public void registerEvents()throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException{ | |
| PluginManager pluginManager = plugin.getServer().getPluginManager(); | |
| String path = DcAPI.class.getProtectionDomain().getCodeSource().getLocation().getPath(); | |
| String decodedPath = URLDecoder.decode(path, "UTF-8"); | |
| for(Class<?> classInfo: getClassesFromJar(new File(decodedPath))) | |
| { | |
| for(int i = 0; i<classInfo.getInterfaces().length; i++) | |
| { | |
| if(classInfo.getInterfaces()[i].getName().equalsIgnoreCase("org.bukkit.event.Listener")) | |
| { | |
| pluginManager.registerEvents((Listener) Class.forName(classInfo.getName()).newInstance(), plugin); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| public void registerCommands() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException{ | |
| String path = DcAPI.class.getProtectionDomain().getCodeSource().getLocation().getPath(); | |
| String decodedPath = URLDecoder.decode(path, "UTF-8"); | |
| for(Class<?> classInfo: getClassesFromJar(new File(decodedPath))) | |
| { | |
| if(Class.forName(classInfo.getName()).isAnnotationPresent(RegisterCommand.class)) | |
| { | |
| RegisterCommand schinken = (RegisterCommand) Class.forName(classInfo.getName()).getAnnotation(RegisterCommand.class); | |
| commandMap.put(schinken.cmdname().toLowerCase(), Class.forName(classInfo.getName()).newInstance()); | |
| for(int i = 0; i<schinken.aliases().length; i++) | |
| { | |
| commandMap.put(schinken.aliases()[i].toLowerCase(), Class.forName(classInfo.getName()).newInstance()); | |
| RuntimeCommands.registerNewRandomCmd(this.plugin, schinken.aliases()[i].toLowerCase(), Arrays.asList(schinken.aliases()), schinken.usage(), schinken.description(), schinken.permission(), schinken.permission_message()); | |
| permissionMap.put(schinken.aliases()[i].toLowerCase(), new String[]{schinken.permission(), schinken.permission_message()}); | |
| } | |
| RuntimeCommands.registerNewRandomCmd(this.plugin, schinken.cmdname(), Arrays.asList(schinken.aliases()), schinken.usage(), schinken.description(), schinken.permission(), schinken.permission_message()); | |
| if(schinken.permission()!="none"){ | |
| permissionMap.put(schinken.cmdname().toLowerCase(), new String[]{schinken.permission(), schinken.permission_message()}); | |
| } | |
| } | |
| } | |
| } | |
| public boolean onCommand(CommandSender sender, Command cmd, | |
| String commandLabel, String[] args) { | |
| if(!(sender instanceof Player)) | |
| { | |
| return true; | |
| } | |
| Player p = (Player) sender; | |
| if(commandMap.get(cmd.getName().toLowerCase()) != null) | |
| { | |
| if(permissionMap.get(cmd.getName().toLowerCase()) != null) | |
| { | |
| if(!p.hasPermission(permissionMap.get(cmd.getName().toLowerCase())[0])) | |
| { | |
| p.sendMessage(permissionMap.get(cmd.getName().toLowerCase())[1]); | |
| return true; | |
| } | |
| } | |
| try { | |
| java.lang.reflect.Method method = commandMap.get(cmd.getName().toLowerCase()).getClass().getDeclaredConstructor(org.bukkit.command.Command.class, String[].class, Player.class).newInstance(cmd, args,p).getClass().getMethod("execute"); | |
| method.invoke(commandMap.get(cmd.getName().toLowerCase()).getClass().getDeclaredConstructor(org.bukkit.command.Command.class, String[].class, Player.class).newInstance(cmd, args,p), null); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| return true; | |
| } | |
| @SuppressWarnings("resource") | |
| public static List<Class<?>> getClassesFromJar( File file) | |
| { | |
| Class<?> classSearched = null; | |
| String packageName = ""; | |
| List<Class<?>> classes = new ArrayList<Class<?>>(); | |
| String dirSearched = packageName.replace( ".", "/" ); | |
| ZipFile zipFile = null; | |
| try { | |
| zipFile = new ZipFile( file ); | |
| } catch( Exception ex ) { | |
| return classes; | |
| } | |
| for( Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); zipEntries.hasMoreElements(); ) | |
| { | |
| String entryName = zipEntries.nextElement().getName(); | |
| if( !entryName.startsWith( dirSearched ) || | |
| !entryName.toLowerCase().endsWith( ".class" ) ) | |
| continue; | |
| entryName = entryName.substring( 0, entryName.length() - ".class".length() ); | |
| entryName = entryName.replace( "/","." ) ; | |
| try { | |
| Class<?> clazz = Class.forName( entryName ); | |
| if( classSearched == null || classSearched.isAssignableFrom( clazz ) ) | |
| classes.add( clazz ); | |
| } catch( Throwable ex ) { | |
| } | |
| } | |
| return Collections.unmodifiableList( classes ); | |
| } | |
| } | |
| class RuntimeCommands { | |
| public static void registerNewRandomCmd(Plugin plugin, String name, | |
| List<String> aliases, String usage, String description, String permission, String permissionMessage) { | |
| PluginCommand cmd = null; | |
| try { | |
| Constructor<PluginCommand> constr = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); | |
| constr.setAccessible(true); | |
| cmd = constr.newInstance(name, plugin); | |
| getCommandMap().register(plugin.getDescription().getName(), cmd); | |
| if (description != null) { | |
| cmd.setDescription(description.toString()); | |
| } | |
| if (usage != null) { | |
| cmd.setUsage(usage.toString()); | |
| } | |
| if (aliases != null) { | |
| cmd.setAliases(aliases); | |
| } | |
| if (permission != null) { | |
| cmd.setPermission(permission); | |
| } | |
| if (permissionMessage != null) { | |
| cmd.setPermissionMessage(permissionMessage); | |
| } | |
| } catch (SecurityException e) { | |
| e.printStackTrace(); | |
| } catch (IllegalArgumentException e) { | |
| e.printStackTrace(); | |
| } catch (IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } catch (InstantiationException e) { | |
| e.printStackTrace(); | |
| } catch (InvocationTargetException e) { | |
| e.printStackTrace(); | |
| } catch (NoSuchMethodException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static CommandMap getCommandMap() { | |
| CommandMap commandMap = null; | |
| try { | |
| if (Bukkit.getPluginManager() instanceof SimplePluginManager) { | |
| Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); | |
| f.setAccessible(true); | |
| commandMap = (CommandMap) f.get(Bukkit.getPluginManager()); | |
| } | |
| } catch (NoSuchFieldException e) { | |
| e.printStackTrace(); | |
| } catch (SecurityException e) { | |
| e.printStackTrace(); | |
| } catch (IllegalArgumentException e) { | |
| e.printStackTrace(); | |
| } catch (IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } | |
| return commandMap; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package eu.dc.lib; | |
| import java.lang.annotation.Target; | |
| import java.lang.annotation.ElementType; | |
| import java.lang.annotation.Retention; | |
| import java.lang.annotation.RetentionPolicy; | |
| @Target(ElementType.TYPE) | |
| @Retention(RetentionPolicy.RUNTIME) | |
| public @interface RegisterCommand { | |
| public String cmdname(); | |
| public String[] aliases() default {}; | |
| public String description() default "/"; | |
| public String permission() default "none"; | |
| public String usage() default "No Usage defined"; | |
| public String permission_message() default "§4You don't have the permission to perform this command!"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment