2014-09-30 00:42:41 +02:00
args = Array . prototype . slice . call ( args , 1 ) ;
// [0] = type, [1] = lib.jar [2] = blockX, [3] = classX
2014-04-26 17:56:57 +02:00
var File = java . io . File ,
FileReader = java . io . FileReader ,
FileInputStream = java . io . FileInputStream ,
out = java . lang . System . out ,
2014-05-18 01:35:26 +02:00
err = java . lang . System . err ,
Modifier = java . lang . reflect . Modifier ,
2014-06-14 16:36:56 +02:00
clz ,
2014-04-26 17:56:57 +02:00
ZipInputStream = java . util . zip . ZipInputStream ,
2014-09-30 00:42:41 +02:00
zis = new ZipInputStream ( new FileInputStream ( args [ 1 ] ) ) ,
2014-04-26 17:56:57 +02:00
entry = null ;
var content = [
'/*********************' ,
2014-11-09 12:52:10 +01:00
'## Events Helper Module (' + args [ 0 ] + ' version)' ,
2014-04-26 21:31:52 +02:00
'The Events helper module provides a suite of functions - one for each possible event.' ,
2014-09-30 00:42:41 +02:00
'For example, the events.' + args [ 2 ] + '() function is just a wrapper function which calls events.on(' + args [ 3 ] + ', callback, priority)' ,
2014-04-26 21:31:52 +02:00
'This module is a convenience wrapper for easily adding new event handling functions in Javascript. ' ,
'At the in-game or server-console prompt, players/admins can type `events.` and use TAB completion ' ,
'to choose from any of the approx. 160 different event types to listen to.' ,
'' ,
2014-04-26 17:56:57 +02:00
'### Usage' ,
'' ,
2014-09-30 00:42:41 +02:00
' events.' + args [ 2 ] + '( function( event ) { ' ,
' echo( event.player, \'You broke a block!\'); ' ,
2014-04-26 17:56:57 +02:00
' });' ,
'' ,
2014-06-14 16:36:56 +02:00
'The crucial difference is that the events module now has functions for each of the built-in events. The functions are accessible via TAB-completion so will help beginning programmers to explore the events at the server console window.' ,
2014-04-26 18:14:32 +02:00
'' ,
'***/'
2014-04-26 17:56:57 +02:00
] ;
2014-11-09 12:52:10 +01:00
var canary = false ;
if ( args [ 0 ] == 'canary' ) {
canary = true ;
}
2014-04-26 17:56:57 +02:00
for ( var i = 0 ; i < content . length ; i ++ ) {
out . println ( content [ i ] ) ;
}
while ( ( entry = zis . nextEntry ) != null ) {
2014-10-18 19:48:57 +02:00
var name = new String ( entry . name ) ;
2014-09-30 00:42:41 +02:00
var re1 = /org\/bukkit\/event\/.+Event\.class$/ ;
2014-11-09 12:52:10 +01:00
if ( canary ) {
2014-09-30 00:42:41 +02:00
re1 = /net\/canarymod\/hook\/.+Hook\.class$/ ;
}
2014-10-18 19:48:57 +02:00
if ( re1 . test ( name ) ) {
2014-04-26 17:56:57 +02:00
name = name . replace ( /\//g , '.' ) . replace ( '.class' , '' ) ;
2014-06-14 16:36:56 +02:00
try {
clz = java . lang . Class . forName ( name ) ;
} catch ( e ) {
2014-11-09 12:52:10 +01:00
err . println ( 'Warning: could not Class.forName("' + name + '")' ) ;
2014-06-14 16:36:56 +02:00
clz = engine . eval ( name ) ;
}
2014-05-18 01:35:26 +02:00
var isAbstract = Modifier . isAbstract ( clz . getModifiers ( ) ) ;
if ( isAbstract ) {
2014-04-26 21:20:10 +02:00
continue ;
}
2014-04-26 17:56:57 +02:00
var parts = name . split ( '.' ) ;
2014-09-30 00:42:41 +02:00
var shortName = null ;
2014-11-09 12:52:10 +01:00
if ( canary ) {
2014-09-30 00:42:41 +02:00
shortName = name . replace ( 'net.canarymod.hook.' , '' ) ;
}
2014-11-09 12:52:10 +01:00
if ( ! canary ) {
2014-09-30 00:42:41 +02:00
shortName = name . replace ( 'org.bukkit.event.' , '' ) ;
}
var fname = parts . reverse ( ) . shift ( ) . replace ( /^(.)/ , function ( a ) {
return a . toLowerCase ( ) ; } ) ;
2014-11-09 12:52:10 +01:00
if ( ! canary ) {
2014-09-30 00:42:41 +02:00
fname = fname . replace ( /Event$/ , '' ) ;
}
2014-11-09 12:52:10 +01:00
if ( canary ) {
2014-09-30 00:42:41 +02:00
fname = fname . replace ( /Hook$/ , '' ) ;
}
2014-11-09 12:52:10 +01:00
var javaDoc = canary ? 'https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/' : 'http://jd.bukkit.org/rb/apidocs/org/bukkit/event/' ;
2014-04-26 17:56:57 +02:00
var comment = [
'/*********************' ,
2014-04-26 18:03:08 +02:00
'### events.' + fname + '()' ,
2014-04-26 18:14:32 +02:00
'' ,
2014-04-26 17:56:57 +02:00
'#### Parameters ' ,
2014-04-26 18:14:32 +02:00
'' ,
2014-11-09 12:52:10 +01:00
' * callback - A function which is called whenever the [' + shortName + ' event](' + javaDoc + shortName . replace ( '.' , '/' ) + '.html) is fired' ,
2014-04-26 18:14:32 +02:00
'' ,
2014-04-26 21:24:45 +02:00
' * priority - optional - see events.on() for more information.' ,
2014-04-26 18:14:32 +02:00
'' ,
2014-04-26 17:56:57 +02:00
'***/'
2014-08-24 11:20:02 +02:00
//http://jd.bukkit.org/rb/apidocs/org/bukkit/event/player/PlayerJoinEvent.html
2014-04-26 17:56:57 +02:00
] ;
for ( var i = 0 ; i < comment . length ; i ++ ) {
out . println ( comment [ i ] ) ;
}
out . println ( 'exports.' + fname + ' = function(callback,priority){ ' ) ;
2014-09-30 00:42:41 +02:00
if ( args [ 0 ] == 'canary' ) {
out . println ( ' return events.on(Packages.' + name + ',callback,priority);' ) ;
}
if ( args [ 0 ] == 'bukkit' ) {
out . println ( ' return events.on(' + name + ',callback,priority);' ) ;
}
2014-04-26 17:56:57 +02:00
out . println ( '};' ) ;
}
}