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-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.*;
|
2013-01-25 00:46:28 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2013-01-08 00:48:43 +01:00
|
|
|
|
|
|
|
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-13 22:06:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unzips bundled javascript code.
|
|
|
|
*/
|
|
|
|
private void unzipJS()
|
2013-01-08 03:33:18 +01:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// does the js-plugins directory exist?
|
|
|
|
//
|
|
|
|
File jsPlugins = new File(JS_PLUGINS_DIR);
|
|
|
|
if (!jsPlugins.exists())
|
|
|
|
{
|
2013-01-15 20:15:40 +01:00
|
|
|
getLogger().finest("Directory " + JS_PLUGINS_DIR + " does not exist.");
|
|
|
|
getLogger().finest("Initializing " + JS_PLUGINS_DIR + " directory with contents from plugin archive.");
|
2013-01-08 03:33:18 +01:00
|
|
|
jsPlugins.mkdir();
|
2013-01-19 18:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ZipInputStream zis = new ZipInputStream(getResource(JS_PLUGINS_DIR + ".zip"));
|
|
|
|
ZipEntry entry;
|
|
|
|
try {
|
|
|
|
while ( ( entry = zis.getNextEntry() ) != null)
|
|
|
|
{
|
|
|
|
String filename = entry.getName();
|
|
|
|
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{
|
|
|
|
//
|
|
|
|
// only write out to file if zip entry is newer than file
|
|
|
|
//
|
|
|
|
long zTime = entry.getTime();
|
|
|
|
boolean unzip = false;
|
|
|
|
if (!newFile.exists())
|
|
|
|
unzip = true;
|
|
|
|
else{
|
|
|
|
long fTime = newFile.lastModified();
|
|
|
|
if (zTime > fTime)
|
|
|
|
unzip = true;
|
|
|
|
}
|
|
|
|
if (unzip){
|
|
|
|
getLogger().info("Unzipping " + filename);
|
2013-01-08 03:33:18 +01:00
|
|
|
FileOutputStream fout = new FileOutputStream(newFile);
|
|
|
|
for (int c = zis.read(); c != -1; c = zis.read()) {
|
|
|
|
fout.write(c);
|
|
|
|
}
|
|
|
|
fout.close();
|
|
|
|
}
|
2013-01-19 18:00:22 +01:00
|
|
|
|
2013-01-08 03:33:18 +01:00
|
|
|
}
|
2013-01-19 18:00:22 +01:00
|
|
|
zis.closeEntry();
|
2013-01-08 03:33:18 +01:00
|
|
|
}
|
2013-01-19 18:00:22 +01:00
|
|
|
zis.close();
|
|
|
|
}catch (IOException ioe){
|
|
|
|
getLogger().warning(ioe.getMessage());
|
|
|
|
ioe.printStackTrace();
|
2013-01-08 03:33:18 +01:00
|
|
|
}
|
2013-01-13 22:06:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnable()
|
|
|
|
{
|
|
|
|
unzipJS();
|
2013-01-08 03:33:18 +01:00
|
|
|
|
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);
|
2013-01-25 00:46:28 +01:00
|
|
|
this.engine.put("plugin",this);
|
2013-01-08 00:48:43 +01:00
|
|
|
this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/"));
|
|
|
|
this.engine.eval(new FileReader(boot));
|
|
|
|
}catch(Exception e){
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 22:06:46 +01:00
|
|
|
public List<String> onTabComplete(CommandSender sender, Command cmd,
|
|
|
|
String alias,
|
|
|
|
String[] args)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// delegate to javascript
|
|
|
|
//
|
|
|
|
List<String> result = new ArrayList<String>();
|
|
|
|
try {
|
|
|
|
this.engine.put("__onTC_result",result);
|
|
|
|
this.engine.put("__onTC_sender",sender);
|
|
|
|
this.engine.put("__onTC_cmd",cmd);
|
|
|
|
this.engine.put("__onTC_alias",alias);
|
|
|
|
this.engine.put("__onTC_args",args);
|
|
|
|
this.engine.eval("_onTabComplete()");
|
|
|
|
}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)
|
|
|
|
{
|
2013-01-16 00:09:43 +01:00
|
|
|
boolean result = false;
|
|
|
|
String javascriptCode = "";
|
|
|
|
|
|
|
|
if(cmd.getName().equalsIgnoreCase("js")){
|
2013-01-08 00:48:43 +01:00
|
|
|
for (int i = 0;i < args.length; i++){
|
|
|
|
javascriptCode = javascriptCode + args[i] + " ";
|
|
|
|
}
|
2013-01-16 00:09:43 +01:00
|
|
|
result = true;
|
|
|
|
} else if (cmd.getName().equalsIgnoreCase("jsp")){
|
2013-01-17 01:05:17 +01:00
|
|
|
javascriptCode = "command()";
|
2013-01-16 00:09:43 +01:00
|
|
|
this.engine.put("__cmdArgs",args);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
if (result){
|
2013-01-25 00:46:28 +01:00
|
|
|
this.engine.put("self",sender);
|
2013-01-08 00:48:43 +01:00
|
|
|
try{
|
2013-01-16 00:09:43 +01:00
|
|
|
Object resultObj = this.engine.eval(javascriptCode);
|
|
|
|
if (resultObj != null){
|
|
|
|
if (resultObj instanceof java.util.Collection){
|
|
|
|
java.util.Collection collection = (java.util.Collection)resultObj;
|
2013-01-09 22:58:11 +01:00
|
|
|
sender.sendMessage(Arrays.toString(collection.toArray()));
|
|
|
|
}else{
|
2013-01-16 00:09:43 +01:00
|
|
|
sender.sendMessage(resultObj.toString());
|
2013-01-09 22:58:11 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-08 00:48:43 +01:00
|
|
|
}catch (Exception e){
|
|
|
|
sender.sendMessage(e.getMessage());
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-01-16 00:09:43 +01:00
|
|
|
}
|
|
|
|
return result;
|
2013-01-08 00:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|