From 5a415d9838b1f56d0b185c8f92af17bba95e83bb Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Thu, 8 Aug 2013 08:02:27 +0100 Subject: [PATCH 1/5] fix issue #90 --- docs/api.md | 3 ++- src/main/javascript/drone/drone.js | 10 +++++++--- src/main/javascript/utils/utils.js | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/api.md b/docs/api.md index bd7383d..51d4caa 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1389,10 +1389,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/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/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); ***/ From d3831bb91031696a555b56160f04adfc7ae253d2 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Sun, 11 Aug 2013 22:15:19 +0100 Subject: [PATCH 2/5] Disable CoffeeScript evaluation until issue #92 is fixed --- build.xml | 42 ++++++++++++------- .../scriptcraft/ScriptCraftPlugin.java | 11 +++-- 2 files changed, 35 insertions(+), 18 deletions(-) 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/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){ From 396a8e61076ca4f9617feea4e69f7ff9f282ce15 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Wed, 9 Oct 2013 18:42:39 +0100 Subject: [PATCH 3/5] Added setTimeout(), clearTimeout(), setInterval() and clearInterval() functions so javascript programmers familiar with these functions can use them in Minecraft --- docs/api.md | 32 ++++++++++++ src/main/javascript/core/_scriptcraft.js | 63 ++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/docs/api.md b/docs/api.md index 51d4caa..db37e32 100644 --- a/docs/api.md +++ b/docs/api.md @@ -232,6 +232,38 @@ See [issue #69][issue69] for more information. [issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 +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(). + Drone Module ============ The Drone is a convenience class for building. It can be used for... diff --git a/src/main/javascript/core/_scriptcraft.js b/src/main/javascript/core/_scriptcraft.js index 68a213b..4739d1b 100644 --- a/src/main/javascript/core/_scriptcraft.js +++ b/src/main/javascript/core/_scriptcraft.js @@ -698,7 +698,70 @@ See [issue #69][issue69] for more information. global.command = _command; global._onTabComplete = __onTabCompleteJS; global.addUnloadHandler = _addUnloadHandler; + +/************************************************************************* +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(); + }; + // // assumes this was loaded from js-plugins/core/ // load all of the plugins. From 53d04fade000be14ae866c7324edd5b5dffa9bcd Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Sun, 13 Oct 2013 21:08:04 +0100 Subject: [PATCH 4/5] A new simple number-guessing game to try out the Bukkit Conversation API --- src/main/javascript/minigames/NumberGuess.js | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/javascript/minigames/NumberGuess.js 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(); + }; +}); From 6f725423c0e7122e5d3f866c821f576809f20e8b Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Sun, 13 Oct 2013 21:08:31 +0100 Subject: [PATCH 5/5] reordered comments --- docs/api.md | 45 ++++---- src/main/javascript/core/_scriptcraft.js | 132 ++++++++++++----------- 2 files changed, 90 insertions(+), 87 deletions(-) diff --git a/docs/api.md b/docs/api.md index db37e32..6125c04 100644 --- a/docs/api.md +++ b/docs/api.md @@ -209,29 +209,6 @@ 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. - -Core Module - Special Variables -=============================== -There are a couple of special javascript variables available in ScriptCraft... - - * __folder - The current working directory - this variable is only to be used within the main body of a .js file. - * __plugin - The ScriptCraft JavaPlugin object. - * server - The Minecraft Server object. - * self - the current player. (Note - this value should not be used in multi-threaded scripts - it's not thread-safe) - -refresh() function ------------------- -The refresh() function will ... - -1. Disable the ScriptCraft plugin. -2. Unload all event listeners associated with the ScriptCraft plugin. -3. Enable the ScriptCraft plugin. - -... refresh() can be used during development to reload only scriptcraft javascript files. -See [issue #69][issue69] for more information. - -[issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 - setTimeout() function --------------------- @@ -264,6 +241,28 @@ clearInterval() function --------------------- A scriptcraft implementation of clearInterval(). +Core Module - Special Variables +=============================== +There are a couple of special javascript variables available in ScriptCraft... + + * __folder - The current working directory - this variable is only to be used within the main body of a .js file. + * __plugin - The ScriptCraft JavaPlugin object. + * server - The Minecraft Server object. + * self - the current player. (Note - this value should not be used in multi-threaded scripts - it's not thread-safe) + +refresh() function +------------------ +The refresh() function will ... + +1. Disable the ScriptCraft plugin. +2. Unload all event listeners associated with the ScriptCraft plugin. +3. Enable the ScriptCraft plugin. + +... refresh() can be used during development to reload only scriptcraft javascript files. +See [issue #69][issue69] for more information. + +[issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 + Drone Module ============ The Drone is a convenience class for building. It can be used for... diff --git a/src/main/javascript/core/_scriptcraft.js b/src/main/javascript/core/_scriptcraft.js index 4739d1b..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; @@ -699,68 +765,6 @@ See [issue #69][issue69] for more information. global._onTabComplete = __onTabCompleteJS; global.addUnloadHandler = _addUnloadHandler; -/************************************************************************* -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(); - }; // // assumes this was loaded from js-plugins/core/