rework - moved logic from java to js

This commit is contained in:
walterhiggins 2013-01-08 02:33:18 +00:00
parent 5bf5714d60
commit b9e80ce8bc

View file

@ -2,7 +2,11 @@ package net.walterhiggins.scriptcraft;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.script.*; import javax.script.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.*; import org.bukkit.command.*;
@ -15,13 +19,56 @@ public class ScriptCraftPlugin extends JavaPlugin
// need to look at possibly having context/scope per operator // need to look at possibly having context/scope per operator
//protected Map<CommandSender,ScriptCraftEvaluator> playerContexts = new HashMap<CommandSender,ScriptCraftEvaluator>(); //protected Map<CommandSender,ScriptCraftEvaluator> playerContexts = new HashMap<CommandSender,ScriptCraftEvaluator>();
protected ScriptEngine engine = null; protected ScriptEngine engine = null;
private static final String JS_PLUGINS_DIR = "js-plugins";
@Override @Override
public void onEnable(){ 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();
}
}
if (this.engine == null){ if (this.engine == null){
try{ try{
ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngineManager factory = new ScriptEngineManager();
File boot = new File("js-plugins2/core/_scriptcraft.js"); File boot = new File(JS_PLUGINS_DIR + "/core/_scriptcraft.js");
this.engine = factory.getEngineByName("JavaScript"); this.engine = factory.getEngineByName("JavaScript");
this.engine.put("__engine",engine); this.engine.put("__engine",engine);
this.engine.put("__plugin",this); this.engine.put("__plugin",this);
@ -30,7 +77,6 @@ public class ScriptCraftPlugin extends JavaPlugin
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)