22
22
import java .net .URLClassLoader ;
23
23
import java .nio .charset .StandardCharsets ;
24
24
import java .nio .file .Files ;
25
- import java .util .Arrays ;
26
- import java .util .List ;
27
- import java .util .Map ;
28
- import java .util .SortedSet ;
25
+ import java .util .*;
29
26
import java .util .jar .JarEntry ;
30
27
import java .util .jar .JarOutputStream ;
31
28
import java .util .zip .ZipEntry ;
38
35
*/
39
36
public record PluginLoader (LocalJarBoundary jarFinder ) implements LoadBoundary , UnloadBoundary , ReloadBoundary
40
37
{
38
+ private static final boolean DEBUG = false ;
41
39
private static final File HELPER_JAR = new File ("plugins/mcpm-helper.jar" );
42
40
43
41
public PluginLoader
@@ -46,6 +44,16 @@ public record PluginLoader(LocalJarBoundary jarFinder) implements LoadBoundary,
46
44
deleteHelper ();
47
45
}
48
46
47
+ /**
48
+ * Warning for debugging the program
49
+ *
50
+ * @param msg Debug message
51
+ */
52
+ private static void debug (Object msg )
53
+ {
54
+ if (DEBUG ) System .err .println (msg );
55
+ }
56
+
49
57
@ Override
50
58
public boolean loadPlugin (String name ) throws PluginNotFoundException
51
59
{
@@ -136,25 +144,25 @@ static File unloadPlugin(Plugin plugin)
136
144
.filter (cmd -> cmd .getPlugin () == plugin ).toList ().forEach (cmd -> {
137
145
cmd .unregister (cmdMap );
138
146
cmds .values ().removeIf (cmd ::equals );
139
- }), () -> System . err . println ("knownCommands cannot be accessed" )
140
- ), () -> System . err . println ("commandMap cannot be accessed" )
147
+ }), () -> debug ("knownCommands cannot be accessed" )
148
+ ), () -> debug ("commandMap cannot be accessed" )
141
149
);
142
150
143
151
// 5. Remove plugin from list
144
152
getPrivateField (pm , "plugins" , new TypeToken <List <Plugin >>(){})
145
153
.ifPresentOrElse (plugins -> plugins .remove (plugin ),
146
- () -> System . err . println ("plugins cannot be accessed" ));
154
+ () -> debug ("plugins cannot be accessed" ));
147
155
148
156
// 6. Remove lookup name
149
157
getPrivateField (pm , "lookupNames" , new TypeToken <Map <String , Plugin >>(){})
150
158
.ifPresentOrElse (names -> names .remove (plugin .getName ()),
151
- () -> System . err . println ("lookupNames cannot be accessed" ));
159
+ () -> debug ("lookupNames cannot be accessed" ));
152
160
153
161
// 7. Unload Java classes so that the jar can be deleted on Windows
154
162
if (plugin .getClass ().getClassLoader () instanceof URLClassLoader cl )
155
163
{
156
164
if (!setPrivateField (cl , "plugin" , null ) || !setPrivateField (cl , "pluginInit" , null ))
157
- System . err . println ("Error in setPrivateField, skipping unload" );
165
+ debug ("Error in setPrivateField, skipping unload" );
158
166
159
167
// Close classloader
160
168
try
@@ -198,8 +206,8 @@ private void reloadSelf()
198
206
// 4. Reflect! Since MCPM and MCPM-Helper are in different class loaders, I cannot call/cast it directly
199
207
// or else it would give me a nonsense error "class PluginLoaderHelper cannot be cast to PluginLoaderHelper"
200
208
var cls = pl .getClass ();
201
- System . out . println (cls );
202
- System . out . println (Arrays .toString (cls .getDeclaredMethods ()));
209
+ debug (cls );
210
+ debug (Arrays .toString (cls .getDeclaredMethods ()));
203
211
var reload = cls .getDeclaredMethod ("reloadMcpm" , Plugin .class , File .class );
204
212
reload .setAccessible (true );
205
213
reload .invoke (pl , SpigotEntry .instance (), jar );
@@ -223,7 +231,7 @@ private Plugin injectHelper()
223
231
{
224
232
// 1. Get the class bytecode from the jar
225
233
var jar = new File (PluginLoader .class .getProtectionDomain ().getCodeSource ().getLocation ().toURI ());
226
- System . out . println (jar );
234
+ debug (jar );
227
235
try (var ji = new PluginJarFile (jar ))
228
236
{
229
237
var yml = """
@@ -241,13 +249,13 @@ private Plugin injectHelper()
241
249
it .contains ("org/hydev/mcpm/utils/PluginJarFile" )).toList ();
242
250
243
251
// 2. Create a new jar file
244
- System . out . println ("Creating jar file..." );
252
+ debug ("Creating jar file..." );
245
253
try (var jo = new JarOutputStream (new FileOutputStream (HELPER_JAR )))
246
254
{
247
255
// Copy classes
248
256
for (String c : classes )
249
257
{
250
- System . out . println ("Copying " + c );
258
+ debug ("Copying " + c );
251
259
jo .putNextEntry (new JarEntry (c ));
252
260
jo .write (ji .readBytes (c ));
253
261
jo .closeEntry ();
0 commit comments