minor tweaks to fort, ladder, sign and young person's guide.

This commit is contained in:
walterhiggins 2015-01-03 09:50:37 +00:00
parent 964aba1278
commit c7e2eeed85
6 changed files with 105 additions and 82 deletions

View file

@ -818,7 +818,8 @@ through arrays. The following loop prints out all of the players on
the server... the server...
```javascript ```javascript
var players = server.onlinePlayers; var utils = require('utils');
var players = utils.players();
var i = 0; var i = 0;
while ( i < players.length ) { while ( i < players.length ) {
console.log( players[i] ); console.log( players[i] );
@ -847,7 +848,9 @@ loops. utils.foreach() takes two parameters...
Let's see it in action, the following code will `console.log()` (print) the Let's see it in action, the following code will `console.log()` (print) the
name of each online player in the server console window... name of each online player in the server console window...
utils.foreach( server.onlinePlayers, console.log ); var utils = require('utils');
var players = utils.players;
utils.foreach( players, console.log );
... in the above example, the list of online players is processed one ... in the above example, the list of online players is processed one
at a time and each item (player) is passed to the `console.log` at a time and each item (player) is passed to the `console.log`
@ -863,8 +866,9 @@ utils.foreach() function...
give every player the ability to fly. give every player the ability to fly.
*/ */
var utils = require('utils'); var utils = require('utils');
utils.foreach( server.onlinePlayers, function( player ) { var players = utils.players();
player.setAllowFlight(true); utils.foreach( players, function( player ) {
player.capabilities.setMayFly(true);
} ); } );
``` ```
@ -875,19 +879,21 @@ utils.foreach( server.onlinePlayers, function( player ) {
Play a Cat's Meow sound for each player. Play a Cat's Meow sound for each player.
*/ */
var utils = require('utils'); var utils = require('utils');
utils.foreach( server.onlinePlayers, function( player ) { var players = utils.players();
player.playSound(player.location, var sounds = require('sounds');
org.bukkit.Sound.CAT_MEOW, utils.foreach( players, function( player ) {
1, sounds.catMeow( player );
1);
} ); } );
``` ```
### Exercise ### Exercise
Try changing the above function so that different sounds are played Try changing the above function so that different sounds are played
instead of a Cat's Meow. You'll need to lookup the [CanaryMod API's instead of a Cat's Meow. To see all of the possible sounds that can be
Sound class][soundapi] to see all of the possible sounds that can be played, load the sounds module at the in-game prompt using the following statement:
played.
/js var sounds = require('sounds');
... then type `/js sounds.` and press the TAB key to see a list of all possible sounds.
Loops are a key part of programming in any language. Javascript Loops are a key part of programming in any language. Javascript
provides `for` and `while` statements for looping and many javascript provides `for` and `while` statements for looping and many javascript

View file

@ -782,7 +782,8 @@ through arrays. The following loop prints out all of the players on
the server... the server...
```javascript ```javascript
var players = server.onlinePlayers; var utils = require('utils');
var players = utils.players();
var i = 0; var i = 0;
while ( i < players.length ) { while ( i < players.length ) {
console.log( players[i] ); console.log( players[i] );
@ -811,7 +812,9 @@ loops. utils.foreach() takes two parameters...
Let's see it in action, the following code will `console.log()` (print) the Let's see it in action, the following code will `console.log()` (print) the
name of each online player in the server console window... name of each online player in the server console window...
utils.foreach( server.onlinePlayers, console.log ); var utils = require('utils');
var players = utils.players;
utils.foreach( players, console.log );
... in the above example, the list of online players is processed one ... in the above example, the list of online players is processed one
at a time and each item (player) is passed to the `console.log` at a time and each item (player) is passed to the `console.log`
@ -827,8 +830,9 @@ utils.foreach() function...
give every player the ability to fly. give every player the ability to fly.
*/ */
var utils = require('utils'); var utils = require('utils');
utils.foreach( server.onlinePlayers, function( player ) { var players = utils.players();
player.setAllowFlight(true); utils.foreach( players, function( player ) {
player.capabilities.setMayFly(true);
} ); } );
``` ```
@ -839,19 +843,21 @@ utils.foreach( server.onlinePlayers, function( player ) {
Play a Cat's Meow sound for each player. Play a Cat's Meow sound for each player.
*/ */
var utils = require('utils'); var utils = require('utils');
utils.foreach( server.onlinePlayers, function( player ) { var players = utils.players();
player.playSound(player.location, var sounds = require('sounds');
org.bukkit.Sound.CAT_MEOW, utils.foreach( players, function( player ) {
1, sounds.catMeow( player );
1);
} ); } );
``` ```
### Exercise ### Exercise
Try changing the above function so that different sounds are played Try changing the above function so that different sounds are played
instead of a Cat's Meow. You'll need to lookup the [CanaryMod API's instead of a Cat's Meow. To see all of the possible sounds that can be
Sound class][soundapi] to see all of the possible sounds that can be played, load the sounds module at the in-game prompt using the following statement:
played.
/js var sounds = require('sounds');
... then type `/js sounds.` and press the TAB key to see a list of all possible sounds.
Loops are a key part of programming in any language. Javascript Loops are a key part of programming in any language. Javascript
provides `for` and `while` statements for looping and many javascript provides `for` and `while` statements for looping and many javascript

View file

@ -1,3 +1,4 @@
'use strict';
var Drone = require('../drone').Drone; var Drone = require('../drone').Drone;
var blocks = require('blocks'); var blocks = require('blocks');
// //
@ -6,8 +7,7 @@ var blocks = require('blocks');
function fort( side, height ) { function fort( side, height ) {
var turret, var turret,
i, i,
torch, torch;
ladder;
if ( typeof side == 'undefined' ) { if ( typeof side == 'undefined' ) {
side = 18; side = 18;
@ -93,6 +93,7 @@ function fort( side, height ) {
.right((side/2)-3) .right((side/2)-3)
.fwd(1) // move inside fort .fwd(1) // move inside fort
.turn(2) .turn(2)
.box(blocks.air, 1, height-1, 1)
.ladder(height-1) .ladder(height-1)
.move('fort'); .move('fort');
} }

View file

@ -1,3 +1,5 @@
'use strict';
/*global require, setInterval, clearInterval, __plugin, exports*/
/* /*
Experimental: Experimental:
Point at a block and issue the following ... Point at a block and issue the following ...
@ -7,68 +9,69 @@
... start the clock... ... start the clock...
/js clock.stop24(); /js clock.stop24();
... stops the clock... ... stops the clock...
*/ */
var Drone = require('../drone').Drone; var Drone = require('../drone').Drone,
var blocktype = require('../blocktype'); blocktype = require('../blocktype'),
blocks = require('blocks');
exports.LCDClock = function(drone, fgColor,bgColor,border) { exports.LCDClock = function(drone, fgColor,bgColor,border) {
var lastSecs = [0,0,0,0],
world = drone.world,
intervalId = -1;
var lastSecs = [0,0,0,0],
world = drone.world,
intervalId = -1;
function update(secs) {
var digits = [0,0,0,0],
s = secs % 60,
m = (secs - s) / 60;
digits[3] = s%10;
digits[2] = (s-digits[3])/10;
digits[1] = m%10;
digits[0] = (m-digits[1])/10;
//
// updating all 4 digits each time is expensive
// only update digits which have changed (in most cases - just 1)
//
if (digits[3] != lastSecs[3])
drone.right(14).blocktype(''+digits[3],fgColor,bgColor).left(14);
if (digits[2] != lastSecs[2])
drone.right(10).blocktype(''+digits[2],fgColor,bgColor).left(10);
if (digits[1] != lastSecs[1])
drone.right(4).blocktype(''+digits[1], fgColor, bgColor).left(4);
if (digits[0] != lastSecs[0])
drone.blocktype(''+digits[0], fgColor, bgColor);
lastSecs[0] = digits[0];
lastSecs[1] = digits[1];
lastSecs[2] = digits[2];
lastSecs[3] = digits[3];
}
if ( typeof bgColor == 'undefined' ) { if ( typeof bgColor == 'undefined' ) {
bgColor = '35:15'; // black wool bgColor = blocks.wool.black;
} }
if ( typeof fgColor == 'undefined' ) { if ( typeof fgColor == 'undefined' ) {
fgColor = 35 ; // white wool fgColor = blocks.wool.white ; // white wool
} }
if ( border ) { if ( border ) {
drone.box(border,21,9,1); drone.box(border,21,9,1);
drone.up().right(); drone.up().right();
} }
drone.blocktype('00:00',fgColor,bgColor); drone.blocktype('00:00', fgColor, bgColor);
return { return {
start24: function( ) { start24: function( ) {
var clock = this;
function tick() { function tick() {
var rolloverMins = 24*60; var rolloverMins = 24*60,
var timeOfDayInMins = Math.floor(((world.totalTime + 6000) % 24000) / 16.6667); mcTime = __plugin.canary ? world.totalTime : world.time,
timeOfDayInMins = Math.floor(((mcTime + 6000) % 24000) / 16.6667);
timeOfDayInMins = timeOfDayInMins % rolloverMins; timeOfDayInMins = timeOfDayInMins % rolloverMins;
console.log('Minecraft time: ' + world.totalTime + ' timeOfDayInMins: ' + timeOfDayInMins); update( timeOfDayInMins );
clock.update(timeOfDayInMins);
}; };
intervalId = setInterval(tick, 800); intervalId = setInterval(tick, 800);
}, },
stop24: function() { stop24: function() {
clearInterval( intervalId ); clearInterval( intervalId );
},
update: function(secs) {
var digits = [0,0,0,0],
s = secs % 60;
m = (secs - s) / 60;
digits[3] = s%10;
digits[2] = (s-digits[3])/10;
digits[1] = m%10;
digits[0] = (m-digits[1])/10;
//
// updating all 4 digits each time is expensive
// only update digits which have changed (in most cases - just 1)
//
if (digits[3] != lastSecs[3])
drone.right(14).blocktype(''+digits[3],fgColor,bgColor).left(14);
if (digits[2] != lastSecs[2])
drone.right(10).blocktype(''+digits[2],fgColor,bgColor).left(10);
if (digits[1] != lastSecs[1])
drone.right(4).blocktype(''+digits[1], fgColor, bgColor).left(4);
if (digits[0] != lastSecs[0])
drone.blocktype(''+digits[0], fgColor, bgColor);
lastSecs[0] = digits[0];
lastSecs[1] = digits[1];
lastSecs[2] = digits[2];
lastSecs[3] = digits[3];
} }
}; };
}; };

View file

@ -26,15 +26,18 @@ A ladder 10 blocks high will be created at the point you were looking at.
##### 3.0.3 ##### 3.0.3
***/ ***/
var blocks = require('blocks'); var blocks = require('blocks');
function ladder( height ){ function ladder( height ){
var block = this.getBlock(); this.then(function ladderLater(){
if (block.typeId == blocks.air || block.typeId == blocks.ladder){ var block = this.getBlock();
this.box(blocks.ladder, 1, height, 1); if (block.typeId == blocks.air || block.typeId == blocks.ladder){
} else { this.box(blocks.ladder, 1, height, 1);
this } else {
.back() this
.box(blocks.ladder, 1, height, 1) .back()
.fwd(); .box(blocks.ladder, 1, height, 1)
} .fwd();
}
});
} }
Drone.extend( ladder ); Drone.extend( ladder );

View file

@ -59,6 +59,7 @@ To create a free-standing sign...
***/ ***/
function putSign( drone, texts, blockId, meta ) { function putSign( drone, texts, blockId, meta ) {
var i, var i,
len = texts.length,
block, block,
state, state,
getState, getState,
@ -74,23 +75,26 @@ function putSign( drone, texts, blockId, meta ) {
var sign = block.getTileEntity(); var sign = block.getTileEntity();
return sign.setTextOnLine; return sign.setTextOnLine;
}; };
setLine = function( block, i, text) { setLine = function( block, i) {
var sign = block.getTileEntity(); var sign = block.getTileEntity();
sign.setTextOnLine( text, i ); sign.setTextOnLine( texts[i], i );
sign.update(); sign.update();
}; };
} }
if (__plugin.bukkit){ if (__plugin.bukkit){
isSign = function(block){ return block.state && block.state.setLine; }; isSign = function(block){ return block.state && block.state.setLine; };
setLine = function( block, i, text) { setLine = function( block, i) {
var sign = block.state; var sign = block.state;
sign.setLine( i, text ); sign.setLine( i, texts[i] );
sign.update(true); sign.update(true);
}; };
} }
if ( isSign(block) ) { if ( isSign(block) ) {
for ( i = 0; i < texts.length; i++ ) { if (len > 4){
setLine(block, i % 4, texts[ i ] ); len = 4;
}
for ( i = 0; i < len; i++ ) {
setLine(block, i, texts[ i ] );
} }
} }
}; };