Skip to content

Commit

Permalink
Simplify access to item components
Browse files Browse the repository at this point in the history
  • Loading branch information
NGSpace committed Nov 18, 2024
1 parent 08039fa commit 64d0dc8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.RhinoException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
Expand All @@ -14,6 +15,7 @@

import io.github.ngspace.hudder.Hudder;
import io.github.ngspace.hudder.util.ObjectWrapper;
import io.github.ngspace.hudder.util.ValueGetter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand Down Expand Up @@ -49,9 +51,23 @@ public JavaScriptEngine() {
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
try {
ObjectWrapper[] vals = new ObjectWrapper[args.length];
for (int i = 0;i<args.length;i++)
for (int i = 0;i<args.length;i++) {
vals[i] = new JavaScriptValue(args[i]);
return function.exec(vals);
}
var obje = function.exec(vals);
if (obje instanceof ValueGetter r) {
return new ScriptableObject() {
private static final long serialVersionUID = -6145385781375908982L;

@Override public String getClassName() {return r.getClass().getName();}
@Override public Object get(String name, Scriptable start) {
var v = r.get(name);
if (v==null) return super.get(name, start);
return v;
}
};
}
return obje;
} catch (Exception e) {
throw new WrappedException(e);
}
Expand Down Expand Up @@ -92,17 +108,17 @@ private void insertObject(Object obj, String name) {


@Override
public Object callFunction(String name, String... args) throws IOException {
public ObjectWrapper callFunction(String name, String... args) throws IOException {
Object func = scope.get(name, scope);
if (func instanceof Function f) return f.call(cx, scope, scope, args);
if (func instanceof Function f) return new JavaScriptValue(f.call(cx, scope, scope, args));
else throw new IOException(name + " is not a function or is not defined!");
}

@Override
public Object callFunctionSafe(String name, Object defualt, String... args) throws IOException {
public ObjectWrapper callFunctionSafe(String name, Object defualt, String... args) throws IOException {
Object func = scope.get(name, scope);
if (func==null||func==Scriptable.NOT_FOUND) return defualt;
else if (func instanceof Function f) return f.call(cx, scope, scope, args);
if (func==null||func==Scriptable.NOT_FOUND) return new JavaScriptValue(defualt);
else if (func instanceof Function f) return new JavaScriptValue(f.call(cx, scope, scope, args));
else throw new IOException(name + " is not a function!");
}

Expand Down Expand Up @@ -141,7 +157,10 @@ public void showToast(String title, String content) {

private class JavaScriptValue extends ObjectWrapper {
private Object value;
private JavaScriptValue(Object value) {this.value=value;}
private JavaScriptValue(Object value) {
this.value=value;
if (value instanceof NativeJavaObject o) {this.value = o.unwrap();}
}

@Override public Object get() throws CompileException {return value;}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import io.github.ngspace.hudder.util.HudCompilationManager;
import io.github.ngspace.hudder.util.HudFileUtils;
import io.github.ngspace.hudder.util.ObjectWrapper;
import io.github.ngspace.hudder.util.ValueGetter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.component.ComponentMap;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

Expand Down Expand Up @@ -152,6 +154,7 @@ public void applyFunctions(FunctionBinder binder) {

binder.bindFunction((m,c,s)->HudFileUtils.exists(s[0].asString()),"exists");
binder.bindFunction((m,c,s)->mc.textRenderer.getWidth(s[0].asString()), "strWidth", "strwidth");
binder.bindFunction((m,c,s)->s[0].get().toString(), "toString");
}


Expand All @@ -173,14 +176,13 @@ public void applyFunctions(FunctionBinder binder) {
}


public static class TranslatedItemStack {
public static class TranslatedItemStack implements ValueGetter {
public String name;
public int count;
public int maxcount;
public int durability;
public int maxdurability;
public Object[] enchantments;
public ComponentMap components;
private ComponentMap components;
public TranslatedItemStack(ItemStack stack) {
name = stack.getItemName().getString();
count = stack.getCount();
Expand All @@ -194,5 +196,8 @@ public TranslatedItemStack(ItemStack stack) {
return "{name:\"" + name + "\", count:" + count + ", maxcount: " + maxcount + ", durability: " + durability
+ ", maxdurability: " + maxdurability + "}";
}
@Override public Object get(String Id) {
return components.get(Registries.DATA_COMPONENT_TYPE.get(Identifier.of(Id)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ObjectData {private ObjectData() {}

public static Object getObject(String key) {
return switch (key) {

case "componenttypes": yield (ValueGetter) name->Registries.DATA_COMPONENT_TYPE.get(Identifier.of(name));

default: yield null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Set;

import io.github.ngspace.hudder.Hudder;
import io.github.ngspace.hudder.compilers.utils.CompileException;
Expand Down Expand Up @@ -52,8 +53,16 @@ protected V2ClassPropertyCall(int line, int charpos, String v, AV2Compiler c, V2
if (forbidden.equals(fieldName)) throw new CompileException("No property named \""+fieldName+'"',line,charpos);
}
}
@Override
public Object get() throws CompileException {
Object obj = smartGet();
if (obj instanceof Set<?> r) {
obj = r.toArray();
}
return obj;
}

@Override public Object get() throws CompileException {
public Object smartGet() throws CompileException {

Object objValue = classobj.get();
if (objValue==null)
Expand All @@ -76,7 +85,6 @@ protected V2ClassPropertyCall(int line, int charpos, String v, AV2Compiler c, V2
for (Method method : objClass.getMethods()) {
if (!funcName.equals(method.getName())||method.getParameterCount()!=classes.length
||!isAccessible(method)) continue;
method.setAccessible(true);
boolean isCompatible = true;
var v = method.getParameterTypes();

Expand Down

0 comments on commit 64d0dc8

Please sign in to comment.