mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-04 18:50:46 +00:00
add player.handleCommand to the api, improve command handling
This commit is contained in:
parent
8dded86058
commit
6e747b6fc0
3 changed files with 30 additions and 23 deletions
|
|
@ -61,6 +61,7 @@
|
||||||
- [player.sendInitialPosition()](#playersendinitialposition)
|
- [player.sendInitialPosition()](#playersendinitialposition)
|
||||||
- [player.spawn()](#playerspawn)
|
- [player.spawn()](#playerspawn)
|
||||||
- [player.setGameMode(gameMode)](#playersetgamemodegamemode)
|
- [player.setGameMode(gameMode)](#playersetgamemodegamemode)
|
||||||
|
- [player.handleCommand(command)](#playerhandlecommandcommand)
|
||||||
- [Low level properties](#low-level-properties)
|
- [Low level properties](#low-level-properties)
|
||||||
- [player._client](#player_client)
|
- [player._client](#player_client)
|
||||||
- [Low level methods](#low-level-methods)
|
- [Low level methods](#low-level-methods)
|
||||||
|
|
@ -283,6 +284,10 @@ tell everybody else that the player spawned
|
||||||
|
|
||||||
set player gameMode to `gameMode`
|
set player gameMode to `gameMode`
|
||||||
|
|
||||||
|
#### player.handleCommand(command)
|
||||||
|
|
||||||
|
handle `command`
|
||||||
|
|
||||||
### Low level properties
|
### Low level properties
|
||||||
|
|
||||||
#### player._client
|
#### player._client
|
||||||
|
|
|
||||||
|
|
@ -1,35 +1,18 @@
|
||||||
module.exports=inject;
|
module.exports=inject;
|
||||||
|
|
||||||
function inject(serv, player, options)
|
function inject(serv, player)
|
||||||
{
|
{
|
||||||
player._client.on('chat', function (packet) {
|
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);
|
serv.broadcast('<' + player.username + '>' + ' ' + packet.message);
|
||||||
player.emit("chat",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) {
|
function chat(message) {
|
||||||
player._client.write('chat', { message: JSON.stringify(message), position: 0 });
|
player._client.write('chat', { message: JSON.stringify(message), position: 0 });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
lib/playerPlugins/commands.js
Normal file
19
lib/playerPlugins/commands.js
Normal 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;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue