This commit is contained in:
walterhiggins 2013-12-24 00:16:07 +00:00
parent eb098ff176
commit 4d807373da
2 changed files with 234 additions and 90 deletions

View file

@ -45,12 +45,16 @@ Example:
------ ------
The following code will print a message on screen every time a block is broken in the game The following code will print a message on screen every time a block is broken in the game
var events = require('./events/events');
events.on("block.BlockBreakEvent", function(listener, evt){ events.on("block.BlockBreakEvent", function(listener, evt){
echo (evt.player.name + " broke a block!"); echo (evt.player.name + " broke a block!");
}); });
To handle an event only once and unregister from further events... To handle an event only once and unregister from further events...
var events = require('./events/events');
events.on("block.BlockBreakEvent", function(listener, evt){ events.on("block.BlockBreakEvent", function(listener, evt){
print (evt.player.name + " broke a block!"); print (evt.player.name + " broke a block!");
evt.handlers.unregister(listener); evt.handlers.unregister(listener);
@ -58,6 +62,8 @@ To handle an event only once and unregister from further events...
To unregister a listener *outside* of the listener function... To unregister a listener *outside* of the listener function...
var events = require('./events/events');
var myBlockBreakListener = events.on("block.BlockBreakEvent",function(l,e){ ... }); var myBlockBreakListener = events.on("block.BlockBreakEvent",function(l,e){ ... });
... ...
var handlers = org.bukkit.event.block.BlockBreakEvent.getHandlerList(); var handlers = org.bukkit.event.block.BlockBreakEvent.getHandlerList();
@ -68,35 +74,27 @@ To unregister a listener *outside* of the listener function...
***/ ***/
var events = events || {
//
// handle events in Minecraft
// --------------------------
// eventType can be a string (assumed to be a sub package of org.bukkit.event - e.g.
// if the string "block.BlockBreakEvent" is supplied then it's converted to the
// org.bukkit.event.block.BlockBreakEvent class . For custom event classes, just
// supply the custom event class e.g.
// events.on(net.yourdomain.events.YourCustomEvent,function(l,e){ ... });
//
on: function(
/* String or java Class */ eventType,
/* function( registeredListener, event) */ handler,
/* (optional) String (HIGH, HIGHEST, LOW, LOWEST, NORMAL, MONITOR), */ priority
){}
};
// //
// private implementation from here on in... // handle events in Minecraft
// --------------------------
// eventType can be a string (assumed to be a sub package of org.bukkit.event - e.g.
// if the string "block.BlockBreakEvent" is supplied then it's converted to the
// org.bukkit.event.block.BlockBreakEvent class . For custom event classes, just
// supply the custom event class e.g.
// events.on(net.yourdomain.events.YourCustomEvent,function(l,e){ ... });
// //
(function(events){ var bkEvent = org.bukkit.event;
if (events._eventsLoaded){ var bkEvtExecutor = org.bukkit.plugin.EventExecutor;
return; var bkRegListener = org.bukkit.plugin.RegisteredListener;
}
var bkEvent = org.bukkit.event; exports.on = function(
var bkEvtExecutor = org.bukkit.plugin.EventExecutor; /* String or java Class */
var bkRegListener = org.bukkit.plugin.RegisteredListener; eventType,
/* function( registeredListener, event) */
handler,
/* (optional) String (HIGH, HIGHEST, LOW, LOWEST, NORMAL, MONITOR), */
priority ) {
var _on = function(eventType, handler, priority)
{
if (typeof priority == "undefined"){ if (typeof priority == "undefined"){
priority = bkEvent.EventPriority.HIGHEST; priority = bkEvent.EventPriority.HIGHEST;
}else{ }else{
@ -127,7 +125,4 @@ var events = events || {
listener.reg = new bkRegListener( __plugin, eventExecutor, priority, __plugin, true); listener.reg = new bkRegListener( __plugin, eventExecutor, priority, __plugin, true);
handlerList.register(listener.reg); handlerList.register(listener.reg);
return listener.reg; return listener.reg;
}; };
events.on = _on;
events._eventsLoaded = true;
}(events));

View file

@ -55,7 +55,153 @@ 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(__plugin, __engine, verbose){ ( function (logger, evaluator, verbose, rootDir) {
if (verbose)
logger.info("Setting up 'require' module system. Root Directory: " + rootDir);
var File = java.io.File;
var readModuleFromDirectory = function(dir){
// look for a package.json file
var pkgJsonFile = new File(dir, './package.json');
if (pkgJsonFile.exists()){
var pkg = load(pkgJsonFile);
var mainFile = new File(dir, pkg.main);
if (mainFile.exists()){
return mainFile;
} else {
return null;
}
}else{
// look for an index.js file
var indexJsFile = new File(dir + './index.js');
if (indexJsFile.exists()){
return indexJsFile;
} else {
return null;
}
}
};
var LIB_DIR = rootDir + '/lib/';
var MODULE_DIR = rootDir + '/modules/';
var resolveModuleToFile = function(moduleName, parentDir) {
/**********************************************************************
## When resolving module names to file paths, ScriptCraft uses the following rules...
1. if the module does not begin with './' or '/' then ...
1.1 Look in the 'scriptcraft/lib' directory. If it's not there then...
1.2 Look in the 'scriptcraft/modules' directory. If it's not there then
Throw an Error.
2. If the module begins with './' or '/' then ...
2.1 if the module begins with './' then it's treated as a file path. File paths are
always relative to the module from which the require() call is being made.
2.2 If the module begins with '/' then it's treated as an absolute path.
If the module does not have a '.js' suffix, and a file with the same name and a .js sufix exists,
then the file will be loaded.
3. If the module name resolves to a directory then...
3.1 look for a package.json file in the directory and load the `main` property e.g.
// package.json located in './some-library/'
{
"main": './some-lib.js',
"name": 'some-library'
}
3.2 if no package.json file exists then look for an index.js file in the directory
***/
var file = new File(moduleName);
var fileExists = function(file) {
if (file.isDirectory()){
return readModuleFromDirectory(file);
}else {
return file;
}
};
if (file.exists()){
return fileExists(file);
}
if (moduleName.match(/^[^\.\/]/)){
// it's a module named like so ... 'events' , 'net/http'
//
var resolvedFile = new File(LIB_DIR + moduleName);
if (resolvedFile.exists()){
return fileExists(resolvedFile);
} else{
// try appending a .js to the end
resolvedFile = new File(LIB_DIR + moduleName + '.js');
if (resolvedFile.exists()){
return resolvedFile;
}else{
if (verbose){
logger.info("File not found in " + LIB_DIR + ': ' + resolvedFile.canonicalPath);
}
resolvedFile = new File(MODULE_DIR + moduleName);
if (resolvedFile.exists()){
return fileExists(resolvedFile);
}else {
if (verbose){
logger.info("File not found in " + MODULE_DIR + ': ' + resolvedFile.canonicalPath);
}
resolvedFile = new File(MODULE_DIR + moduleName + '.js');
if (resolvedFile.exists())
return resolvedFile;
else{
if (verbose){
logger.info("File not found in " + MODULE_DIR + ': ' + resolvedFile.canonicalPath);
}
}
}
}
}
} else {
// it's of the form ./path
file = new File(parentDir, moduleName);
if (file.exists()){
if (file.isDirectory()){
return readModuleFromDirectory(file);
}else {
return file;
}
}else {
// try appending a .js to the end
var pathWithJSExt = file.canonicalPath + '.js';
file = new File( parentDir, pathWithJSExt);
if (file.exists())
return file;
else{
file = new File(pathWithJSExt);
if (file.exists())
return file;
}
}
}
return null;
};
/* /*
wph 20131215 Experimental wph 20131215 Experimental
*/ */
@ -67,24 +213,9 @@ module specification, the '.js' suffix is optional.
return "" + file.canonicalPath.replaceAll("\\\\","/"); return "" + file.canonicalPath.replaceAll("\\\\","/");
}; };
var file = new java.io.File(parentFile, path); var file = resolveModuleToFile(path, parentFile);
if (!file.exists()) if (!file){
{ throw new Error("require('" + path + "'," + parentFile.canonicalPath + ") failed");
if (path.match(/\.js$/i)){
__plugin.logger.warning('require("' + path + '") failed. File [' + file.canonicalPath + '] not found');
return;
}else{
path = path + '.js';
file = new java.io.File(parentFile, path);
if (!file.exists()){
__plugin.logger.warning('require("' + path + '") failed. File [' + file.canonicalPath + '] not found');
return;
}
}
}
if (file.isDirectory()){
__plugin.logger.warning('require("' + path + '") directories not yet supported. ' + file.canonicalPath);
return;
} }
var canonizedFilename = _canonize(file); var canonizedFilename = _canonize(file);
@ -93,12 +224,14 @@ module specification, the '.js' suffix is optional.
return moduleInfo; return moduleInfo;
} }
if (verbose){ if (verbose){
print("loading module " + canonizedFilename); logger.info("loading module " + canonizedFilename);
} }
var reader = new java.io.FileReader(file); var reader = new java.io.FileReader(file);
var br = new java.io.BufferedReader(reader); var br = new java.io.BufferedReader(reader);
var code = ""; var code = "";
while ((r = br.readLine()) !== null) code += r + "\n"; var r = null;
while ((r = br.readLine()) !== null)
code += r + "\n";
var head = "(function(exports,module,require,__filename,__dirname){ "; var head = "(function(exports,module,require,__filename,__dirname){ ";
@ -111,12 +244,28 @@ module specification, the '.js' suffix is optional.
code = head + code + tail; code = head + code + tail;
_loadedModules[canonizedFilename] = moduleInfo; _loadedModules[canonizedFilename] = moduleInfo;
moduleInfo.main = __engine.eval(code); var compiledWrapper = null;
moduleInfo.main(moduleInfo.exports, try {
compiledWrapper = evaluator.eval(code);
}catch (e){
logger.severe("Error:" + e + " while evaluating module " + canonizedFilename);
throw e;
}
var __dirname = file.parentFile.canonicalPath;
try {
compiledWrapper.apply(moduleInfo.exports,
[moduleInfo.exports,
moduleInfo, moduleInfo,
_requireClosure(file.parentFile), _requireClosure(file.parentFile),
canonizedFilename, canonizedFilename,
"" + parentFile?parentFile.canonicalPath:""); "" + __dirname]);
} catch (e){
logger.severe("Error:" + e + " while executing module " + canonizedFilename);
throw e;
}
if (verbose)
logger.info("loaded module " + canonizedFilename);
moduleInfo.loaded = true; moduleInfo.loaded = true;
return moduleInfo; return moduleInfo;
}; };
@ -127,5 +276,5 @@ module specification, the '.js' suffix is optional.
return module.exports; return module.exports;
}; };
}; };
return _requireClosure(new java.io.File("./")); return _requireClosure(new java.io.File(rootDir));
}) })