mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-20 04:09:57 +00:00
HookDefinition requiredImports now has both enclosed type & enclosing type i.e "IPlayerEvent" & "IPlayerEvent.UpdateEvent"'
Cached all collected imports in JaninoScript
This commit is contained in:
1 parent
96239118e7
commit
190fa5431f
2 files changed
+33
-18
No files matched your search
@@ -148,9 +148,9 @@ public class HookDefinition implements IHookDefinition {
|
||||
}
|
||||
|
||||
// Build required imports from event type
|
||||
String importName = getImportForClass(eventType);
|
||||
if (importName != null) {
|
||||
builder.requiredImports(importName);
|
||||
String[] importNames = getImportsForClass(eventType);
|
||||
if (importNames.length > 0) {
|
||||
builder.requiredImports(importNames);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,26 +185,31 @@ public class HookDefinition implements IHookDefinition {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the import statement needed for a class.
|
||||
* For nested classes, returns the enclosing class.
|
||||
* Get the import statements needed for a class.
|
||||
* For nested classes, includes both enclosing and nested names.
|
||||
*/
|
||||
private static String getImportForClass(Class<?> clazz) {
|
||||
private static String[] getImportsForClass(Class<?> clazz) {
|
||||
if (clazz == null || clazz.isPrimitive()) {
|
||||
return null;
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
String fullName = clazz.getName();
|
||||
if (fullName.startsWith("java.lang.")) {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
// For nested classes, get the top-level enclosing class
|
||||
Class<?> enclosing = clazz;
|
||||
while (enclosing.getEnclosingClass() != null) {
|
||||
enclosing = enclosing.getEnclosingClass();
|
||||
}
|
||||
|
||||
String name = enclosing.getName();
|
||||
if (name.startsWith("java.lang.")) {
|
||||
return null;
|
||||
String enclosingName = enclosing.getName();
|
||||
String nestedName = fullName.replace('$', '.');
|
||||
if (!nestedName.equals(enclosingName)) {
|
||||
return new String[]{enclosingName, nestedName};
|
||||
}
|
||||
|
||||
return name;
|
||||
return new String[]{enclosingName};
|
||||
}
|
||||
|
||||
// ==================== Builder ====================
|
||||
@@ -231,9 +236,9 @@ public class HookDefinition implements IHookDefinition {
|
||||
if (clazz != null) {
|
||||
this.eventClassName = clazz.getName();
|
||||
|
||||
String importName = getImportForClass(clazz);
|
||||
if (importName != null) {
|
||||
this.requiredImports = new String[]{importName};
|
||||
String[] importNames = getImportsForClass(clazz);
|
||||
if (importNames.length > 0) {
|
||||
this.requiredImports = importNames;
|
||||
}
|
||||
|
||||
if (clazz.isAnnotationPresent(Cancelable.class)) {
|
||||
|
||||
@@ -43,7 +43,9 @@ public abstract class JaninoScript<T> implements IScriptUnit {
|
||||
|
||||
private final JaninoHookResolver hookResolver = new JaninoHookResolver();
|
||||
private final String[] defaultImports;
|
||||
|
||||
// Cache of imports used in the last compilation
|
||||
private String[] cachedImports;
|
||||
|
||||
private Map<String, IHookDefinition> hookDefCache;
|
||||
private int lastHookRevision = -1;
|
||||
private int lastSeenGlobalRevision;
|
||||
@@ -122,8 +124,7 @@ public abstract class JaninoScript<T> implements IScriptUnit {
|
||||
|
||||
public void compileScript(String code) {
|
||||
try {
|
||||
String[] imports = collectImportsForCode(code);
|
||||
builder.setDefaultImports(imports);
|
||||
builder.setDefaultImports(cachedImports = collectImportsForCode(code));
|
||||
this.scriptBody = builder.build();
|
||||
scriptBody.setScript(code);
|
||||
} catch (InternalCompilerException e) {
|
||||
@@ -400,6 +401,13 @@ public abstract class JaninoScript<T> implements IScriptUnit {
|
||||
return defaultImports;
|
||||
}
|
||||
|
||||
private String[] getCachedImports() {
|
||||
if (cachedImports == null) {
|
||||
cachedImports = collectImportsForCode(getFullCode());
|
||||
}
|
||||
return cachedImports;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all types used in hook method signatures (parameters and return types).
|
||||
* This includes event types like INpcEvent.InitEvent, INpcEvent.DamagedEvent, etc.,
|
||||
@@ -453,6 +461,7 @@ public abstract class JaninoScript<T> implements IScriptUnit {
|
||||
public void setScript(String script) {
|
||||
this.script = script;
|
||||
this.evaluated = false;
|
||||
this.cachedImports = null;
|
||||
hookResolver.clearResolutionCaches();
|
||||
}
|
||||
|
||||
@@ -465,6 +474,7 @@ public abstract class JaninoScript<T> implements IScriptUnit {
|
||||
public void setExternalScripts(List<String> scripts) {
|
||||
this.externalScripts = scripts;
|
||||
this.evaluated = false;
|
||||
this.cachedImports = null;
|
||||
hookResolver.clearResolutionCaches();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user