This repository has been archived on 2021-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
scriptcraft/src/main/js/plugins/drone/sign.js
2015-01-01 10:00:55 +00:00

138 lines
3.1 KiB
JavaScript

'use strict';
/*global require, echo,__plugin*/
var Drone = require('./drone').Drone,
blocks = require('blocks');
/************************************************************************
### Drone.wallsign() method
Creates a wall sign (A sign attached to a wall)
#### Parameters
* message - can be a string or an array of strings
#### Example
drone.wallsign(['Welcome','to','Scriptopia']);
![wall sign](img/signex2.png)
### Drone.signpost() method
Creates a free-standing signpost
#### Parameters
* message - can be a string or an array of strings
#### Example
drone.signpost(['Hello','World']);
![ground sign](img/signex1.png)
### Drone.sign() method
Deprecated: Use signpost() or wallsign() methods instead.
Signs must use block 63 (stand-alone signs) or 68 (signs on walls)
#### Parameters
* message - can be a string or an array of strings.
* block - can be 63 or 68
#### Example
To create a free-standing sign...
drone.sign(["Hello","World"], blocks.sign_post);
![ground sign](img/signex1.png)
... to create a wall mounted sign...
drone.sign(["Welcome","to","Scriptopia"], blocks.sign );
![wall sign](img/signex2.png)
***/
function putSign( drone, texts, blockId, meta ) {
var i,
block,
state,
getState,
isSign,
setLine;
if ( blockId != blocks.sign_post && blockId != blocks.sign ) {
throw new Error( 'Invalid Parameter: blockId must be blocks.sign_post or blocks.sign' );
}
drone.setBlock( blockId, meta);
block = drone.getBlock();
if (__plugin.canary){
isSign = function(block){
var sign = block.getTileEntity();
return sign.setTextOnLine;
};
setLine = function( block, i, text) {
var sign = block.getTileEntity();
sign.setTextOnLine( text, i );
sign.update();
};
}
if (__plugin.bukkit){
isSign = function(block){ return block.state && block.state.setLine; };
setLine = function( block, i, text) {
var sign = block.state;
sign.setLine( i, text );
sign.update(true);
};
}
if ( isSign(block) ) {
for ( i = 0; i < texts.length; i++ ) {
setLine(block, i % 4, texts[ i ] );
}
}
};
function signpost( message ){
this.sign(message, blocks.sign_post);
}
function wallsign( message ){
/*
must allow for /js wallsign() while looking at a wall block
*/
this.then(function(){
if (this.getBlock().typeId == blocks.air){
this.sign(message, blocks.sign);
} else {
this
.back()
.sign(message, blocks.sign)
.fwd();
}
});
}
function sign( message, block ) {
if ( message.constructor != Array ) {
message = [message];
}
var bm = this._getBlockIdAndMeta( block );
block = bm[0];
var meta = bm[1];
if ( block !== blocks.sign_post && block !== blocks.sign ) {
var usage = 'Usage: sign("message", blocks.sign_post) or sign("message", blocks.sign)';
if ( this.player ) {
echo( this.player, usage);
}
console.error(usage);
return;
}
this.then(function(){
putSign( this, message, block, meta);
});
}
Drone.extend(sign);
Drone.extend(signpost);
Drone.extend(wallsign);