-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
104 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 8 additions & 24 deletions
32
fabric/src/main/java/com/lx862/jcm/mod/data/scripting/PIDSScriptInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,26 @@ | ||
package com.lx862.jcm.mod.data.scripting; | ||
|
||
import com.lx862.jcm.mod.block.entity.PIDSBlockEntity; | ||
import com.lx862.jcm.mod.data.pids.preset.components.base.PIDSComponent; | ||
import com.lx862.jcm.mod.data.scripting.base.ScriptInstance; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.Function; | ||
import org.mozilla.javascript.Scriptable; | ||
import org.mtr.mapping.mapper.GraphicsHolder; | ||
import org.mtr.mapping.holder.BlockEntity; | ||
import org.mtr.mapping.holder.BlockPos; | ||
import org.mtr.mapping.holder.MinecraftClient; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PIDSScriptInstance extends ScriptInstance { | ||
public List<PIDSComponent> components; | ||
private final BlockEntity be; | ||
|
||
public PIDSScriptInstance(Scriptable scope) { | ||
public PIDSScriptInstance(BlockPos pos, Scriptable scope) { | ||
super(scope); | ||
this.be = MinecraftClient.getInstance().getWorldMapped().getBlockEntity(pos); | ||
this.components = new ArrayList<>(); | ||
} | ||
|
||
public void execute(GraphicsHolder graphicsHolder, PIDSBlockEntity be) { | ||
execute(() -> { | ||
try { | ||
Context cx = Context.enter(); | ||
cx.setLanguageVersion(Context.VERSION_ES6); | ||
ScriptContext ctx = new ScriptContext(); | ||
PIDSScriptObject pidsContext = new PIDSScriptObject(graphicsHolder, be.getCustomMessages(), be.getRowHidden(), be.platformNumberHidden()); | ||
Object renderFunction = scope.get("render", scope); | ||
Object[] renderParams = new Object[]{ctx, null, pidsContext}; | ||
|
||
if(renderFunction instanceof Scriptable && renderFunction != Scriptable.NOT_FOUND) { | ||
((Function)renderFunction).call(cx, scope, scope, renderParams); | ||
} | ||
components.clear(); | ||
components.addAll(pidsContext.getDrawCalls()); | ||
} finally { | ||
Context.exit(); | ||
} | ||
}); | ||
public boolean isDead() { | ||
return be.isRemoved(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
fabric/src/main/java/com/lx862/jcm/mod/data/scripting/ScriptInstanceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.lx862.jcm.mod.data.scripting; | ||
|
||
import com.lx862.jcm.mod.data.scripting.base.ScriptInstance; | ||
import com.lx862.jcm.mod.util.JCMLogger; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
public class ScriptInstanceManager { | ||
private final static Map<Long, ScriptInstance> instances = new HashMap<>(); | ||
|
||
public static ScriptInstance getInstance(long uuid, Supplier<ScriptInstance> getInstance) { | ||
if(instances.containsKey(uuid)) { | ||
return instances.get(uuid); | ||
} else { | ||
ScriptInstance instance = getInstance.get(); | ||
instances.put(uuid, instance); | ||
return instance; | ||
} | ||
} | ||
|
||
public static void clearDeadInstance() { | ||
int count = 0; | ||
for(Map.Entry<Long, ScriptInstance> entry : new HashMap<>(instances).entrySet()) { | ||
if(entry.getValue().isDead()) { | ||
count++; | ||
instances.remove(entry.getKey()); | ||
} | ||
} | ||
if(count > 0) { | ||
JCMLogger.info("[Scripting] Removed {} dead instance", count); | ||
} | ||
} | ||
|
||
public static void reset() { | ||
instances.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters