3
3
import cn .nukkit .Server ;
4
4
import cn .nukkit .event .plugin .PluginDisableEvent ;
5
5
import cn .nukkit .event .plugin .PluginEnableEvent ;
6
+ import cn .nukkit .plugin .simple .Command ;
7
+ import cn .nukkit .plugin .simple .Main ;
6
8
import cn .nukkit .utils .PluginException ;
7
9
import cn .nukkit .utils .Utils ;
8
10
9
11
import java .io .File ;
10
12
import java .io .IOException ;
11
13
import java .io .InputStream ;
12
- import java .util .HashMap ;
13
- import java .util .Map ;
14
+ import java .util .*;
14
15
import java .util .jar .JarEntry ;
15
16
import java .util .jar .JarFile ;
16
17
import java .util .regex .Pattern ;
@@ -46,9 +47,7 @@ public Plugin loadPlugin(File file) throws Exception {
46
47
try {
47
48
Class javaClass = classLoader .loadClass (className );
48
49
49
- if (!PluginBase .class .isAssignableFrom (javaClass )) {
50
- throw new PluginException ("Main class `" + description .getMain () + "' does not extend PluginBase" );
51
- }
50
+ PluginAssert .isPluginBaseChild (javaClass ,description .getMain ());
52
51
53
52
try {
54
53
Class <PluginBase > pluginClass = (Class <PluginBase >) javaClass .asSubclass (PluginBase .class );
@@ -64,13 +63,89 @@ public Plugin loadPlugin(File file) throws Exception {
64
63
}
65
64
66
65
} catch (ClassNotFoundException e ) {
67
- throw new PluginException ( "Couldn't load plugin " + description .getName () + ": main class not found" );
66
+ PluginAssert . findMainClass ( description .getName ());
68
67
}
69
68
}
70
69
71
70
return null ;
72
71
}
73
72
73
+ //TODO
74
+ @ Override
75
+ public Plugin simpleLoadPlugin (File file ) {
76
+ try {
77
+ Class <?> pluginClass = getSimplePlugin (file );
78
+ PluginDescription pluginDescription = getSimpleDescription (pluginClass );
79
+ this .server .getLogger ().info (this .server .getLanguage ().translateString ("nukkit.plugin.load" , pluginDescription .getFullName ()));
80
+ File dataFolder = new File (file .getParentFile (), pluginDescription .getName ());
81
+ PluginBase plugin = (PluginBase ) pluginClass .newInstance ();
82
+ this .initPlugin (plugin ,pluginDescription ,dataFolder ,file );
83
+ return plugin ;
84
+ }catch (InstantiationException | IllegalAccessException e ){
85
+ throw new PluginException (e .getMessage ());
86
+ }
87
+ //do it
88
+ }
89
+
90
+ private Class getSimplePlugin (File file ){
91
+ try (JarFile jarFile = new JarFile (file )){
92
+ PluginClassLoader classLoader = new PluginClassLoader (this , this .getClass ().getClassLoader (),file );
93
+ Enumeration <JarEntry > entries = jarFile .entries ();
94
+ while (entries .hasMoreElements ()){
95
+ String name = entries .nextElement ().getName ();
96
+ if (name .endsWith (".class" )) {
97
+ String mainName = name .substring (0 , name .lastIndexOf ("." )).replace ("/" , "." );
98
+ Class <?> clz = classLoader .loadClass (mainName );
99
+ Main main = clz .getAnnotation (Main .class );
100
+ if (main != null ){
101
+ PluginAssert .isPluginBaseChild (clz ,mainName );
102
+ return clz ;
103
+ }
104
+ }
105
+ }
106
+ PluginAssert .findMainClass ("" );
107
+ }catch (IOException |ClassNotFoundException e ){
108
+ throw new PluginException (e .getMessage ());
109
+ }
110
+ return null ;
111
+ }
112
+
113
+ private PluginDescription getSimpleDescription (Class <?> plugin ){
114
+ Main main = plugin .getAnnotation (Main .class );
115
+ if (main == null ){
116
+ throw new PluginException ("this is not a main class" );
117
+ }
118
+ String name = main .name ();
119
+ String version = main .version ();
120
+ String author = main .author ();
121
+ String description = main .description ();
122
+ String pluginLoadOrder = main .load ();
123
+ String website = main .website ();
124
+ String prefix = main .prefix ();
125
+ List <String > api = Arrays .asList (main .api ());
126
+ List <String > depend = Arrays .asList (main .depend ());
127
+ List <String > loadBefore = Arrays .asList (main .loadBefore ());
128
+ List <String > softDepend = Arrays .asList (main .softDepend ());
129
+ Map <String ,Object > hashMap = new HashMap <>();
130
+ hashMap .put ("name" ,name );
131
+ hashMap .put ("version" ,version );
132
+ hashMap .put ("author" ,author );
133
+ hashMap .put ("api" ,api );
134
+ hashMap .put ("depend" ,depend );
135
+ hashMap .put ("loadBefore" ,loadBefore );
136
+ hashMap .put ("softDepend" ,softDepend );
137
+ hashMap .put ("description" ,description );
138
+ hashMap .put ("load" ,pluginLoadOrder );
139
+ hashMap .put ("website" ,website );
140
+ hashMap .put ("prefix" ,prefix );
141
+ PluginDescription descript = new PluginDescription (hashMap );
142
+ Command [] commands = main .commands ();
143
+ for (Command command :commands ){
144
+ descript .getCommands ().put (command .name (),command );
145
+ }
146
+ return descript ;
147
+ }
148
+
74
149
@ Override
75
150
public Plugin loadPlugin (String filename ) throws Exception {
76
151
return this .loadPlugin (new File (filename ));
0 commit comments