Skip to content

Commit 9127186

Browse files
committed
[-] Remove debug outputs in plugin loader
1 parent adbb055 commit 9127186

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/main/java/org/hydev/mcpm/client/loader/PluginLoader.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
import java.net.URLClassLoader;
2323
import java.nio.charset.StandardCharsets;
2424
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.*;
2926
import java.util.jar.JarEntry;
3027
import java.util.jar.JarOutputStream;
3128
import java.util.zip.ZipEntry;
@@ -38,6 +35,7 @@
3835
*/
3936
public record PluginLoader(LocalJarBoundary jarFinder) implements LoadBoundary, UnloadBoundary, ReloadBoundary
4037
{
38+
private static final boolean DEBUG = false;
4139
private static final File HELPER_JAR = new File("plugins/mcpm-helper.jar");
4240

4341
public PluginLoader
@@ -46,6 +44,16 @@ public record PluginLoader(LocalJarBoundary jarFinder) implements LoadBoundary,
4644
deleteHelper();
4745
}
4846

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+
4957
@Override
5058
public boolean loadPlugin(String name) throws PluginNotFoundException
5159
{
@@ -136,25 +144,25 @@ static File unloadPlugin(Plugin plugin)
136144
.filter(cmd -> cmd.getPlugin() == plugin).toList().forEach(cmd -> {
137145
cmd.unregister(cmdMap);
138146
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")
141149
);
142150

143151
// 5. Remove plugin from list
144152
getPrivateField(pm, "plugins", new TypeToken<List<Plugin>>(){})
145153
.ifPresentOrElse(plugins -> plugins.remove(plugin),
146-
() -> System.err.println("plugins cannot be accessed"));
154+
() -> debug("plugins cannot be accessed"));
147155

148156
// 6. Remove lookup name
149157
getPrivateField(pm, "lookupNames", new TypeToken<Map<String, Plugin>>(){})
150158
.ifPresentOrElse(names -> names.remove(plugin.getName()),
151-
() -> System.err.println("lookupNames cannot be accessed"));
159+
() -> debug("lookupNames cannot be accessed"));
152160

153161
// 7. Unload Java classes so that the jar can be deleted on Windows
154162
if (plugin.getClass().getClassLoader() instanceof URLClassLoader cl)
155163
{
156164
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");
158166

159167
// Close classloader
160168
try
@@ -198,8 +206,8 @@ private void reloadSelf()
198206
// 4. Reflect! Since MCPM and MCPM-Helper are in different class loaders, I cannot call/cast it directly
199207
// or else it would give me a nonsense error "class PluginLoaderHelper cannot be cast to PluginLoaderHelper"
200208
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()));
203211
var reload = cls.getDeclaredMethod("reloadMcpm", Plugin.class, File.class);
204212
reload.setAccessible(true);
205213
reload.invoke(pl, SpigotEntry.instance(), jar);
@@ -223,7 +231,7 @@ private Plugin injectHelper()
223231
{
224232
// 1. Get the class bytecode from the jar
225233
var jar = new File(PluginLoader.class.getProtectionDomain().getCodeSource().getLocation().toURI());
226-
System.out.println(jar);
234+
debug(jar);
227235
try (var ji = new PluginJarFile(jar))
228236
{
229237
var yml = """
@@ -241,13 +249,13 @@ private Plugin injectHelper()
241249
it.contains("org/hydev/mcpm/utils/PluginJarFile")).toList();
242250

243251
// 2. Create a new jar file
244-
System.out.println("Creating jar file...");
252+
debug("Creating jar file...");
245253
try (var jo = new JarOutputStream(new FileOutputStream(HELPER_JAR)))
246254
{
247255
// Copy classes
248256
for (String c : classes)
249257
{
250-
System.out.println("Copying " + c);
258+
debug("Copying " + c);
251259
jo.putNextEntry(new JarEntry(c));
252260
jo.write(ji.readBytes(c));
253261
jo.closeEntry();

0 commit comments

Comments
 (0)