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

163 lines
5.7 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-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;
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;
private static final String JS_PLUGINS_DIR = "plugins/scriptcraft";
private static final String JS_PLUGINS_ZIP = "scriptcraft.zip";
2013-01-13 22:06:46 +01:00
/**
* Unzips bundled javascript code.
*/
private void unzipJS() throws IOException
2013-01-08 03:33:18 +01:00
{
//
// does the js-plugins directory exist?
//
File jsPlugins = new File(JS_PLUGINS_DIR);
if (!jsPlugins.exists())
{
getLogger().info("Directory " + jsPlugins.getCanonicalPath() + " does not exist.");
getLogger().info("Initializing " + jsPlugins.getCanonicalPath() + " directory with contents from plugin archive.");
try{
jsPlugins.mkdirs();
}catch(Exception e){
throw new RuntimeException("Failed to create directory " + jsPlugins.getCanonicalPath() + ":" + e.getMessage());
}
}
ZipInputStream zis = new ZipInputStream(getResource(JS_PLUGINS_ZIP));
ZipEntry entry;
try {
while ( ( entry = zis.getNextEntry() ) != null)
{
String filename = entry.getName();
File newFile = new File(jsPlugins, 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
//
String reason = null;
long zTime = entry.getTime();
boolean unzip = false;
if (!newFile.exists()){
reason = "NE";
unzip = true;
}
else{
long fTime = newFile.lastModified();
if (zTime > fTime){
reason = "" + new Long((zTime-fTime)/3600000) + "h";
unzip = true;
}
}
if (unzip){
getLogger().info("Unzipping " + newFile.getCanonicalPath() + " (" + reason + ")" );
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-08 03:33:18 +01:00
}
zis.closeEntry();
2013-01-08 03:33:18 +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()
{
2013-03-09 17:50:12 +01:00
FileReader reader = null;
try{
unzipJS();
2013-03-09 17:50:12 +01:00
ScriptEngineManager factory = new ScriptEngineManager();
File bootScript = new File(JS_PLUGINS_DIR + "/lib/scriptcraft.js");
2013-03-09 17:50:12 +01:00
this.engine = factory.getEngineByName("JavaScript");
reader = new FileReader(bootScript);
this.engine.eval(reader);
Invocable inv = (Invocable)this.engine;
inv.invokeFunction("__onEnable", engine, this, bootScript);
2013-03-09 17:50:12 +01:00
}catch(Exception e){
e.printStackTrace();
this.getLogger().severe(e.getMessage());
2013-03-09 17:50:12 +01:00
}finally {
if (reader != null){
try {
reader.close();
}catch(IOException ioe){
// fail silently
}
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)
{
//
// delegate to javascript
//
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
}
}