This repository has been archived on 2021-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
scriptcraft/src/generateEventsHelper.js
walterhiggins a4968d8dc6 Events handling changes. Added new convenience functions to events module
to make event handling easier still for newbies
2014-04-26 16:56:57 +01:00

61 lines
2.2 KiB
JavaScript

var File = java.io.File,
FileReader = java.io.FileReader,
FileInputStream = java.io.FileInputStream,
out = java.lang.System.out,
ZipInputStream = java.util.zip.ZipInputStream,
zis = new ZipInputStream(new FileInputStream('./target/minecraft/craftbukkit.jar')),
entry = null;
var content = [
'/*********************',
'## Events Helper Module',
'The Events helper module provides a suite of functions one for each possible event.',
'For example, the blockBreak function in turn calls events.on(org.bukkit.event.block.BlockBreakEvent, callback, priority)',
'This module is a convenience wrapper for easily add new event handlers.',
'### Usage',
'',
' events.blockBreak(function(evt){ ',
' evt.player.sendMessage("You broke a block!"); ',
' });',
'',
'... which is just a shorter way of writing ...',
'',
' events.on("block.BlockBreakEvent",function(evt){ ',
' evt.player.sendMessage("You broke a block!");',
' });',
'',
'The crucial difference is that the events module will have functions for each ',
'of the built-in events so tab-completion will help ',
'beginning programmers to explore the events at the server console window.',
''
];
for (var i = 0; i< content.length; i++){
out.println(content[i]);
}
while ( ( entry = zis.nextEntry) != null) {
var name = '' + entry.name;
if (name.match(/org\/bukkit\/event\/.+Event\.class$/)){
name = name.replace(/\//g,'.').replace('.class','');
var parts = name.split('.');
var shortName = name.replace('org.bukkit.event.','');
var fname = parts.reverse().shift().replace(/^(.)/,function(a){ return a.toLowerCase()}).replace(/Event$/,'');
var comment = [
'/*********************',
'### events.' + fname + '(callback)',
'#### Parameters ',
' * callback - A function which is called whenever the ' + shortName + ' event is fired',
'see events.on() for more information',
'***/'
];
for (var i = 0; i < comment.length; i++){
out.println(comment[i]);
}
out.println('exports.' + fname + ' = function(callback,priority){ ');
out.println(' return events.on(' + name + ',callback,priority);');
out.println('};');
}
}