From 2ba3335a16c43ff13d634361c23f2ac078768e35 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Sat, 26 Apr 2014 20:20:10 +0100 Subject: [PATCH] Omit abstract event classes from events-helper. Improved tab completion for jre8 --- build.properties | 4 +- build.xml | 1 + docs/API-Reference.md | 146 ++---------------- src/generateEventsHelper.js | 7 + src/main/js/lib/java-utils.js | 3 + src/main/js/lib/scriptcraft.js | 7 +- src/main/js/lib/tabcomplete.js | 10 +- src/main/js/modules/signs/menu.js | 2 +- src/main/js/plugins/alias/alias.js | 4 +- src/main/js/plugins/arrows.js | 2 +- src/main/js/plugins/chat/color.js | 2 +- src/main/js/plugins/classroom/classroom.js | 2 +- src/main/js/plugins/commando/commando.js | 4 +- src/main/js/plugins/drone/drone.js | 2 +- .../examples/example-7-hello-events.js | 9 +- .../js/plugins/minigames/SnowballFight.js | 3 +- src/main/js/plugins/minigames/cow-clicker.js | 6 +- 17 files changed, 55 insertions(+), 159 deletions(-) diff --git a/build.properties b/build.properties index eacfba2..254474c 100644 --- a/build.properties +++ b/build.properties @@ -1,2 +1,2 @@ -bukkit-version=1.7.2 -scriptcraft-version=2.0.6 +bukkit-version=1.7.9 +scriptcraft-version=2.0.8 diff --git a/build.xml b/build.xml index 50340c2..8a1fe2c 100644 --- a/build.xml +++ b/build.xml @@ -89,6 +89,7 @@ + diff --git a/docs/API-Reference.md b/docs/API-Reference.md index 25301b4..0049729 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -48,9 +48,6 @@ Walter Higgins * [Using string substitutions](#using-string-substitutions) * [Events Helper Module](#events-helper-module) * [Usage](#usage) - * [events.hanging()](#eventshanging) - * [events.world()](#eventsworld) - * [events.chunk()](#eventschunk) * [events.worldUnload()](#eventsworldunload) * [events.worldLoad()](#eventsworldload) * [events.chunkLoad()](#eventschunkload) @@ -59,7 +56,6 @@ Walter Higgins * [events.spawnChange()](#eventsspawnchange) * [events.chunkUnload()](#eventschunkunload) * [events.worldInit()](#eventsworldinit) - * [events.entity()](#eventsentity) * [events.horseJump()](#eventshorsejump) * [events.entityCombust()](#eventsentitycombust) * [events.entityRegainHealth()](#eventsentityregainhealth) @@ -92,30 +88,22 @@ Walter Higgins * [events.itemSpawn()](#eventsitemspawn) * [events.sheepDyeWool()](#eventssheepdyewool) * [events.entityTeleport()](#eventsentityteleport) - * [events.block()](#eventsblock) * [events.blockFade()](#eventsblockfade) * [events.blockDamage()](#eventsblockdamage) - * [events.blockPiston()](#eventsblockpiston) * [events.blockPistonExtend()](#eventsblockpistonextend) * [events.blockExp()](#eventsblockexp) * [events.blockGrow()](#eventsblockgrow) * [events.blockPistonRetract()](#eventsblockpistonretract) * [events.blockDispense()](#eventsblockdispense) * [events.blockBreak()](#eventsblockbreak) - * [events.painting()](#eventspainting) * [events.paintingPlace()](#eventspaintingplace) - * [events.weather()](#eventsweather) * [events.lightningStrike()](#eventslightningstrike) - * [events.vehicle()](#eventsvehicle) * [events.vehicleEnter()](#eventsvehicleenter) * [events.vehicleMove()](#eventsvehiclemove) - * [events.vehicleCollision()](#eventsvehiclecollision) * [events.vehicleCreate()](#eventsvehiclecreate) * [events.asyncPlayerPreLogin()](#eventsasyncplayerprelogin) * [events.playerUnleashEntity()](#eventsplayerunleashentity) * [events.playerPreLogin()](#eventsplayerprelogin) - * [events.player()](#eventsplayer) - * [events.server()](#eventsserver) * [events.inventoryPickupItem()](#eventsinventorypickupitem) * [events.inventoryMoveItem()](#eventsinventorymoveitem) * [events.furnaceBurn()](#eventsfurnaceburn) @@ -194,7 +182,6 @@ Walter Higgins * [events.playerToggleFlight()](#eventsplayertoggleflight) * [events.playerAnimation()](#eventsplayeranimation) * [events.asyncPlayerChat()](#eventsasyncplayerchat) - * [events.playerBucket()](#eventsplayerbucket) * [events.playerRegisterChannel()](#eventsplayerregisterchannel) * [events.playerMove()](#eventsplayermove) * [events.playerTeleport()](#eventsplayerteleport) @@ -206,8 +193,6 @@ Walter Higgins * [events.serverCommand()](#eventsservercommand) * [events.remoteServerCommand()](#eventsremoteservercommand) * [events.mapInitialize()](#eventsmapinitialize) - * [events.service()](#eventsservice) - * [events.plugin()](#eventsplugin) * [events.serviceRegister()](#eventsserviceregister) * [events.serverListPing()](#eventsserverlistping) * [events.serviceUnregister()](#eventsserviceunregister) @@ -963,30 +948,6 @@ 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. -### events.hanging() - -#### Parameters - - * callback - A function which is called whenever the hanging.HangingEvent event is fired - -see events.on() for more information. - -### events.world() - -#### Parameters - - * callback - A function which is called whenever the world.WorldEvent event is fired - -see events.on() for more information. - -### events.chunk() - -#### Parameters - - * callback - A function which is called whenever the world.ChunkEvent event is fired - -see events.on() for more information. - ### events.worldUnload() #### Parameters @@ -1051,14 +1012,6 @@ see events.on() for more information. see events.on() for more information. -### events.entity() - -#### Parameters - - * callback - A function which is called whenever the entity.EntityEvent event is fired - -see events.on() for more information. - ### events.horseJump() #### Parameters @@ -1315,14 +1268,6 @@ see events.on() for more information. see events.on() for more information. -### events.block() - -#### Parameters - - * callback - A function which is called whenever the block.BlockEvent event is fired - -see events.on() for more information. - ### events.blockFade() #### Parameters @@ -1339,14 +1284,6 @@ see events.on() for more information. see events.on() for more information. -### events.blockPiston() - -#### Parameters - - * callback - A function which is called whenever the block.BlockPistonEvent event is fired - -see events.on() for more information. - ### events.blockPistonExtend() #### Parameters @@ -1395,14 +1332,6 @@ see events.on() for more information. see events.on() for more information. -### events.painting() - -#### Parameters - - * callback - A function which is called whenever the painting.PaintingEvent event is fired - -see events.on() for more information. - ### events.paintingPlace() #### Parameters @@ -1411,14 +1340,6 @@ see events.on() for more information. see events.on() for more information. -### events.weather() - -#### Parameters - - * callback - A function which is called whenever the weather.WeatherEvent event is fired - -see events.on() for more information. - ### events.lightningStrike() #### Parameters @@ -1427,14 +1348,6 @@ see events.on() for more information. see events.on() for more information. -### events.vehicle() - -#### Parameters - - * callback - A function which is called whenever the vehicle.VehicleEvent event is fired - -see events.on() for more information. - ### events.vehicleEnter() #### Parameters @@ -1451,14 +1364,6 @@ see events.on() for more information. see events.on() for more information. -### events.vehicleCollision() - -#### Parameters - - * callback - A function which is called whenever the vehicle.VehicleCollisionEvent event is fired - -see events.on() for more information. - ### events.vehicleCreate() #### Parameters @@ -1491,22 +1396,6 @@ see events.on() for more information. see events.on() for more information. -### events.player() - -#### Parameters - - * callback - A function which is called whenever the player.PlayerEvent event is fired - -see events.on() for more information. - -### events.server() - -#### Parameters - - * callback - A function which is called whenever the server.ServerEvent event is fired - -see events.on() for more information. - ### events.inventoryPickupItem() #### Parameters @@ -2131,14 +2020,6 @@ see events.on() for more information. see events.on() for more information. -### events.playerBucket() - -#### Parameters - - * callback - A function which is called whenever the player.PlayerBucketEvent event is fired - -see events.on() for more information. - ### events.playerRegisterChannel() #### Parameters @@ -2227,22 +2108,6 @@ see events.on() for more information. see events.on() for more information. -### events.service() - -#### Parameters - - * callback - A function which is called whenever the server.ServiceEvent event is fired - -see events.on() for more information. - -### events.plugin() - -#### Parameters - - * callback - A function which is called whenever the server.PluginEvent event is fired - -see events.on() for more information. - ### events.serviceRegister() #### Parameters @@ -3138,7 +3003,7 @@ Drones can be created in any of the following ways... block is broken at the block's location you would do so like this... - events.on('block.BlockBreakEvent',function( event) { + events.blockBreak( function( event) { var location = event.block.location; var drone = new Drone(location); // do more stuff with the drone here... @@ -4046,7 +3911,6 @@ Source Code ... A simple event-driven minecraft plugin. How to handle Events. - This example demonstrates event-driven programming. The code below will display the version of ScriptCraft every time an operator joins the game. This module is notable from previous modules for the @@ -4128,6 +3992,14 @@ cleaner and more readable. Similarly where you see a method like } }); +Update: Since version 2.0.8 the above code can be replaced by the more succinct: + + events.playerJoin( function( event ) { + if ( event.player.op ) { + event.player.sendMessage('Welcome to ' + __plugin); + } + }); + ## Arrows Plugin The arrows mod adds fancy arrows to the game. Arrows which... diff --git a/src/generateEventsHelper.js b/src/generateEventsHelper.js index 9bc6b9e..920c65a 100644 --- a/src/generateEventsHelper.js +++ b/src/generateEventsHelper.js @@ -36,6 +36,13 @@ while ( ( entry = zis.nextEntry) != null) { var name = '' + entry.name; if (name.match(/org\/bukkit\/event\/.+Event\.class$/)){ name = name.replace(/\//g,'.').replace('.class',''); + + // abstract events don't have a static getHandlerList method so + // shouldn't be added to this module + var hasHandlerList = eval(name + '.getHandlerList'); + if ( !hasHandlerList ) { + continue; + } var parts = name.split('.'); var shortName = name.replace('org.bukkit.event.',''); var fname = parts.reverse().shift().replace(/^(.)/,function(a){ return a.toLowerCase()}).replace(/Event$/,''); diff --git a/src/main/js/lib/java-utils.js b/src/main/js/lib/java-utils.js index d6cf812..6ed83f5 100644 --- a/src/main/js/lib/java-utils.js +++ b/src/main/js/lib/java-utils.js @@ -1,3 +1,6 @@ exports.isJavaObject = function( o ) { + if (o === global){ + return false; + } return o instanceof java.lang.Object; }; diff --git a/src/main/js/lib/scriptcraft.js b/src/main/js/lib/scriptcraft.js index 9edf6b8..e0350d7 100644 --- a/src/main/js/lib/scriptcraft.js +++ b/src/main/js/lib/scriptcraft.js @@ -619,15 +619,16 @@ function __onEnable ( __engine, __plugin, __script ) global.plugin = plugins.plugin; var events = require('events'); - events.on( 'server.PluginDisableEvent', function( evt ) { + // wph 20131226 - make events global as it is used by many plugins/modules + global.events = events; + + events.pluginDisable(function( evt ) { // save config _save( global.config, new File( jsPluginsRootDir, 'data/global-config.json' ) ); _runUnloadHandlers(); org.bukkit.event.HandlerList['unregisterAll(org.bukkit.plugin.Plugin)'](__plugin); }); - // wph 20131226 - make events global as it is used by many plugins/modules - global.events = events; global.__onCommand = function( sender, cmd, label, args) { diff --git a/src/main/js/lib/tabcomplete.js b/src/main/js/lib/tabcomplete.js index 810155b..514e7ba 100644 --- a/src/main/js/lib/tabcomplete.js +++ b/src/main/js/lib/tabcomplete.js @@ -24,6 +24,7 @@ var _getProperties = function( o ) { i, j, isObjectMethod, + propValue, typeofProperty; if ( isJavaObject( o ) ) { @@ -49,12 +50,14 @@ var _getProperties = function( o ) { } typeofProperty = null; try { - typeofProperty = typeof o[i]; + propValue = o[i]; + typeofProperty = typeof propValue; } catch( e ) { if ( e.message == 'java.lang.IllegalStateException: Entity not leashed' ) { // wph 20131020 fail silently for Entity leashing in craftbukkit } else { - throw e; + // don't throw an error during tab completion just make a best effort to + // do the job. } } if ( typeofProperty == 'function' ) { @@ -119,6 +122,9 @@ var onTabCompleteJS = function( result, cmdSender, pluginCmd, cmdAlias, cmdArgs if ( statement.length == 0 ) { propsOfLastArg = _globalSymbols; } else { + if (statement.match(/\)$/)){ + return; + } statementSyms = statement.split(/[^\$a-zA-Z0-9_\.]/); lastSymbol = statementSyms[statementSyms.length-1]; diff --git a/src/main/js/modules/signs/menu.js b/src/main/js/modules/signs/menu.js index df1c7af..a792a76 100644 --- a/src/main/js/modules/signs/menu.js +++ b/src/main/js/modules/signs/menu.js @@ -184,7 +184,7 @@ signs.menu = function( /* String */ label, /* Array */ options, /* Function */ c // // update it every time player interacts with it. // -events.on( 'player.PlayerInteractEvent', function( event ) { +events.playerInteract( function( event ) { /* look up our list of menu signs. If there's a matching location and there's a sign, then update it. diff --git a/src/main/js/plugins/alias/alias.js b/src/main/js/plugins/alias/alias.js index 3ed7d30..6ffe4af 100644 --- a/src/main/js/plugins/alias/alias.js +++ b/src/main/js/plugins/alias/alias.js @@ -223,7 +223,7 @@ var _intercept = function( msg, invoker, exec ) { Intercept all command processing and replace with aliased commands if the command about to be issued matches an alias. */ -events.on( 'player.PlayerCommandPreprocessEvent', function( evt ) { +events.playerCommandPreprocess( function( evt ) { var invoker = evt.player; var exec = function( cmd ) { invoker.performCommand(cmd); @@ -237,7 +237,7 @@ events.on( 'player.PlayerCommandPreprocessEvent', function( evt ) { command('void',function( ) { } ); -events.on( 'server.ServerCommandEvent', function( evt ) { +events.serverCommand( function( evt ) { var invoker = evt.sender; var exec = function( cmd ) { invoker.server.dispatchCommand( invoker, cmd); diff --git a/src/main/js/plugins/arrows.js b/src/main/js/plugins/arrows.js index 4e2c8b5..2aa54e0 100644 --- a/src/main/js/plugins/arrows.js +++ b/src/main/js/plugins/arrows.js @@ -121,5 +121,5 @@ var _onArrowHit = function( event ) { } } }; -events.on( 'entity.ProjectileHitEvent', _onArrowHit ); +events.projectileHit( _onArrowHit ); diff --git a/src/main/js/plugins/chat/color.js b/src/main/js/plugins/chat/color.js index 533b557..482caca 100644 --- a/src/main/js/plugins/chat/color.js +++ b/src/main/js/plugins/chat/color.js @@ -73,7 +73,7 @@ foreach( colors, function ( color, i ) { colorCodes[color] = i.toString( 16 ); } ); -events.on( 'player.AsyncPlayerChatEvent', function( event ) { +events.asyncPlayerChat( function( event ) { var player = event.player; var playerChatColor = _store.players[ player.name ]; if ( playerChatColor ) { diff --git a/src/main/js/plugins/classroom/classroom.js b/src/main/js/plugins/classroom/classroom.js index 2f74caf..fa4edfc 100644 --- a/src/main/js/plugins/classroom/classroom.js +++ b/src/main/js/plugins/classroom/classroom.js @@ -158,7 +158,7 @@ var classroom = plugin('classroom', { exports.classroom = classroom; -events.on( 'player.PlayerJoinEvent', function( event ) { +events.playerJoin( function( event ) { if ( _store.enableScripting ) { grantScripting(event.player); } diff --git a/src/main/js/plugins/commando/commando.js b/src/main/js/plugins/commando/commando.js index 64437d6..30f2707 100644 --- a/src/main/js/plugins/commando/commando.js +++ b/src/main/js/plugins/commando/commando.js @@ -84,7 +84,7 @@ exports.commando = function( name, func, options, intercepts ) { return result; }; -events.on( 'player.PlayerCommandPreprocessEvent', function( evt ) { +events.playerCommandPreprocess( function( evt ) { var msg = '' + evt.message; var parts = msg.match( /^\/([^\s]+)/ ); if ( !parts ) { @@ -98,7 +98,7 @@ events.on( 'player.PlayerCommandPreprocessEvent', function( evt ) { evt.message = '/jsp ' + msg.replace( /^\//, '' ); } } ); -events.on( 'server.ServerCommandEvent', function( evt ) { +events.serverCommand( function( evt ) { var msg = '' + evt.command; var parts = msg.match( /^\/*([^\s]+)/ ); if ( !parts ) { diff --git a/src/main/js/plugins/drone/drone.js b/src/main/js/plugins/drone/drone.js index a18f594..b478cb2 100644 --- a/src/main/js/plugins/drone/drone.js +++ b/src/main/js/plugins/drone/drone.js @@ -118,7 +118,7 @@ Drones can be created in any of the following ways... block is broken at the block's location you would do so like this... - events.on('block.BlockBreakEvent',function( event) { + events.blockBreak( function( event) { var location = event.block.location; var drone = new Drone(location); // do more stuff with the drone here... diff --git a/src/main/js/plugins/examples/example-7-hello-events.js b/src/main/js/plugins/examples/example-7-hello-events.js index 8723c91..58b62b5 100644 --- a/src/main/js/plugins/examples/example-7-hello-events.js +++ b/src/main/js/plugins/examples/example-7-hello-events.js @@ -3,7 +3,6 @@ A simple event-driven minecraft plugin. How to handle Events. - This example demonstrates event-driven programming. The code below will display the version of ScriptCraft every time an operator joins the game. This module is notable from previous modules for the @@ -85,6 +84,14 @@ cleaner and more readable. Similarly where you see a method like } }); +Update: Since version 2.0.8 the above code can be replaced by the more succinct: + + events.playerJoin( function( event ) { + if ( event.player.op ) { + event.player.sendMessage('Welcome to ' + __plugin); + } + }); + ***/ events.on( 'player.PlayerJoinEvent', function( event ) { if ( event.player.op ) { diff --git a/src/main/js/plugins/minigames/SnowballFight.js b/src/main/js/plugins/minigames/SnowballFight.js index 5116caf..8d1dfbd 100644 --- a/src/main/js/plugins/minigames/SnowballFight.js +++ b/src/main/js/plugins/minigames/SnowballFight.js @@ -43,7 +43,6 @@ cover to make the game more fun. ***/ var bkGameMode = org.bukkit.GameMode, - bkEntityDamageByEntityEvent = org.bukkit.event.entity.EntityDamageByEntityEvent, bkItemStack = org.bukkit.inventory.ItemStack, bkMaterial = org.bukkit.Material, bkSnowball = org.bukkit.entity.Snowball; @@ -196,7 +195,7 @@ var createGame = function( duration, teams ) { return { start: function( ) { _startGame( _gameState ); - _gameState.listener = events.on(bkEntityDamageByEntityEvent,_onSnowballHit); + _gameState.listener = events.entityDamageByEntity( _onSnowballHit ); new java.lang.Thread( function( ) { while ( _gameState.duration-- ) { java.lang.Thread.sleep( 1000 ); // sleep 1,000 millisecs (1 second) diff --git a/src/main/js/plugins/minigames/cow-clicker.js b/src/main/js/plugins/minigames/cow-clicker.js index 124b037..6ed4250 100644 --- a/src/main/js/plugins/minigames/cow-clicker.js +++ b/src/main/js/plugins/minigames/cow-clicker.js @@ -94,9 +94,9 @@ var _startGame = function( ) { console.log('Staring game: Cow Clicker'); } - events.on( 'player.PlayerQuitEvent', _onPlayerQuit ); - events.on( 'player.PlayerJoinEvent', _onPlayerJoin ); - events.on( 'player.PlayerInteractEntityEvent', _onPlayerInteract ); + events.playerQuit( _onPlayerQuit ); + events.playerJoin( _onPlayerJoin ); + events.playerInteractEntity( _onPlayerInteract ); scoreboard.start();