Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MineQueryLoader for setting up JS
MineQueryLoader loads script files and sets up the JS environment for use within minequery.
- Loading branch information
1 parent
4a57c67
commit fc0e7f9
Showing
3 changed files
with
66 additions
and
0 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
42 changes: 42 additions & 0 deletions
42
src/main/java/tech/unstable/mineQuery/engine/MineQueryLoader.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,42 @@ | ||
package tech.unstable.mineQuery.engine; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
|
||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.RhinoException; | ||
import org.mozilla.javascript.Scriptable; | ||
|
||
import tech.unstable.mineQuery.QueryMod; | ||
import tech.unstable.mineQuery.jsHelers.JsConsole; | ||
|
||
public class MineQueryLoader { | ||
|
||
public static void loadScript(File script) throws IOException{ | ||
Reader src = null; | ||
try { | ||
src = new FileReader(script); | ||
Context cx = Context.enter(); | ||
cx.evaluateReader(getGlobalScope(script.getName()), src, script.getName(), 0, null); | ||
} catch (RhinoException ex) { | ||
QueryMod.logger.error(ex.getMessage() + " from " + ex.sourceName()); | ||
QueryMod.logger.error(ex.sourceName() + " : " + ex.lineNumber()); | ||
} finally { | ||
src.close(); | ||
} | ||
} | ||
|
||
public static Scriptable getGlobalScope(String scriptName) { | ||
Context cx = Context.enter(); | ||
Scriptable scope = cx.initStandardObjects(); | ||
|
||
scope.put("console", scope, new JsConsole(QueryMod.logger, scriptName)); | ||
scope.put("$", scope, new MineQuery()); | ||
scope.put("Minecraft", scope, null); // TODO | ||
|
||
return scope; | ||
} | ||
|
||
} |