From 7547c6810003af275a60492410471b64bfcff359 Mon Sep 17 00:00:00 2001 From: DemiPixel Date: Sat, 5 Sep 2015 18:23:27 -0700 Subject: [PATCH] Added player.sendBlock and serv.setBlock. Added an example command /setblock and made changes (and fixes) to api.md --- doc/api.md | 18 ++++++++++++++++-- lib/playerPlugins/blocks.js | 13 ++++++++++--- lib/playerPlugins/commands.js | 18 ++++++++++++++++++ lib/serverPlugins/blocks.js | 14 ++++++++++++++ 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 lib/serverPlugins/blocks.js diff --git a/doc/api.md b/doc/api.md index 616426e..70967ef 100644 --- a/doc/api.md +++ b/doc/api.md @@ -21,6 +21,7 @@ - [serv.createLog()](#servcreatelog) - [serv.log(message)](#servlogmessage) - [serv.broadcast(message[,color])](#servbroadcastmessagecolor) + - [serv.setBlock(position,blockType)](#servsetblockpositionblocktype) - [Player](#player) - [Properties](#properties-1) - [player.entity](#playerentity) @@ -33,9 +34,10 @@ - ["chat" (message)](#chat-message) - [Methods](#methods-1) - [player.login()](#playerlogin) - - [player.others()](#playerothers) + - [player.getOthers()](#playergetothers) - [player.chat(message)](#playerchatmessage) - [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype) + - [player.sendBlock(position,blockType)](#playersendblockpositionblocktype) - [player.sendInitialPosition()](#playersendinitialposition) - [player.spawn()](#playerspawn) - [player.setGameMode(gameMode)](#playersetgamemodegamemode) @@ -113,6 +115,10 @@ logs a `message` broadcasts `message` to all the players with the optional `color`. +#### serv.setBlock(position,blockType) + +Saves block in world and sends block update to all players. + ## Player ### Properties @@ -153,7 +159,7 @@ Fires when the player says `message`. login -#### player.others() +#### player.getOthers() return the other players than `player` @@ -165,6 +171,14 @@ sends `message` to the player change the block at position `position` to `blockType` +this will not change the block for the user themself. It is mainly useful when a user places a block and only needs to send it to other players on the server + +#### player.sendBlock(position,blockType) + +change the block at position `position` to `blockType` + +this will not make any changes on the server's world and only sends it to the user as a "fake" or "local" block + #### player.sendInitialPosition() send its initial position to the player diff --git a/lib/playerPlugins/blocks.js b/lib/playerPlugins/blocks.js index 7602a19..1013bbf 100644 --- a/lib/playerPlugins/blocks.js +++ b/lib/playerPlugins/blocks.js @@ -4,12 +4,19 @@ function inject(serv,player) { function changeBlock(position,blockType) { - player._writeOthers("block_change",{ - location:position, - type:blockType<<4 + player.getOthers().forEach(function(player) { + player.sendBlock(position, blockType); }); serv.world.setBlockType(position,blockType); } + + function sendBlock(position, blockType) { // Call from serv.setBlock unless you want "local" fake blocks + player._client.write("block_change",{ + location:position, + type:blockType<<4 + }); + } player.changeBlock=changeBlock; + player.sendBlock=sendBlock; } \ No newline at end of file diff --git a/lib/playerPlugins/commands.js b/lib/playerPlugins/commands.js index 475cd13..82e14f1 100644 --- a/lib/playerPlugins/commands.js +++ b/lib/playerPlugins/commands.js @@ -1,3 +1,5 @@ +var Vec3 = require('vec3'); + module.exports=inject; function inject(serv, player, options) @@ -11,6 +13,22 @@ function inject(serv, player, options) var gameMode=parseInt(results[1]); player.setGameMode(gameMode); } + else if(results=command.match(/^setblock/)) { // Like old version which uses ids + results = command.match(/^setblock (~|~?-?[0-9]*) (~|~?-?[0-9]*) (~|~?-?[0-9]*) ([0-9]{1,3})/); + if(!results) { + player.chat("Usage: /setblock "); + } + else { + results = results.map(function(num, i) { // parseInt paramaters + if (num.indexOf('~') == 0) { + return (player.entity.position[['','x', 'y', 'z'][i]]>>5) + parseInt(num.slice(1) || 0); + } + else + return parseInt(num); // return parseInt>>5 if position, not id + }); + serv.setBlock(new Vec3(results[1], results[2], results[3]), results[4]); + } + } else player.chat("Invalid command."); } diff --git a/lib/serverPlugins/blocks.js b/lib/serverPlugins/blocks.js new file mode 100644 index 0000000..16133ae --- /dev/null +++ b/lib/serverPlugins/blocks.js @@ -0,0 +1,14 @@ +module.exports=inject; + +function inject(serv) +{ + function setBlock(position,blockType) + { + serv.players.forEach(function(player){ + player.sendBlock(position, blockType); + }); + serv.world.setBlockType(position,blockType); + } + + serv.setBlock = setBlock; +} \ No newline at end of file