From e6f18e65c4c257d2738dce3e68e38f53e5c7cc8f Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sat, 23 Nov 2019 16:42:44 +0800 Subject: [PATCH 01/17] add the simple plugin class loader --- .../cn/nukkit/plugin/JavaPluginLoader.java | 87 +++++++++++++++++-- .../java/cn/nukkit/plugin/PluginAssert.java | 16 ++++ .../java/cn/nukkit/plugin/PluginLoader.java | 11 +++ .../java/cn/nukkit/plugin/simple/Command.java | 21 +++++ .../java/cn/nukkit/plugin/simple/Main.java | 47 ++++++++++ .../cn/nukkit/plugin/simple/Permission.java | 16 ++++ 6 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 src/main/java/cn/nukkit/plugin/PluginAssert.java create mode 100644 src/main/java/cn/nukkit/plugin/simple/Command.java create mode 100644 src/main/java/cn/nukkit/plugin/simple/Main.java create mode 100644 src/main/java/cn/nukkit/plugin/simple/Permission.java diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index 8c1b7501567..c66e16eebd1 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -3,14 +3,15 @@ import cn.nukkit.Server; import cn.nukkit.event.plugin.PluginDisableEvent; import cn.nukkit.event.plugin.PluginEnableEvent; +import cn.nukkit.plugin.simple.Command; +import cn.nukkit.plugin.simple.Main; import cn.nukkit.utils.PluginException; import cn.nukkit.utils.Utils; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.regex.Pattern; @@ -46,9 +47,7 @@ public Plugin loadPlugin(File file) throws Exception { try { Class javaClass = classLoader.loadClass(className); - if (!PluginBase.class.isAssignableFrom(javaClass)) { - throw new PluginException("Main class `" + description.getMain() + "' does not extend PluginBase"); - } + PluginAssert.isPluginBaseChild(javaClass,description.getMain()); try { Class pluginClass = (Class) javaClass.asSubclass(PluginBase.class); @@ -64,13 +63,89 @@ public Plugin loadPlugin(File file) throws Exception { } } catch (ClassNotFoundException e) { - throw new PluginException("Couldn't load plugin " + description.getName() + ": main class not found"); + PluginAssert.findMainClass(description.getName()); } } return null; } + //TODO + @Override + public Plugin simpleLoadPlugin(File file) { + try { + Class pluginClass = getSimplePlugin(file); + PluginDescription pluginDescription = getSimpleDescription(pluginClass); + this.server.getLogger().info(this.server.getLanguage().translateString("nukkit.plugin.load", pluginDescription.getFullName())); + File dataFolder = new File(file.getParentFile(), pluginDescription.getName()); + PluginBase plugin = (PluginBase) pluginClass.newInstance(); + this.initPlugin(plugin,pluginDescription,dataFolder,file); + return plugin; + }catch (InstantiationException | IllegalAccessException e){ + throw new PluginException(e.getMessage()); + } + //do it + } + + private Class getSimplePlugin(File file){ + try(JarFile jarFile = new JarFile(file)){ + PluginClassLoader classLoader = new PluginClassLoader(this, this.getClass().getClassLoader(),file); + Enumeration entries = jarFile.entries(); + while (entries.hasMoreElements()){ + String name = entries.nextElement().getName(); + if(name.endsWith(".class")) { + String mainName = name.substring(0, name.lastIndexOf(".")).replace("/", "."); + Class clz = classLoader.loadClass(mainName); + Main main = clz.getAnnotation(Main.class); + if(main != null){ + PluginAssert.isPluginBaseChild(clz,mainName); + return clz; + } + } + } + PluginAssert.findMainClass(""); + }catch (IOException|ClassNotFoundException e){ + throw new PluginException(e.getMessage()); + } + return null; + } + + private PluginDescription getSimpleDescription(Class plugin){ + Main main = plugin.getAnnotation(Main.class); + if(main == null){ + throw new PluginException("this is not a main class"); + } + String name = main.name(); + String version = main.version(); + String author = main.author(); + String description = main.description(); + String pluginLoadOrder = main.load(); + String website = main.website(); + String prefix = main.prefix(); + List api = Arrays.asList(main.api()); + List depend = Arrays.asList(main.depend()); + List loadBefore = Arrays.asList(main.loadBefore()); + List softDepend = Arrays.asList(main.softDepend()); + Map hashMap = new HashMap<>(); + hashMap.put("name",name); + hashMap.put("version",version); + hashMap.put("author",author); + hashMap.put("api",api); + hashMap.put("depend",depend); + hashMap.put("loadBefore",loadBefore); + hashMap.put("softDepend",softDepend); + hashMap.put("description",description); + hashMap.put("load",pluginLoadOrder); + hashMap.put("website",website); + hashMap.put("prefix",prefix); + PluginDescription descript = new PluginDescription(hashMap); + Command[] commands = main.commands(); + for(Command command:commands){ + descript.getCommands().put(command.name(),command); + } + return descript; + } + @Override public Plugin loadPlugin(String filename) throws Exception { return this.loadPlugin(new File(filename)); diff --git a/src/main/java/cn/nukkit/plugin/PluginAssert.java b/src/main/java/cn/nukkit/plugin/PluginAssert.java new file mode 100644 index 00000000000..1b83c08476f --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/PluginAssert.java @@ -0,0 +1,16 @@ +package cn.nukkit.plugin; + +import cn.nukkit.utils.PluginException; + +public class PluginAssert { + + static void isPluginBaseChild(Class javaClass,String main){ + if (!PluginBase.class.isAssignableFrom(javaClass)) { + throw new PluginException("Main class `" + main + "' does not extend PluginBase"); + } + } + + static void findMainClass(String name) throws PluginException{ + throw new PluginException("Couldn't load plugin "+name+": main class not found"); + } +} diff --git a/src/main/java/cn/nukkit/plugin/PluginLoader.java b/src/main/java/cn/nukkit/plugin/PluginLoader.java index 4d36c55fbbf..cc67f4b7358 100644 --- a/src/main/java/cn/nukkit/plugin/PluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/PluginLoader.java @@ -58,6 +58,16 @@ public interface PluginLoader { */ Plugin loadPlugin(File file) throws Exception; + /** + * 一个简便开发的插件加载系统,基于原来的插件系统
+ * A simple plugin loading system based on the original plugin system + * @param file 这个插件的文件的 {@code File}对象。
A {@code File} object for this plugin. + * @return 加载完毕的插件的 {@code Plugin}对象。
The loaded plugin as a {@code Plugin} object. + * @see #loadPlugin(File) + * @since Nukkit 1.0 | Nukkit API 1.0.9 + */ + Plugin simpleLoadPlugin(File file); + /** * 通过插件文件名的字符串,来获得描述这个插件的 {@code PluginDescription}对象。
* Gets a {@code PluginDescription} object describes the plugin by its file name. @@ -135,4 +145,5 @@ public interface PluginLoader { */ void disablePlugin(Plugin plugin); + } diff --git a/src/main/java/cn/nukkit/plugin/simple/Command.java b/src/main/java/cn/nukkit/plugin/simple/Command.java new file mode 100644 index 00000000000..cb4bb8419d0 --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/simple/Command.java @@ -0,0 +1,21 @@ +package cn.nukkit.plugin.simple; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Command { + + String name(); + + String usage() default ""; + + String description() default ""; + + Permission permission(); + + String[] aliases() default {}; +} diff --git a/src/main/java/cn/nukkit/plugin/simple/Main.java b/src/main/java/cn/nukkit/plugin/simple/Main.java new file mode 100644 index 00000000000..d095d0a3a7e --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/simple/Main.java @@ -0,0 +1,47 @@ +package cn.nukkit.plugin.simple; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** +@ Main(name="11",api = "1.0.9",commands ={ + @ Command(name="fp", + permission = @ Permission( + theDefault = "op" + ) + ) +}) +*/ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Main { + + + String name(); + + String version() default "0.0.1"; + + String author() default ""; + + String[] api(); + + String[] depend() default {}; + + String[] loadBefore() default {}; + + String [] softDepend() default {}; + + String description() default ""; + + String load() default "POSTWORLD"; + + String website() default ""; + + String prefix() default ""; + + Command[] commands() default {}; + + +} diff --git a/src/main/java/cn/nukkit/plugin/simple/Permission.java b/src/main/java/cn/nukkit/plugin/simple/Permission.java new file mode 100644 index 00000000000..3264113be95 --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/simple/Permission.java @@ -0,0 +1,16 @@ +package cn.nukkit.plugin.simple; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Permission { + + String description() default ""; + + String theDefault(); + +} From 9aaa39d1c5f131ceef205945f05e12f77d91d22f Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sat, 23 Nov 2019 23:12:09 +0800 Subject: [PATCH 02/17] Add the simple plugin class loader [#2 finished] --- .../cn/nukkit/plugin/JavaPluginLoader.java | 18 ++++++++++++- .../java/cn/nukkit/plugin/PluginLoader.java | 2 ++ .../java/cn/nukkit/plugin/PluginManager.java | 25 +++++++++++++++++++ .../java/cn/nukkit/plugin/simple/Command.java | 2 +- .../java/cn/nukkit/plugin/simple/Main.java | 2 ++ .../cn/nukkit/plugin/simple/Permission.java | 2 ++ 6 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index c66e16eebd1..9784f7f3c3d 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -5,6 +5,7 @@ import cn.nukkit.event.plugin.PluginEnableEvent; import cn.nukkit.plugin.simple.Command; import cn.nukkit.plugin.simple.Main; +import cn.nukkit.plugin.simple.Permission; import cn.nukkit.utils.PluginException; import cn.nukkit.utils.Utils; @@ -70,7 +71,7 @@ public Plugin loadPlugin(File file) throws Exception { return null; } - //TODO + //simple load Plugin @Override public Plugin simpleLoadPlugin(File file) { try { @@ -87,6 +88,11 @@ public Plugin simpleLoadPlugin(File file) { //do it } + @Override + public Plugin simpleLoadPlugin(String fileName) { + return simpleLoadPlugin(new File(fileName)); + } + private Class getSimplePlugin(File file){ try(JarFile jarFile = new JarFile(file)){ PluginClassLoader classLoader = new PluginClassLoader(this, this.getClass().getClassLoader(),file); @@ -139,6 +145,16 @@ private PluginDescription getSimpleDescription(Class plugin){ hashMap.put("website",website); hashMap.put("prefix",prefix); PluginDescription descript = new PluginDescription(hashMap); + Permission[] permissions = main.permissions(); + HashMap> pers = new HashMap<>(); + for(Permission p:permissions){ + HashMap pers_child = new HashMap<>(); + pers_child.put("description",p.description()); + pers_child.put("default",p.theDefault()); + pers.put(p.permission(),pers_child); + + } + hashMap.put("permissions",pers); Command[] commands = main.commands(); for(Command command:commands){ descript.getCommands().put(command.name(),command); diff --git a/src/main/java/cn/nukkit/plugin/PluginLoader.java b/src/main/java/cn/nukkit/plugin/PluginLoader.java index cc67f4b7358..44418314f53 100644 --- a/src/main/java/cn/nukkit/plugin/PluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/PluginLoader.java @@ -68,6 +68,8 @@ public interface PluginLoader { */ Plugin simpleLoadPlugin(File file); + Plugin simpleLoadPlugin(String fileName); + /** * 通过插件文件名的字符串,来获得描述这个插件的 {@code PluginDescription}对象。
* Gets a {@code PluginDescription} object describes the plugin by its file name. diff --git a/src/main/java/cn/nukkit/plugin/PluginManager.java b/src/main/java/cn/nukkit/plugin/PluginManager.java index fab4bc5c7a1..29b08cb62ab 100644 --- a/src/main/java/cn/nukkit/plugin/PluginManager.java +++ b/src/main/java/cn/nukkit/plugin/PluginManager.java @@ -6,6 +6,7 @@ import cn.nukkit.event.*; import cn.nukkit.permission.Permissible; import cn.nukkit.permission.Permission; +import cn.nukkit.plugin.simple.Command; import cn.nukkit.utils.MainLogger; import cn.nukkit.utils.PluginException; import cn.nukkit.utils.Utils; @@ -108,6 +109,14 @@ public Plugin loadPlugin(File file, Map loaders) { Server.getInstance().getLogger().critical("Could not load plugin", e); return null; } + }else{ + Plugin plugin = loader.simpleLoadPlugin(file); + this.plugins.put(plugin.getDescription().getName(),plugin); + List pluginCommands = this.parseSimplePluginCommands(plugin); + + if (!pluginCommands.isEmpty()) { + this.commandMap.registerAll(plugin.getDescription().getName(), pluginCommands); + } } } } @@ -116,6 +125,7 @@ public Plugin loadPlugin(File file, Map loaders) { return null; } + public Map loadPlugins(String dictionary) { return this.loadPlugins(new File(dictionary)); } @@ -447,6 +457,21 @@ public void enablePlugin(Plugin plugin) { } } + protected List parseSimplePluginCommands(Plugin plugin){ + List pluginCmds = new ArrayList<>(); + Set> commands = plugin.getDescription().getCommands().entrySet(); + for(Object obj : commands){ + Command command = (Command)obj; + PluginCommand newCmd = new PluginCommand<>(command.name(),plugin); + newCmd.setDescription(command.description()); + newCmd.setUsage(command.usage()); + newCmd.setAliases(command.aliases()); + newCmd.setPermission(command.permission()); + pluginCmds.add(newCmd); + } + return pluginCmds; + } + protected List parseYamlCommands(Plugin plugin) { List pluginCmds = new ArrayList<>(); diff --git a/src/main/java/cn/nukkit/plugin/simple/Command.java b/src/main/java/cn/nukkit/plugin/simple/Command.java index cb4bb8419d0..ce5e74b1ebd 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Command.java +++ b/src/main/java/cn/nukkit/plugin/simple/Command.java @@ -15,7 +15,7 @@ String description() default ""; - Permission permission(); + String permission(); String[] aliases() default {}; } diff --git a/src/main/java/cn/nukkit/plugin/simple/Main.java b/src/main/java/cn/nukkit/plugin/simple/Main.java index d095d0a3a7e..4dfead5430f 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Main.java +++ b/src/main/java/cn/nukkit/plugin/simple/Main.java @@ -43,5 +43,7 @@ Command[] commands() default {}; + Permission[] permissions() default {}; + } diff --git a/src/main/java/cn/nukkit/plugin/simple/Permission.java b/src/main/java/cn/nukkit/plugin/simple/Permission.java index 3264113be95..8e770a7bda6 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Permission.java +++ b/src/main/java/cn/nukkit/plugin/simple/Permission.java @@ -9,6 +9,8 @@ @Target(ElementType.TYPE) public @interface Permission { + String permission(); + String description() default ""; String theDefault(); From dfb4480c742d471a21103a06b7bbbe0b6656fdca Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sun, 24 Nov 2019 07:27:52 +0800 Subject: [PATCH 03/17] Add the simple plugin class loader [#3 bug fixed] --- .../cn/nukkit/plugin/JavaPluginLoader.java | 14 +++++++++++-- src/test/java/cn/nukkit/test/PluginTest.java | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/test/java/cn/nukkit/test/PluginTest.java diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index 9784f7f3c3d..d875502c2a6 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -27,6 +27,7 @@ public class JavaPluginLoader implements PluginLoader { private final Map classes = new HashMap<>(); private final Map classLoaders = new HashMap<>(); + private final Map loadedSimplePlugin = new HashMap<>(); public JavaPluginLoader(Server server) { this.server = server; } @@ -94,6 +95,9 @@ public Plugin simpleLoadPlugin(String fileName) { } private Class getSimplePlugin(File file){ + if(loadedSimplePlugin.containsKey(file)){ + return loadedSimplePlugin.get(file); + } try(JarFile jarFile = new JarFile(file)){ PluginClassLoader classLoader = new PluginClassLoader(this, this.getClass().getClassLoader(),file); Enumeration entries = jarFile.entries(); @@ -104,6 +108,7 @@ private Class getSimplePlugin(File file){ Class clz = classLoader.loadClass(mainName); Main main = clz.getAnnotation(Main.class); if(main != null){ + loadedSimplePlugin.put(file,clz); PluginAssert.isPluginBaseChild(clz,mainName); return clz; } @@ -119,7 +124,7 @@ private Class getSimplePlugin(File file){ private PluginDescription getSimpleDescription(Class plugin){ Main main = plugin.getAnnotation(Main.class); if(main == null){ - throw new PluginException("this is not a main class"); + return null; } String name = main.name(); String version = main.version(); @@ -143,7 +148,8 @@ private PluginDescription getSimpleDescription(Class plugin){ hashMap.put("description",description); hashMap.put("load",pluginLoadOrder); hashMap.put("website",website); - hashMap.put("prefix",prefix); + hashMap.put("prefix",prefix.equals("")?name:prefix); + hashMap.put("main",plugin.getName()); PluginDescription descript = new PluginDescription(hashMap); Permission[] permissions = main.permissions(); HashMap> pers = new HashMap<>(); @@ -174,6 +180,10 @@ public PluginDescription getPluginDescription(File file) { if (entry == null) { entry = jar.getJarEntry("plugin.yml"); if (entry == null) { + Class clz = getSimplePlugin(file); + if(clz != null){ + return getSimpleDescription(clz); + } return null; } } diff --git a/src/test/java/cn/nukkit/test/PluginTest.java b/src/test/java/cn/nukkit/test/PluginTest.java new file mode 100644 index 00000000000..6a51e0aa900 --- /dev/null +++ b/src/test/java/cn/nukkit/test/PluginTest.java @@ -0,0 +1,20 @@ +package cn.nukkit.test; + +import cn.nukkit.plugin.PluginBase; +import cn.nukkit.plugin.simple.Command; +import cn.nukkit.plugin.simple.Main; +import cn.nukkit.plugin.simple.Permission; + +@Main(name="hello",api="1.0.9",commands = { + @Command( + name="hello", + permission = "hello.test" + ) +},permissions = { + @Permission( + permission = "hello.test", + theDefault = "op" + ) +}) +public class PluginTest extends PluginBase { +} From 445feb69c71bab52bae864d3ca0ea2e86be14331 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sun, 24 Nov 2019 07:29:58 +0800 Subject: [PATCH 04/17] Add some comments --- src/main/java/cn/nukkit/plugin/JavaPluginLoader.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index d875502c2a6..f226141a711 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -121,6 +121,11 @@ private Class getSimplePlugin(File file){ return null; } + /** + * the simple description for simple plugin system + * @param plugin the main class + * @return the description for simple plugin system + */ private PluginDescription getSimpleDescription(Class plugin){ Main main = plugin.getAnnotation(Main.class); if(main == null){ From cae8e06286a6e094452692d4c29bb2819a4bc0d2 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sun, 24 Nov 2019 08:17:44 +0800 Subject: [PATCH 05/17] Fix a bug that couldn't register the command --- .../java/cn/nukkit/plugin/JavaPluginLoader.java | 2 +- .../java/cn/nukkit/plugin/PluginDescription.java | 13 +++++++++++++ src/main/java/cn/nukkit/plugin/PluginManager.java | 8 ++++---- src/main/java/cn/nukkit/plugin/simple/Command.java | 3 --- .../java/cn/nukkit/plugin/simple/Permission.java | 3 --- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index f226141a711..630edd85690 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -155,6 +155,7 @@ private PluginDescription getSimpleDescription(Class plugin){ hashMap.put("website",website); hashMap.put("prefix",prefix.equals("")?name:prefix); hashMap.put("main",plugin.getName()); + hashMap.put("isSimple",true); PluginDescription descript = new PluginDescription(hashMap); Permission[] permissions = main.permissions(); HashMap> pers = new HashMap<>(); @@ -163,7 +164,6 @@ private PluginDescription getSimpleDescription(Class plugin){ pers_child.put("description",p.description()); pers_child.put("default",p.theDefault()); pers.put(p.permission(),pers_child); - } hashMap.put("permissions",pers); Command[] commands = main.commands(); diff --git a/src/main/java/cn/nukkit/plugin/PluginDescription.java b/src/main/java/cn/nukkit/plugin/PluginDescription.java index e62bc82c608..00a63e59f9d 100644 --- a/src/main/java/cn/nukkit/plugin/PluginDescription.java +++ b/src/main/java/cn/nukkit/plugin/PluginDescription.java @@ -122,6 +122,7 @@ public class PluginDescription { private String website; private String prefix; private PluginLoadOrder order = PluginLoadOrder.POSTWORLD; + private boolean isSimple; private List permissions = new ArrayList<>(); @@ -204,6 +205,9 @@ private void loadMap(Map plugin) throws PluginException { if (plugin.containsKey("permissions")) { this.permissions = Permission.loadPermissions((Map) plugin.get("permissions")); } + if(plugin.containsKey("isSimple")){ + this.isSimple = (Boolean) plugin.get("isSimple"); + } } /** @@ -408,4 +412,13 @@ public String getVersion() { public String getWebsite() { return website; } + + /** + * the plugin use the simple plugin system,the isSimple will + * be true + * @return is it a Simple Plugin? + */ + public boolean isSimple() { + return isSimple; + } } diff --git a/src/main/java/cn/nukkit/plugin/PluginManager.java b/src/main/java/cn/nukkit/plugin/PluginManager.java index 29b08cb62ab..0081bd30c34 100644 --- a/src/main/java/cn/nukkit/plugin/PluginManager.java +++ b/src/main/java/cn/nukkit/plugin/PluginManager.java @@ -91,7 +91,7 @@ public Plugin loadPlugin(File file, Map loaders) { for (Pattern pattern : loader.getPluginFilters()) { if (pattern.matcher(file.getName()).matches()) { PluginDescription description = loader.getPluginDescription(file); - if (description != null) { + if (description != null&&!description.isSimple()) { try { Plugin plugin = loader.loadPlugin(file); if (plugin != null) { @@ -113,10 +113,10 @@ public Plugin loadPlugin(File file, Map loaders) { Plugin plugin = loader.simpleLoadPlugin(file); this.plugins.put(plugin.getDescription().getName(),plugin); List pluginCommands = this.parseSimplePluginCommands(plugin); - if (!pluginCommands.isEmpty()) { this.commandMap.registerAll(plugin.getDescription().getName(), pluginCommands); } + return plugin; } } } @@ -460,8 +460,8 @@ public void enablePlugin(Plugin plugin) { protected List parseSimplePluginCommands(Plugin plugin){ List pluginCmds = new ArrayList<>(); Set> commands = plugin.getDescription().getCommands().entrySet(); - for(Object obj : commands){ - Command command = (Command)obj; + for(Map.Entry obj : commands){ + Command command = (Command)(obj.getValue()); PluginCommand newCmd = new PluginCommand<>(command.name(),plugin); newCmd.setDescription(command.description()); newCmd.setUsage(command.usage()); diff --git a/src/main/java/cn/nukkit/plugin/simple/Command.java b/src/main/java/cn/nukkit/plugin/simple/Command.java index ce5e74b1ebd..05fed562fea 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Command.java +++ b/src/main/java/cn/nukkit/plugin/simple/Command.java @@ -1,12 +1,9 @@ package cn.nukkit.plugin.simple; -import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) public @interface Command { String name(); diff --git a/src/main/java/cn/nukkit/plugin/simple/Permission.java b/src/main/java/cn/nukkit/plugin/simple/Permission.java index 8e770a7bda6..b83bb25286f 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Permission.java +++ b/src/main/java/cn/nukkit/plugin/simple/Permission.java @@ -1,12 +1,9 @@ package cn.nukkit.plugin.simple; -import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) public @interface Permission { String permission(); From b1cd6313f860f31108e01585b3f90dbb225006c8 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sun, 24 Nov 2019 11:56:41 +0800 Subject: [PATCH 06/17] Fix a bug that could cause NullPointerException --- src/main/java/cn/nukkit/plugin/PluginDescription.java | 3 ++- src/main/java/cn/nukkit/plugin/simple/Command.java | 3 +++ src/main/java/cn/nukkit/plugin/simple/Permission.java | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/cn/nukkit/plugin/PluginDescription.java b/src/main/java/cn/nukkit/plugin/PluginDescription.java index 00a63e59f9d..70d86547170 100644 --- a/src/main/java/cn/nukkit/plugin/PluginDescription.java +++ b/src/main/java/cn/nukkit/plugin/PluginDescription.java @@ -206,7 +206,8 @@ private void loadMap(Map plugin) throws PluginException { this.permissions = Permission.loadPermissions((Map) plugin.get("permissions")); } if(plugin.containsKey("isSimple")){ - this.isSimple = (Boolean) plugin.get("isSimple"); + Boolean obj = (Boolean) plugin.get("isSimple"); + this.isSimple = obj==null?false:obj; } } diff --git a/src/main/java/cn/nukkit/plugin/simple/Command.java b/src/main/java/cn/nukkit/plugin/simple/Command.java index 05fed562fea..71c3a412148 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Command.java +++ b/src/main/java/cn/nukkit/plugin/simple/Command.java @@ -1,9 +1,12 @@ package cn.nukkit.plugin.simple; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE,ElementType.METHOD}) public @interface Command { String name(); diff --git a/src/main/java/cn/nukkit/plugin/simple/Permission.java b/src/main/java/cn/nukkit/plugin/simple/Permission.java index b83bb25286f..8d07b0ad201 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Permission.java +++ b/src/main/java/cn/nukkit/plugin/simple/Permission.java @@ -1,9 +1,12 @@ package cn.nukkit.plugin.simple; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE,ElementType.METHOD}) public @interface Permission { String permission(); From 0fc046145682ce90740a0acf1bd05529c783bfc1 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sun, 24 Nov 2019 13:02:54 +0800 Subject: [PATCH 07/17] Added a new feature to simplify some development --- .../java/cn/nukkit/command/PluginCommand.java | 4 +- .../cn/nukkit/plugin/JavaPluginLoader.java | 20 ++++---- .../java/cn/nukkit/plugin/PluginManager.java | 29 ++++++++++-- .../plugin/simple/SimplePluginCommand.java | 47 +++++++++++++++++++ 4 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 src/main/java/cn/nukkit/plugin/simple/SimplePluginCommand.java diff --git a/src/main/java/cn/nukkit/command/PluginCommand.java b/src/main/java/cn/nukkit/command/PluginCommand.java index 597ecedfb43..9387bb6e88d 100644 --- a/src/main/java/cn/nukkit/command/PluginCommand.java +++ b/src/main/java/cn/nukkit/command/PluginCommand.java @@ -9,9 +9,9 @@ */ public class PluginCommand extends Command implements PluginIdentifiableCommand { - private final T owningPlugin; + protected final T owningPlugin; - private CommandExecutor executor; + protected CommandExecutor executor; public PluginCommand(String name, T owner) { super(name); diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index 630edd85690..7e93c929a6f 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -158,14 +158,7 @@ private PluginDescription getSimpleDescription(Class plugin){ hashMap.put("isSimple",true); PluginDescription descript = new PluginDescription(hashMap); Permission[] permissions = main.permissions(); - HashMap> pers = new HashMap<>(); - for(Permission p:permissions){ - HashMap pers_child = new HashMap<>(); - pers_child.put("description",p.description()); - pers_child.put("default",p.theDefault()); - pers.put(p.permission(),pers_child); - } - hashMap.put("permissions",pers); + hashMap.put("permissions",parsePermission(permissions)); Command[] commands = main.commands(); for(Command command:commands){ descript.getCommands().put(command.name(),command); @@ -173,6 +166,17 @@ private PluginDescription getSimpleDescription(Class plugin){ return descript; } + public static HashMap parsePermission(Permission[] permissions){ + HashMap pers = new HashMap<>(); + for(Permission p:permissions){ + HashMap pers_child = new HashMap<>(); + pers_child.put("description",p.description()); + pers_child.put("default",p.theDefault()); + pers.put(p.permission(),pers_child); + } + return pers; + } + @Override public Plugin loadPlugin(String filename) throws Exception { return this.loadPlugin(new File(filename)); diff --git a/src/main/java/cn/nukkit/plugin/PluginManager.java b/src/main/java/cn/nukkit/plugin/PluginManager.java index 0081bd30c34..d6f986dd609 100644 --- a/src/main/java/cn/nukkit/plugin/PluginManager.java +++ b/src/main/java/cn/nukkit/plugin/PluginManager.java @@ -7,6 +7,7 @@ import cn.nukkit.permission.Permissible; import cn.nukkit.permission.Permission; import cn.nukkit.plugin.simple.Command; +import cn.nukkit.plugin.simple.SimplePluginCommand; import cn.nukkit.utils.MainLogger; import cn.nukkit.utils.PluginException; import cn.nukkit.utils.Utils; @@ -463,15 +464,35 @@ protected List parseSimplePluginCommands(Plugin plugin){ for(Map.Entry obj : commands){ Command command = (Command)(obj.getValue()); PluginCommand newCmd = new PluginCommand<>(command.name(),plugin); - newCmd.setDescription(command.description()); - newCmd.setUsage(command.usage()); - newCmd.setAliases(command.aliases()); - newCmd.setPermission(command.permission()); + initCommand(newCmd,command); pluginCmds.add(newCmd); } + Class pluginClass = plugin.getClass(); + Method[] methods = pluginClass.getDeclaredMethods(); + for(Method method:methods){ + Command cmd = method.getAnnotation(Command.class); + cn.nukkit.plugin.simple.Permission permission = + method.getAnnotation(cn.nukkit.plugin.simple.Permission.class); + if(cmd!=null&&permission!=null){ + plugin.getDescription().getPermissions().addAll(Permission.loadPermissions(JavaPluginLoader.parsePermission(new cn.nukkit.plugin.simple.Permission[] { + permission + }))); + SimplePluginCommand command = new SimplePluginCommand(cmd.name(),plugin); + initCommand(command,cmd); + command.setCommandMethod(method); + pluginCmds.add(command); + } + } return pluginCmds; } + private void initCommand(PluginCommand newCmd,Command command){ + newCmd.setDescription(command.description()); + newCmd.setUsage(command.usage()); + newCmd.setAliases(command.aliases()); + newCmd.setPermission(command.permission()); + } + protected List parseYamlCommands(Plugin plugin) { List pluginCmds = new ArrayList<>(); diff --git a/src/main/java/cn/nukkit/plugin/simple/SimplePluginCommand.java b/src/main/java/cn/nukkit/plugin/simple/SimplePluginCommand.java new file mode 100644 index 00000000000..d2e8cea993b --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/simple/SimplePluginCommand.java @@ -0,0 +1,47 @@ +package cn.nukkit.plugin.simple; + +import cn.nukkit.command.CommandSender; +import cn.nukkit.command.PluginCommand; +import cn.nukkit.lang.TranslationContainer; +import cn.nukkit.plugin.Plugin; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class SimplePluginCommand extends PluginCommand { + + private Method commandMethod; + + public SimplePluginCommand(String name, Plugin owner) { + super(name, owner); + } + + public void setCommandMethod(Method commandMethod) { + this.commandMethod = commandMethod; + } + + + @Override + public boolean execute(CommandSender sender, String commandLabel, String[] args) { + try { + if (!this.owningPlugin.isEnabled()) { + return false; + } + + if (!this.testPermission(sender)) { + return false; + } + + boolean success = (Boolean) commandMethod.invoke(owningPlugin, sender, this, commandLabel, args); + + if (!success && !this.usageMessage.equals("")) { + sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage)); + } + + return success; + }catch (IllegalAccessException| InvocationTargetException e){ + e.printStackTrace(); + } + return false; + } +} From 9936cf196c9ccf8d2ab790b462f40421162a6f23 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sun, 24 Nov 2019 13:52:11 +0800 Subject: [PATCH 08/17] Added a new feature to simplify some development: You can use SimpleCommandData --- .../plugin/simple/SimpleCommandData.java | 38 +++++++++++++++++++ .../plugin/simple/SimplePluginCommand.java | 10 ++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/nukkit/plugin/simple/SimpleCommandData.java diff --git a/src/main/java/cn/nukkit/plugin/simple/SimpleCommandData.java b/src/main/java/cn/nukkit/plugin/simple/SimpleCommandData.java new file mode 100644 index 00000000000..eb4f189849e --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/simple/SimpleCommandData.java @@ -0,0 +1,38 @@ +package cn.nukkit.plugin.simple; + +import cn.nukkit.command.Command; +import cn.nukkit.command.CommandSender; + +public class SimpleCommandData { + + public SimpleCommandData(CommandSender sender, Command command, String label, String[] args) { + this.sender = sender; + this.command = command; + this.label = label; + this.args = args; + } + + private CommandSender sender; + + private cn.nukkit.command.Command command; + + private String label; + + private String[] args; + + public Command getCommand() { + return command; + } + + public CommandSender getSender() { + return sender; + } + + public String getLabel() { + return label; + } + + public String[] getArgs() { + return args; + } +} diff --git a/src/main/java/cn/nukkit/plugin/simple/SimplePluginCommand.java b/src/main/java/cn/nukkit/plugin/simple/SimplePluginCommand.java index d2e8cea993b..dbc689071b1 100644 --- a/src/main/java/cn/nukkit/plugin/simple/SimplePluginCommand.java +++ b/src/main/java/cn/nukkit/plugin/simple/SimplePluginCommand.java @@ -31,8 +31,16 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args) if (!this.testPermission(sender)) { return false; } + boolean success = false; + Class[] clzs = commandMethod.getParameterTypes(); + if(clzs.length!=0){ + if(clzs[0].equals(SimpleCommandData.class)){ + success = (Boolean) commandMethod.invoke(owningPlugin,new SimpleCommandData(sender,this,commandLabel,args)); + }else{ + success = (Boolean) commandMethod.invoke(owningPlugin, sender, this, commandLabel, args); + } + } - boolean success = (Boolean) commandMethod.invoke(owningPlugin, sender, this, commandLabel, args); if (!success && !this.usageMessage.equals("")) { sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage)); From 6e37bf6d97b074f837f626cd80c0693f5bba12f1 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sun, 24 Nov 2019 15:00:51 +0800 Subject: [PATCH 09/17] Add a Annotation: EnableRegister,can register the listener automatically. --- .../cn/nukkit/plugin/JavaPluginLoader.java | 48 ++++++++++++++++++- .../nukkit/plugin/simple/EnableRegister.java | 18 +++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/nukkit/plugin/simple/EnableRegister.java diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index 7e93c929a6f..38861a27f4f 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -1,9 +1,11 @@ package cn.nukkit.plugin; import cn.nukkit.Server; +import cn.nukkit.event.Listener; import cn.nukkit.event.plugin.PluginDisableEvent; import cn.nukkit.event.plugin.PluginEnableEvent; import cn.nukkit.plugin.simple.Command; +import cn.nukkit.plugin.simple.EnableRegister; import cn.nukkit.plugin.simple.Main; import cn.nukkit.plugin.simple.Permission; import cn.nukkit.utils.PluginException; @@ -28,6 +30,9 @@ public class JavaPluginLoader implements PluginLoader { private final Map classLoaders = new HashMap<>(); private final Map loadedSimplePlugin = new HashMap<>(); + + private final Map> simplePluginEnableClasses = new HashMap<>(); + public JavaPluginLoader(Server server) { this.server = server; } @@ -101,18 +106,48 @@ private Class getSimplePlugin(File file){ try(JarFile jarFile = new JarFile(file)){ PluginClassLoader classLoader = new PluginClassLoader(this, this.getClass().getClassLoader(),file); Enumeration entries = jarFile.entries(); + boolean isEnableRegister = false; + boolean hasGeted = false; + List haveLoaded = new ArrayList<>(); + Class mainClass = null; while (entries.hasMoreElements()){ String name = entries.nextElement().getName(); if(name.endsWith(".class")) { String mainName = name.substring(0, name.lastIndexOf(".")).replace("/", "."); Class clz = classLoader.loadClass(mainName); Main main = clz.getAnnotation(Main.class); + haveLoaded.add(clz); if(main != null){ loadedSimplePlugin.put(file,clz); PluginAssert.isPluginBaseChild(clz,mainName); - return clz; + mainClass = clz; + } + } + } + EnableRegister register; + List> noRegister = null; + if(mainClass!=null) { + for (Class clz1 : haveLoaded) { + if (!hasGeted) { + register = mainClass.getAnnotation(EnableRegister.class); + isEnableRegister = register != null; + hasGeted = true; + List classes = new ArrayList<>(); + simplePluginEnableClasses.put(file, classes); + if (register != null) { + noRegister = Arrays.asList(register.noRegister()); + } + } + if (isEnableRegister) { + + if (Arrays.asList(clz1.getInterfaces()).contains(Listener.class)) { + if (!noRegister.contains(clz1)) { + simplePluginEnableClasses.get(file).add(clz1); + } + } } } + return mainClass; } PluginAssert.findMainClass(""); }catch (IOException|ClassNotFoundException e){ @@ -226,6 +261,17 @@ public void enablePlugin(Plugin plugin) { ((PluginBase) plugin).setEnabled(true); + try { + if (plugin.getDescription().isSimple()) { + List listeners = simplePluginEnableClasses.get(((PluginBase) plugin).getFile()); + for (Class clz : listeners) { + server.getPluginManager().registerEvents((Listener) clz.newInstance(), plugin); + } + } + }catch (InstantiationException|IllegalAccessException e){ + throw new PluginException(e.getMessage()); + } + this.server.getPluginManager().callEvent(new PluginEnableEvent(plugin)); } } diff --git a/src/main/java/cn/nukkit/plugin/simple/EnableRegister.java b/src/main/java/cn/nukkit/plugin/simple/EnableRegister.java new file mode 100644 index 00000000000..52a84c262b9 --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/simple/EnableRegister.java @@ -0,0 +1,18 @@ +package cn.nukkit.plugin.simple; + +import cn.nukkit.event.Listener; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Listener auto-registration for launching SimplePlugin + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface EnableRegister { + + Class[] noRegister() default {}; +} From 28cf55756e729393236daa9c4a59fd5d337fcf40 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Tue, 26 Nov 2019 15:49:00 +0800 Subject: [PATCH 10/17] Permission: add the children tags,Command: add the `permissionMessage` --- .../java/cn/nukkit/permission/Permission.java | 23 +++++++++++++++++++ .../cn/nukkit/plugin/JavaPluginLoader.java | 12 ++-------- .../java/cn/nukkit/plugin/PluginManager.java | 3 ++- .../cn/nukkit/plugin/simple/Children.java | 15 ++++++++++++ .../java/cn/nukkit/plugin/simple/Command.java | 2 ++ .../cn/nukkit/plugin/simple/Permission.java | 3 ++- 6 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 src/main/java/cn/nukkit/plugin/simple/Children.java diff --git a/src/main/java/cn/nukkit/permission/Permission.java b/src/main/java/cn/nukkit/permission/Permission.java index 00039105b87..c67b6d57094 100644 --- a/src/main/java/cn/nukkit/permission/Permission.java +++ b/src/main/java/cn/nukkit/permission/Permission.java @@ -1,6 +1,7 @@ package cn.nukkit.permission; import cn.nukkit.Server; +import cn.nukkit.plugin.simple.Children; import java.util.*; @@ -130,6 +131,28 @@ public Permission addParent(String name, boolean value) { return perm; } + public static HashMap parsePermission(cn.nukkit.plugin.simple.Permission[] permissions){ + HashMap pers = new HashMap<>(); + for(cn.nukkit.plugin.simple.Permission p:permissions){ + HashMap pers_child = new HashMap<>(); + pers_child.put("description",p.description()); + pers_child.put("default",p.theDefault()); + Children[] children = p.childeren(); + if(children.length!=0) { + HashMap map = new HashMap<>(); + for (Children c : children) { + HashMap childValue = new HashMap<>(); + childValue.put("description",c.description()); + childValue.put("default",c.theDefault()); + map.put(c.name(),childValue); + } + pers_child.put("children",map); + } + pers.put(p.permission(),pers_child); + } + return pers; + } + public static List loadPermissions(Map data) { return loadPermissions(data, DEFAULT_OP); } diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index 38861a27f4f..165bbe7ac4c 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -19,6 +19,8 @@ import java.util.jar.JarFile; import java.util.regex.Pattern; +import static cn.nukkit.permission.Permission.parsePermission; + /** * Created by Nukkit Team. */ @@ -201,16 +203,6 @@ private PluginDescription getSimpleDescription(Class plugin){ return descript; } - public static HashMap parsePermission(Permission[] permissions){ - HashMap pers = new HashMap<>(); - for(Permission p:permissions){ - HashMap pers_child = new HashMap<>(); - pers_child.put("description",p.description()); - pers_child.put("default",p.theDefault()); - pers.put(p.permission(),pers_child); - } - return pers; - } @Override public Plugin loadPlugin(String filename) throws Exception { diff --git a/src/main/java/cn/nukkit/plugin/PluginManager.java b/src/main/java/cn/nukkit/plugin/PluginManager.java index d6f986dd609..0668b04306e 100644 --- a/src/main/java/cn/nukkit/plugin/PluginManager.java +++ b/src/main/java/cn/nukkit/plugin/PluginManager.java @@ -474,7 +474,7 @@ protected List parseSimplePluginCommands(Plugin plugin){ cn.nukkit.plugin.simple.Permission permission = method.getAnnotation(cn.nukkit.plugin.simple.Permission.class); if(cmd!=null&&permission!=null){ - plugin.getDescription().getPermissions().addAll(Permission.loadPermissions(JavaPluginLoader.parsePermission(new cn.nukkit.plugin.simple.Permission[] { + plugin.getDescription().getPermissions().addAll(Permission.loadPermissions(Permission.parsePermission(new cn.nukkit.plugin.simple.Permission[] { permission }))); SimplePluginCommand command = new SimplePluginCommand(cmd.name(),plugin); @@ -491,6 +491,7 @@ private void initCommand(PluginCommand newCmd,Command command){ newCmd.setUsage(command.usage()); newCmd.setAliases(command.aliases()); newCmd.setPermission(command.permission()); + newCmd.setPermissionMessage(command.permissionMessage()); } protected List parseYamlCommands(Plugin plugin) { diff --git a/src/main/java/cn/nukkit/plugin/simple/Children.java b/src/main/java/cn/nukkit/plugin/simple/Children.java new file mode 100644 index 00000000000..b4da6b444ba --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/simple/Children.java @@ -0,0 +1,15 @@ +package cn.nukkit.plugin.simple; + +import cn.nukkit.permission.Permission; + +/** + * The children permission + */ +public @interface Children { + + String name(); + + String description() default ""; + + String theDefault() default Permission.DEFAULT_PERMISSION; +} diff --git a/src/main/java/cn/nukkit/plugin/simple/Command.java b/src/main/java/cn/nukkit/plugin/simple/Command.java index 71c3a412148..92e2c526956 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Command.java +++ b/src/main/java/cn/nukkit/plugin/simple/Command.java @@ -18,4 +18,6 @@ String permission(); String[] aliases() default {}; + + String permissionMessage() default ""; } diff --git a/src/main/java/cn/nukkit/plugin/simple/Permission.java b/src/main/java/cn/nukkit/plugin/simple/Permission.java index 8d07b0ad201..71b74cd543c 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Permission.java +++ b/src/main/java/cn/nukkit/plugin/simple/Permission.java @@ -13,6 +13,7 @@ String description() default ""; - String theDefault(); + String theDefault() default cn.nukkit.permission.Permission.DEFAULT_PERMISSION; + Children[] childeren() default {}; } From 5221242025731752627a1a6f416ccbe3afea5d23 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Fri, 29 Nov 2019 22:46:06 +0800 Subject: [PATCH 11/17] You can use Plugin.yml while using the content of SimplePluginSystem --- .../cn/nukkit/plugin/JavaPluginLoader.java | 85 ++++++++++++------- .../java/cn/nukkit/plugin/PluginManager.java | 48 ++++++----- .../java/cn/nukkit/plugin/simple/Main.java | 4 +- 3 files changed, 85 insertions(+), 52 deletions(-) diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index 165bbe7ac4c..30eb6865a4d 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -33,7 +33,7 @@ public class JavaPluginLoader implements PluginLoader { private final Map loadedSimplePlugin = new HashMap<>(); - private final Map> simplePluginEnableClasses = new HashMap<>(); + private final Map> simplePluginEnableClasses = new HashMap<>(); public JavaPluginLoader(Server server) { this.server = server; @@ -64,6 +64,7 @@ public Plugin loadPlugin(File file) throws Exception { plugin = pluginClass.newInstance(); this.initPlugin(plugin, description, dataFolder, file); + loadEnableRegister(new ArrayList<>(),plugin.getClass(),false,file,classLoader); return plugin; } catch (ClassCastException e) { throw new PluginException("Error whilst initializing main class `" + description.getMain() + "'", e); @@ -108,47 +109,28 @@ private Class getSimplePlugin(File file){ try(JarFile jarFile = new JarFile(file)){ PluginClassLoader classLoader = new PluginClassLoader(this, this.getClass().getClassLoader(),file); Enumeration entries = jarFile.entries(); - boolean isEnableRegister = false; - boolean hasGeted = false; + List haveLoaded = new ArrayList<>(); Class mainClass = null; while (entries.hasMoreElements()){ String name = entries.nextElement().getName(); if(name.endsWith(".class")) { - String mainName = name.substring(0, name.lastIndexOf(".")).replace("/", "."); + String mainName = getClassName(name); Class clz = classLoader.loadClass(mainName); Main main = clz.getAnnotation(Main.class); haveLoaded.add(clz); if(main != null){ + classLoaders.put(main.name(),classLoader); loadedSimplePlugin.put(file,clz); PluginAssert.isPluginBaseChild(clz,mainName); mainClass = clz; } } } - EnableRegister register; - List> noRegister = null; - if(mainClass!=null) { - for (Class clz1 : haveLoaded) { - if (!hasGeted) { - register = mainClass.getAnnotation(EnableRegister.class); - isEnableRegister = register != null; - hasGeted = true; - List classes = new ArrayList<>(); - simplePluginEnableClasses.put(file, classes); - if (register != null) { - noRegister = Arrays.asList(register.noRegister()); - } - } - if (isEnableRegister) { - if (Arrays.asList(clz1.getInterfaces()).contains(Listener.class)) { - if (!noRegister.contains(clz1)) { - simplePluginEnableClasses.get(file).add(clz1); - } - } - } - } + + if(mainClass!=null) { + loadEnableRegister(haveLoaded,mainClass,true,file,classLoader); return mainClass; } PluginAssert.findMainClass(""); @@ -158,6 +140,49 @@ private Class getSimplePlugin(File file){ return null; } + private void loadEnableRegister(List haveLoaded,Class mainClass,boolean hasLoaded,File file,PluginClassLoader loader){ + boolean isEnableRegister; + EnableRegister register; + List> noRegister = null; + register = mainClass.getAnnotation(EnableRegister.class); + isEnableRegister = register != null; + List classes = new ArrayList<>(); + simplePluginEnableClasses.put(mainClass, classes); + if (register != null) { + noRegister = Arrays.asList(register.noRegister()); + } + if (isEnableRegister) { + if(!hasLoaded){ + try(JarFile jarFile = new JarFile(file)){ + Enumeration entries = jarFile.entries(); + while (entries.hasMoreElements()) { + String name = entries.nextElement().getName(); + if (name.endsWith(".class")) { + haveLoaded.add(loader.loadClass(getClassName(name))); + } + } + }catch (IOException|ClassNotFoundException e){ + throw new PluginException(e.getMessage()); + } + } + for (Class loaded : haveLoaded) { + + if (Arrays.asList(loaded.getInterfaces()).contains(Listener.class)) { + + if (!noRegister.contains(loaded)) { + simplePluginEnableClasses.get(mainClass).add(loaded); + } + } + + } + } + + + } + + private String getClassName(String name){ + return name.substring(0, name.lastIndexOf(".")).replace("/", "."); + } /** * the simple description for simple plugin system * @param plugin the main class @@ -172,7 +197,7 @@ private PluginDescription getSimpleDescription(Class plugin){ String version = main.version(); String author = main.author(); String description = main.description(); - String pluginLoadOrder = main.load(); + PluginLoadOrder pluginLoadOrder = main.load(); String website = main.website(); String prefix = main.prefix(); List api = Arrays.asList(main.api()); @@ -188,7 +213,7 @@ private PluginDescription getSimpleDescription(Class plugin){ hashMap.put("loadBefore",loadBefore); hashMap.put("softDepend",softDepend); hashMap.put("description",description); - hashMap.put("load",pluginLoadOrder); + hashMap.put("load",pluginLoadOrder.toString()); hashMap.put("website",website); hashMap.put("prefix",prefix.equals("")?name:prefix); hashMap.put("main",plugin.getName()); @@ -254,8 +279,8 @@ public void enablePlugin(Plugin plugin) { ((PluginBase) plugin).setEnabled(true); try { - if (plugin.getDescription().isSimple()) { - List listeners = simplePluginEnableClasses.get(((PluginBase) plugin).getFile()); + if (simplePluginEnableClasses.get(plugin.getClass())!=null) { + List listeners = simplePluginEnableClasses.get(plugin.getClass()); for (Class clz : listeners) { server.getPluginManager().registerEvents((Listener) clz.newInstance(), plugin); } diff --git a/src/main/java/cn/nukkit/plugin/PluginManager.java b/src/main/java/cn/nukkit/plugin/PluginManager.java index 0668b04306e..fc674c35e6e 100644 --- a/src/main/java/cn/nukkit/plugin/PluginManager.java +++ b/src/main/java/cn/nukkit/plugin/PluginManager.java @@ -99,7 +99,8 @@ public Plugin loadPlugin(File file, Map loaders) { this.plugins.put(plugin.getDescription().getName(), plugin); List pluginCommands = this.parseYamlCommands(plugin); - + // simple command + pluginCommands.addAll(this.parseSimplePluginCommands(plugin)); if (!pluginCommands.isEmpty()) { this.commandMap.registerAll(plugin.getDescription().getName(), pluginCommands); } @@ -161,14 +162,15 @@ public Map loadPlugins(File dictionary, List newLoaders, } for (final PluginLoader loader : loaders.values()) { - for (File file : dictionary.listFiles((dir, name) -> { + File[] files = dictionary.listFiles((dir, name) -> { for (Pattern pattern : loader.getPluginFilters()) { if (pattern.matcher(name).matches()) { return true; } } return false; - })) { + }); + for (File file : files!=null?files:new File[0]) { if (file.isDirectory() && !includeDir) { continue; } @@ -278,14 +280,8 @@ public Map loadPlugins(File dictionary, List newLoaders, } if (!dependencies.containsKey(name) && !softDependencies.containsKey(name)) { - plugins.remove(name); missingDependency = false; - Plugin plugin = this.loadPlugin(file, loaders); - if (plugin != null) { - loadedPlugins.put(name, plugin); - } else { - this.server.getLogger().critical(this.server.getLanguage().translateString("nukkit.plugin.genericLoadError", name)); - } + load(plugins,name,file,loaders,loadedPlugins); } } @@ -294,14 +290,8 @@ public Map loadPlugins(File dictionary, List newLoaders, File file = plugins.get(name); if (!dependencies.containsKey(name)) { softDependencies.remove(name); - plugins.remove(name); missingDependency = false; - Plugin plugin = this.loadPlugin(file, loaders); - if (plugin != null) { - loadedPlugins.put(name, plugin); - } else { - this.server.getLogger().critical(this.server.getLanguage().translateString("nukkit.plugin.genericLoadError", name)); - } + load(plugins,name,file,loaders,loadedPlugins); } } @@ -320,6 +310,17 @@ public Map loadPlugins(File dictionary, List newLoaders, } } + + private void load(Map plugins,String name,File file,Map loaders,Map loadedPlugins){ + plugins.remove(name); + Plugin plugin = this.loadPlugin(file, loaders); + if (plugin != null) { + loadedPlugins.put(name, plugin); + } else { + this.server.getLogger().critical(this.server.getLanguage().translateString("nukkit.plugin.genericLoadError", name)); + } + } + public Permission getPermission(String name) { if (this.permissions.containsKey(name)) { return this.permissions.get(name); @@ -462,10 +463,12 @@ protected List parseSimplePluginCommands(Plugin plugin){ List pluginCmds = new ArrayList<>(); Set> commands = plugin.getDescription().getCommands().entrySet(); for(Map.Entry obj : commands){ - Command command = (Command)(obj.getValue()); - PluginCommand newCmd = new PluginCommand<>(command.name(),plugin); - initCommand(newCmd,command); - pluginCmds.add(newCmd); + if(obj.getValue() instanceof Command) { + Command command = (Command) (obj.getValue()); + PluginCommand newCmd = new PluginCommand<>(command.name(), plugin); + initCommand(newCmd, command); + pluginCmds.add(newCmd); + } } Class pluginClass = plugin.getClass(); Method[] methods = pluginClass.getDeclaredMethods(); @@ -498,6 +501,9 @@ protected List parseYamlCommands(Plugin plugin) { List pluginCmds = new ArrayList<>(); for (Map.Entry entry : plugin.getDescription().getCommands().entrySet()) { + if(entry.getValue() instanceof Command){ + continue; + } String key = (String) entry.getKey(); Object data = entry.getValue(); if (key.contains(":")) { diff --git a/src/main/java/cn/nukkit/plugin/simple/Main.java b/src/main/java/cn/nukkit/plugin/simple/Main.java index 4dfead5430f..42779a191b7 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Main.java +++ b/src/main/java/cn/nukkit/plugin/simple/Main.java @@ -1,5 +1,7 @@ package cn.nukkit.plugin.simple; +import cn.nukkit.plugin.PluginLoadOrder; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -35,7 +37,7 @@ String description() default ""; - String load() default "POSTWORLD"; + PluginLoadOrder load() default PluginLoadOrder.POSTWORLD; String website() default ""; From 8a08b18ab5714ee64b1dbf28b656e39ce22723fa Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Fri, 29 Nov 2019 23:36:11 +0800 Subject: [PATCH 12/17] Update AutoPlugin.class,You can directly get the main class object through this annotation --- .../cn/nukkit/plugin/JavaPluginLoader.java | 59 ++++++++++++++----- .../cn/nukkit/plugin/simple/AutoPlugin.java | 15 +++++ .../nukkit/plugin/simple/EnableCommand.java | 15 +++++ 3 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java create mode 100644 src/main/java/cn/nukkit/plugin/simple/EnableCommand.java diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index 30eb6865a4d..34b605ecea3 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -4,16 +4,14 @@ import cn.nukkit.event.Listener; import cn.nukkit.event.plugin.PluginDisableEvent; import cn.nukkit.event.plugin.PluginEnableEvent; -import cn.nukkit.plugin.simple.Command; -import cn.nukkit.plugin.simple.EnableRegister; -import cn.nukkit.plugin.simple.Main; -import cn.nukkit.plugin.simple.Permission; +import cn.nukkit.plugin.simple.*; import cn.nukkit.utils.PluginException; import cn.nukkit.utils.Utils; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Field; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -141,17 +139,21 @@ private Class getSimplePlugin(File file){ } private void loadEnableRegister(List haveLoaded,Class mainClass,boolean hasLoaded,File file,PluginClassLoader loader){ - boolean isEnableRegister; - EnableRegister register; List> noRegister = null; - register = mainClass.getAnnotation(EnableRegister.class); - isEnableRegister = register != null; + EnableRegister register = mainClass.getAnnotation(EnableRegister.class); + boolean isEnableRegister = register != null; List classes = new ArrayList<>(); simplePluginEnableClasses.put(mainClass, classes); if (register != null) { noRegister = Arrays.asList(register.noRegister()); } - if (isEnableRegister) { + EnableCommand command = mainClass.getAnnotation(EnableCommand.class); + boolean isEnableCommand = command !=null; + List> noCommands = null; + if(command != null){ + noCommands = Arrays.asList(command.noRegister()); + } + if(isEnableCommand||isEnableRegister){ if(!hasLoaded){ try(JarFile jarFile = new JarFile(file)){ Enumeration entries = jarFile.entries(); @@ -165,16 +167,27 @@ private void loadEnableRegister(List haveLoaded,Class mainClass,boolea throw new PluginException(e.getMessage()); } } - for (Class loaded : haveLoaded) { + } + for (Class loaded : haveLoaded) { + if(isEnableRegister){ if (Arrays.asList(loaded.getInterfaces()).contains(Listener.class)) { - + if (!noRegister.contains(loaded)) { simplePluginEnableClasses.get(mainClass).add(loaded); } } + } + if(isEnableCommand){ + if (cn.nukkit.command.Command.class.isAssignableFrom(loaded)) { + if (!noCommands.contains(loaded)) { + simplePluginEnableClasses.get(mainClass).add(loaded); + } + } } + + } @@ -280,9 +293,16 @@ public void enablePlugin(Plugin plugin) { try { if (simplePluginEnableClasses.get(plugin.getClass())!=null) { - List listeners = simplePluginEnableClasses.get(plugin.getClass()); - for (Class clz : listeners) { - server.getPluginManager().registerEvents((Listener) clz.newInstance(), plugin); + List assemblies = simplePluginEnableClasses.get(plugin.getClass()); + for (Class clz : assemblies) { + Object obj = clz.newInstance(); + autoPlugin(obj,plugin); + if(cn.nukkit.command.Command.class.isAssignableFrom(clz)){ + server.getCommandMap().register(plugin.getName(),(cn.nukkit.command.Command)obj); + }else{ + server.getPluginManager().registerEvents((Listener) obj, plugin); + } + } } }catch (InstantiationException|IllegalAccessException e){ @@ -293,6 +313,17 @@ public void enablePlugin(Plugin plugin) { } } + private void autoPlugin(Object obj,Plugin plugin) throws IllegalAccessException{ + Class clz = obj.getClass(); + Field[] fields = clz.getFields(); + for(Field field:fields){ + if(field.getAnnotation(AutoPlugin.class)!=null) { + field.setAccessible(true); + field.set(obj,plugin); + } + } + } + @Override public void disablePlugin(Plugin plugin) { if (plugin instanceof PluginBase && plugin.isEnabled()) { diff --git a/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java b/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java new file mode 100644 index 00000000000..b254503c6b8 --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java @@ -0,0 +1,15 @@ +package cn.nukkit.plugin.simple; + + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface AutoPlugin { + + +} diff --git a/src/main/java/cn/nukkit/plugin/simple/EnableCommand.java b/src/main/java/cn/nukkit/plugin/simple/EnableCommand.java new file mode 100644 index 00000000000..5990fbf30bd --- /dev/null +++ b/src/main/java/cn/nukkit/plugin/simple/EnableCommand.java @@ -0,0 +1,15 @@ +package cn.nukkit.plugin.simple; + +import cn.nukkit.command.Command; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface EnableCommand { + + Class[] noRegister() default {}; +} From 679555b015fb20bdfeea307f5fd80911ba3987c9 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Fri, 29 Nov 2019 23:39:20 +0800 Subject: [PATCH 13/17] modify TYPE to METHOD --- src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java b/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java index b254503c6b8..7623feea0da 100644 --- a/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java +++ b/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java @@ -8,7 +8,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) +@Target({ElementType.METHOD}) public @interface AutoPlugin { From 4bbb256a3f8b66a705b2f148f212d001bf70fd3f Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Fri, 29 Nov 2019 23:40:45 +0800 Subject: [PATCH 14/17] modify METHOD to FIELD --- src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java b/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java index 7623feea0da..f5d6c5d1fb5 100644 --- a/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java +++ b/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java @@ -8,7 +8,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD}) +@Target({ElementType.FIELD}) public @interface AutoPlugin { From 20c4dea0fc0223edf5a9e692c6ad93a8a8ce212c Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sat, 30 Nov 2019 08:03:49 +0800 Subject: [PATCH 15/17] Fix a bug: modify getField to getDeclaredFiled --- src/main/java/cn/nukkit/plugin/JavaPluginLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java index 34b605ecea3..e0e39f9cf6e 100644 --- a/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java +++ b/src/main/java/cn/nukkit/plugin/JavaPluginLoader.java @@ -315,7 +315,7 @@ public void enablePlugin(Plugin plugin) { private void autoPlugin(Object obj,Plugin plugin) throws IllegalAccessException{ Class clz = obj.getClass(); - Field[] fields = clz.getFields(); + Field[] fields = clz.getDeclaredFields(); for(Field field:fields){ if(field.getAnnotation(AutoPlugin.class)!=null) { field.setAccessible(true); From 7e4fed03f92ecf8ec899d74e8019408994050367 Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sat, 30 Nov 2019 09:14:11 +0800 Subject: [PATCH 16/17] add some comments --- .../java/cn/nukkit/plugin/simple/AutoPlugin.java | 15 +++++++++++++++ .../java/cn/nukkit/plugin/simple/Children.java | 1 + .../java/cn/nukkit/plugin/simple/Command.java | 4 ++++ .../cn/nukkit/plugin/simple/EnableCommand.java | 3 +++ .../cn/nukkit/plugin/simple/EnableRegister.java | 1 + src/main/java/cn/nukkit/plugin/simple/Main.java | 15 +++++++-------- .../java/cn/nukkit/plugin/simple/Permission.java | 4 ++++ 7 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java b/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java index f5d6c5d1fb5..6d8487c4d12 100644 --- a/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java +++ b/src/main/java/cn/nukkit/plugin/simple/AutoPlugin.java @@ -7,6 +7,21 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * You can use the AutoPlugin annotation to inject objects of the main class, + * which was recently added based on a simple plugin system. + * + * @ AutoPlugin + * private Plugin plugin; + * @ Override + * public boolean execute(CommandSender commandSender, String label, String[] strings) { + * plugin.getLogger().info("233");//no npe + * return true; + * } + * + * @author magiclu # DreamServer + * @see java.lang.annotation.Annotation + */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD}) public @interface AutoPlugin { diff --git a/src/main/java/cn/nukkit/plugin/simple/Children.java b/src/main/java/cn/nukkit/plugin/simple/Children.java index b4da6b444ba..10d8e441127 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Children.java +++ b/src/main/java/cn/nukkit/plugin/simple/Children.java @@ -4,6 +4,7 @@ /** * The children permission + * @author magiclu # DreamServer */ public @interface Children { diff --git a/src/main/java/cn/nukkit/plugin/simple/Command.java b/src/main/java/cn/nukkit/plugin/simple/Command.java index 92e2c526956..c31b4b4a123 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Command.java +++ b/src/main/java/cn/nukkit/plugin/simple/Command.java @@ -5,6 +5,10 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * Command auto-registration for launching SimplePlugin + * @author magiclu # DreamServer + */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.METHOD}) public @interface Command { diff --git a/src/main/java/cn/nukkit/plugin/simple/EnableCommand.java b/src/main/java/cn/nukkit/plugin/simple/EnableCommand.java index 5990fbf30bd..56b5e5cb551 100644 --- a/src/main/java/cn/nukkit/plugin/simple/EnableCommand.java +++ b/src/main/java/cn/nukkit/plugin/simple/EnableCommand.java @@ -7,6 +7,9 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * @author magiclu # DreamServer + */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public @interface EnableCommand { diff --git a/src/main/java/cn/nukkit/plugin/simple/EnableRegister.java b/src/main/java/cn/nukkit/plugin/simple/EnableRegister.java index 52a84c262b9..b766c95aa65 100644 --- a/src/main/java/cn/nukkit/plugin/simple/EnableRegister.java +++ b/src/main/java/cn/nukkit/plugin/simple/EnableRegister.java @@ -9,6 +9,7 @@ /** * Listener auto-registration for launching SimplePlugin + * @author magiclu # DreamServer */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) diff --git a/src/main/java/cn/nukkit/plugin/simple/Main.java b/src/main/java/cn/nukkit/plugin/simple/Main.java index 42779a191b7..d44e96b08e3 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Main.java +++ b/src/main/java/cn/nukkit/plugin/simple/Main.java @@ -8,14 +8,13 @@ import java.lang.annotation.Target; /** -@ Main(name="11",api = "1.0.9",commands ={ - @ Command(name="fp", - permission = @ Permission( - theDefault = "op" - ) - ) -}) -*/ + * main class positioning annotation. With this annotation, + * you can locate the main class and load configuration parameters + * without writing plugin.yml. + * + * @ Main(name="hello",api="1.0.9") + * @author magiclu # DreamServer + */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Main { diff --git a/src/main/java/cn/nukkit/plugin/simple/Permission.java b/src/main/java/cn/nukkit/plugin/simple/Permission.java index 71b74cd543c..22e12ba897c 100644 --- a/src/main/java/cn/nukkit/plugin/simple/Permission.java +++ b/src/main/java/cn/nukkit/plugin/simple/Permission.java @@ -5,6 +5,10 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * used with Command. + * @author magiclu # DreamServer + */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.METHOD}) public @interface Permission { From 3d6ecfbf5a819ac35a93122c01aa1bdcdf8b36bc Mon Sep 17 00:00:00 2001 From: magiclu550 <843983728@qq.com> Date: Sat, 30 Nov 2019 11:36:04 +0800 Subject: [PATCH 17/17] Code duplication --- src/main/java/cn/nukkit/Server.java | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/main/java/cn/nukkit/Server.java b/src/main/java/cn/nukkit/Server.java index b1138654b63..9549df6bdec 100644 --- a/src/main/java/cn/nukkit/Server.java +++ b/src/main/java/cn/nukkit/Server.java @@ -598,24 +598,14 @@ public int broadcastMessage(TextContainer message, Collection recipients = new HashSet<>(); - - for (String permission : permissions.split(";")) { - for (Permissible permissible : this.pluginManager.getPermissionSubscriptions(permission)) { - if (permissible instanceof CommandSender && permissible.hasPermission(permission)) { - recipients.add((CommandSender) permissible); - } - } - } - - for (CommandSender recipient : recipients) { - recipient.sendMessage(message); - } - - return recipients.size(); + return broadcast((Object) message,permissions); } public int broadcast(TextContainer message, String permissions) { + return broadcast((Object) message,permissions); + } + + private int broadcast(Object message,String permissions){ Set recipients = new HashSet<>(); for (String permission : permissions.split(";")) { @@ -627,7 +617,11 @@ public int broadcast(TextContainer message, String permissions) { } for (CommandSender recipient : recipients) { - recipient.sendMessage(message); + if(message instanceof String) { + recipient.sendMessage((String) message); + }else if(message instanceof TextContainer){ + recipient.sendMessage((TextContainer) message); + } } return recipients.size();