Skip to content

Commit aa42b74

Browse files
committed
Added lightning module
1 parent 4640597 commit aa42b74

7 files changed

Lines changed: 224 additions & 14 deletions

File tree

build.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,24 @@
8989
err="${dist}/gen-api-error.log">
9090
<arg value="${dist}/js"/>
9191
</jscript>
92+
9293
<jscript src="src/docs/js/generateItemsDoc.js"
9394
out="${dist}/items.md"
9495
err="${dist}/gen-items-error.log" />
96+
97+
<jscript src="src/docs/js/generateEntitiesDoc.js"
98+
out="${dist}/entities.md"
99+
err="${dist}/gen-entities-error.log" />
100+
95101
<concat destfile="${dist}/apiref-con.md">
96102
<fileset file="${dist}/apiref.md" />
97103
<fileset file="${dist}/items.md" />
104+
<fileset file="${dist}/entities.md" />
98105
</concat>
99106

100107
</target>
101108

109+
102110
<target name="gen-events-helper-canary" depends="compile-docs,init">
103111
<mkdir dir="${dist}/js/lib"/>
104112
<jscript src="src/docs/js/generateEventsHelper.js"

docs/API-Reference.md

Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,14 @@ Walter Higgins
407407
* [jsp classroom command](#jsp-classroom-command)
408408
* [classroom.allowScripting() function](#classroomallowscripting-function)
409409
* [Asynchronous Input Module](#asynchronous-input-module)
410+
* [Lightning module](#lightning-module)
411+
* [Usage](#usage-3)
410412
* [The recipes module](#the-recipes-module)
411413
* [Example](#example-1)
412414
* [Http Module](#http-module)
413415
* [http.request() function](#httprequest-function)
414416
* [sc-mqtt module](#sc-mqtt-module)
415-
* [Usage](#usage-3)
417+
* [Usage](#usage-4)
416418
* [Signs Module](#signs-module)
417419
* [signs.menu() function](#signsmenu-function)
418420
* [signs.getTargetedBy() function](#signsgettargetedby-function)
@@ -448,22 +450,22 @@ Walter Higgins
448450
* [watcher.unwatchFile() function](#watcherunwatchfile-function)
449451
* [watcher.unwatchDir() function](#watcherunwatchdir-function)
450452
* [Example Plugin #1 - A simple extension to Minecraft.](#example-plugin-1---a-simple-extension-to-minecraft)
451-
* [Usage:](#usage-4)
452-
* [Example Plugin #2 - Making extensions available for all players.](#example-plugin-2---making-extensions-available-for-all-players)
453453
* [Usage:](#usage-5)
454-
* [Example Plugin #3 - Limiting use of commands to operators only.](#example-plugin-3---limiting-use-of-commands-to-operators-only)
454+
* [Example Plugin #2 - Making extensions available for all players.](#example-plugin-2---making-extensions-available-for-all-players)
455455
* [Usage:](#usage-6)
456-
* [Example Plugin #4 - Using parameters in commands.](#example-plugin-4---using-parameters-in-commands)
456+
* [Example Plugin #3 - Limiting use of commands to operators only.](#example-plugin-3---limiting-use-of-commands-to-operators-only)
457457
* [Usage:](#usage-7)
458-
* [Example Plugin #5 - Re-use - Using your own and others modules.](#example-plugin-5---re-use---using-your-own-and-others-modules)
458+
* [Example Plugin #4 - Using parameters in commands.](#example-plugin-4---using-parameters-in-commands)
459459
* [Usage:](#usage-8)
460-
* [Example Plugin #6 - Re-use - Using 'utils' to get Player objects.](#example-plugin-6---re-use---using-utils-to-get-player-objects)
460+
* [Example Plugin #5 - Re-use - Using your own and others modules.](#example-plugin-5---re-use---using-your-own-and-others-modules)
461461
* [Usage:](#usage-9)
462+
* [Example Plugin #6 - Re-use - Using 'utils' to get Player objects.](#example-plugin-6---re-use---using-utils-to-get-player-objects)
463+
* [Usage:](#usage-10)
462464
* [Example Plugin #7 - Listening for events, Greet players when they join the game.](#example-plugin-7---listening-for-events-greet-players-when-they-join-the-game)
463465
* [Arrows Plugin](#arrows-plugin)
464-
* [Usage:](#usage-10)
466+
* [Usage:](#usage-11)
465467
* [Spawn Plugin](#spawn-plugin)
466-
* [Usage](#usage-11)
468+
* [Usage](#usage-12)
467469
* [alias Plugin](#alias-plugin)
468470
* [Examples](#examples-2)
469471
* [Commando Plugin](#commando-plugin)
@@ -483,7 +485,9 @@ Walter Higgins
483485
* [Rules](#rules)
484486
* [Gameplay Mechanics](#gameplay-mechanics)
485487
* [Items module](#items-module)
486-
* [Usage](#usage-12)
488+
* [Usage](#usage-13)
489+
* [Entities module](#entities-module)
490+
* [Usage](#usage-14)
487491

488492
## Modules in Scriptcraft
489493

@@ -4854,6 +4858,23 @@ The callback function as well as being bound to an object with the above propert
48544858

48554859
The `value` parameter will be the same as `this.value`, the `repeat` parameter will be the same as `this.repeat` and so on.
48564860

4861+
## Lightning module
4862+
4863+
Causes a bolt of lightning to strike.
4864+
4865+
### Usage
4866+
```javascript
4867+
// strike lightning wherever a player's arrow lands
4868+
var lightning = require('lightning');
4869+
events.projectileHit( function( event ){
4870+
if ( entities.arrow( event.projectile ) // it's an arrow
4871+
&& entities.player( event.projectile.owner ) // it was shot by a player
4872+
) {
4873+
lightning( event.projectile ); // strike lightning at the arrow location
4874+
}
4875+
});
4876+
```
4877+
48574878
## The recipes module
48584879

48594880
The Recipes module provides convenience functions for adding and removing recipes
@@ -6695,3 +6716,84 @@ The following functions are provided:
66956716
* zombieHead()
66966717
66976718
***/
6719+
/*********************
6720+
## Entities module
6721+
The Entities module provides a suite of functions - one for each possible entity type.
6722+
6723+
### Usage
6724+
6725+
entities.zombie(); // returns a canaryMod/Bukkit EntityType.ZOMBIE enum value
6726+
entities.zombie( mob ); // compares the entity's type to a zombie, returns true if mob type is zombie, false otherwise
6727+
entities.player( self ); // at the in-game prompt this should return true (compares self to a player entity type)
6728+
entities.rabbit( self ); // at the in-game prompt this should return false (compares self to a rabbit entity type)
6729+
6730+
The following functions are provided:
6731+
6732+
* aRMOR_STAND()
6733+
* aRROW()
6734+
* bAT()
6735+
* bLAZE()
6736+
* bOAT()
6737+
* cAVE_SPIDER()
6738+
* cHICKEN()
6739+
* cOMPLEX_PART()
6740+
* cOW()
6741+
* cREEPER()
6742+
* dROPPED_ITEM()
6743+
* eGG()
6744+
* eNDERMAN()
6745+
* eNDERMITE()
6746+
* eNDER_CRYSTAL()
6747+
* eNDER_DRAGON()
6748+
* eNDER_PEARL()
6749+
* eNDER_SIGNAL()
6750+
* eXPERIENCE_ORB()
6751+
* fALLING_BLOCK()
6752+
* fIREBALL()
6753+
* fIREWORK()
6754+
* fISHING_HOOK()
6755+
* gHAST()
6756+
* gIANT()
6757+
* gUARDIAN()
6758+
* hORSE()
6759+
* iRON_GOLEM()
6760+
* iTEM_FRAME()
6761+
* lEASH_HITCH()
6762+
* lIGHTNING()
6763+
* mAGMA_CUBE()
6764+
* mINECART()
6765+
* mINECART_CHEST()
6766+
* mINECART_COMMAND()
6767+
* mINECART_FURNACE()
6768+
* mINECART_HOPPER()
6769+
* mINECART_MOB_SPAWNER()
6770+
* mINECART_TNT()
6771+
* mUSHROOM_COW()
6772+
* oCELOT()
6773+
* pAINTING()
6774+
* pIG()
6775+
* pIG_ZOMBIE()
6776+
* pLAYER()
6777+
* pRIMED_TNT()
6778+
* rABBIT()
6779+
* sHEEP()
6780+
* sILVERFISH()
6781+
* sKELETON()
6782+
* sLIME()
6783+
* sMALL_FIREBALL()
6784+
* sNOWBALL()
6785+
* sNOWMAN()
6786+
* sPIDER()
6787+
* sPLASH_POTION()
6788+
* sQUID()
6789+
* tHROWN_EXP_BOTTLE()
6790+
* uNKNOWN()
6791+
* vILLAGER()
6792+
* wEATHER()
6793+
* wITCH()
6794+
* wITHER()
6795+
* wITHER_SKULL()
6796+
* wOLF()
6797+
* zOMBIE()
6798+
6799+
***/

src/docs/js/generateEntitiesDoc.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
args = Array.prototype.slice.call(args,1);
2+
// [0] = type, [1] = lib.jar [2] = blockX, [3] = classX
3+
var out = java.lang.System.out,
4+
err = java.lang.System.err,
5+
entry = null;
6+
var content = [
7+
'/*********************',
8+
'## Entities module',
9+
'The Entities module provides a suite of functions - one for each possible entity type.',
10+
'',
11+
'### Usage',
12+
'',
13+
' entities.zombie(); // returns a canaryMod/Bukkit EntityType.ZOMBIE enum value',
14+
' entities.zombie( mob ); // compares the entity\'s type to a zombie, returns true if mob type is zombie, false otherwise',
15+
' entities.player( self ); // at the in-game prompt this should return true (compares self to a player entity type)',
16+
' entities.rabbit( self ); // at the in-game prompt this should return false (compares self to a rabbit entity type)',
17+
'',
18+
'The following functions are provided:',
19+
''
20+
];
21+
22+
var enumVals = [], t, i, name;
23+
var entitytypes = org.bukkit.entity.EntityType.values();
24+
for (t in entitytypes) {
25+
if (entitytypes[t] && entitytypes[t].ordinal) {
26+
name = entitytypes[t].name();
27+
name = ('' + name).replace(/^(.)/,function(a){ return a.toLowerCase(); });
28+
enumVals.push(' * ' + name + '()');
29+
}
30+
}
31+
enumVals.sort();
32+
content = content.concat(enumVals);
33+
content.push('');
34+
content.push('***/');
35+
for (i = 0; i< content.length; i++){
36+
out.println(content[i]);
37+
}
38+
39+
40+

src/main/js/modules/entities.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/*global __plugin, org, Packages, module, exports*/
21
'use strict';
2+
/*global __plugin, org, Packages, module, exports*/
33
var entities = {},
44
entitytypes,
55
t, i, name;
@@ -9,10 +9,30 @@ if (__plugin.bukkit) {
99
if (__plugin.canary) {
1010
entitytypes = Packages.net.canarymod.api.entity.EntityType.values();
1111
}
12+
function getEntityHandler( entityType ) {
13+
return function( entity ){
14+
if (arguments.length == 0){
15+
return entityType;
16+
}
17+
if (arguments.length == 1){
18+
if (entity){
19+
if (__plugin.bukkit){
20+
return entity.type == entityType;
21+
}
22+
if (__plugin.canary){
23+
return entity.entityType == entityType;
24+
}
25+
}
26+
}
27+
return null;
28+
};
29+
}
1230
for (t in entitytypes) {
1331
if (entitytypes[t] && entitytypes[t].ordinal) {
14-
name = entitytypes[t].name();
15-
entities[name] = entitytypes[t];
32+
name = ('' + entitytypes[t].name()).replace(/^(.*)/,function(a){
33+
return a.toLowerCase();
34+
});
35+
entities[name] = getEntityHandler(entitytypes[t]);
1636
}
1737
}
1838
module.exports = entities;

src/main/js/modules/lightning.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
/************************************************************************
3+
## Lightning module
4+
5+
Causes a bolt of lightning to strike.
6+
7+
### Usage
8+
```javascript
9+
// strike lightning wherever a player's arrow lands
10+
var lightning = require('lightning');
11+
events.projectileHit( function( event ){
12+
if ( entities.arrow( event.projectile ) // it's an arrow
13+
&& entities.player( event.projectile.owner ) // it was shot by a player
14+
) {
15+
lightning( event.projectile ); // strike lightning at the arrow location
16+
}
17+
});
18+
```
19+
20+
***/
21+
module.exports = function lightning( something ) {
22+
if (__plugin.canary && something.location){
23+
return something.location.world.makeLightningBolt(something.location);
24+
}
25+
if (__plugin.bukkit && something.location){
26+
return something.location.world.strikeLightning(something.location);
27+
}
28+
console.log('Need an object with a location property for lightning strike');
29+
return null;
30+
};

src/main/js/modules/spawn.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
var entities = require('entities');
44

55
module.exports = function(entityType, location){
6+
var entityTypeFn;
67
if (typeof entityType === 'string'){
7-
entityType = entities[entityType];
8+
entityTypeFn = entities[entityType.toLowerCase()];
9+
entityType = entityTypeFn();
810
}
911
var world = location.world;
1012
if (__plugin.bukkit){

src/main/js/plugins/entities.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
/*global reuire, exports*/
3+
/*
4+
make entities a global variable for use at in-game prompt
5+
Tab completion is a useful way to discover what entity types are available.
6+
*/
7+
var entities = require('entities');
8+
exports.entities = entities;

0 commit comments

Comments
 (0)