Hooked script fields/methods up properly with modifiers and fixed FINAL assignment errors

This commit is contained in:
bigguy345 committed 2025-12-27 20:21:20 +02:00
1 parent 9e21e4b47f
commit 1786d215ad
2 files changed
+137 -20

No files matched your search

@@ -20,13 +20,13 @@ public final class MethodInfo {
private final int bodyEnd; // End of method body (before })
private final boolean resolved;
private final boolean isDeclaration; // true if this is a declaration, false if it's a call
private final boolean isStatic; // true if this is a static method
private final int modifiers; // Java Modifier flags (e.g., Modifier.PUBLIC | Modifier.STATIC)
private final String documentation; // Javadoc/comment documentation for this method
private MethodInfo(String name, TypeInfo returnType, TypeInfo containingType,
List<FieldInfo> parameters, int declarationOffset,
int bodyStart, int bodyEnd, boolean resolved, boolean isDeclaration,
boolean isStatic, String documentation) {
int modifiers, String documentation) {
this.name = name;
this.returnType = returnType;
this.containingType = containingType;
@@ -36,24 +36,31 @@ public final class MethodInfo {
this.bodyEnd = bodyEnd;
this.resolved = resolved;
this.isDeclaration = isDeclaration;
this.isStatic = isStatic;
this.modifiers = modifiers;
this.documentation = documentation;
}
// Factory methods
public static MethodInfo declaration(String name, TypeInfo returnType, List<FieldInfo> params,
int declOffset, int bodyStart, int bodyEnd) {
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, false, null);
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, 0, null);
}
public static MethodInfo declaration(String name, TypeInfo returnType, List<FieldInfo> params,
int declOffset, int bodyStart, int bodyEnd, boolean isStatic) {
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, isStatic, null);
int modifiers = isStatic ? Modifier.STATIC : 0;
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, modifiers, null);
}
public static MethodInfo declaration(String name, TypeInfo returnType, List<FieldInfo> params,
int declOffset, int bodyStart, int bodyEnd, boolean isStatic, String documentation) {
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, isStatic, documentation);
int modifiers = isStatic ? Modifier.STATIC : 0;
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, modifiers, documentation);
}
public static MethodInfo declaration(String name, TypeInfo returnType, List<FieldInfo> params,
int declOffset, int bodyStart, int bodyEnd, int modifiers, String documentation) {
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, modifiers, documentation);
}
public static MethodInfo call(String name, TypeInfo containingType, int paramCount) {
@@ -63,7 +70,7 @@ public final class MethodInfo {
for (int i = 0; i < paramCount; i++) {
params.add(FieldInfo.unresolved("arg" + i, FieldInfo.Scope.PARAMETER));
}
return new MethodInfo(name, null, containingType, params, -1, -1, -1, resolved, false, false, null);
return new MethodInfo(name, null, containingType, params, -1, -1, -1, resolved, false, 0, null);
}
public static MethodInfo unresolvedCall(String name, int paramCount) {
@@ -71,7 +78,7 @@ public final class MethodInfo {
for (int i = 0; i < paramCount; i++) {
params.add(FieldInfo.unresolved("arg" + i, FieldInfo.Scope.PARAMETER));
}
return new MethodInfo(name, null, null, params, -1, -1, -1, false, false, false, null);
return new MethodInfo(name, null, null, params, -1, -1, -1, false, false, 0, null);
}
/**
@@ -81,7 +88,7 @@ public final class MethodInfo {
public static MethodInfo fromReflection(java.lang.reflect.Method method, TypeInfo containingType) {
String name = method.getName();
TypeInfo returnType = TypeInfo.fromClass(method.getReturnType());
boolean isStatic = Modifier.isStatic(method.getModifiers());
int modifiers = method.getModifiers();
List<FieldInfo> params = new ArrayList<>();
Class<?>[] paramTypes = method.getParameterTypes();
@@ -90,7 +97,7 @@ public final class MethodInfo {
params.add(FieldInfo.reflectionParam("arg" + i, paramType));
}
return new MethodInfo(name, returnType, containingType, params, -1, -1, -1, true, false, isStatic, null);
return new MethodInfo(name, returnType, containingType, params, -1, -1, -1, true, false, modifiers, null);
}
// Getters
@@ -105,7 +112,15 @@ public final class MethodInfo {
public boolean isResolved() { return resolved; }
public boolean isDeclaration() { return isDeclaration; }
public boolean isCall() { return !isDeclaration; }
public boolean isStatic() { return isStatic; }
public int getModifiers() { return modifiers; }
public boolean isStatic() { return Modifier.isStatic(modifiers); }
public boolean isFinal() { return Modifier.isFinal(modifiers); }
public boolean isAbstract() { return Modifier.isAbstract(modifiers); }
public boolean isSynchronized() { return Modifier.isSynchronized(modifiers); }
public boolean isNative() { return Modifier.isNative(modifiers); }
public boolean isPublic() { return Modifier.isPublic(modifiers); }
public boolean isPrivate() { return Modifier.isPrivate(modifiers); }
public boolean isProtected() { return Modifier.isProtected(modifiers); }
public String getDocumentation() { return documentation; }
/**
@@ -1,5 +1,6 @@
package noppes.npcs.client.gui.util.script.interpreter;
import net.minecraft.client.Minecraft;
import noppes.npcs.client.ClientProxy;
import java.util.*;
@@ -385,9 +386,13 @@ public class ScriptDocument {
int absPos = bodyStart + 1 + fm.start();
if (isExcluded(absPos)) continue;
String typeName = fm.group(1).trim();
String typeNameRaw = fm.group(1).trim();
String fieldName = fm.group(2);
// Parse modifiers and strip them
int modifiers = parseModifiers(typeNameRaw);
String typeName = stripModifiers(typeNameRaw);
// Skip if this looks like a method declaration
if (typeName.equals("return") || typeName.equals("if") || typeName.equals("while") ||
typeName.equals("for") || typeName.equals("switch") || typeName.equals("catch") ||
@@ -404,7 +409,7 @@ public class ScriptDocument {
String documentation = extractDocumentationBefore(absPos);
TypeInfo fieldType = resolveType(typeName);
FieldInfo fieldInfo = FieldInfo.globalField(fieldName, fieldType, absPos, documentation);
FieldInfo fieldInfo = FieldInfo.globalField(fieldName, fieldType, absPos, documentation, -1, -1, modifiers);
scriptType.addField(fieldInfo);
}
@@ -433,12 +438,15 @@ public class ScriptDocument {
// Extract documentation before this method
String documentation = extractDocumentationBefore(absPos);
// Extract modifiers by scanning backwards in bodyText from match start
int modifiers = extractModifiersBackwards(mm.start() - 1, bodyText);
TypeInfo returnType = resolveType(returnTypeName);
List<FieldInfo> params = parseParametersWithPositions(paramList,
bodyStart + 1 + mm.start(3));
MethodInfo methodInfo = MethodInfo.declaration(
methodName, returnType, params, absPos, methodBodyStart, methodBodyEnd, false, documentation);
methodName, returnType, params, absPos, methodBodyStart, methodBodyEnd, modifiers, documentation);
scriptType.addMethod(methodInfo);
}
}
@@ -488,6 +496,9 @@ public class ScriptDocument {
// Extract documentation before this method
String documentation = extractDocumentationBefore(m.start());
// Extract modifiers by scanning backwards from match start
int modifiers = extractModifiersBackwards(m.start() - 1, text);
// Parse parameters with their actual positions
List<FieldInfo> params = parseParametersWithPositions(paramList, m.start(3));
@@ -498,7 +509,7 @@ public class ScriptDocument {
m.start(),
bodyStart,
bodyEnd,
false,
modifiers,
documentation
);
methods.add(methodInfo);
@@ -578,6 +589,9 @@ public class ScriptDocument {
continue;
}
// Parse modifiers from the raw type declaration
int modifiers = parseModifiers(typeName);
TypeInfo typeInfo;
// For var/let/const, infer type from the right-hand side expression
@@ -611,7 +625,7 @@ public class ScriptDocument {
}
int declPos = bodyStart + m.start(2);
FieldInfo fieldInfo = FieldInfo.localField(varName, typeInfo, declPos, method, initStart, initEnd);
FieldInfo fieldInfo = FieldInfo.localField(varName, typeInfo, declPos, method, initStart, initEnd, modifiers);
locals.put(varName, fieldInfo);
}
}
@@ -810,6 +824,9 @@ public class ScriptDocument {
if (isExcluded(position))
continue;
// Parse modifiers from the raw type declaration
int modifiers = parseModifiers(typeNameRaw);
// Strip modifiers (public, private, protected, static, final, etc.) from type name
String typeName = stripModifiers(typeNameRaw);
@@ -847,7 +864,7 @@ public class ScriptDocument {
}
TypeInfo typeInfo = resolveType(typeName);
FieldInfo fieldInfo = FieldInfo.globalField(fieldName, typeInfo, position, documentation, initStart, initEnd);
FieldInfo fieldInfo = FieldInfo.globalField(fieldName, typeInfo, position, documentation, initStart, initEnd, modifiers);
globalFields.put(fieldName, fieldInfo);
}
}
@@ -872,6 +889,81 @@ public class ScriptDocument {
return result.toString();
}
/**
* Parse modifiers from a declaration string and return the corresponding Modifier flags.
* e.g., "public static final" -> Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL
*/
private int parseModifiers(String declaration) {
if (declaration == null) return 0;
int modifiers = 0;
String[] parts = declaration.trim().split("\\s+");
for (String part : parts) {
if (part.equals("public")) modifiers |= java.lang.reflect.Modifier.PUBLIC;
else if (part.equals("private")) modifiers |= java.lang.reflect.Modifier.PRIVATE;
else if (part.equals("protected")) modifiers |= java.lang.reflect.Modifier.PROTECTED;
else if (part.equals("static")) modifiers |= java.lang.reflect.Modifier.STATIC;
else if (part.equals("final")) modifiers |= java.lang.reflect.Modifier.FINAL;
else if (part.equals("abstract")) modifiers |= java.lang.reflect.Modifier.ABSTRACT;
else if (part.equals("synchronized")) modifiers |= java.lang.reflect.Modifier.SYNCHRONIZED;
else if (part.equals("volatile")) modifiers |= java.lang.reflect.Modifier.VOLATILE;
else if (part.equals("transient")) modifiers |= java.lang.reflect.Modifier.TRANSIENT;
else if (part.equals("native")) modifiers |= java.lang.reflect.Modifier.NATIVE;
else if (part.equals("strictfp")) modifiers |= java.lang.reflect.Modifier.STRICT;
}
return modifiers;
}
/**
* Scan backwards from scanStart position in the given text to extract modifier keywords.
* Returns the parsed modifier flags.
*
* @param scanStart The position to start scanning backwards from (exclusive)
* @param sourceText The text to scan in
* @return The combined modifier flags
*/
private int extractModifiersBackwards(int scanStart, String sourceText) {
// Skip whitespace
while (scanStart >= 0 && Character.isWhitespace(sourceText.charAt(scanStart))) {
scanStart--;
}
// Scan backwards collecting modifier words
StringBuilder modifiersText = new StringBuilder();
while (scanStart >= 0) {
// Skip whitespace
while (scanStart >= 0 && Character.isWhitespace(sourceText.charAt(scanStart))) {
scanStart--;
}
if (scanStart < 0) break;
// Read a word backwards
int wordEnd = scanStart + 1;
while (scanStart >= 0 && Character.isJavaIdentifierPart(sourceText.charAt(scanStart))) {
scanStart--;
}
int wordStart = scanStart + 1;
if (wordStart < wordEnd) {
String word = sourceText.substring(wordStart, wordEnd);
// Check if it's a modifier
if (TypeResolver.isModifier(word)) {
if (modifiersText.length() > 0) {
modifiersText.insert(0, " ");
}
modifiersText.insert(0, word);
} else {
// Hit a non-modifier word, stop scanning
break;
}
} else {
break;
}
}
// Parse the collected modifiers
return parseModifiers(modifiersText.toString());
}
/**
* Extract documentation comment immediately preceding a position.
* Supports both single-line (//) and multi-line (/* *\/) comment styles.
@@ -2949,6 +3041,8 @@ public class ScriptDocument {
reflectionField = targetField.getReflectionField();
}
}
// Minecraft.getMinecraft().thePlayer.PERSISTED_NBT_TAG = null;
} else {
// Simple variable
targetField = resolveVariable(targetName, lhsStart);
@@ -2957,7 +3051,7 @@ public class ScriptDocument {
reflectionField = targetField.getReflectionField();
}
}
// Resolve the source type (RHS)
TypeInfo sourceType = resolveExpressionType(rhs, equalsPos + 1);
@@ -2967,6 +3061,13 @@ public class ScriptDocument {
(globalFields.containsValue(targetField) ||
methodLocals.values().stream().anyMatch(m -> m.containsValue(finalTargetField)));
// Determine if the target field is final
// For script fields, use the modifiers; for external fields, use reflection
boolean isFinal = false;
if (targetField != null) {
isFinal = targetField.isFinal();
}
// Create the assignment info using the new constructor
AssignmentInfo info = new AssignmentInfo(
targetName,
@@ -2980,7 +3081,7 @@ public class ScriptDocument {
rhs,
receiverType,
reflectionField,
targetField != null ? targetField.isFinal() : false
isFinal
);
// Validate the assignment
@@ -3054,6 +3155,7 @@ public class ScriptDocument {
TypeInfo sourceType = resolveExpressionType(rhs, rhsStart);
// Create the assignment info
// Declaration assignments should NOT check final status - this is the one place where final fields can be assigned
AssignmentInfo info = new AssignmentInfo(
varName,
stmtStart,
@@ -3066,7 +3168,7 @@ public class ScriptDocument {
rhs,
null, // No receiver for simple declarations
targetField.getReflectionField(),
targetField.isFinal()
false // Don't flag as final for declaration assignments - initial assignment is always allowed
);
// Validate the assignment