Merge pull request #38 from demipixel/block_stuff

Added setBlock, sendBlock, some fixes, and /setblock
This commit is contained in:
Romain Beaumont 2015-09-06 03:32:22 +02:00
commit fbc9a10cef
4 changed files with 58 additions and 5 deletions

View file

@ -21,6 +21,7 @@
- [serv.createLog()](#servcreatelog) - [serv.createLog()](#servcreatelog)
- [serv.log(message)](#servlogmessage) - [serv.log(message)](#servlogmessage)
- [serv.broadcast(message[,color])](#servbroadcastmessagecolor) - [serv.broadcast(message[,color])](#servbroadcastmessagecolor)
- [serv.setBlock(position,blockType)](#servsetblockpositionblocktype)
- [Player](#player) - [Player](#player)
- [Properties](#properties-1) - [Properties](#properties-1)
- [player.entity](#playerentity) - [player.entity](#playerentity)
@ -33,9 +34,10 @@
- ["chat" (message)](#chat-message) - ["chat" (message)](#chat-message)
- [Methods](#methods-1) - [Methods](#methods-1)
- [player.login()](#playerlogin) - [player.login()](#playerlogin)
- [player.others()](#playerothers) - [player.getOthers()](#playergetothers)
- [player.chat(message)](#playerchatmessage) - [player.chat(message)](#playerchatmessage)
- [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype) - [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype)
- [player.sendBlock(position,blockType)](#playersendblockpositionblocktype)
- [player.sendInitialPosition()](#playersendinitialposition) - [player.sendInitialPosition()](#playersendinitialposition)
- [player.spawn()](#playerspawn) - [player.spawn()](#playerspawn)
- [player.setGameMode(gameMode)](#playersetgamemodegamemode) - [player.setGameMode(gameMode)](#playersetgamemodegamemode)
@ -113,6 +115,10 @@ logs a `message`
broadcasts `message` to all the players with the optional `color`. 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 ## Player
### Properties ### Properties
@ -153,7 +159,7 @@ Fires when the player says `message`.
login login
#### player.others() #### player.getOthers()
return the other players than `player` return the other players than `player`
@ -165,6 +171,14 @@ sends `message` to the player
change the block at position `position` to `blockType` 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() #### player.sendInitialPosition()
send its initial position to the player send its initial position to the player

View file

@ -4,12 +4,19 @@ function inject(serv,player)
{ {
function changeBlock(position,blockType) function changeBlock(position,blockType)
{ {
player._writeOthers("block_change",{ player.getOthers().forEach(function(player) {
location:position, player.sendBlock(position, blockType);
type:blockType<<4
}); });
serv.world.setBlockType(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.changeBlock=changeBlock;
player.sendBlock=sendBlock;
} }

View file

@ -1,3 +1,5 @@
var Vec3 = require('vec3');
module.exports=inject; module.exports=inject;
function inject(serv, player, options) function inject(serv, player, options)
@ -11,6 +13,22 @@ function inject(serv, player, options)
var gameMode=parseInt(results[1]); var gameMode=parseInt(results[1]);
player.setGameMode(gameMode); 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 <x> <y> <z> <id>");
}
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 else
player.chat("Invalid command."); player.chat("Invalid command.");
} }

View file

@ -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;
}