Added 'use strict' to lib modules. Added legacy directory check

This commit is contained in:
walterhiggins 2014-01-02 18:46:46 +00:00
parent 8a65765f08
commit 4803f3027a
12 changed files with 95 additions and 33 deletions

View file

@ -49,14 +49,14 @@ module.exports instead of exports.
## Module Loading ## Module Loading
When the ScriptCraft Java plugin is first installed, a new 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 craftbukkit directory is called 'craftbukkit' then the new
subdirectories will be ... subdirectories will be ...
* craftbukkit/scriptcraft/ * craftbukkit/plugins/scriptcraft/
* craftbukkit/scriptcraft/plugins * craftbukkit/plugins/scriptcraft/plugins
* craftbukkit/scriptcraft/modules * craftbukkit/plugins/scriptcraft/modules
* craftbukkit/scriptcraft/lib * craftbukkit/plugins/scriptcraft/lib
... The `plugins`, `modules` and `lib` directories each serve a different purpose. ... 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 players choose a default chat color. See also [Anatomy of a
ScriptCraft Plugin][anatomy]. ScriptCraft Plugin][anatomy].
[anatomy]: http://walterhiggins.net/blog/ScriptCraft-1-Month-later [anatomy]: ./Anatomy-of-a-Plugin.md
### command() function ### command() function
@ -330,8 +330,18 @@ plugin author) safely expose javascript functions for use by players.
#### Parameters #### Parameters
* commandName : The name to give your command - the command will be invoked like this by players `/jsp commandName` * commandName : The name to give your command - the command will
* commandFunction: The javascript function which will be invoked when the command is invoked by a player. 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 * options (Array - optional) : An array of command options/parameters
which the player can supply (It's useful to supply an array so that which the player can supply (It's useful to supply an array so that
Tab-Completion works for the `/jsp ` commands. Tab-Completion works for the `/jsp ` commands.
@ -393,7 +403,9 @@ A scriptcraft implementation of clearInterval().
### refresh() function ### 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. 1. Disable the ScriptCraft plugin.
2. Unload all event listeners associated with 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 unloaded (usually as a result of a a `reload` command or server
shutdown). 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 ## require - Node.js-style module loading in ScriptCraft
Node.js is a server-side javascript environment with an excellent Node.js is a server-side javascript environment with an excellent

View file

@ -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 ... then save the file in a new directory
`craftbukkit/scriptcraft/plugins/{your_name}` (replace {your_name} with your `craftbukkit/plugins/scriptcraft/plugins/{your_name}` (replace
own name) and call the file `greet.js` (be sure to change the file-type {your_name} with your own name) and call the file `greet.js` (be sure
option to '*.* All Files' when saving or NotePad++ will add a '.txt' to change the file-type option to '*.* All Files' when saving or
extension to the filename. Now switch back to the Minecraft game and NotePad++ will add a '.txt' extension to the filename. Now switch back
type... to the Minecraft game and type...
/js refresh() /js refresh()
@ -501,10 +501,10 @@ loaded. Try it out by typing this command...
minecraft username. Congratulations - You've just written your very minecraft username. Congratulations - You've just written your very
first Minecraft Mod! With ScriptCraft installed, writing Minecraft first Minecraft Mod! With ScriptCraft installed, writing Minecraft
Mods is as simple as writing a new javascript function and saving it Mods is as simple as writing a new javascript function and saving it
in a file in the craftbukkit/scriptcraft/plugins directory. This in a file in the craftbukkit/plugins/scriptcraft/plugins
function will now be avaible every time you launch minecraft. This is directory. This function will now be avaible every time you launch
a deliberately trivial minecraft mod but the principles are the same minecraft. This is a deliberately trivial minecraft mod but the
when creating more complex mods. principles are the same when creating more complex mods.
The `exports` variable is a special variable you can use in your mod 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 to provide functions, objects and variables for others to use. If you

View file

@ -1,3 +1,4 @@
'use strict';
/* /*
command management - allow for non-ops to execute approved javascript code. command management - allow for non-ops to execute approved javascript code.
*/ */

View file

@ -1,3 +1,4 @@
'use strict';
/************************************************************************* /*************************************************************************
## console global variable ## console global variable

View file

@ -1,3 +1,4 @@
'use strict';
/************************************************************************ /************************************************************************
## events Module ## events Module

View file

@ -1,3 +1,4 @@
'use strict';
var console = require('./console'); var console = require('./console');
var File = java.io.File; var File = java.io.File;
var FileWriter = java.io.FileWriter; var FileWriter = java.io.FileWriter;

View file

@ -54,7 +54,7 @@ module specification, the '.js' suffix is optional.
[cjsmodules]: http://wiki.commonjs.org/wiki/Modules/1.1.1. [cjsmodules]: http://wiki.commonjs.org/wiki/Modules/1.1.1.
***/ ***/
( function (logger, evaluator, verbose, rootDir, modulePaths) { (function (logger, evaluator, verbose, rootDir, modulePaths) {
if (verbose){ if (verbose){
logger.info("Setting up 'require' module system. Root Directory: " + rootDir); 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)); return _requireClosure(new java.io.File(rootDir));
}) })

View file

@ -1,3 +1,4 @@
'use strict';
/************************************************************************ /************************************************************************
# ScriptCraft API Reference # ScriptCraft API Reference
@ -50,14 +51,14 @@ module.exports instead of exports.
## Module Loading ## Module Loading
When the ScriptCraft Java plugin is first installed, a new 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 craftbukkit directory is called 'craftbukkit' then the new
subdirectories will be ... subdirectories will be ...
* craftbukkit/scriptcraft/ * craftbukkit/plugins/scriptcraft/
* craftbukkit/scriptcraft/plugins * craftbukkit/plugins/scriptcraft/plugins
* craftbukkit/scriptcraft/modules * craftbukkit/plugins/scriptcraft/modules
* craftbukkit/scriptcraft/lib * craftbukkit/plugins/scriptcraft/lib
... The `plugins`, `modules` and `lib` directories each serve a different purpose. ... 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 players choose a default chat color. See also [Anatomy of a
ScriptCraft Plugin][anatomy]. ScriptCraft Plugin][anatomy].
[anatomy]: http://walterhiggins.net/blog/ScriptCraft-1-Month-later [anatomy]: ./Anatomy-of-a-Plugin.md
### command() function ### command() function
@ -331,8 +332,18 @@ plugin author) safely expose javascript functions for use by players.
#### Parameters #### Parameters
* commandName : The name to give your command - the command will be invoked like this by players `/jsp commandName` * commandName : The name to give your command - the command will
* commandFunction: The javascript function which will be invoked when the command is invoked by a player. 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 * options (Array - optional) : An array of command options/parameters
which the player can supply (It's useful to supply an array so that which the player can supply (It's useful to supply an array so that
Tab-Completion works for the `/jsp ` commands. Tab-Completion works for the `/jsp ` commands.
@ -394,7 +405,9 @@ A scriptcraft implementation of clearInterval().
### refresh() function ### 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. 1. Disable the ScriptCraft plugin.
2. Unload all event listeners associated with 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 unloaded (usually as a result of a a `reload` command or server
shutdown). 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("\\\\","/"); return "" + file.getCanonicalPath().replaceAll("\\\\","/");
}; };
var parentFileObj = __script.parentFile; var libDir = __script.parentFile; // lib (assumes scriptcraft.js is in craftbukkit/plugins/scriptcraft/lib directory
var jsPluginsRootDir = parentFileObj.parentFile; var jsPluginsRootDir = libDir.parentFile; // scriptcraft
var jsPluginsRootDirName = _canonize(jsPluginsRootDir); var jsPluginsRootDirName = _canonize(jsPluginsRootDir);
var _loaded = {}; var _loaded = {};
@ -466,11 +483,13 @@ function __onEnable (__engine, __plugin, __script)
var reader = new FileReader(file); var reader = new FileReader(file);
var br = new BufferedReader(reader); var br = new BufferedReader(reader);
var code = ""; var code = "";
var wrappedCode;
try{ try{
while ((r = br.readLine()) !== null) while ((r = br.readLine()) !== null)
code += r + "\n"; code += r + "\n";
result = __engine.eval("(" + code + ")"); wrappedCode = "(" + code + ")";
result = __engine.eval(wrappedCode);
// issue #103 avoid side-effects of || operator on Mac Rhino // issue #103 avoid side-effects of || operator on Mac Rhino
_loaded[canonizedFilename] = result ; _loaded[canonizedFilename] = result ;
if (!_loaded[canonizedFilename]) if (!_loaded[canonizedFilename])
@ -619,4 +638,23 @@ function __onEnable (__engine, __plugin, __script)
} }
return result; 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);
}
} }

View file

@ -1,3 +1,4 @@
'use strict';
var _commands = require('command').commands; var _commands = require('command').commands;
/* /*
Tab completion for the /jsp commmand Tab completion for the /jsp commmand

View file

@ -1,3 +1,4 @@
'use strict';
var tabCompleteJSP = require('tabcomplete-jsp'); var tabCompleteJSP = require('tabcomplete-jsp');
/* /*
Tab Completion of the /js and /jsp commands Tab Completion of the /js and /jsp commands

View file

@ -1,3 +1,4 @@
'use strict';
/************************************************************************ /************************************************************************
String class extensions String class extensions
----------------------- -----------------------
@ -61,7 +62,6 @@ var formattingCodes = {
italic: c.ITALIC, italic: c.ITALIC,
lightpurple: c.LIGHT_PURPLE, lightpurple: c.LIGHT_PURPLE,
indigo: c.BLUE, indigo: c.BLUE,
green: c.GREEN,
red: c.RED, red: c.RED,
pink: c.LIGHT_PURPLE, pink: c.LIGHT_PURPLE,
yellow: c.YELLOW, yellow: c.YELLOW,

View file

@ -1,3 +1,4 @@
'use strict';
/************************************************************************ /************************************************************************
## Utilities Module ## Utilities Module