Created
October 28, 2012 00:34
-
-
Save tehbeard/3967028 to your computer and use it in GitHub Desktop.
Buscript.java.chunk
This file contains hidden or 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
| /** | |
| * Set a top level variable | |
| * @param name variable to set to | |
| * @param object value for variable | |
| */ | |
| public void setTopLevelVariable(String name,Object object){ | |
| getGlobalScope().put(name, getGlobalScope(), object); | |
| } | |
| /** | |
| * Returns a top level variable | |
| * @param name variable to get | |
| * @return value | |
| */ | |
| public Object getTopLevelVariable(String name){ | |
| return getGlobalScope().get(name, getGlobalScope()); | |
| } | |
| /** | |
| * Returns a top level variable, auto casted to a value. | |
| * @param name variable to get | |
| * @param type Attempt to cast to this type | |
| * @return value or null on cast fail. | |
| */ | |
| @SuppressWarnings("unchecked") | |
| public <T> T getTopLevelVariable(String name,Class<T> type){ | |
| try{ | |
| return (T) getGlobalScope().get(name, getGlobalScope()); | |
| } | |
| catch(Exception e){ | |
| return null; | |
| } | |
| } | |
| /** | |
| * Execute a function | |
| * @param sender sender for debug error | |
| * @param functionName function name | |
| * @param _this - Scriptable object for use as the 'this' object in JS | |
| * @param args - Arguments for the script | |
| */ | |
| public void runFunction(CommandSender sender,String functionName,Scriptable _this, Object... args){ | |
| Object o = getTopLevelVariable(functionName); | |
| if(o instanceof Function){ | |
| Function f = (Function)o; | |
| Context cx = Context.enter(); | |
| try{ | |
| f.call(cx, getGlobalScope(),_this , args); | |
| }catch(Exception e){ | |
| sender.sendMessage("An error occured:"); | |
| sender.sendMessage(e.getMessage()); | |
| } | |
| Context.exit(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment