Potential Fix to Usuable Type names

This commit is contained in:
Kam committed 2026-01-22 16:59:13 -05:00
1 parent 8da42e7b7c
commit 638c915965
4 files changed
+76 -10

No files matched your search

@@ -229,14 +229,24 @@ public class ScriptHookController implements IScriptHookHandler {
}
}
@Override
public void registerHooks(ScriptContext context, String functionName) {
/**
* Convenience method to register a hook using ScriptContext.
* @param context The script context
* @param functionName The hook function name
*/
public void registerHook(ScriptContext context, String functionName) {
registerHook(context.hookContext, functionName);
}
@Override
/**
* Convenience method to register multiple hooks using ScriptContext.
* @param context The script context
* @param functionNames The hook function names
*/
public void registerHooks(ScriptContext context, String... functionNames) {
registerHooks(context.hookContext, functionNames);
for (String fn : functionNames) {
registerHook(context.hookContext, fn);
}
}
@Override
@@ -344,6 +344,7 @@ public abstract class JaninoScript<T> implements IScriptUnit {
/**
* Generates a stub string for a given Method.
* Handles nested classes properly (e.g., IPlayerEvent.InitEvent instead of just InitEvent).
*/
public static String generateMethodStub(Method method) {
String mods = Modifier.toString(method.getModifiers());
@@ -352,13 +353,14 @@ public abstract class JaninoScript<T> implements IScriptUnit {
if (!mods.isEmpty())
mods += " ";
String returnTypeStr = method.getReturnType().getSimpleName();
String returnTypeStr = getUsableTypeName(method.getReturnType());
String name = method.getName();
Map<String, Integer> typeCount = new HashMap<>();
String params = Arrays.stream(method.getParameters())
.map(p -> {
String typeName = p.getType().getSimpleName();
String typeName = getUsableTypeName(p.getType());
String simpleTypeName = p.getType().getSimpleName();
// Check for @ParamName annotation first
ParamName paramNameAnnotation = p.getAnnotation(ParamName.class);
@@ -368,8 +370,8 @@ public abstract class JaninoScript<T> implements IScriptUnit {
// Use annotated name
paramName = paramNameAnnotation.value();
} else {
// Fall back to generated name
String baseName = Character.toLowerCase(typeName.charAt(0)) + typeName.substring(1);
// Fall back to generated name based on simple name
String baseName = Character.toLowerCase(simpleTypeName.charAt(0)) + simpleTypeName.substring(1);
int count = typeCount.getOrDefault(baseName, 0) + 1;
typeCount.put(baseName, count);
paramName = count == 1 ? baseName : baseName + (count - 1);
@@ -397,6 +399,30 @@ public abstract class JaninoScript<T> implements IScriptUnit {
return String.format("%s%s %s(%s) {\n%s\n}\n", mods, returnTypeStr, name, params, body);
}
/**
* Gets a usable type name for stub generation.
* For nested classes like IPlayerEvent.InitEvent, returns "IPlayerEvent.InitEvent"
* For top-level classes, returns the simple name.
* For primitives, returns the primitive name.
*/
private static String getUsableTypeName(Class<?> type) {
if (type.isPrimitive() || type.getEnclosingClass() == null) {
return type.getSimpleName();
}
// Build the nested class chain: OuterClass.InnerClass.DeeperClass
StringBuilder sb = new StringBuilder();
Class<?> current = type;
while (current != null) {
if (sb.length() > 0) {
sb.insert(0, ".");
}
sb.insert(0, current.getSimpleName());
current = current.getEnclosingClass();
}
return sb.toString();
}
public List<String> getHookList() {
return hookResolver.getDisplayHookList();
@@ -14,8 +14,21 @@ import noppes.npcs.janino.annotations.ScriptHook;
*/
public class JaninoNpcScript extends JaninoScript<JaninoNpcScript.Functions> {
/**
* Default imports for NPC scripts.
* These are automatically available in user code without explicit import statements.
*/
private static final String[] DEFAULT_IMPORTS = {
"noppes.npcs.api.event.INpcEvent",
"noppes.npcs.api.event.IProjectileEvent",
"noppes.npcs.api.event.IAbilityEvent",
"noppes.npcs.api.entity.ICustomNpc",
"noppes.npcs.api.entity.IPlayer",
"noppes.npcs.api.NpcAPI"
};
public JaninoNpcScript() {
super(Functions.class, null);
super(Functions.class, DEFAULT_IMPORTS);
}
/**
@@ -40,8 +40,25 @@ import noppes.npcs.janino.annotations.ScriptHook;
*/
public class JaninoPlayerScript extends JaninoScript<JaninoPlayerScript.Functions> {
/**
* Default imports for player scripts.
* These are automatically available in user code without explicit import statements.
*/
private static final String[] DEFAULT_IMPORTS = {
"noppes.npcs.api.event.IPlayerEvent",
"noppes.npcs.api.event.IQuestEvent",
"noppes.npcs.api.event.IDialogEvent",
"noppes.npcs.api.event.IFactionEvent",
"noppes.npcs.api.event.IPartyEvent",
"noppes.npcs.api.event.ICustomGuiEvent",
"noppes.npcs.api.event.IAnimationEvent",
"noppes.npcs.api.event.ICustomNPCsEvent",
"noppes.npcs.api.entity.IPlayer",
"noppes.npcs.api.NpcAPI"
};
public JaninoPlayerScript() {
super(Functions.class, null);
super(Functions.class, DEFAULT_IMPORTS, false);
}
/**