This repository has been archived on 2021-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
scriptcraft/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java

68 lines
2.3 KiB
Java
Raw Normal View History

2013-01-08 00:48:43 +01:00
package net.walterhiggins.scriptcraft;
import java.io.InputStreamReader;
2013-01-08 00:48:43 +01:00
import javax.script.*;
2013-01-13 22:06:46 +01:00
import java.util.List;
import java.util.ArrayList;
2013-01-08 00:48:43 +01:00
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.*;
import org.bukkit.event.Listener;
2013-01-08 00:48:43 +01:00
public class ScriptCraftPlugin extends JavaPlugin implements Listener
2013-01-08 00:48:43 +01:00
{
// right now all ops share the same JS context/scope
// need to look at possibly having context/scope per operator
//protected Map<CommandSender,ScriptCraftEvaluator> playerContexts = new HashMap<CommandSender,ScriptCraftEvaluator>();
protected ScriptEngine engine = null;
2013-01-13 22:06:46 +01:00
@Override
public void onEnable()
{
2013-03-09 17:50:12 +01:00
try{
2013-03-09 17:50:12 +01:00
ScriptEngineManager factory = new ScriptEngineManager();
this.engine = factory.getEngineByName("JavaScript");
Invocable inv = (Invocable)this.engine;
this.engine.eval(new InputStreamReader(this.getResource("boot.js")));
inv.invokeFunction("__scboot", this, engine);
2013-03-09 17:50:12 +01:00
}catch(Exception e){
e.printStackTrace();
this.getLogger().severe(e.getMessage());
2013-01-08 00:48:43 +01:00
}
}
2013-01-13 22:06:46 +01:00
public List<String> onTabComplete(CommandSender sender, Command cmd,
String alias,
String[] args)
{
List<String> result = new ArrayList<String>();
try {
Invocable inv = (Invocable)this.engine;
inv.invokeFunction("__onTabComplete", result, sender, cmd, alias, args);
2013-01-13 22:06:46 +01:00
}catch (Exception e){
sender.sendMessage(e.getMessage());
e.printStackTrace();
}
return result;
}
2013-01-08 00:48:43 +01:00
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
boolean result = false;
String javascriptCode = "";
Object jsResult = null;
try {
jsResult = ((Invocable)this.engine).invokeFunction("__onCommand", sender, cmd, label, args);
}catch (Exception se){
this.getLogger().severe(se.toString());
se.printStackTrace();
sender.sendMessage(se.getMessage());
}
if (jsResult != null){
return ((Boolean)jsResult).booleanValue();
}
return result;
2013-01-08 00:48:43 +01:00
}
}