diff --git a/build.xml b/build.xml index c1c35d8..98cb1eb 100644 --- a/build.xml +++ b/build.xml @@ -1,6 +1,7 @@ - Builds the scriptcraft.jar file - a plugin for bukkit + Builds the scriptcraft.jar file - a plugin for bukkit + @@ -21,8 +22,9 @@ - - Retrieving CraftBukkit artifact info + + Retrieving CraftBukkit artifact info + @@ -31,22 +33,22 @@ style="build/bukkit-to-url.xsl"/> - Retrieving CraftBukkit jar + Retrieving CraftBukkit jar + - Creating default ops.txt for your user + Creating default ops.txt for your user + - - Retrieving Coffeescript compiler - - + + - Starting Bukkit with ScriptCraft + Starting Bukkit with ScriptCraft + - - + + Retrieving Coffeescript compiler + + + + + + + + + + - [[version]] + [[version]] + diff --git a/docs/api.md b/docs/api.md index bd7383d..6125c04 100644 --- a/docs/api.md +++ b/docs/api.md @@ -209,6 +209,37 @@ The execution of the function object passed to the `ready()` function is *deferred* until all of the plugins/modules have loaded. That way you are guaranteed that when the function is invoked, all of the plugins/modules have been loaded and evaluated and are ready to use. +setTimeout() function +--------------------- + +This function mimics the setTimeout() function used in browser-based javascript. +However, the function will only accept a function reference, not a string of javascript code. +Where setTimeout() in the browser returns a numeric value which can be subsequently passed to +clearTimeout(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearTimeout() implementation. + +If Node.js supports setTimeout() then it's probably good for ScriptCraft to support it too. + +[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html + +clearTimeout() function +--------------------- +A scriptcraft implementation of clearTimeout(). + +setInterval() function +--------------------- + +This function mimics the setInterval() function used in browser-based javascript. +However, the function will only accept a function reference, not a string of javascript code. +Where setInterval() in the browser returns a numeric value which can be subsequently passed to +clearInterval(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearInterval() implementation. + +If Node.js supports setInterval() then it's probably good for ScriptCraft to support it too. + +[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html + +clearInterval() function +--------------------- +A scriptcraft implementation of clearInterval(). Core Module - Special Variables =============================== @@ -1389,10 +1420,11 @@ Example To warn players when night is approaching... utils.at( "19:00", function() { - /* it's 7 in the evening so warn all players that night is coming ! */ + utils.foreach( server.onlinePlayers, function(player){ player.chat("The night is dark and full of terrors!"); }); + }, self.world); String class extensions diff --git a/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java b/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java index d843eb3..2df8324 100644 --- a/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java +++ b/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java @@ -99,10 +99,13 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/")); reader = new FileReader(boot); this.engine.eval(reader); - + /* + wph 20130811 Need to disable coffeescript support until issues loading and evaluating it are resolved. + See issue #92 // 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(); @@ -153,10 +156,10 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener this.engine.put("__cmdArgs",args); result = true; } else if (cmd.getName().equalsIgnoreCase("coffee")) { - for (int i = 0;i < args.length; i++) + for (int i = 0;i < args.length; i++) javascriptCode += args[i] + " "; - javascriptCode = "eval(CoffeeScript.compile(\""+javascriptCode+"\", {bare: true}))"; - result = true; + javascriptCode = "eval(CoffeeScript.compile(\""+javascriptCode+"\", {bare: true}))"; + result = true; } if (result){ diff --git a/src/main/javascript/core/_scriptcraft.js b/src/main/javascript/core/_scriptcraft.js index 68a213b..e012e8b 100644 --- a/src/main/javascript/core/_scriptcraft.js +++ b/src/main/javascript/core/_scriptcraft.js @@ -210,7 +210,74 @@ The execution of the function object passed to the `ready()` function is *deferred* until all of the plugins/modules have loaded. That way you are guaranteed that when the function is invoked, all of the plugins/modules have been loaded and evaluated and are ready to use. +***/ +var global = this; + +/************************************************************************* +setTimeout() function +--------------------- + +This function mimics the setTimeout() function used in browser-based javascript. +However, the function will only accept a function reference, not a string of javascript code. +Where setTimeout() in the browser returns a numeric value which can be subsequently passed to +clearTimeout(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearTimeout() implementation. + +If Node.js supports setTimeout() then it's probably good for ScriptCraft to support it too. + +[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html + +***/ + global.setTimeout = function( callback, delayInMillis){ + // + // javascript programmers familiar with setTimeout know that it expects + // a delay in milliseconds. However, bukkit's scheduler expects a delay in ticks + // (where 1 tick = 1/20th second) + // + var bukkitTask = server.scheduler.runTaskLater(__plugin, callback, delayInMillis/50); + return bukkitTask; + }; + +/************************************************************************* +clearTimeout() function +--------------------- +A scriptcraft implementation of clearTimeout(). + +***/ + global.clearTimeout = function(bukkitTask){ + bukkitTask.cancel(); + }; + +/************************************************************************* +setInterval() function +--------------------- + +This function mimics the setInterval() function used in browser-based javascript. +However, the function will only accept a function reference, not a string of javascript code. +Where setInterval() in the browser returns a numeric value which can be subsequently passed to +clearInterval(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearInterval() implementation. + +If Node.js supports setInterval() then it's probably good for ScriptCraft to support it too. + +[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html + +***/ + global.setInterval = function(callback, intervalInMillis){ + var delay = intervalInMillis/ 50; + var bukkitTask = server.scheduler.runTaskTimer(__plugin, callback, delay, delay); + return bukkitTask; + }; +/************************************************************************* +clearInterval() function +--------------------- +A scriptcraft implementation of clearInterval(). + +***/ + global.clearInterval = function(bukkitTask){ + bukkitTask.cancel(); + }; + +/************************************************************************* Core Module - Special Variables =============================== There are a couple of special javascript variables available in ScriptCraft... @@ -221,8 +288,6 @@ There are a couple of special javascript variables available in ScriptCraft... * self - the current player. (Note - this value should not be used in multi-threaded scripts - it's not thread-safe) ***/ - -var global = this; var verbose = verbose || false; /* wph 20130124 - make self, plugin and server public - these are far more useful now that tab-complete works. @@ -691,6 +756,7 @@ See [issue #69][issue69] for more information. __plugin.pluginLoader.enablePlugin(__plugin); }; + global.load = _load; global.save = _save; global.plugin = _plugin; @@ -698,7 +764,8 @@ See [issue #69][issue69] for more information. global.command = _command; global._onTabComplete = __onTabCompleteJS; global.addUnloadHandler = _addUnloadHandler; - + + // // assumes this was loaded from js-plugins/core/ // load all of the plugins. diff --git a/src/main/javascript/drone/drone.js b/src/main/javascript/drone/drone.js index 771a0eb..590ec11 100644 --- a/src/main/javascript/drone/drone.js +++ b/src/main/javascript/drone/drone.js @@ -700,12 +700,15 @@ Used when placing torches so that they face towards the drone. this.record = false; var usePlayerCoords = false; var playerPos = getPlayerPos(); - if (typeof x == "undefined"){ + if (typeof x == "undefined") + { var mp = getMousePos(); if (mp){ this.x = mp.x; this.y = mp.y; this.z = mp.z; + if (playerPos) + this.dir = _getDirFromRotation(playerPos.yaw); this.world = mp.world; }else{ // base it on the player's current location @@ -720,6 +723,7 @@ Used when placing torches so that they face towards the drone. this.x = playerPos.x; this.y = playerPos.y; this.z = playerPos.z; + this.dir = _getDirFromRotation(playerPos.yaw); this.world = playerPos.world; } }else{ @@ -746,14 +750,14 @@ Used when placing torches so that they face towards the drone. } } - // for debugging - //self.sendMessage("New Drone " + this.toString()); if (usePlayerCoords){ this.fwd(3); } this.chkpt('start'); this.record = true; this.history = []; + // for debugging + // self.sendMessage("New Drone " + this.toString()); return this; }; // diff --git a/src/main/javascript/minigames/NumberGuess.js b/src/main/javascript/minigames/NumberGuess.js new file mode 100644 index 0000000..b70d931 --- /dev/null +++ b/src/main/javascript/minigames/NumberGuess.js @@ -0,0 +1,48 @@ +/* + A basic number-guessing game that uses the Bukkit Conversation API. + */ +ready(function(){ + + global.GuessTheNumber = function() + { + importPackage(org.bukkit.conversations); + + var number = Math.ceil(Math.random() * 10); + + var prompt = new Prompt() + { + getPromptText: function(ctx){ + var hint = ""; + var h = ctx.getSessionData("hint"); + if (h){ + hint = h; + } + return hint + "Think of a number between 1 and 10"; + }, + acceptInput: function(ctx, s) + { + s = s.replace(/^[^0-9]+/,""); // strip leading non-numeric characters (e.g. '/' ) + s = parseInt(s); + if (s == number){ + setTimeout(function(){ + ctx.forWhom.sendRawMessage("You guessed Correct!"); + },100); + return null; + }else{ + if (s < number) + ctx.setSessionData("hint","too low\n"); + if (s > number) + ctx.setSessionData("hint","too high\n"); + return prompt; + } + }, + blocksForInput: function(ctx){ return true; } + }; + var cf = new ConversationFactory(__plugin); + var conv = cf.withModality(true) + .withFirstPrompt(prompt) + .withPrefix(new ConversationPrefix(){ getPrefix: function(ctx){ return "[1-10] ";} }) + .buildConversation(self); + conv.begin(); + }; +}); diff --git a/src/main/javascript/utils/utils.js b/src/main/javascript/utils/utils.js index 8a73907..f2165fe 100644 --- a/src/main/javascript/utils/utils.js +++ b/src/main/javascript/utils/utils.js @@ -178,10 +178,11 @@ Example To warn players when night is approaching... utils.at( "19:00", function() { - /* it's 7 in the evening so warn all players that night is coming ! */ + utils.foreach( server.onlinePlayers, function(player){ player.chat("The night is dark and full of terrors!"); }); + }, self.world); ***/