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'),
stringExt = require('utils/string-exts'),
_store = {},
store = persist('signs',{}),
bkBukkit = org.bukkit.Bukkit,
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
server has shut down and started up) plugins now have persistent state - Yay!
*/
var signs = plugin("signs", {
/*
construct an interactive menu which can then be attached to a Sign.
*/
menu: function(
/* String */ label,
/* Array */ options,
/* Function */ onInteract,
/* Number */ defaultSelection ){},
store: _store
},
true);
var signs = { };
var hasSign = null;
module.exports = function(hs){
hasSign = hs;
return signs;
};
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
@ -41,9 +47,14 @@ var _redrawMenuSign = function( p_sign, p_selectedIndex, p_displayOptions ) {
if ( offset+i == p_selectedIndex ) {
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 = {};
@ -51,7 +62,9 @@ var _updaters = {};
construct an interactive menu to be subsequently attached to
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" ) {
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.
*/
var convertToMenuSign = function(/* Sign */ sign, save) {
var mouseLoc;
if (typeof save == "undefined") {
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
//
var cSelectedIndex = selectedIndex;
sign.setLine( 0, paddedLabel.bold() );
setLine(sign, 0, paddedLabel.bold());
var _updateSign = function( p_player, p_sign ) {
cSelectedIndex = ( cSelectedIndex + 1 ) % optLen;
_redrawMenuSign( p_sign, cSelectedIndex, displayOptions );
@ -136,17 +130,17 @@ signs.menu = function( /* String */ label, /* Array */ options, /* Function */ c
when the server starts up again.
*/
if ( save ) {
if ( typeof _store.menus == "undefined") {
_store.menus = {};
if ( typeof store.menus == "undefined") {
store.menus = {};
}
var signLocations = _store.menus[label];
var signLocations = store.menus[label];
if ( typeof signLocations == "undefined" ) {
signLocations = _store.menus[label] = [];
signLocations = store.menus[label] = [];
}
signLocations.push( menuSignSaveData );
}
return sign;
};
}; // end of convertToMenuSign function
/*
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.
*/
if ( _store.menus && _store.menus[label] ) {
var signsOfSameLabel = _store.menus[ label ];
if ( store.menus && store.menus[label] ) {
var signsOfSameLabel = store.menus[ label ];
var defragged = [];
var len = signsOfSameLabel.length;
for ( i = 0; i < len; i++ ) {
var loc = signsOfSameLabel[i];
var world = bkBukkit.getWorld(loc.world);
if ( !world ) {
continue;
}
var block = world.getBlockAt( loc.x, loc.y, loc.z );
if ( block.state instanceof bkSign ) {
convertToMenuSign( block.state, false );
var loc = utils.locationFromJSON(signsOfSameLabel[i]);
var block = utils.blockAt(loc);
var sign = hasSign(block);
if ( sign) {
convertToMenuSign( sign, false );
defragged.push( loc );
}
}
@ -175,33 +166,52 @@ signs.menu = function( /* String */ label, /* Array */ options, /* Function */ c
remove data for signs which no longer exist.
*/
if ( defragged.length != len ) {
_store.menus[label] = defragged;
store.menus[label] = defragged;
}
}
return convertToMenuSign;
};
/*
if (__plugin.canary){
console.warn('signs/menu is not yet supported in CanaryMod');
return;
}
//
// 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.
*/
if ( ! event.clickedBlock.state instanceof bkSign ) {
return;
}
var evtLocStr = utils.locationToString(event.clickedBlock.location);
var signUpdater = _updaters[evtLocStr];
if ( signUpdater ) {
signUpdater( event.player, event.clickedBlock.state );
}
});
*/
if (__plugin.canary){
events.blockRightClick( function( event ){
var sign = hasSign(event.blockClicked);
if (! sign){
// it's not a sign
return;
}
var evtLocStr = utils.locationToString(event.blockClicked.location);
var signUpdater = _updaters[evtLocStr];
if ( signUpdater ) {
signUpdater( event.player, sign);
}
});
}
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
@ -92,21 +94,32 @@ if ( !sign ) {
[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 menu = require('./menu');
var menu = require('./menu')(hasSign);
// include all menu exports
for ( var i in menu ) {
exports[i] = menu[i];
}
exports.getTargetedBy = function( livingEntity ) {
function getTargetedBy( livingEntity ) {
var location = utils.getMousePos( livingEntity );
if ( !location ) {
return null;
}
var state = location.block.state;
if ( ! (state || state.setLine) ) {
return null;
}
return state;
};
return hasSign(utils.blockAt(location));
}
exports.getTargetedBy = getTargetedBy;
exports.hasSign = hasSign;

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

View file

@ -1,3 +1,4 @@
/*global __plugin, org, exports*/
'use strict';
var File = java.io.File;
@ -47,7 +48,7 @@ if ( player ) {
[bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html
***/
var _player = function ( playerName ) {
function _player( playerName ) {
if ( typeof playerName == 'undefined' ) {
if ( typeof self == 'undefined' ) {
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() returns a [org.bukkit.Location][bkloc] object in JSON form...
@ -139,15 +177,15 @@ exports.locationFromJSON = function( json ) {
var world;
if ( json.constuctor == Array ) {
// for support of legacy format
world = bkBukkit.getWorld( json[0] );
world = _world( json[0] );
return new bkLocation( world, json[1], json[2] , json[3] );
} else {
if (__plugin.canary){
world = Canary.server.getWorld( json.world );
world = _world( json.world );
var cmLocation = Packages.net.canarymod.api.world.position.Location;
return new cmLocation(world, json.x, json.y, json.z, json.pitch, json.yaw);
} else {
world = bkBukkit.getWorld( json.world );
world = _world( json.world );
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
@ -24,23 +26,19 @@ as a parameter. For example: `/js arrows.explosive('player23')` makes
player23's arrows explosive.
***/
if (__plugin.canary){
console.warn('arrows plugin not yet supported in CanaryMod');
return;
}
var signs = require('signs'),
fireworks = require('fireworks'),
utils = require('utils'),
bkTeleportCause = org.bukkit.event.player.PlayerTeleportEvent.TeleportCause,
bkArrow = org.bukkit.entity.Arrow,
bkPlayer = org.bukkit.entity.Player,
bkTreeType = org.bukkit.TreeType,
EXPLOSIVE_YIELD = 2.5,
_store = { players: { } },
arrows = plugin( 'arrows', { store: _store }, true ),
i,
type,
_types = [ 'Normal', 'Explosive', 'Teleport', 'Flourish', 'Lightning', 'Firework' ];
var Drone = require('drone'),
teleport = require('teleport'),
signs = require('signs'),
fireworks = require('fireworks'),
utils = require('utils'),
bkArrow = org.bukkit.entity.Arrow,
bkPlayer = org.bukkit.entity.Player,
EXPLOSIVE_YIELD = 2.5,
store = persist('arrows',{ players: { } }),
arrows = {},
i,
type,
_types = [ 'Normal', 'Explosive', 'Teleport', 'Flourish', 'Lightning', 'Firework' ];
exports.arrows = arrows;
@ -52,7 +50,7 @@ for ( i = 0; i < _types.length; i++ ) {
return function( player ) {
player = utils.player( player );
if ( player ) {
arrows.store.players[ player.name ] = n;
store.players[ player.name ] = n;
} else {
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
*/
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 );
/*
@ -82,23 +81,23 @@ arrows.sign = function( cmdSender ) {
/*
event handler called when a projectile hits something
*/
var _onArrowHit = function( event ) {
function onBukkitArrowHit( event ) {
var projectile = event.entity,
world = projectile.world,
shooter = projectile.shooter,
fireworkCount = 5,
arrowType,
launch = function( ) {
fireworks.firework( projectile.location );
if ( --fireworkCount ) {
setTimeout( launch, 2000 );
}
};
world = projectile.world,
shooter = projectile.shooter,
fireworkCount = 5,
arrowType;
function launch(){
fireworks.firework( projectile.location );
if ( --fireworkCount ) {
setTimeout( launch, 2000 );
}
}
if (projectile instanceof bkArrow
&& shooter instanceof bkPlayer) {
arrowType = arrows.store.players[ shooter.name ];
arrowType = store.players[ shooter.name ];
switch ( arrowType ) {
case 1:
@ -107,11 +106,11 @@ var _onArrowHit = function( event ) {
break;
case 2:
projectile.remove();
shooter.teleport( projectile.location, bkTeleportCause.PLUGIN );
teleport(shooter, projectile.location);
break;
case 3:
projectile.remove();
world.generateTree( projectile.location, bkTreeType.BIG_TREE );
new Drone(projectile.location).oak();
break;
case 4:
projectile.remove();
@ -123,6 +122,51 @@ var _onArrowHit = function( event ) {
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);