add player.handleCommand to the api, improve command handling

This commit is contained in:
Romain Beaumont 2015-08-28 01:10:35 +02:00
parent 8dded86058
commit 6e747b6fc0
3 changed files with 30 additions and 23 deletions

View file

@ -61,6 +61,7 @@
- [player.sendInitialPosition()](#playersendinitialposition)
- [player.spawn()](#playerspawn)
- [player.setGameMode(gameMode)](#playersetgamemodegamemode)
- [player.handleCommand(command)](#playerhandlecommandcommand)
- [Low level properties](#low-level-properties)
- [player._client](#player_client)
- [Low level methods](#low-level-methods)
@ -283,6 +284,10 @@ tell everybody else that the player spawned
set player gameMode to `gameMode`
#### player.handleCommand(command)
handle `command`
### Low level properties
#### player._client

View file

@ -1,35 +1,18 @@
module.exports=inject;
function inject(serv, player, options)
function inject(serv, player)
{
player._client.on('chat', function (packet) {
if(!handleCommand(packet.message)) {
if(packet.message[0]=="/") {
var command = packet.message.slice(1);
player.handleCommand(command);
}
else {
serv.broadcast('<' + player.username + '>' + ' ' + packet.message);
player.emit("chat",packet.message);
}
});
function handleCommand(message)
{
var command;
if(message[0]=="/")
command=message.slice(1);
else return false;
if(options.commands[command]) {
player.chat("" + options.commands[command]);
return true;
}
var results;
if(results=command.match(/^gamemode ([0-3])$/)) {
var gameMode=parseInt(results[1]);
player.setGameMode(gameMode);
return true;
}
player.chat("Invalid command.");
return true;
}
function chat(message) {
player._client.write('chat', { message: JSON.stringify(message), position: 0 });
}

View file

@ -0,0 +1,19 @@
module.exports=inject;
function inject(serv, player, options)
{
function handleCommand(command)
{
var results;
if(options.commands[command])
player.chat("" + options.commands[command]);
else if(results=command.match(/^gamemode ([0-3])$/)) {
var gameMode=parseInt(results[1]);
player.setGameMode(gameMode);
}
else
player.chat("Invalid command.");
}
player.handleCommand=handleCommand;
}