Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transistion to JS for partial scripting. Untested. 'Should' work.
- Loading branch information
Showing
13 changed files
with
321 additions
and
60 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
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,6 +1,6 @@ | ||
#Wed Jul 02 15:54:47 CDT 2014 | ||
#Mon Mar 23 19:11:26 CDT 2015 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.0-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.0-all.zip |
13 changes: 13 additions & 0 deletions
13
src/main/java/mods/quiddity/redux/JavaScript/JavascriptEngine.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,13 @@ | ||
package mods.quiddity.redux.JavaScript; | ||
|
||
import javax.script.ScriptException; | ||
|
||
public interface JavascriptEngine { | ||
public void init(); | ||
public void loadScript(String script) throws ScriptException; | ||
public void killEngine(); | ||
public void restartEngine(); | ||
public void addJavaObject(String name, Object object); | ||
public boolean hasMethod(String name); | ||
public void callMethod(String name, Object... args) throws ScriptException, NoSuchMethodException; | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/mods/quiddity/redux/JavaScript/NashornEngine.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,57 @@ | ||
package mods.quiddity.redux.JavaScript; | ||
|
||
import jdk.nashorn.api.scripting.ClassFilter; | ||
import jdk.nashorn.api.scripting.NashornScriptEngine; | ||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory; | ||
|
||
import javax.script.ScriptException; | ||
|
||
public class NashornEngine implements JavascriptEngine { | ||
private NashornScriptEngine nashornScriptEngine; | ||
|
||
@Override | ||
public void init() { | ||
if (nashornScriptEngine != null) | ||
killEngine(); | ||
ClassLoader ccl = Thread.currentThread().getContextClassLoader(); | ||
ccl = (ccl == null ? NashornScriptEngineFactory.class.getClassLoader() : ccl); | ||
nashornScriptEngine = (NashornScriptEngine) new NashornScriptEngineFactory().getScriptEngine(new String[]{"--no-java"}, ccl, new ReduxClassFilter()); | ||
} | ||
|
||
@Override | ||
public void loadScript(String script) throws ScriptException { | ||
nashornScriptEngine.eval(script); | ||
} | ||
|
||
@Override | ||
public void killEngine() { | ||
nashornScriptEngine = null; | ||
} | ||
|
||
@Override | ||
public void restartEngine() { | ||
init(); | ||
} | ||
|
||
@Override | ||
public void addJavaObject(String name, Object object) { | ||
nashornScriptEngine.put(name, object); | ||
} | ||
|
||
@Override | ||
public boolean hasMethod(String name) { | ||
return nashornScriptEngine.get(name) != null; | ||
} | ||
|
||
@Override | ||
public void callMethod(String name, Object... args) throws ScriptException, NoSuchMethodException { | ||
nashornScriptEngine.invokeFunction(name, args); | ||
} | ||
|
||
class ReduxClassFilter implements ClassFilter { | ||
@Override | ||
public boolean exposeToScripts(String s) { | ||
return s.contentEquals("mods.quiddity.redux.Javascript.ReduxJavascriptEngine.ReduxAPI"); | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/mods/quiddity/redux/JavaScript/ReduxJavascriptEngine.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,100 @@ | ||
package mods.quiddity.redux.JavaScript; | ||
|
||
import mods.quiddity.redux.json.model.Pack; | ||
import net.minecraft.command.CommandResultStats; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.util.BlockPos; | ||
import net.minecraft.util.ChatComponentText; | ||
import net.minecraft.util.IChatComponent; | ||
import net.minecraft.util.Vec3; | ||
import net.minecraft.world.World; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import net.minecraft.command.ICommandManager; | ||
import net.minecraft.command.ICommandSender; | ||
import net.minecraftforge.fml.common.FMLCommonHandler; | ||
|
||
import javax.script.ScriptEngineManager; | ||
|
||
public class ReduxJavascriptEngine { | ||
public static final ScriptEngineManager engineManager = new ScriptEngineManager(); | ||
private JavascriptEngine engine; | ||
private final Pack packRefrence; | ||
|
||
public ReduxJavascriptEngine(Pack pack) { | ||
if (engineManager.getEngineByName("nashorn") != null) { | ||
engine = new NashornEngine(); | ||
} else if (engineManager.getEngineByName("rhino") != null) { | ||
engine = new RhinoEngine(); | ||
} else { | ||
throw new AssertionError("Your Java Runtime Environment does not support JSR-223"); | ||
} | ||
|
||
engine.addJavaObject("ReduxAPI", new ReduxAPI()); | ||
|
||
this.packRefrence = pack; | ||
} | ||
|
||
public JavascriptEngine getEngine() { | ||
return engine; | ||
} | ||
|
||
public class ReduxAPI implements ICommandSender { | ||
@SuppressWarnings("unused") | ||
public int runCommand(String... args) { | ||
if (FMLCommonHandler.instance().getSide().isServer()) { | ||
ICommandManager manager = FMLCommonHandler.instance().getMinecraftServerInstance().getCommandManager(); | ||
manager.executeCommand(this, StringUtils.join(args)); | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return packRefrence.getName(); | ||
} | ||
|
||
@Override | ||
public IChatComponent getDisplayName() { | ||
return new ChatComponentText(packRefrence.getName()); | ||
} | ||
|
||
@Override | ||
public void addChatMessage(IChatComponent message) { | ||
} | ||
|
||
@Override | ||
public boolean canUseCommand(int permLevel, String commandName) { | ||
return permLevel <= 2; | ||
} | ||
|
||
@Override | ||
public BlockPos getPosition() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Vec3 getPositionVector() { | ||
return new Vec3(0, 0, 0); | ||
} | ||
|
||
@Override | ||
public World getEntityWorld() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Entity getCommandSenderEntity() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean sendCommandFeedback() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void setCommandStat(CommandResultStats.Type type, int amount) { | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/mods/quiddity/redux/JavaScript/RhinoEngine.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,53 @@ | ||
package mods.quiddity.redux.JavaScript; | ||
|
||
import javax.script.Invocable; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptException; | ||
|
||
public class RhinoEngine implements JavascriptEngine { | ||
private ScriptEngine rhinoEngine; | ||
|
||
@Override | ||
public void init() { | ||
if (rhinoEngine != null) | ||
killEngine(); | ||
rhinoEngine = ReduxJavascriptEngine.engineManager.getEngineByName("rhino"); | ||
if (rhinoEngine == null) | ||
throw new AssertionError("Error making the Rhino Engine!"); | ||
} | ||
|
||
@Override | ||
public void loadScript(final String script) throws ScriptException { | ||
rhinoEngine.eval(script); | ||
} | ||
|
||
@Override | ||
public void killEngine() { | ||
rhinoEngine = null; | ||
} | ||
|
||
@Override | ||
public void restartEngine() { | ||
init(); | ||
} | ||
|
||
@Override | ||
public void addJavaObject(String name, Object object) { | ||
rhinoEngine.put(name, object); | ||
} | ||
|
||
@Override | ||
public boolean hasMethod(String name) { | ||
return rhinoEngine.get(name) != null; | ||
} | ||
|
||
@Override | ||
public void callMethod(String name, Object... args) throws ScriptException, NoSuchMethodException { | ||
if (rhinoEngine instanceof Invocable) { | ||
Invocable invokableEngine = (Invocable) rhinoEngine; | ||
invokableEngine.invokeFunction(name, args); | ||
} else { | ||
throw new AssertionError("Your Rhino engine cannot invoke javascript functions! Redux will not function."); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.