Re-enabling signs and arrows modules/plugins for CanaryMod

This commit is contained in:
walterhiggins 2015-01-11 00:09:56 +00:00
parent 4819a0531f
commit c21be34609
6 changed files with 262 additions and 147 deletions

View file

@ -1,6 +1,8 @@
'use strict';
/*global events, require, org, module, persist, __plugin*/
var utils = require('utils'), var utils = require('utils'),
stringExt = require('utils/string-exts'), stringExt = require('utils/string-exts'),
_store = {}, store = persist('signs',{}),
bkBukkit = org.bukkit.Bukkit, bkBukkit = org.bukkit.Bukkit,
bkSign = org.bukkit.block.Sign; bkSign = org.bukkit.block.Sign;
@ -9,20 +11,24 @@ var utils = require('utils'),
(that is - a menu sign will still be a menu after the (that is - a menu sign will still be a menu after the
server has shut down and started up) plugins now have persistent state - Yay! server has shut down and started up) plugins now have persistent state - Yay!
*/ */
var signs = plugin("signs", { var signs = { };
/* var hasSign = null;
construct an interactive menu which can then be attached to a Sign. module.exports = function(hs){
*/ hasSign = hs;
menu: function( return signs;
/* String */ label, };
/* Array */ options,
/* Function */ onInteract,
/* Number */ defaultSelection ){},
store: _store
},
true);
module.exports = signs; var setLine = null;
if (__plugin.canary){
setLine = function(sign, i, text){
sign.setTextOnLine( text, i);
};
}
if (__plugin.bukkit){
setLine = function(sign, i, text){
sign.setLine( i, text);
};
}
/* /*
redraw a menu sign redraw a menu sign
@ -41,9 +47,14 @@ var _redrawMenuSign = function( p_sign, p_selectedIndex, p_displayOptions ) {
if ( offset+i == p_selectedIndex ) { if ( offset+i == p_selectedIndex ) {
text = ('' + text).replace(/^ /,">"); text = ('' + text).replace(/^ /,">");
} }
p_sign.setLine( i+1, text ); setLine(p_sign, i+1, text);
}
if (__plugin.canary){
p_sign.update();
}
if (__plugin.bukkit){
p_sign.update( true );
} }
p_sign.update( true );
}; };
var _updaters = {}; var _updaters = {};
@ -51,7 +62,9 @@ var _updaters = {};
construct an interactive menu to be subsequently attached to construct an interactive menu to be subsequently attached to
one or more Signs. one or more Signs.
*/ */
signs.menu = function( /* String */ label, /* Array */ options, /* Function */ callback, /* Number */ selectedIndex ) { signs.menu = signMenu;
function signMenu( label, options, callback, selectedIndex ) {
if ( typeof selectedIndex == "undefined" ) { if ( typeof selectedIndex == "undefined" ) {
selectedIndex = 0; selectedIndex = 0;
@ -76,33 +89,14 @@ signs.menu = function( /* String */ label, /* Array */ options, /* Function */ c
The function returned by signs.menu is for use by admins/ops. The function returned by signs.menu is for use by admins/ops.
*/ */
var convertToMenuSign = function(/* Sign */ sign, save) { var convertToMenuSign = function(/* Sign */ sign, save) {
var mouseLoc;
if (typeof save == "undefined") { if (typeof save == "undefined") {
save = true; save = true;
} }
/*
@deprecated start
all calls should explicitly provide a [org.bukkit.block.Sign][buksign] parameter.
*/
if ( typeof sign == "undefined" ) {
mouseLoc = utils.getMousePos();
if ( mouseLoc ) {
sign = mouseLoc.block.state;
if ( !( sign && sign.setLine ) ) {
throw new Error("You must first provide a sign!");
}
} else {
throw new Error("You must first provide a sign!");
}
}
/*
@deprecated end
*/
// //
// per-sign variables go here // per-sign variables go here
// //
var cSelectedIndex = selectedIndex; var cSelectedIndex = selectedIndex;
sign.setLine( 0, paddedLabel.bold() ); setLine(sign, 0, paddedLabel.bold());
var _updateSign = function( p_player, p_sign ) { var _updateSign = function( p_player, p_sign ) {
cSelectedIndex = ( cSelectedIndex + 1 ) % optLen; cSelectedIndex = ( cSelectedIndex + 1 ) % optLen;
_redrawMenuSign( p_sign, cSelectedIndex, displayOptions ); _redrawMenuSign( p_sign, cSelectedIndex, displayOptions );
@ -136,17 +130,17 @@ signs.menu = function( /* String */ label, /* Array */ options, /* Function */ c
when the server starts up again. when the server starts up again.
*/ */
if ( save ) { if ( save ) {
if ( typeof _store.menus == "undefined") { if ( typeof store.menus == "undefined") {
_store.menus = {}; store.menus = {};
} }
var signLocations = _store.menus[label]; var signLocations = store.menus[label];
if ( typeof signLocations == "undefined" ) { if ( typeof signLocations == "undefined" ) {
signLocations = _store.menus[label] = []; signLocations = store.menus[label] = [];
} }
signLocations.push( menuSignSaveData ); signLocations.push( menuSignSaveData );
} }
return sign; return sign;
}; }; // end of convertToMenuSign function
/* /*
a new sign definition - need to store (in-memory only) a new sign definition - need to store (in-memory only)
@ -155,19 +149,16 @@ signs.menu = function( /* String */ label, /* Array */ options, /* Function */ c
world with this same label and make dynamic again. world with this same label and make dynamic again.
*/ */
if ( _store.menus && _store.menus[label] ) { if ( store.menus && store.menus[label] ) {
var signsOfSameLabel = _store.menus[ label ]; var signsOfSameLabel = store.menus[ label ];
var defragged = []; var defragged = [];
var len = signsOfSameLabel.length; var len = signsOfSameLabel.length;
for ( i = 0; i < len; i++ ) { for ( i = 0; i < len; i++ ) {
var loc = signsOfSameLabel[i]; var loc = utils.locationFromJSON(signsOfSameLabel[i]);
var world = bkBukkit.getWorld(loc.world); var block = utils.blockAt(loc);
if ( !world ) { var sign = hasSign(block);
continue; if ( sign) {
} convertToMenuSign( sign, false );
var block = world.getBlockAt( loc.x, loc.y, loc.z );
if ( block.state instanceof bkSign ) {
convertToMenuSign( block.state, false );
defragged.push( loc ); defragged.push( loc );
} }
} }
@ -175,33 +166,52 @@ signs.menu = function( /* String */ label, /* Array */ options, /* Function */ c
remove data for signs which no longer exist. remove data for signs which no longer exist.
*/ */
if ( defragged.length != len ) { if ( defragged.length != len ) {
_store.menus[label] = defragged; store.menus[label] = defragged;
} }
} }
return convertToMenuSign; return convertToMenuSign;
}; };
/*
if (__plugin.canary){ if (__plugin.canary){
console.warn('signs/menu is not yet supported in CanaryMod'); console.warn('signs/menu is not yet supported in CanaryMod');
return; return;
} }
// */
// update it every time player interacts with it. if (__plugin.canary){
// events.blockRightClick( function( event ){
events.playerInteract( function( event ) { var sign = hasSign(event.blockClicked);
/* if (! sign){
look up our list of menu signs. If there's a matching location and there's // it's not a sign
a sign, then update it. return;
*/ }
var evtLocStr = utils.locationToString(event.blockClicked.location);
if ( ! event.clickedBlock.state instanceof bkSign ) { var signUpdater = _updaters[evtLocStr];
return; if ( signUpdater ) {
} signUpdater( event.player, sign);
var evtLocStr = utils.locationToString(event.clickedBlock.location); }
var signUpdater = _updaters[evtLocStr];
if ( signUpdater ) { });
signUpdater( event.player, event.clickedBlock.state ); }
} if (__plugin.bukkit){
}); //
// update it every time player interacts with it.
//
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.
*/
var sign = hasSign(event.clickedBlock);
if ( ! sign ) {
return;
}
var evtLocStr = utils.locationToString(event.clickedBlock.location);
var signUpdater = _updaters[evtLocStr];
if ( signUpdater ) {
signUpdater( event.player, sign );
}
});
}

View file

@ -1,3 +1,5 @@
'use strict';
/*global __plugin, require, module, exports*/
/************************************************************************ /************************************************************************
## Signs Module ## Signs Module
@ -92,21 +94,32 @@ if ( !sign ) {
[buksign]: http://jd.bukkit.org/dev/apidocs/org/bukkit/block/Sign.html [buksign]: http://jd.bukkit.org/dev/apidocs/org/bukkit/block/Sign.html
***/ ***/
function hasSign( block ){
if (__plugin.canary){
if (block && block.tileEntity && block.tileEntity.setTextOnLine){
return block.tileEntity;
}
}
if (__plugin.bukkit){
if (block && block.state && block.state.setLine){
return block.state;
}
}
return false;
}
var utils = require('utils'); var utils = require('utils');
var menu = require('./menu'); var menu = require('./menu')(hasSign);
// include all menu exports // include all menu exports
for ( var i in menu ) { for ( var i in menu ) {
exports[i] = menu[i]; exports[i] = menu[i];
} }
exports.getTargetedBy = function( livingEntity ) { function getTargetedBy( livingEntity ) {
var location = utils.getMousePos( livingEntity ); var location = utils.getMousePos( livingEntity );
if ( !location ) { if ( !location ) {
return null; return null;
} }
var state = location.block.state; return hasSign(utils.blockAt(location));
if ( ! (state || state.setLine) ) { }
return null; exports.getTargetedBy = getTargetedBy;
} exports.hasSign = hasSign;
return state;
};

View file

@ -0,0 +1,10 @@
function teleport( entity, location){
if (__plugin.bukkit){
var bkTeleportCause = org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
entity.teplort( location, bkTeleportCause.PLUGIN);
}
if (__plugin.canary){
entity['teleportTo(Location)'](location);
}
}
module.exports = teleport;

View file

@ -42,38 +42,38 @@ Example
<p style="color:gold;font-weight:bold">Hello World</p> <p style="color:gold;font-weight:bold">Hello World</p>
***/ ***/
var c = org.bukkit.ChatColor; var COLOR_CHAR = '\u00a7';
var formattingCodes = { var formattingCodes = {
aqua: c.AQUA, aqua: 'b',
black: c.BLACK, black: '0',
blue: c.BLUE, blue: '9',
bold: c.BOLD, bold: 'l',
brightgreen: c.GREEN, brightgreen: 'a',
darkaqua: c.DARK_AQUA, darkaqua: '3',
darkblue: c.DARK_BLUE, darkblue: '1',
darkgray: c.DARK_GRAY, darkgray: '8',
darkgreen: c.DARK_GREEN, darkgreen: '2',
purple: c.LIGHT_PURPLE, purple: 'd',
darkpurple: c.DARK_PURPLE, darkpurple: '5',
darkred: c.DARK_RED, darkred: '4',
gold: c.GOLD, gold: '6',
gray: c.GRAY, gray: '7',
green: c.GREEN, green: 'a',
italic: c.ITALIC, italic: 'o',
lightpurple: c.LIGHT_PURPLE, lightpurple: 'd',
indigo: c.BLUE, indigo: '9',
red: c.RED, red: 'c',
pink: c.LIGHT_PURPLE, pink: 'd',
yellow: c.YELLOW, yellow: 'e',
white: c.WHITE, white: 'f',
strike: c.STRIKETHROUGH, strike: 'm',
random: c.MAGIC, random: 'k',
magic: c.MAGIC, magic: 'k',
underline: c.UNDERLINE, underline: 'n',
reset: c.RESET reset: 'r'
}; };
for ( var method in formattingCodes ) { for ( var method in formattingCodes ) {
String.prototype[method] = function( c ) { String.prototype[method] = function( c ) {
return function(){ return c + this; }; return function(){ return c + this; };
}( formattingCodes[method] ); }( COLOR_CHAR + formattingCodes[method] );
} }

View file

@ -1,3 +1,4 @@
/*global __plugin, org, exports*/
'use strict'; 'use strict';
var File = java.io.File; var File = java.io.File;
@ -47,7 +48,7 @@ if ( player ) {
[bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html [bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html
***/ ***/
var _player = function ( playerName ) { function _player( playerName ) {
if ( typeof playerName == 'undefined' ) { if ( typeof playerName == 'undefined' ) {
if ( typeof self == 'undefined' ) { if ( typeof self == 'undefined' ) {
return null; return null;
@ -66,6 +67,43 @@ var _player = function ( playerName ) {
} }
}; };
/************************************************************************* /*************************************************************************
### utils.world( worldName ) function
Returns a World object matching the given name
***/
function _world( worldName ){
if (__plugin.canary){
try {
return Canary.server.worldManager.getWorld( worldName, true );
} catch (error) {
console.error( 'utils.world() failed to load ' + worldName + ',Error:' + error );
}
}
if (__plugin.bukkit){
return bkBukkit.getWorld( worldName );
}
return null;
}
exports.world = _world;
/*************************************************************************
### utils.blockAt( Location ) function
Returns the Block at the given location.
***/
function _blockAt( location ){
if (__plugin.canary){
return location.world.getBlockAt(location);
}
if (__plugin.bukkit){
return location.block;
}
return null;
}
exports.blockAt = _blockAt;
/*************************************************************************
### utils.locationToJSON() function ### utils.locationToJSON() function
utils.locationToJSON() returns a [org.bukkit.Location][bkloc] object in JSON form... utils.locationToJSON() returns a [org.bukkit.Location][bkloc] object in JSON form...
@ -139,15 +177,15 @@ exports.locationFromJSON = function( json ) {
var world; var world;
if ( json.constuctor == Array ) { if ( json.constuctor == Array ) {
// for support of legacy format // for support of legacy format
world = bkBukkit.getWorld( json[0] ); world = _world( json[0] );
return new bkLocation( world, json[1], json[2] , json[3] ); return new bkLocation( world, json[1], json[2] , json[3] );
} else { } else {
if (__plugin.canary){ if (__plugin.canary){
world = Canary.server.getWorld( json.world ); world = _world( json.world );
var cmLocation = Packages.net.canarymod.api.world.position.Location; var cmLocation = Packages.net.canarymod.api.world.position.Location;
return new cmLocation(world, json.x, json.y, json.z, json.pitch, json.yaw); return new cmLocation(world, json.x, json.y, json.z, json.pitch, json.yaw);
} else { } else {
world = bkBukkit.getWorld( json.world ); world = _world( json.world );
return new bkLocation( world, json.x, json.y , json.z, json.yaw, json.pitch ); return new bkLocation( world, json.x, json.y , json.z, json.yaw, json.pitch );
} }
} }

View file

@ -1,3 +1,5 @@
'use strict';
/*global require, __plugin, exports, events, setTimeout */
/************************************************************************* /*************************************************************************
## Arrows Plugin ## Arrows Plugin
@ -24,23 +26,19 @@ as a parameter. For example: `/js arrows.explosive('player23')` makes
player23's arrows explosive. player23's arrows explosive.
***/ ***/
if (__plugin.canary){ var Drone = require('drone'),
console.warn('arrows plugin not yet supported in CanaryMod'); teleport = require('teleport'),
return; signs = require('signs'),
} fireworks = require('fireworks'),
var signs = require('signs'), utils = require('utils'),
fireworks = require('fireworks'), bkArrow = org.bukkit.entity.Arrow,
utils = require('utils'), bkPlayer = org.bukkit.entity.Player,
bkTeleportCause = org.bukkit.event.player.PlayerTeleportEvent.TeleportCause, EXPLOSIVE_YIELD = 2.5,
bkArrow = org.bukkit.entity.Arrow, store = persist('arrows',{ players: { } }),
bkPlayer = org.bukkit.entity.Player, arrows = {},
bkTreeType = org.bukkit.TreeType, i,
EXPLOSIVE_YIELD = 2.5, type,
_store = { players: { } }, _types = [ 'Normal', 'Explosive', 'Teleport', 'Flourish', 'Lightning', 'Firework' ];
arrows = plugin( 'arrows', { store: _store }, true ),
i,
type,
_types = [ 'Normal', 'Explosive', 'Teleport', 'Flourish', 'Lightning', 'Firework' ];
exports.arrows = arrows; exports.arrows = arrows;
@ -52,7 +50,7 @@ for ( i = 0; i < _types.length; i++ ) {
return function( player ) { return function( player ) {
player = utils.player( player ); player = utils.player( player );
if ( player ) { if ( player ) {
arrows.store.players[ player.name ] = n; store.players[ player.name ] = n;
} else { } else {
console.warn('arrows.' + n + ' No player ' + player); console.warn('arrows.' + n + ' No player ' + player);
} }
@ -64,8 +62,9 @@ for ( i = 0; i < _types.length; i++ ) {
called when the player chooses an arrow option from a menu sign called when the player chooses an arrow option from a menu sign
*/ */
var _onMenuChoice = function( event ) { var _onMenuChoice = function( event ) {
arrows.store.players[ event.player.name ] = event.number; store.players[ event.player.name ] = event.number;
}; };
var convertToArrowSign = signs.menu( 'Arrow', _types, _onMenuChoice ); var convertToArrowSign = signs.menu( 'Arrow', _types, _onMenuChoice );
/* /*
@ -82,23 +81,23 @@ arrows.sign = function( cmdSender ) {
/* /*
event handler called when a projectile hits something event handler called when a projectile hits something
*/ */
var _onArrowHit = function( event ) { function onBukkitArrowHit( event ) {
var projectile = event.entity, var projectile = event.entity,
world = projectile.world, world = projectile.world,
shooter = projectile.shooter, shooter = projectile.shooter,
fireworkCount = 5, fireworkCount = 5,
arrowType, arrowType;
launch = function( ) {
fireworks.firework( projectile.location );
if ( --fireworkCount ) {
setTimeout( launch, 2000 );
}
};
function launch(){
fireworks.firework( projectile.location );
if ( --fireworkCount ) {
setTimeout( launch, 2000 );
}
}
if (projectile instanceof bkArrow if (projectile instanceof bkArrow
&& shooter instanceof bkPlayer) { && shooter instanceof bkPlayer) {
arrowType = arrows.store.players[ shooter.name ]; arrowType = store.players[ shooter.name ];
switch ( arrowType ) { switch ( arrowType ) {
case 1: case 1:
@ -107,11 +106,11 @@ var _onArrowHit = function( event ) {
break; break;
case 2: case 2:
projectile.remove(); projectile.remove();
shooter.teleport( projectile.location, bkTeleportCause.PLUGIN ); teleport(shooter, projectile.location);
break; break;
case 3: case 3:
projectile.remove(); projectile.remove();
world.generateTree( projectile.location, bkTreeType.BIG_TREE ); new Drone(projectile.location).oak();
break; break;
case 4: case 4:
projectile.remove(); projectile.remove();
@ -123,6 +122,51 @@ var _onArrowHit = function( event ) {
break; break;
} }
} }
}; }
events.projectileHit( _onArrowHit );
function onCanaryArrowHit( event ) {
var projectile = event.projectile,
world = projectile.world,
shooter = projectile.owner,
fireworkCount = 5,
arrowType,
cmArrow = Packages.net.canarymod.api.entity.Arrow,
cmPlayer = Packages.net.canarymod.api.entity.living.humanoid.Player,
loc = projectile.location,
launch = function( ) {
fireworks.firework( loc);
if ( --fireworkCount ) {
setTimeout( launch, 2000 );
}
};
if (projectile instanceof cmArrow && shooter instanceof cmPlayer) {
arrowType = store.players[ shooter.name ];
switch ( arrowType ) {
case 1:
projectile.destroy();
world.makeExplosion( shooter, loc, EXPLOSIVE_YIELD, true );
break;
case 2:
projectile.destroy();
teleport(shooter, loc);
break;
case 3:
projectile.destroy();
new Drone( loc ).oak();
break;
case 4:
projectile.destroy();
world.makeLightningBolt( loc );
break;
case 5:
projectile.destroy();
launch();
break;
}
}
}
events.projectileHit( __plugin.bukkit ? onBukkitArrowHit : onCanaryArrowHit);