diff --git a/docs/API-Reference.md b/docs/API-Reference.md index 70249b5..ac8b153 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -49,14 +49,14 @@ module.exports instead of exports. ## Module Loading When the ScriptCraft Java plugin is first installed, a new -subdirectory is created in the craftbukkit directory. If your +subdirectory is created in the craftbukkit/plugins directory. If your craftbukkit directory is called 'craftbukkit' then the new subdirectories will be ... - * craftbukkit/scriptcraft/ - * craftbukkit/scriptcraft/plugins - * craftbukkit/scriptcraft/modules - * craftbukkit/scriptcraft/lib + * craftbukkit/plugins/scriptcraft/ + * craftbukkit/plugins/scriptcraft/plugins + * craftbukkit/plugins/scriptcraft/modules + * craftbukkit/plugins/scriptcraft/lib ... The `plugins`, `modules` and `lib` directories each serve a different purpose. @@ -317,7 +317,7 @@ See chat/color.js for an example of a simple plugin - one which lets players choose a default chat color. See also [Anatomy of a ScriptCraft Plugin][anatomy]. -[anatomy]: http://walterhiggins.net/blog/ScriptCraft-1-Month-later +[anatomy]: ./Anatomy-of-a-Plugin.md ### command() function @@ -330,8 +330,18 @@ plugin author) safely expose javascript functions for use by players. #### Parameters - * commandName : The name to give your command - the command will be invoked like this by players `/jsp commandName` - * commandFunction: The javascript function which will be invoked when the command is invoked by a player. + * commandName : The name to give your command - the command will + be invoked like this by players `/jsp commandName` + * commandFunction: The javascript function which will be invoked when + the command is invoked by a player. The callback function in turn + takes 2 parameters... + + * params : An Array of type String - the list of parameters + passed to the command. + * sender : The [CommandSender][bukcs] object that invoked the + command (this is usually a Player object but can be a Block + ([BlockCommandSender][bukbcs]). + * options (Array - optional) : An array of command options/parameters which the player can supply (It's useful to supply an array so that Tab-Completion works for the `/jsp ` commands. @@ -393,7 +403,9 @@ A scriptcraft implementation of clearInterval(). ### refresh() function -The refresh() function will ... +The refresh() function can be used to only reload the ScriptCraft +plugin (it's like the `reload` command except it only reloads +ScriptCraft). The refresh() function will ... 1. Disable the ScriptCraft plugin. 2. Unload all event listeners associated with the ScriptCraft plugin. @@ -411,6 +423,10 @@ parameter. The callback will be called when the ScriptCraft plugin is unloaded (usually as a result of a a `reload` command or server shutdown). +This function provides a way for ScriptCraft modules to do any +required cleanup/housekeeping just prior to the ScriptCraft Plugin +unloading. + ## require - Node.js-style module loading in ScriptCraft Node.js is a server-side javascript environment with an excellent diff --git a/docs/YoungPersonsGuideToProgrammingMinecraft.md b/docs/YoungPersonsGuideToProgrammingMinecraft.md index 6f547be..88a296f 100644 --- a/docs/YoungPersonsGuideToProgrammingMinecraft.md +++ b/docs/YoungPersonsGuideToProgrammingMinecraft.md @@ -480,11 +480,11 @@ Once you've installed Notepad++, Launch it, create a new file and type the follo } ... then save the file in a new directory -`craftbukkit/scriptcraft/plugins/{your_name}` (replace {your_name} with your -own name) and call the file `greet.js` (be sure to change the file-type -option to '*.* All Files' when saving or NotePad++ will add a '.txt' -extension to the filename. Now switch back to the Minecraft game and -type... +`craftbukkit/plugins/scriptcraft/plugins/{your_name}` (replace +{your_name} with your own name) and call the file `greet.js` (be sure +to change the file-type option to '*.* All Files' when saving or +NotePad++ will add a '.txt' extension to the filename. Now switch back +to the Minecraft game and type... /js refresh() @@ -501,10 +501,10 @@ loaded. Try it out by typing this command... minecraft username. Congratulations - You've just written your very first Minecraft Mod! With ScriptCraft installed, writing Minecraft Mods is as simple as writing a new javascript function and saving it -in a file in the craftbukkit/scriptcraft/plugins directory. This -function will now be avaible every time you launch minecraft. This is -a deliberately trivial minecraft mod but the principles are the same -when creating more complex mods. +in a file in the craftbukkit/plugins/scriptcraft/plugins +directory. This function will now be avaible every time you launch +minecraft. This is a deliberately trivial minecraft mod but the +principles are the same when creating more complex mods. The `exports` variable is a special variable you can use in your mod to provide functions, objects and variables for others to use. If you diff --git a/src/main/javascript/lib/command.js b/src/main/javascript/lib/command.js index 67b6ace..411aa17 100644 --- a/src/main/javascript/lib/command.js +++ b/src/main/javascript/lib/command.js @@ -1,3 +1,4 @@ +'use strict'; /* command management - allow for non-ops to execute approved javascript code. */ diff --git a/src/main/javascript/lib/console.js b/src/main/javascript/lib/console.js index 122af34..91c437b 100644 --- a/src/main/javascript/lib/console.js +++ b/src/main/javascript/lib/console.js @@ -1,3 +1,4 @@ +'use strict'; /************************************************************************* ## console global variable diff --git a/src/main/javascript/lib/events.js b/src/main/javascript/lib/events.js index fdb4357..4945955 100644 --- a/src/main/javascript/lib/events.js +++ b/src/main/javascript/lib/events.js @@ -1,3 +1,4 @@ +'use strict'; /************************************************************************ ## events Module diff --git a/src/main/javascript/lib/plugin.js b/src/main/javascript/lib/plugin.js index c43503c..87d7978 100644 --- a/src/main/javascript/lib/plugin.js +++ b/src/main/javascript/lib/plugin.js @@ -1,3 +1,4 @@ +'use strict'; var console = require('./console'); var File = java.io.File; var FileWriter = java.io.FileWriter; diff --git a/src/main/javascript/lib/require.js b/src/main/javascript/lib/require.js index 1cd1677..5bce81e 100644 --- a/src/main/javascript/lib/require.js +++ b/src/main/javascript/lib/require.js @@ -54,7 +54,7 @@ module specification, the '.js' suffix is optional. [cjsmodules]: http://wiki.commonjs.org/wiki/Modules/1.1.1. ***/ -( function (logger, evaluator, verbose, rootDir, modulePaths) { +(function (logger, evaluator, verbose, rootDir, modulePaths) { if (verbose){ logger.info("Setting up 'require' module system. Root Directory: " + rootDir); @@ -260,3 +260,4 @@ When resolving module names to file paths, ScriptCraft uses the following rules. }; return _requireClosure(new java.io.File(rootDir)); }) + diff --git a/src/main/javascript/lib/scriptcraft.js b/src/main/javascript/lib/scriptcraft.js index ec34e01..5cce36d 100644 --- a/src/main/javascript/lib/scriptcraft.js +++ b/src/main/javascript/lib/scriptcraft.js @@ -1,3 +1,4 @@ +'use strict'; /************************************************************************ # ScriptCraft API Reference @@ -50,14 +51,14 @@ module.exports instead of exports. ## Module Loading When the ScriptCraft Java plugin is first installed, a new -subdirectory is created in the craftbukkit directory. If your +subdirectory is created in the craftbukkit/plugins directory. If your craftbukkit directory is called 'craftbukkit' then the new subdirectories will be ... - * craftbukkit/scriptcraft/ - * craftbukkit/scriptcraft/plugins - * craftbukkit/scriptcraft/modules - * craftbukkit/scriptcraft/lib + * craftbukkit/plugins/scriptcraft/ + * craftbukkit/plugins/scriptcraft/plugins + * craftbukkit/plugins/scriptcraft/modules + * craftbukkit/plugins/scriptcraft/lib ... The `plugins`, `modules` and `lib` directories each serve a different purpose. @@ -318,7 +319,7 @@ See chat/color.js for an example of a simple plugin - one which lets players choose a default chat color. See also [Anatomy of a ScriptCraft Plugin][anatomy]. -[anatomy]: http://walterhiggins.net/blog/ScriptCraft-1-Month-later +[anatomy]: ./Anatomy-of-a-Plugin.md ### command() function @@ -331,8 +332,18 @@ plugin author) safely expose javascript functions for use by players. #### Parameters - * commandName : The name to give your command - the command will be invoked like this by players `/jsp commandName` - * commandFunction: The javascript function which will be invoked when the command is invoked by a player. + * commandName : The name to give your command - the command will + be invoked like this by players `/jsp commandName` + * commandFunction: The javascript function which will be invoked when + the command is invoked by a player. The callback function in turn + takes 2 parameters... + + * params : An Array of type String - the list of parameters + passed to the command. + * sender : The [CommandSender][bukcs] object that invoked the + command (this is usually a Player object but can be a Block + ([BlockCommandSender][bukbcs]). + * options (Array - optional) : An array of command options/parameters which the player can supply (It's useful to supply an array so that Tab-Completion works for the `/jsp ` commands. @@ -394,7 +405,9 @@ A scriptcraft implementation of clearInterval(). ### refresh() function -The refresh() function will ... +The refresh() function can be used to only reload the ScriptCraft +plugin (it's like the `reload` command except it only reloads +ScriptCraft). The refresh() function will ... 1. Disable the ScriptCraft plugin. 2. Unload all event listeners associated with the ScriptCraft plugin. @@ -412,6 +425,10 @@ parameter. The callback will be called when the ScriptCraft plugin is unloaded (usually as a result of a a `reload` command or server shutdown). +This function provides a way for ScriptCraft modules to do any +required cleanup/housekeeping just prior to the ScriptCraft Plugin +unloading. + ***/ /* @@ -437,8 +454,8 @@ function __onEnable (__engine, __plugin, __script) return "" + file.getCanonicalPath().replaceAll("\\\\","/"); }; - var parentFileObj = __script.parentFile; - var jsPluginsRootDir = parentFileObj.parentFile; + var libDir = __script.parentFile; // lib (assumes scriptcraft.js is in craftbukkit/plugins/scriptcraft/lib directory + var jsPluginsRootDir = libDir.parentFile; // scriptcraft var jsPluginsRootDirName = _canonize(jsPluginsRootDir); var _loaded = {}; @@ -466,11 +483,13 @@ function __onEnable (__engine, __plugin, __script) var reader = new FileReader(file); var br = new BufferedReader(reader); var code = ""; + var wrappedCode; try{ while ((r = br.readLine()) !== null) code += r + "\n"; - - result = __engine.eval("(" + code + ")"); + + wrappedCode = "(" + code + ")"; + result = __engine.eval(wrappedCode); // issue #103 avoid side-effects of || operator on Mac Rhino _loaded[canonizedFilename] = result ; if (!_loaded[canonizedFilename]) @@ -619,4 +638,23 @@ function __onEnable (__engine, __plugin, __script) } return result; }; + /* + wph 20140102 - warn if legacy 'craftbukkit/js-plugins' or 'craftbukkit/scriptcraft' directories are present + */ + var cbPluginsDir = jsPluginsRootDir.parentFile; + var cbDir = new File(cbPluginsDir.canonicalPath).parentFile; + var legacyDirs = [ + new File(cbDir, 'js-plugins'), + new File(cbDir, 'scriptcraft') + ]; + var legacyExists = false; + for (var i = 0; i < legacyDirs.length; i++){ + if (legacyDirs[i].exists() && legacyDirs[i].isDirectory()){ + legacyExists = true; + console.warn('Legacy ScriptCraft directory ' + legacyDirs[i].canonicalPath + ' was found. This directory is no longer used.'); + } + } + if (legacyExists){ + console.info('Please note that the working directory for ' + __plugin + ' is ' + jsPluginsRootDir.canonicalPath); + } } diff --git a/src/main/javascript/lib/tabcomplete-jsp.js b/src/main/javascript/lib/tabcomplete-jsp.js index 8a0c129..e2106ed 100644 --- a/src/main/javascript/lib/tabcomplete-jsp.js +++ b/src/main/javascript/lib/tabcomplete-jsp.js @@ -1,3 +1,4 @@ +'use strict'; var _commands = require('command').commands; /* Tab completion for the /jsp commmand diff --git a/src/main/javascript/lib/tabcomplete.js b/src/main/javascript/lib/tabcomplete.js index 885f6d0..9b85d3d 100644 --- a/src/main/javascript/lib/tabcomplete.js +++ b/src/main/javascript/lib/tabcomplete.js @@ -1,3 +1,4 @@ +'use strict'; var tabCompleteJSP = require('tabcomplete-jsp'); /* Tab Completion of the /js and /jsp commands diff --git a/src/main/javascript/modules/utils/string-exts.js b/src/main/javascript/modules/utils/string-exts.js index 8be2180..bb33e23 100644 --- a/src/main/javascript/modules/utils/string-exts.js +++ b/src/main/javascript/modules/utils/string-exts.js @@ -1,3 +1,4 @@ +'use strict'; /************************************************************************ String class extensions ----------------------- @@ -61,7 +62,6 @@ var formattingCodes = { italic: c.ITALIC, lightpurple: c.LIGHT_PURPLE, indigo: c.BLUE, - green: c.GREEN, red: c.RED, pink: c.LIGHT_PURPLE, yellow: c.YELLOW, diff --git a/src/main/javascript/modules/utils/utils.js b/src/main/javascript/modules/utils/utils.js index 1e45a3b..9ed6730 100644 --- a/src/main/javascript/modules/utils/utils.js +++ b/src/main/javascript/modules/utils/utils.js @@ -1,3 +1,4 @@ +'use strict'; /************************************************************************ ## Utilities Module