javascript lfg
This commit is contained in:
parent
88fad02b96
commit
108afd360f
4 changed files with 48 additions and 15 deletions
|
@ -52,7 +52,7 @@ dependencies {
|
||||||
// And its provides the option to then use net.minecraft as the group, and one of; client, server or joined as the module name, plus the game version as version.
|
// And its provides the option to then use net.minecraft as the group, and one of; client, server or joined as the module name, plus the game version as version.
|
||||||
// For all intends and purposes: You can treat this dependency as if it is a normal library you would use.
|
// For all intends and purposes: You can treat this dependency as if it is a normal library you would use.
|
||||||
implementation "net.neoforged:neoforge:${neo_version}"
|
implementation "net.neoforged:neoforge:${neo_version}"
|
||||||
libraries 'com.dylibso.chicory:runtime:0.0.12'
|
libraries 'org.mozilla:rhino-engine:1.7.15'
|
||||||
// Example optional mod dependency with JEI
|
// Example optional mod dependency with JEI
|
||||||
// The JEI API is declared for compile time use, while the full JEI artifact is used at runtime
|
// The JEI API is declared for compile time use, while the full JEI artifact is used at runtime
|
||||||
// compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}"
|
// compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}"
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.mojang.serialization.MapCodec;
|
||||||
import dev.exhq.ajarc.Ajar;
|
import dev.exhq.ajarc.Ajar;
|
||||||
import dev.exhq.ajarc.network.ComputerScreenUpdate;
|
import dev.exhq.ajarc.network.ComputerScreenUpdate;
|
||||||
import dev.exhq.ajarc.register.Register;
|
import dev.exhq.ajarc.register.Register;
|
||||||
|
import dev.exhq.ajarc.vm.JsVm;
|
||||||
import dev.exhq.ajarc.vm.WasmVm;
|
import dev.exhq.ajarc.vm.WasmVm;
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.core.HolderLookup;
|
import net.minecraft.core.HolderLookup;
|
||||||
|
@ -34,7 +35,6 @@ public class ComputerBlockEntity extends BlockEntity {
|
||||||
private static final MapCodec<ComputerTerminal> screenCodec = ComputerTerminal.CODEC
|
private static final MapCodec<ComputerTerminal> screenCodec = ComputerTerminal.CODEC
|
||||||
.fieldOf("screen")
|
.fieldOf("screen")
|
||||||
.setPartial(() -> ComputerTerminal.ofSize(20, 30));
|
.setPartial(() -> ComputerTerminal.ofSize(20, 30));
|
||||||
private WasmVm vm = new WasmVm(this);
|
|
||||||
private List<String> lines = new ArrayList<>();
|
private List<String> lines = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -76,19 +76,20 @@ public class ComputerBlockEntity extends BlockEntity {
|
||||||
|
|
||||||
public void executeCommand(String line) {
|
public void executeCommand(String line) {
|
||||||
lines.add("$ " + line);
|
lines.add("$ " + line);
|
||||||
if (line.equals("small")) {
|
lines.add(JsVm.EvalJs(line));
|
||||||
screen = ComputerTerminal.ofSize(10, 20);
|
// if (line.equals("small")) {
|
||||||
lines.add("Made small!");
|
// screen = ComputerTerminal.ofSize(10, 20);
|
||||||
} else if (line.equals("big")) {
|
// lines.add("Made small!");
|
||||||
screen = ComputerTerminal.ofSize(20, 30);
|
// } else if (line.equals("big")) {
|
||||||
lines.add("Made big!");
|
// screen = ComputerTerminal.ofSize(20, 30);
|
||||||
} else if (line.equals("add")) {
|
// lines.add("Made big!");
|
||||||
int left = 42, right = 69;
|
// } else if (line.startsWith("add")) {
|
||||||
int result = vm.add(left, right);
|
// var newline = line.replaceFirst("add ", "").split(" ");
|
||||||
lines.add(left + " + " + right + " = " + result);
|
// lines.add(JsVm.EvalJs("mathj.add("+newline[0]+","+newline[1]+")"));
|
||||||
} else {
|
// }
|
||||||
lines.add("Made unknown!");
|
// else {
|
||||||
}
|
// lines.add("Made unknown!");
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
public ComputerScreenUpdate getSyncPacket(int windowId) {
|
public ComputerScreenUpdate getSyncPacket(int windowId) {
|
||||||
|
|
25
src/main/java/dev/exhq/ajarc/vm/JsVm.java
Normal file
25
src/main/java/dev/exhq/ajarc/vm/JsVm.java
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package dev.exhq.ajarc.vm;
|
||||||
|
|
||||||
|
import dev.exhq.ajarc.computer.ComputerBlockEntity;
|
||||||
|
import org.mozilla.javascript.Context;
|
||||||
|
import org.mozilla.javascript.Scriptable;
|
||||||
|
import org.mozilla.javascript.ScriptableObject;
|
||||||
|
|
||||||
|
public class JsVm {
|
||||||
|
public static String EvalJs(String command){
|
||||||
|
var cx = Context.enter();
|
||||||
|
try {
|
||||||
|
Scriptable scope = cx.initStandardObjects();
|
||||||
|
MathJ mathJ = new MathJ();
|
||||||
|
Object wrappedJsVm = Context.javaToJS(mathJ, scope);
|
||||||
|
ScriptableObject.putProperty(scope, "mathj", wrappedJsVm);
|
||||||
|
Object result = cx.evaluateString(scope, command, "<cmd>", 1, null);
|
||||||
|
|
||||||
|
return Context.toString(result);
|
||||||
|
} finally {
|
||||||
|
Context.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
7
src/main/java/dev/exhq/ajarc/vm/MathJ.java
Normal file
7
src/main/java/dev/exhq/ajarc/vm/MathJ.java
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package dev.exhq.ajarc.vm;
|
||||||
|
|
||||||
|
public class MathJ {
|
||||||
|
public int add(int a, int b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue