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/bukkit-plugin/src/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java

114 lines
4.1 KiB
Java
Raw Normal View History

2013-01-08 00:48:43 +01:00
package net.walterhiggins.scriptcraft;
import java.io.File;
import java.io.FileReader;
2013-01-08 03:33:18 +01:00
import java.io.FileOutputStream;
import java.io.IOException;
2013-01-08 00:48:43 +01:00
import javax.script.*;
2013-01-08 03:33:18 +01:00
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
2013-01-09 22:58:11 +01:00
import java.util.Collection;
import java.util.Arrays;
2013-01-08 00:48:43 +01:00
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.*;
public class ScriptCraftPlugin extends JavaPlugin
{
// 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-08 03:33:18 +01:00
private static final String JS_PLUGINS_DIR = "js-plugins";
2013-01-08 00:48:43 +01:00
@Override
2013-01-08 03:33:18 +01:00
public void onEnable()
{
//
// does the js-plugins directory exist?
//
File jsPlugins = new File(JS_PLUGINS_DIR);
if (!jsPlugins.exists())
{
getLogger().info("Directory " + JS_PLUGINS_DIR + " does not exist.");
getLogger().info("Initializing " + JS_PLUGINS_DIR + " directory with contents from plugin archive.");
jsPlugins.mkdir();
ZipInputStream zis = new ZipInputStream(getResource(JS_PLUGINS_DIR + ".zip"));
ZipEntry entry;
try {
while ( ( entry = zis.getNextEntry() ) != null)
{
String filename = entry.getName();
getLogger().info("Unzipping " + filename);
File newFile = new File(jsPlugins.getName() + File.separator + filename);
//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
if (entry.isDirectory()){
newFile.mkdirs();
}else{
FileOutputStream fout = new FileOutputStream(newFile);
for (int c = zis.read(); c != -1; c = zis.read()) {
fout.write(c);
}
fout.close();
}
zis.closeEntry();
}
zis.close();
}catch (IOException ioe){
getLogger().warning(ioe.getMessage());
ioe.printStackTrace();
}
}
2013-01-08 00:48:43 +01:00
if (this.engine == null){
try{
ScriptEngineManager factory = new ScriptEngineManager();
2013-01-08 03:33:18 +01:00
File boot = new File(JS_PLUGINS_DIR + "/core/_scriptcraft.js");
2013-01-08 00:48:43 +01:00
this.engine = factory.getEngineByName("JavaScript");
this.engine.put("__engine",engine);
this.engine.put("__plugin",this);
this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/"));
this.engine.eval(new FileReader(boot));
}catch(Exception e){
e.printStackTrace();
}
}
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("js"))
{
this.engine.put("__self",sender);
String javascriptCode = "";
for (int i = 0;i < args.length; i++){
javascriptCode = javascriptCode + args[i] + " ";
}
try{
Object result = this.engine.eval(javascriptCode);
2013-01-09 22:58:11 +01:00
if (result != null){
if (result instanceof java.util.Collection){
java.util.Collection collection = (java.util.Collection)result;
sender.sendMessage(Arrays.toString(collection.toArray()));
}else{
sender.sendMessage(result.toString());
}
}
2013-01-08 00:48:43 +01:00
return true;
}catch (Exception e){
sender.sendMessage(e.getMessage());
e.printStackTrace();
}
return true;
}
return false;
}
}