Skip to content

Commit dcb4d94

Browse files
committed
Merge remote-tracking branch 'origin/fabric' into fabric
2 parents 655e405 + 7502f5d commit dcb4d94

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

src/main/java/net/earthcomputer/clientcommands/script/ScriptBuiltins.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ static void addBuiltinVariables(ScriptEngine engine) {
6565
@Override
6666
public Object newObject(Object... args) {
6767
if (args.length < 1 || args.length > 2)
68-
throw new UnsupportedOperationException("new Thread() called with wrong number of arguments");
69-
JSObject action;
70-
if (!(args[0] instanceof JSObject) || !(action = (JSObject) args[0]).isFunction())
71-
throw new IllegalArgumentException("action is not a function");
68+
throw new UnsupportedOperationException("new Thread() called with " + args.length + " arguments, but expected 1 or 2");
69+
JSObject action = ScriptUtil.asFunction(args[0]);
7270
boolean daemon = args.length < 2 ? true : (Boolean) ScriptUtils.convert(args[1], Boolean.class);
7371
return ScriptManager.createThread(() -> {
7472
action.call(null);

src/main/java/net/earthcomputer/clientcommands/script/ScriptInventory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.earthcomputer.clientcommands.script;
22

3-
import jdk.nashorn.api.scripting.ScriptObjectMirror;
3+
import jdk.nashorn.api.scripting.JSObject;
44
import jdk.nashorn.api.scripting.ScriptUtils;
55
import net.earthcomputer.clientcommands.interfaces.ISlot;
66
import net.minecraft.client.MinecraftClient;
@@ -66,7 +66,7 @@ public void click(Integer slot) {
6666
click(slot, null);
6767
}
6868

69-
public void click(Integer slot, ScriptObjectMirror options) {
69+
public void click(Integer slot, JSObject options) {
7070
String typeStr = options == null || !options.hasMember("type") ? null : ScriptUtil.asString(options.getMember("type"));
7171
SlotActionType type = typeStr == null ? SlotActionType.PICKUP :
7272
Arrays.stream(SlotActionType.values()).filter(it -> it.name().equalsIgnoreCase(typeStr)).findAny().orElse(SlotActionType.PICKUP);

src/main/java/net/earthcomputer/clientcommands/script/ScriptManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ static ThreadInstance createThread(Callable<Void> task, boolean daemon) {
115115
e.getCause().printStackTrace();
116116
}
117117
} catch (Throwable e) {
118-
ClientCommandManager.sendError(new LiteralText(e.getMessage()));
118+
try {
119+
ClientCommandManager.sendError(new LiteralText(e.getMessage()));
120+
} catch (Throwable e1) {
121+
LOGGER.error("Error sending error to chat");
122+
}
119123
e.printStackTrace();
120124
}
121125
runningThreads.remove(thread);

src/main/java/net/earthcomputer/clientcommands/script/ScriptPlayer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ public boolean pick(Object itemStack) {
189189
if (itemStack instanceof String) {
190190
Item item = Registry.ITEM.get(new Identifier((String) itemStack));
191191
predicate = stack -> stack.getItem() == item;
192-
} else if (itemStack instanceof JSObject && ((JSObject) itemStack).isFunction()) {
193-
JSObject jsObject = (JSObject) itemStack;
192+
} else if (ScriptUtil.isFunction(itemStack)) {
193+
JSObject jsObject = ScriptUtil.asFunction(itemStack);
194194
predicate = stack -> {
195195
Object result = jsObject.call(null, ScriptUtil.fromNbt(stack.toTag(new CompoundTag())));
196196
return (Boolean) ScriptUtils.convert(result, Boolean.class);

src/main/java/net/earthcomputer/clientcommands/script/ScriptUtil.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import jdk.nashorn.api.scripting.AbstractJSObject;
44
import jdk.nashorn.api.scripting.JSObject;
5+
import jdk.nashorn.api.scripting.ScriptObjectMirror;
56
import jdk.nashorn.api.scripting.ScriptUtils;
67
import net.minecraft.nbt.*;
78
import net.minecraft.util.Identifier;
@@ -254,13 +255,52 @@ public static String simplifyIdentifier(Identifier id) {
254255
return id.toString();
255256
}
256257

258+
public static Object safeWrap(Object obj) {
259+
try {
260+
return ScriptUtils.wrap(obj);
261+
} catch (NoSuchMethodError e) {
262+
// hackfix for older Java 8 builds
263+
return ScriptObjectMirror.wrap(obj, getGlobalContext());
264+
} catch (IllegalArgumentException e) {
265+
return obj;
266+
}
267+
}
268+
257269
public static String asString(Object obj) {
258270
if (obj == null) return null;
271+
obj = safeWrap(obj);
259272
if (obj instanceof JSObject) {
260273
return (String) AbstractJSObject.getDefaultValue((JSObject) obj, String.class);
261274
} else {
262275
return String.valueOf(obj);
263276
}
264277
}
265278

279+
public static boolean isFunction(Object obj) {
280+
try {
281+
asFunction(obj);
282+
return true;
283+
} catch (IllegalArgumentException e) {
284+
return false;
285+
}
286+
}
287+
288+
public static JSObject asFunction(Object obj) {
289+
obj = safeWrap(obj);
290+
if (obj instanceof JSObject && ((JSObject) obj).isFunction())
291+
return (JSObject) obj;
292+
throw new IllegalArgumentException("Cannot interpret " + obj + " as a function");
293+
}
294+
295+
// unsafe function, should only be used for compatibility with older Java releases
296+
private static Object getGlobalContext() {
297+
try {
298+
return Class.forName("jdk.nashorn.internal.runtime.Context")
299+
.getMethod("getGlobal")
300+
.invoke(null);
301+
} catch (ReflectiveOperationException e) {
302+
throw new RuntimeException("Unable to get global context", e);
303+
}
304+
}
305+
266306
}

0 commit comments

Comments
 (0)