Fixed events-helper to include all non-abstract handlers.

Added items.js module and fixed sounds.js (more work needed on items.js)
This commit is contained in:
walterhiggins 2014-05-18 00:35:26 +01:00
parent 4d230bd514
commit e3078804ab
9 changed files with 282 additions and 47 deletions

View File

@ -89,7 +89,7 @@
<target name="gen-events-helper" depends="compile-docs,server-setup,init">
<mkdir dir="${dist}/js/lib"/>
<java classname="jscript" failonerror="true" fork="true" output="${dist}/js/lib/events-helper.js">
<java classname="jscript" failonerror="true" fork="true" output="${dist}/js/lib/events-helper.js" error="${dist}/geneventserror.log">
<classpath>
<pathelement path="${build}"/>
<pathelement path="${minecraft.dir}/craftbukkit.jar"/>

View File

@ -59,6 +59,8 @@ Walter Higgins
* [events.horseJump()](#eventshorsejump)
* [events.entityCombust()](#eventsentitycombust)
* [events.entityRegainHealth()](#eventsentityregainhealth)
* [events.entityCombustByBlock()](#eventsentitycombustbyblock)
* [events.entityCombustByEntity()](#eventsentitycombustbyentity)
* [events.playerLeashEntity()](#eventsplayerleashentity)
* [events.pigZap()](#eventspigzap)
* [events.itemDespawn()](#eventsitemdespawn)
@ -76,6 +78,7 @@ Walter Higgins
* [events.creatureSpawn()](#eventscreaturespawn)
* [events.foodLevelChange()](#eventsfoodlevelchange)
* [events.entityInteract()](#eventsentityinteract)
* [events.entityBreakDoor()](#eventsentitybreakdoor)
* [events.entityCreatePortal()](#eventsentitycreateportal)
* [events.sheepRegrowWool()](#eventssheepregrowwool)
* [events.explosionPrime()](#eventsexplosionprime)
@ -92,27 +95,35 @@ Walter Higgins
* [events.blockGrow()](#eventsblockgrow)
* [events.blockPistonRetract()](#eventsblockpistonretract)
* [events.blockDispense()](#eventsblockdispense)
* [events.blockBreak()](#eventsblockbreak)
* [events.paintingPlace()](#eventspaintingplace)
* [events.lightningStrike()](#eventslightningstrike)
* [events.vehicleEnter()](#eventsvehicleenter)
* [events.vehicleMove()](#eventsvehiclemove)
* [events.vehicleCreate()](#eventsvehiclecreate)
* [events.asyncPlayerPreLogin()](#eventsasyncplayerprelogin)
* [events.playerUnleashEntity()](#eventsplayerunleashentity)
* [events.playerPreLogin()](#eventsplayerprelogin)
* [events.inventoryPickupItem()](#eventsinventorypickupitem)
* [events.inventoryMoveItem()](#eventsinventorymoveitem)
* [events.furnaceBurn()](#eventsfurnaceburn)
* [events.inventory()](#eventsinventory)
* [events.brew()](#eventsbrew)
* [events.furnaceExtract()](#eventsfurnaceextract)
* [events.furnaceSmelt()](#eventsfurnacesmelt)
* [events.inventoryClose()](#eventsinventoryclose)
* [events.inventoryDrag()](#eventsinventorydrag)
* [events.inventoryClick()](#eventsinventoryclick)
* [events.inventoryCreative()](#eventsinventorycreative)
* [events.hangingPlace()](#eventshangingplace)
* [events.hangingBreak()](#eventshangingbreak)
* [events.worldSave()](#eventsworldsave)
* [events.structureGrow()](#eventsstructuregrow)
* [events.entityDamage()](#eventsentitydamage)
* [events.entityTargetLivingEntity()](#eventsentitytargetlivingentity)
* [events.playerDeath()](#eventsplayerdeath)
* [events.entityDamageByBlock()](#eventsentitydamagebyblock)
* [events.entityDamageByEntity()](#eventsentitydamagebyentity)
* [events.entityPortal()](#eventsentityportal)
* [events.entityPortalExit()](#eventsentityportalexit)
* [events.signChange()](#eventssignchange)
@ -130,6 +141,7 @@ Walter Higgins
* [events.enchantItem()](#eventsenchantitem)
* [events.prepareItemEnchant()](#eventsprepareitemenchant)
* [events.paintingBreak()](#eventspaintingbreak)
* [events.paintingBreakByEntity()](#eventspaintingbreakbyentity)
* [events.weatherChange()](#eventsweatherchange)
* [events.thunderChange()](#eventsthunderchange)
* [events.vehicleEntityCollision()](#eventsvehicleentitycollision)
@ -156,7 +168,6 @@ Walter Higgins
* [events.playerChat()](#eventsplayerchat)
* [events.playerAchievementAwarded()](#eventsplayerachievementawarded)
* [events.playerBedLeave()](#eventsplayerbedleave)
* [events.playerChannel()](#eventsplayerchannel)
* [events.playerStatisticIncrement()](#eventsplayerstatisticincrement)
* [events.playerToggleSprint()](#eventsplayertogglesprint)
* [events.playerInteractEntity()](#eventsplayerinteractentity)
@ -169,6 +180,7 @@ Walter Higgins
* [events.playerToggleFlight()](#eventsplayertoggleflight)
* [events.playerAnimation()](#eventsplayeranimation)
* [events.asyncPlayerChat()](#eventsasyncplayerchat)
* [events.playerRegisterChannel()](#eventsplayerregisterchannel)
* [events.playerMove()](#eventsplayermove)
* [events.playerTeleport()](#eventsplayerteleport)
* [events.playerBucketFill()](#eventsplayerbucketfill)
@ -184,8 +196,13 @@ Walter Higgins
* [events.serviceUnregister()](#eventsserviceunregister)
* [events.prepareItemCraft()](#eventsprepareitemcraft)
* [events.inventoryOpen()](#eventsinventoryopen)
* [events.craftItem()](#eventscraftitem)
* [events.hangingBreakByEntity()](#eventshangingbreakbyentity)
* [events.blockMultiPlace()](#eventsblockmultiplace)
* [events.entityBlockForm()](#eventsentityblockform)
* [events.playerBucketEmpty()](#eventsplayerbucketempty)
* [events.playerPortal()](#eventsplayerportal)
* [events.playerUnregisterChannel()](#eventsplayerunregisterchannel)
* [events.pluginDisable()](#eventsplugindisable)
* [events.pluginEnable()](#eventspluginenable)
* [Blocks Module](#blocks-module)
@ -1023,6 +1040,22 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.entityCombustByBlock()
#### Parameters
* callback - A function which is called whenever the entity.EntityCombustByBlockEvent event is fired
* priority - optional - see events.on() for more information.
### events.entityCombustByEntity()
#### Parameters
* callback - A function which is called whenever the entity.EntityCombustByEntityEvent event is fired
* priority - optional - see events.on() for more information.
### events.playerLeashEntity()
#### Parameters
@ -1159,6 +1192,14 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.entityBreakDoor()
#### Parameters
* callback - A function which is called whenever the entity.EntityBreakDoorEvent event is fired
* priority - optional - see events.on() for more information.
### events.entityCreatePortal()
#### Parameters
@ -1287,6 +1328,14 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.blockBreak()
#### Parameters
* callback - A function which is called whenever the block.BlockBreakEvent event is fired
* priority - optional - see events.on() for more information.
### events.paintingPlace()
#### Parameters
@ -1335,6 +1384,14 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.playerUnleashEntity()
#### Parameters
* callback - A function which is called whenever the player.PlayerUnleashEntityEvent event is fired
* priority - optional - see events.on() for more information.
### events.playerPreLogin()
#### Parameters
@ -1383,6 +1440,14 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.furnaceExtract()
#### Parameters
* callback - A function which is called whenever the inventory.FurnaceExtractEvent event is fired
* priority - optional - see events.on() for more information.
### events.furnaceSmelt()
#### Parameters
@ -1415,6 +1480,14 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.inventoryCreative()
#### Parameters
* callback - A function which is called whenever the inventory.InventoryCreativeEvent event is fired
* priority - optional - see events.on() for more information.
### events.hangingPlace()
#### Parameters
@ -1455,6 +1528,38 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.entityTargetLivingEntity()
#### Parameters
* callback - A function which is called whenever the entity.EntityTargetLivingEntityEvent event is fired
* priority - optional - see events.on() for more information.
### events.playerDeath()
#### Parameters
* callback - A function which is called whenever the entity.PlayerDeathEvent event is fired
* priority - optional - see events.on() for more information.
### events.entityDamageByBlock()
#### Parameters
* callback - A function which is called whenever the entity.EntityDamageByBlockEvent event is fired
* priority - optional - see events.on() for more information.
### events.entityDamageByEntity()
#### Parameters
* callback - A function which is called whenever the entity.EntityDamageByEntityEvent event is fired
* priority - optional - see events.on() for more information.
### events.entityPortal()
#### Parameters
@ -1591,6 +1696,14 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.paintingBreakByEntity()
#### Parameters
* callback - A function which is called whenever the painting.PaintingBreakByEntityEvent event is fired
* priority - optional - see events.on() for more information.
### events.weatherChange()
#### Parameters
@ -1799,14 +1912,6 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.playerChannel()
#### Parameters
* callback - A function which is called whenever the player.PlayerChannelEvent event is fired
* priority - optional - see events.on() for more information.
### events.playerStatisticIncrement()
#### Parameters
@ -1903,6 +2008,14 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.playerRegisterChannel()
#### Parameters
* callback - A function which is called whenever the player.PlayerRegisterChannelEvent event is fired
* priority - optional - see events.on() for more information.
### events.playerMove()
#### Parameters
@ -2023,6 +2136,38 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.craftItem()
#### Parameters
* callback - A function which is called whenever the inventory.CraftItemEvent event is fired
* priority - optional - see events.on() for more information.
### events.hangingBreakByEntity()
#### Parameters
* callback - A function which is called whenever the hanging.HangingBreakByEntityEvent event is fired
* priority - optional - see events.on() for more information.
### events.blockMultiPlace()
#### Parameters
* callback - A function which is called whenever the block.BlockMultiPlaceEvent event is fired
* priority - optional - see events.on() for more information.
### events.entityBlockForm()
#### Parameters
* callback - A function which is called whenever the block.EntityBlockFormEvent event is fired
* priority - optional - see events.on() for more information.
### events.playerBucketEmpty()
#### Parameters
@ -2039,6 +2184,14 @@ beginning programmers to explore the events at the server console window.
* priority - optional - see events.on() for more information.
### events.playerUnregisterChannel()
#### Parameters
* callback - A function which is called whenever the player.PlayerUnregisterChannelEvent event is fired
* priority - optional - see events.on() for more information.
### events.pluginDisable()
#### Parameters
@ -2128,20 +2281,20 @@ This new `input()` function is best illustrated by example. The following code i
var input = require('input');
exports.numberguess = function(player){
var randomNumber = Math.ceil(Math.random() * 10);
input( player, 'Think of a number between 1 and 10 (q to quit)', function( guess, repeat ) {
input( player, 'Think of a number between 1 and 10 (q to quit)', function( guess, guesser, repeat ) {
if ( guess == 'q'){
return;
}
if ( +guess !== randomNumber ) {
if (+guess < randomNumber ) {
player.sendMessage('Too low - guess again');
guesser.sendMessage('Too low - guess again');
}
if (+guess > randomNumber ) {
player.sendMessage('Too high - guess again');
guesser.sendMessage('Too high - guess again');
}
repeat();
} else {
player.sendMessage('You guessed correctly');
guesser.sendMessage('You guessed correctly');
}
});
};
@ -2158,8 +2311,8 @@ The callback is bound to an object which has the following properties:
The callback function as well as being bound to an object with the above properties (so you can use this.value inside your callback to get the value which has just been input), can also take the following parameters (in exact order):
* value
* repeat
* sender
* repeat
The `value` parameter will be the same as `this.value`, the `repeat` parameter will be the same as `this.repeat` and so on.
@ -2368,11 +2521,23 @@ a simpler way to play sounds. All of the org.bukkit.Sound Enum values are attach
### Usage:
var sounds = require('sounds');
sounds.play( self, sounds.VILLAGER_NO , 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch
sounds.play( self, sounds.VILLAGER_NO ); // same as previous statement
sounds.play( org.bukkit.Sound.VILLAGER_NO , self, 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch
sounds.play( org.bukkit.Sound.VILLAGER_NO , self ); // same as previous statement
The play() function takes either a Location object or any object which has a location.
The volume parameter is in the range 0 to 1 and the pitch parameter is in the range 0 to 4.
In addition, a play function is provided for each possible sound using the following rules:
1. The sound is converted from ALL_CAPS_UNDERSCORE to camelCase so for example there is a sounds.villagerNo() function which will play the VILLAGER_NO sound.
2. Each such function can take 3 parameters: location (which can be either an actual Location object or an object which has a location), volume and pitch
3. Or... each such function can be called without parameters meaning the sound will be played for all online players to hear.
sounds.villagerNo(self, 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch at invoker's location
sounds.villagerNo(); // plays VILLAGER_NO sound for all players online.
These methods are provided for convenience to help beginners explore sounds using TAB completion.
String class extensions
-----------------------
The following chat-formatting methods are added to the javascript String class..

View File

@ -1014,7 +1014,7 @@ Open your favorite editor and type the following code into a new file
in your scriptcraft/plugins directory...
```javascript
function flightStatus( player ) {
exports.flightStatus = function( player ) {
if ( player.flying ) {
player.sendMessage( 'Hey, You are flying!' );
} else {

View File

@ -1,3 +1,10 @@
# 2014 05 12
Turn off modality for conversations which are started via the 'input' module.
(with modality on, player.sendMessage() is suppressed but player.sendRawMessage() isn't.
turning modality off as devs would expect player.sendMessage() to work - I did anyway)
# 2014 05 10
Further simplification of events handling. The events.on() function can still be used but additional functions are now provided for each type of event.

View File

@ -978,7 +978,7 @@ Open your favorite editor and type the following code into a new file
in your scriptcraft/plugins directory...
```javascript
function flightStatus( player ) {
exports.flightStatus = function( player ) {
if ( player.flying ) {
player.sendMessage( 'Hey, You are flying!' );
} else {

View File

@ -2,6 +2,8 @@ var File = java.io.File,
FileReader = java.io.FileReader,
FileInputStream = java.io.FileInputStream,
out = java.lang.System.out,
err = java.lang.System.err,
Modifier = java.lang.reflect.Modifier,
ZipInputStream = java.util.zip.ZipInputStream,
zis = new ZipInputStream(new FileInputStream('./target/minecraft/craftbukkit.jar')),
entry = null;
@ -39,17 +41,9 @@ 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 = false;
try {
hasHandlerList = engine.eval(name + '.getHandlerList');
} catch ( ex ) {
// exception is thrown for JRE7
continue;
}
if ( !hasHandlerList ) {
var clz = java.lang.Class.forName(name);
var isAbstract = Modifier.isAbstract(clz.getModifiers());
if ( isAbstract ) {
continue;
}
var parts = name.split('.');

View File

@ -20,20 +20,20 @@ This new `input()` function is best illustrated by example. The following code i
var input = require('input');
exports.numberguess = function(player){
var randomNumber = Math.ceil(Math.random() * 10);
input( player, 'Think of a number between 1 and 10 (q to quit)', function( guess, repeat ) {
input( player, 'Think of a number between 1 and 10 (q to quit)', function( guess, guesser, repeat ) {
if ( guess == 'q'){
return;
}
if ( +guess !== randomNumber ) {
if (+guess < randomNumber ) {
player.sendMessage('Too low - guess again');
guesser.sendMessage('Too low - guess again');
}
if (+guess > randomNumber ) {
player.sendMessage('Too high - guess again');
guesser.sendMessage('Too high - guess again');
}
repeat();
} else {
player.sendMessage('You guessed correctly');
guesser.sendMessage('You guessed correctly');
}
});
};
@ -50,8 +50,8 @@ The callback is bound to an object which has the following properties:
The callback function as well as being bound to an object with the above properties (so you can use this.value inside your callback to get the value which has just been input), can also take the following parameters (in exact order):
* value
* repeat
* sender
* repeat
The `value` parameter will be the same as `this.value`, the `repeat` parameter will be the same as `this.repeat` and so on.
@ -64,21 +64,22 @@ function asyncInput( sender, promptMesg, callback) {
var repeat = function(){
asyncInput( sender, promptMesg, callback);
};
var prompt = new bkPrompt( ) {
var prompt = new bkPrompt( {
getPromptText: function( ctx ) {
return promptMesg;
},
acceptInput: function( ctx, value ) {
callback.apply( { repeat: repeat, sender: sender, message: promptMesg, value: value },
[value, repeat, sender]);
[value, sender, repeat]);
return null;
},
blocksForInput: function( ctx ) {
return true;
}
};
});
new bkConversationFactory( __plugin )
.withModality( true )
.withModality( false )
.withFirstPrompt( prompt )
.buildConversation( sender )
.begin( );

View File

@ -0,0 +1,27 @@
var bkItemStack = org.bukkit.inventory.ItemStack,
bkMaterial = org.bukkit.Material
var items = function(material, amount){
material = material.toUpperCase();
return new bkItemStack(bkMaterial[material],amount);
};
module.exports = items;
var materials = bkMaterial.values();
for (var i = 0;i < materials.length; i++ ){
var name = (''+materials[i].name()).toLowerCase();
name = name.replace(/(_.)/g,function(a){ return a.replace(/_/,'').toUpperCase(); });
items[name] = (function(material){
return function(amount){
if (typeof amount == 'undefined'){
amount = 1;
}
if (typeof amount == 'number'){
return new bkItemStack(material, amount);
} else {
return amount == material;
}
};
})(materials[i]);
}

View File

@ -1,6 +1,7 @@
var bkSound = org.bukkit.Sound,
bkLocation = org.bukkit.Location,
i = 0,
foreach = require('utils').foreach,
allSounds = bkSound.values(),
len = allSounds.length,
sound,
@ -9,7 +10,33 @@ var bkSound = org.bukkit.Sound,
for ( ; i < len; i++ ) {
sound = allSounds[i];
soundName = '' + sound.name();
exports[soundName] = sound;
var methodName = soundName.toLowerCase().replace(/_(.)/g,function(a,b){ return b.toUpperCase();});
exports[methodName] = (function(sound){
return function()
{
switch (arguments.length) {
case 3:
exports.play(sound, arguments[0], arguments[1], arguments[2]);
break;
case 2:
// TODO: possible combinations:
// location, volume,
// volume pitch
exports.play(sound, arguments[0],arguments[1]);
break;
case 1:
exports.play(sound, arguments[0]);
break;
case 0:
// play the sound at full vol, medium pitch for all players
//
foreach(server.onlinePlayers,function(player){
exports.play(sound, player, 1, 0);
});
default:
}
};
})(sound);
}
/*************************************************************************
## Sounds Module
@ -20,13 +47,25 @@ a simpler way to play sounds. All of the org.bukkit.Sound Enum values are attach
### Usage:
var sounds = require('sounds');
sounds.play( self, sounds.VILLAGER_NO , 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch
sounds.play( self, sounds.VILLAGER_NO ); // same as previous statement
sounds.play( org.bukkit.Sound.VILLAGER_NO , self, 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch
sounds.play( org.bukkit.Sound.VILLAGER_NO , self ); // same as previous statement
The play() function takes either a Location object or any object which has a location.
The volume parameter is in the range 0 to 1 and the pitch parameter is in the range 0 to 4.
In addition, a play function is provided for each possible sound using the following rules:
1. The sound is converted from ALL_CAPS_UNDERSCORE to camelCase so for example there is a sounds.villagerNo() function which will play the VILLAGER_NO sound.
2. Each such function can take 3 parameters: location (which can be either an actual Location object or an object which has a location), volume and pitch
3. Or... each such function can be called without parameters meaning the sound will be played for all online players to hear.
sounds.villagerNo(self, 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch at invoker's location
sounds.villagerNo(); // plays VILLAGER_NO sound for all players online.
These methods are provided for convenience to help beginners explore sounds using TAB completion.
***/
exports.play = function(locationOrHasLocation, sound, volume, pitch) {
exports.play = function(sound, locationOrHasLocation, volume, pitch) {
var location = null;
if (!locationOrHasLocation)
return;
@ -38,11 +77,13 @@ exports.play = function(locationOrHasLocation, sound, volume, pitch) {
location = locationOrHasLocation;
}
}
if (!location)
if (!location){
console.warn('sounds.play() needs a location');
return;
if (!volume)
}
if (typeof volume == 'undefined')
volume = 1;
if (!pitch)
pitch = 0;
if (typeof pitch == 'undefined')
pitch = 1;
location.world.playSound(location, sound, volume, pitch);
};