From a77b550b21645bfeda89e38c2b5287f4acdc9859 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Mon, 22 Dec 2025 21:04:59 +0200 Subject: [PATCH] Properly package index on FMLLoadCompleteEenet --- src/main/java/noppes/npcs/CommonProxy.java | 3 + src/main/java/noppes/npcs/CustomNpcs.java | 12 ++-- .../java/noppes/npcs/client/ClientProxy.java | 8 +++ .../client/gui/util/script/PackageFinder.java | 71 ++++++++++++++----- 4 files changed, 72 insertions(+), 22 deletions(-) diff --git a/src/main/java/noppes/npcs/CommonProxy.java b/src/main/java/noppes/npcs/CommonProxy.java index 58f931ab8..251272c1d 100644 --- a/src/main/java/noppes/npcs/CommonProxy.java +++ b/src/main/java/noppes/npcs/CommonProxy.java @@ -200,4 +200,7 @@ public class CommonProxy implements IGuiHandler { public boolean isGUIOpen() { return false; } + + public void buildPackageIndex() { + } } diff --git a/src/main/java/noppes/npcs/CustomNpcs.java b/src/main/java/noppes/npcs/CustomNpcs.java index ad42bc419..d20828922 100644 --- a/src/main/java/noppes/npcs/CustomNpcs.java +++ b/src/main/java/noppes/npcs/CustomNpcs.java @@ -4,12 +4,7 @@ import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.SidedProxy; -import cpw.mods.fml.common.event.FMLInitializationEvent; -import cpw.mods.fml.common.event.FMLPreInitializationEvent; -import cpw.mods.fml.common.event.FMLServerAboutToStartEvent; -import cpw.mods.fml.common.event.FMLServerStartedEvent; -import cpw.mods.fml.common.event.FMLServerStartingEvent; -import cpw.mods.fml.common.event.FMLServerStoppedEvent; +import cpw.mods.fml.common.event.*; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.EntityRegistry; import cpw.mods.fml.relauncher.Side; @@ -300,6 +295,11 @@ public class CustomNpcs { PacketHandler.Instance.registerChannels(); } + @EventHandler + public void loadComplete(FMLLoadCompleteEvent ev) { + proxy.buildPackageIndex(); + } + @EventHandler public void setAboutToStart(FMLServerAboutToStartEvent event) { globalJaninoCompiler = null; diff --git a/src/main/java/noppes/npcs/client/ClientProxy.java b/src/main/java/noppes/npcs/client/ClientProxy.java index f0346a5d3..da455f5a0 100644 --- a/src/main/java/noppes/npcs/client/ClientProxy.java +++ b/src/main/java/noppes/npcs/client/ClientProxy.java @@ -123,6 +123,7 @@ import noppes.npcs.client.gui.roles.GuiNpcTransporter; import noppes.npcs.client.gui.script.GuiScriptBlock; import noppes.npcs.client.gui.script.GuiScriptGlobal; import noppes.npcs.client.gui.script.GuiScriptItem; +import noppes.npcs.client.gui.util.script.PackageFinder; import noppes.npcs.client.model.ModelNPCGolem; import noppes.npcs.client.model.ModelNpcCrystal; import noppes.npcs.client.model.ModelNpcDragon; @@ -735,6 +736,13 @@ public class ClientProxy extends CommonProxy { return Minecraft.getMinecraft().currentScreen != null; } + public void buildPackageIndex() { + try { + PackageFinder.init(Thread.currentThread().getContextClassLoader()); + } catch (IOException e) { + } + } + public static class FontContainer { public StringCache textFont = null; public boolean useCustomFont = true; diff --git a/src/main/java/noppes/npcs/client/gui/util/script/PackageFinder.java b/src/main/java/noppes/npcs/client/gui/util/script/PackageFinder.java index b5c2fb7a4..391ea0e14 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/PackageFinder.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/PackageFinder.java @@ -20,19 +20,19 @@ import java.util.jar.JarFile; * are O(1) HashSet lookups. */ public class PackageFinder { - private static PackageFinder INSTANCE = null; + private static volatile PackageFinder INSTANCE = null; private final Set packages = new HashSet<>(); - - public static PackageFinder Instance() throws IOException { - if (INSTANCE == null) - INSTANCE = new PackageFinder(Thread.currentThread().getContextClassLoader()); - - return INSTANCE; - } + private final ClassLoader classLoader; private PackageFinder(ClassLoader cl) throws IOException { - if (cl == null) - cl = Thread.currentThread().getContextClassLoader(); + this.classLoader = cl == null ? Thread.currentThread().getContextClassLoader() : cl; + buildIndex(); + } + + private void buildIndex() throws IOException { + packages.clear(); + + ClassLoader cl = this.classLoader; // Try URLClassLoader first (common on most JVM versions used here) if (cl instanceof URLClassLoader) { @@ -147,15 +147,54 @@ public class PackageFinder { return packages.contains(pkg); } - // Convenience factory using the context classloader + public static boolean find(String pkg) { + PackageFinder pf = getInstance(); + if (pf != null) + return pf.contains(pkg); + return false; + } + + /** + * Initialize the global singleton with the provided ClassLoader (or context CL if null). + * This is idempotent; calling again will re-create the index. + */ + public static synchronized void init(ClassLoader cl) throws IOException { + if (INSTANCE == null) { + INSTANCE = new PackageFinder(cl); + } else { + // rebuild index with new classloader if provided + if (cl != null) { + INSTANCE = new PackageFinder(cl); + } else { + INSTANCE.buildIndex(); + } + } + } + + /** + * Get the initialized singleton. Call {@link #init(ClassLoader)} first. + */ + public static PackageFinder getInstance() { + if (INSTANCE == null) { + try { + INSTANCE = fromContext(); + } catch (IOException e) { + } + } + return INSTANCE; + } + + /** + * Convenience factory using the context classloader and building a standalone finder (not touching singleton). + */ public static PackageFinder fromContext() throws IOException { return new PackageFinder(Thread.currentThread().getContextClassLoader()); } - // Demo main that checks the given package (or 'kamkeel.npcdbc' by default) - public static void main(String[] args) throws Exception { - PackageFinder pf = PackageFinder.fromContext(); - String pkg = (args != null && args.length > 0) ? args[0] : "kamkeel.npcdbc"; - System.out.println(pkg + " exists? " + pf.contains(pkg)); + /** + * Reload the index using the configured ClassLoader. + */ + public synchronized void reload() throws IOException { + buildIndex(); } }