mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-17 22:45:09 +00:00
Properly package index on FMLLoadCompleteEenet
This commit is contained in:
1 parent
242d9fa1cb
commit
a77b550b21
4 files changed
+72
-22
No files matched your search
@@ -200,4 +200,7 @@ public class CommonProxy implements IGuiHandler {
|
||||
public boolean isGUIOpen() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void buildPackageIndex() {
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<String> 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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user