From a63648cb9748b03d1ee43124e677160923430e7e Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Thu, 1 Jan 2015 12:47:36 +0000 Subject: [PATCH] Added bed() and improved low-level drone methods. --- src/main/js/modules/blockhelper.js | 24 ------------- src/main/js/plugins/drone/bed.js | 31 ++++++++++++++++ src/main/js/plugins/drone/contrib/cottage.js | 17 +++++++-- src/main/js/plugins/drone/doors.js | 31 +++++++++++----- src/main/js/plugins/drone/drone.js | 38 ++++++++++++++------ src/main/js/plugins/drone/ladder.js | 20 +++++------ src/main/js/plugins/drone/sign.js | 15 ++++---- 7 files changed, 113 insertions(+), 63 deletions(-) create mode 100644 src/main/js/plugins/drone/bed.js diff --git a/src/main/js/modules/blockhelper.js b/src/main/js/modules/blockhelper.js index d20decd..ca6a5d9 100644 --- a/src/main/js/modules/blockhelper.js +++ b/src/main/js/modules/blockhelper.js @@ -10,8 +10,6 @@ if (__plugin.canary){ var lookup = {}; function initLookup(){ var Facing = Packages.net.minecraft.util.EnumFacing, - DoorHalf = Packages.net.minecraft.block.BlockDoor.EnumDoorHalf, - HingePosition = Packages.net.minecraft.block.BlockDoor.EnumHingePosition, DyeColor = Packages.net.minecraft.item.EnumDyeColor; lookup = { @@ -28,8 +26,6 @@ function initLookup(){ up: Facing.UP, down: Facing.DOWN }, - half: { upper: DoorHalf.UPPER, lower: DoorHalf.LOWER }, - hinge: { left: HingePosition.LEFT, right: HingePosition.RIGHT }, color: { black: DyeColor.BLACK, blue: DyeColor.BLUE, @@ -141,26 +137,7 @@ function applyRotation( block, metadata ){ } } } -function applyDoors( block, metadata ){ - switch (block.typeId){ - case blocks.door_wood: - case blocks.door_iron: - switch (metadata) { - case 8: - property(block).set('hinge','left'); - property(block).set('half','upper'); - break; - case 9: - property(block).set('hinge','right'); - property(block).set('half','upper'); - break; - default: - property(block).set('facing',metadata); - property(block).set('half','lower'); - } - } -} function applyProperties( block, metadata ){ if (!bountiful){ block.data = metadata; @@ -172,6 +149,5 @@ function applyProperties( block, metadata ){ applyFacing( block, metadata ); applyColors( block, metadata ); applyRotation( block, metadata ); - applyDoors( block, metadata ); } exports.applyProperties = applyProperties; diff --git a/src/main/js/plugins/drone/bed.js b/src/main/js/plugins/drone/bed.js new file mode 100644 index 0000000..702f40d --- /dev/null +++ b/src/main/js/plugins/drone/bed.js @@ -0,0 +1,31 @@ +'use strict'; +/*global require, Packages*/ +var Drone = require('./drone').Drone, + blocks = require('blocks'); +var bedDirections = { + 0:3, // east + 1:0, // south + 2:1, // west + 3:2 // north +}; +function bed(){ + this.then(function(){ + var foot = this.setBlock(blocks.bed, bedDirections[this.dir], 0,0,0, false); + var head = this.setBlock(blocks.bed, bedDirections[this.dir] + 8, 0,0,1, false); + if (Drone.bountiful){ + var prop = require('blockhelper').property; + var BedHalf = Packages.net.canarymod.api.world.blocks.properties.BlockPropertyEnums.BedHalf; + prop(foot) + .set('facing',this.dir) + .set('part', BedHalf.FOOT); + prop(head) + .set('facing',this.dir) + .set('part', BedHalf.HEAD); + } + foot.update(); + head.update(); + }); + +} +Drone.extend( bed ); + diff --git a/src/main/js/plugins/drone/contrib/cottage.js b/src/main/js/plugins/drone/contrib/cottage.js index d5d7fa6..da81374 100644 --- a/src/main/js/plugins/drone/contrib/cottage.js +++ b/src/main/js/plugins/drone/contrib/cottage.js @@ -22,13 +22,26 @@ function cottage( ) { .box( blocks.glass_pane ) .left(5) .up() - .prism0( blocks.stairs.oak, 7, 6) + .prism0( blocks.stairs.oak, 7, 6) // add a roof .down() .right(4) .back() .wallsign(['Home','Sweet','Home']) .fwd() - .move('cottage'); + .move('cottage') + .right(3) + .fwd(4) + .up() + .hangtorch() // place a torch on wall + .move('cottage') + .right() + .fwd(3) + .bed() // place a bed against left wall + .fwd() + .right(4) + .box(blocks.furnace) // place a furnace against right wall + .move('cottage') + ; } // // a more complex script that builds an tree-lined avenue with diff --git a/src/main/js/plugins/drone/doors.js b/src/main/js/plugins/drone/doors.js index ed56db1..ef1c51d 100644 --- a/src/main/js/plugins/drone/doors.js +++ b/src/main/js/plugins/drone/doors.js @@ -1,3 +1,4 @@ +'use strict'; /************************************************************************* ### Drone.door() method @@ -46,29 +47,43 @@ Create double iron doors ***/ -var Drone = require('./drone').Drone; -/*global require*/ +var Drone = require('./drone').Drone, + blocks = require('blocks'); +/*global require, Packages*/ function door( doorMaterial, hinge) { if ( typeof doorMaterial == 'undefined' ) { - doorMaterial = 64; // wood + doorMaterial = blocks.door_wood; // wood } if (typeof hinge == 'undefined') { hinge = 'left'; } this.then(function(){ - this.setBlock(doorMaterial, this.dir); - this.setBlock(doorMaterial, hinge=='left' ? 8 : 9, 0,1,0); + var lower = this.setBlock(doorMaterial, this.dir, 0, 0, 0, false); + var upper = this.setBlock(doorMaterial, hinge=='left' ? 8 : 9, 0,1,0, false); + if (Drone.bountiful){ + var DoorHalf = Packages.net.minecraft.block.BlockDoor.EnumDoorHalf, + HingePosition = Packages.net.minecraft.block.BlockDoor.EnumHingePosition, + prop = require('blockhelper').property; + prop(lower) + .set('facing', this.dir) + .set('half', DoorHalf.LOWER ); + prop(upper) + .set('hinge', hinge == 'left' ? HingePosition.LEFT: HingePosition.RIGHT) + .set('half', DoorHalf.UPPER); + } + lower.update(); + upper.update(); }); } Drone.extend( door ); Drone.extend( function door_iron( ) { - this.door(71); + this.door(blocks.door_iron); } ); Drone.extend( function door2( doorMaterial ) { if ( typeof doorMaterial == 'undefined' ) { - doorMaterial = 64; + doorMaterial = blocks.door_wood; } this .door( doorMaterial, 'left') @@ -77,5 +92,5 @@ Drone.extend( function door2( doorMaterial ) { .left(); } ); Drone.extend( function door2_iron( ) { - this.door2( 71 ); + this.door2( blocks.door_iron ); } ); diff --git a/src/main/js/plugins/drone/drone.js b/src/main/js/plugins/drone/drone.js index 4f1cc86..d66c7e8 100644 --- a/src/main/js/plugins/drone/drone.js +++ b/src/main/js/plugins/drone/drone.js @@ -1,4 +1,4 @@ -/*global __plugin, require, org, setTimeout, addUnloadHandler, exports, global, Packages*/ +/*global __plugin, require, org, setTimeout, addUnloadHandler, exports, global, Packages, server*/ var utils = require('utils'), blocks = require('blocks'), THOUSAND = 1000, @@ -314,7 +314,7 @@ function getDirFromRotation( location ) { return 0; // east return 1; // south } -function putBlock( x, y, z, blockId, metadata, world, dir ) { +function putBlock( x, y, z, blockId, metadata, world, dir, update ) { if ( typeof metadata == 'undefined' ) { metadata = 0; } @@ -325,12 +325,18 @@ function putBlock( x, y, z, blockId, metadata, world, dir ) { block.type = BlockType.fromId(blockId); var applyProperties = require('blockhelper').applyProperties; applyProperties(block, metadata); - block.update(); + if (typeof update === 'undefined'){ + update = true; + } + if (update){ + block.update(); + } } if (__plugin.bukkit) { block.setTypeIdAndData( blockId, metadata, false ); block.data = metadata; } + return block; } function Drone( x, y, z, dir, world ) { @@ -559,14 +565,23 @@ Drone.prototype.times = function( numTimes, commands ) { Drone.prototype.getBlock = function(){ return this.world.getBlockAt(this.x,this.y,this.z); }; -Drone.prototype.setBlock = function(blockType, data, ox, oy, oz){ - if (typeof ox == 'undefined') - ox = 0; - if (typeof oy == 'undefined') - oy = 0; - if (typeof oz == 'undefined') - oz = 0; - putBlock(this.x + ox, this.y + oy, this.z + oz, blockType, data, this.world, this.dir); +Drone.prototype.setBlock = function(blockType, data, ow, oh, od, update){ + if (typeof ow == 'undefined') + ow = 0; + if (typeof oh == 'undefined') + oh = 0; + if (typeof od == 'undefined') + od = 0; + this + .right(ow) + .up(oh) + .fwd(od); + var result = putBlock(this.x, this.y, this.z, blockType, data, this.world, this.dir, update); + this + .left(ow) + .down(oh) + .back(od); + return result; }; Drone.prototype.traverseWidth = function(width, callback){ _traverse[this.dir].width(this, width, callback); @@ -865,3 +880,4 @@ Drone.clone = function(origin) { // wph 20130130 - make this a method - extensions can use it. // Drone.prototype._getBlockIdAndMeta = _getBlockIdAndMeta; +Drone.bountiful = __plugin.canary ? parseFloat(server.canaryModVersion) > 1.7 : false; diff --git a/src/main/js/plugins/drone/ladder.js b/src/main/js/plugins/drone/ladder.js index f1ad488..15a2d7e 100644 --- a/src/main/js/plugins/drone/ladder.js +++ b/src/main/js/plugins/drone/ladder.js @@ -27,16 +27,14 @@ A ladder 10 blocks high will be created at the point you were looking at. ***/ var blocks = require('blocks'); function ladder( height ){ - this.then(function(){ - var block = this.getBlock(); - if (block.typeId == blocks.air || block.typeId == blocks.ladder){ - this.box(blocks.ladder, 1, height, 1); - } else { - this - .back() - .box(blocks.ladder, 1, height, 1) - .fwd(); - } - }); + var block = this.getBlock(); + if (block.typeId == blocks.air || block.typeId == blocks.ladder){ + this.box(blocks.ladder, 1, height, 1); + } else { + this + .back() + .box(blocks.ladder, 1, height, 1) + .fwd(); + } } Drone.extend( ladder ); diff --git a/src/main/js/plugins/drone/sign.js b/src/main/js/plugins/drone/sign.js index 0619793..670e4f0 100644 --- a/src/main/js/plugins/drone/sign.js +++ b/src/main/js/plugins/drone/sign.js @@ -68,8 +68,7 @@ function putSign( drone, texts, blockId, meta ) { 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(); + block = drone.setBlock( blockId, meta); if (__plugin.canary){ isSign = function(block){ var sign = block.getTileEntity(); @@ -96,14 +95,17 @@ function putSign( drone, texts, blockId, meta ) { } }; function signpost( message ){ - this.sign(message, blocks.sign_post); + this.then(function(){ + 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){ + var block = this.getBlock(); + if (block.typeId == blocks.air || block.typeId == blocks.sign){ this.sign(message, blocks.sign); } else { this @@ -129,9 +131,8 @@ function sign( message, block ) { console.error(usage); return; } - this.then(function(){ - putSign( this, message, block, meta); - }); + putSign( this, message, block, meta); + } Drone.extend(sign); Drone.extend(signpost);