diff --git a/build.xml b/build.xml index eca12ac..c1c35d8 100644 --- a/build.xml +++ b/build.xml @@ -38,6 +38,11 @@ Creating default ops.txt for your user + + Retrieving Coffeescript compiler + + diff --git a/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java b/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java index a68dae8..d843eb3 100644 --- a/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java +++ b/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java @@ -98,7 +98,12 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener this.engine.put("__plugin",this); this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/")); reader = new FileReader(boot); - this.engine.eval(reader); + this.engine.eval(reader); + + // Load the CoffeeScript compiler + File coffeescript = new File(JS_PLUGINS_DIR + "/core/_coffeescript.js"); + this.engine.eval(new FileReader(coffeescript)); + }catch(Exception e){ e.printStackTrace(); }finally { @@ -147,7 +152,13 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener javascriptCode = "command()"; this.engine.put("__cmdArgs",args); result = true; + } else if (cmd.getName().equalsIgnoreCase("coffee")) { + for (int i = 0;i < args.length; i++) + javascriptCode += args[i] + " "; + javascriptCode = "eval(CoffeeScript.compile(\""+javascriptCode+"\", {bare: true}))"; + result = true; } + if (result){ this.engine.put("self",sender); try{ diff --git a/src/main/javascript/core/_scriptcraft.js b/src/main/javascript/core/_scriptcraft.js index 4b80da6..68a213b 100644 --- a/src/main/javascript/core/_scriptcraft.js +++ b/src/main/javascript/core/_scriptcraft.js @@ -276,10 +276,22 @@ var server = org.bukkit.Bukkit.server; if (file.exists()) { var parent = file.getParentFile(); var reader = new java.io.FileReader(file); + var br = new java.io.BufferedReader(reader); __engine.put("__script",canonizedFilename); __engine.put("__folder",(parent?_canonize(parent):"")+"/"); + + var code = ""; try{ - result = __engine.eval(reader); + if (file.getCanonicalPath().endsWith(".coffee")) { + var r = undefined; + while ((r = br.readLine()) !== null) code += "\"" + r + "\" +\n"; + code += "\"\""; + var code = "load(__folder + \"../core/_coffeescript.js\"); var ___code = "+code+"; eval(CoffeeScript.compile(___code, {bare: true}))"; + } else { + while ((r = br.readLine()) !== null) code += r + "\n"; + } + + result = __engine.eval(code); _loaded[canonizedFilename] = true; reader.close(); }catch (e){ @@ -296,7 +308,7 @@ var server = org.bukkit.Bukkit.server; /* recursively walk the given directory and return a list of all .js files */ - var _listJsFiles = function(store,dir) + var _listSourceFiles = function(store,dir) { if (typeof dir == "undefined"){ dir = new java.io.File(_originalScript).getParentFile().getParentFile(); @@ -305,9 +317,9 @@ var server = org.bukkit.Bukkit.server; for (var i = 0;i < files.length; i++){ var file = files[i]; if (file.isDirectory()){ - _listJsFiles(store,file); + _listSourceFiles(store,file); }else{ - if (file.getCanonicalPath().endsWith(".js") && + if ((file.getCanonicalPath().endsWith(".js") || file.getCanonicalPath().endsWith(".coffee")) && !(file.getName().startsWith("_")) && file.exists()) { @@ -348,10 +360,10 @@ var server = org.bukkit.Bukkit.server; var _reload = function(pluginDir) { _loaded = []; - var jsFiles = []; - _listJsFiles(jsFiles,pluginDir); + var sourceFiles = []; + _listSourceFiles(sourceFiles,pluginDir); - jsFiles.sort(sortByModule); + sourceFiles.sort(sortByModule); // // script files whose name begins with _ (underscore) @@ -363,9 +375,9 @@ var server = org.bukkit.Bukkit.server; // then it's assumed that _myMiniGame_currency.js and _myMiniGame_events.js will be loaded // as dependencies by myMiniGame.js and do not need to be loaded via js reload // - var len = jsFiles.length; + var len = sourceFiles.length; for (var i = 0;i < len; i++){ - load(_canonize(jsFiles[i]),true); + load(_canonize(sourceFiles[i]),true); } }; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8a4006b..56f245e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -11,7 +11,12 @@ commands: description: run a javascript plugin command. usage: / command-name command-parameters permission: scriptcraft.proxy - permission-message: You don't have permission. + permission-message: You don't have permission. + coffee: + description: Evaluate coffeescript. + usage: / Coffeescript code + permission: scriptcraft.evaluate + permission-message: You don't have permission. permissions: scriptcraft.*: