Compress Script Hooks and Context System

This commit is contained in:
Kam committed 2026-01-22 14:27:48 -05:00
1 parent e2ca76e626
commit 19b01e2099
17 files changed
+595 -770

No files matched your search

@@ -16,10 +16,13 @@ import java.util.concurrent.ConcurrentHashMap;
* - ICustomGuiEvent (custom GUI hooks)
* - etc.
*
* Each context also has a hookContext string used by ScriptHookController for
* hook registration and lookup.
*
* This is a registerable system - any mod can register their own contexts:
*
* // Register a new context with multiple namespaces:
* ScriptContext.register("DBC", "IDBCEvent", "IDBCPlayerEvent", "IDBCFormEvent");
* ScriptContext.register("DBC", "dbc", IDBCEvent.class, IDBCPlayerEvent.class);
*
* // Or add namespaces to an existing context:
* ScriptContext.PLAYER.addNamespace("IDBCPlayerEvent");
@@ -31,12 +34,12 @@ public class ScriptContext {
// ==================== BUILT-IN CONTEXTS ====================
public static final ScriptContext NPC = register("NPC",
public static final ScriptContext NPC = register("NPC", "npc",
INpcEvent.class,
IProjectileEvent.class
);
public static final ScriptContext PLAYER = register("PLAYER",
public static final ScriptContext PLAYER = register("PLAYER", "player",
IPlayerEvent.class,
IAnimationEvent.class,
IPartyEvent.class,
@@ -46,19 +49,31 @@ public class ScriptContext {
ICustomGuiEvent.class
);
public static final ScriptContext BLOCK = register("BLOCK",
public static final ScriptContext BLOCK = register("BLOCK", "block",
IBlockEvent.class
);
public static final ScriptContext ITEM = register("ITEM",
public static final ScriptContext ITEM = register("ITEM", "item",
IItemEvent.class
);
public static final ScriptContext FORGE = register("FORGE",
public static final ScriptContext FORGE = register("FORGE", "forge",
IForgeEvent.class
);
public static final ScriptContext GLOBAL = register("GLOBAL",
public static final ScriptContext LINKED_ITEM = register("LINKED_ITEM", "linked_item",
IItemEvent.class
);
public static final ScriptContext RECIPE = register("RECIPE", "recipe",
"Recipe"
);
public static final ScriptContext EFFECT = register("EFFECT", "effect",
"Effect"
);
public static final ScriptContext GLOBAL = register("GLOBAL", "",
"Global" // Special case: Global namespace doesn't have a corresponding event class
);
@@ -67,13 +82,17 @@ public class ScriptContext {
/** Unique identifier for this context (e.g., "NPC", "PLAYER", "DBC") */
public final String id;
/** Hook context identifier used by ScriptHookController (e.g., "npc", "player") */
public final String hookContext;
/** The event interface namespaces this context supports */
private final List<String> namespaces;
// ==================== CONSTRUCTOR ====================
private ScriptContext(String id, String... namespaces) {
private ScriptContext(String id, String hookContext, String... namespaces) {
this.id = id;
this.hookContext = hookContext != null ? hookContext : "";
this.namespaces = new ArrayList<>(Arrays.asList(namespaces));
}
@@ -123,14 +142,15 @@ public class ScriptContext {
// ==================== REGISTRATION API ====================
/**
* Register a new script context with one or more namespaces.
* Register a new script context with a hook context and one or more namespaces.
*
* @param id Unique identifier (e.g., "DBC", "CUSTOM")
* @param hookContext The hook context string for ScriptHookController (e.g., "dbc")
* @param namespaces The event interface names (e.g., "IDBCEvent", "IDBCPlayerEvent")
* @return The registered ScriptContext
*/
public static ScriptContext register(String id, String... namespaces) {
ScriptContext context = new ScriptContext(id, namespaces);
public static ScriptContext register(String id, String hookContext, String... namespaces) {
ScriptContext context = new ScriptContext(id, hookContext, namespaces);
REGISTRY.put(id, context);
return context;
}
@@ -140,18 +160,19 @@ public class ScriptContext {
* Automatically extracts the simple name from each class.
*
* Example:
* ScriptContext.register("NPC", INpcEvent.class, IProjectileEvent.class);
* ScriptContext.register("NPC", "npc", INpcEvent.class, IProjectileEvent.class);
*
* @param id Unique identifier (e.g., "DBC", "CUSTOM")
* @param hookContext The hook context string for ScriptHookController (e.g., "npc")
* @param eventClasses The event interface classes (simple names will be extracted)
* @return The registered ScriptContext
*/
public static ScriptContext register(String id, Class<?>... eventClasses) {
public static ScriptContext register(String id, String hookContext, Class<?>... eventClasses) {
String[] namespaces = new String[eventClasses.length];
for (int i = 0; i < eventClasses.length; i++) {
namespaces[i] = eventClasses[i].getSimpleName();
}
return register(id, namespaces);
return register(id, hookContext, namespaces);
}
/**
@@ -180,6 +201,22 @@ public class ScriptContext {
return GLOBAL;
}
/**
* Find a script context by its hook context string.
*
* @param hookContext The hook context string (e.g., "npc", "player")
* @return The ScriptContext with that hook context, or GLOBAL if not found
*/
public static ScriptContext byHookContext(String hookContext) {
if (hookContext == null || hookContext.isEmpty()) return GLOBAL;
for (ScriptContext ctx : REGISTRY.values()) {
if (hookContext.equals(ctx.hookContext)) {
return ctx;
}
}
return GLOBAL;
}
/**
* Get all registered script contexts.
*