Skip to content

Commit

Permalink
Admins + Bans
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeoneWeird committed Dec 11, 2012
1 parent 0642e64 commit 3ff510a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"name": "Another jsmc server",
"mode": 1,
"max_players": 25,
"difficulty": 0
"difficulty": 0,
"admins": [

],
"banned": [

]
}
}
2 changes: 2 additions & 0 deletions lib/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var Game = module.exports = function Game(config) {
this.max_players = config.max_players || 25;
this.mode = config.mode || 0;
this.difficulty = config.difficulty || 0;
this.admins = config.admins || [];
this.banned = config.banned || [];

this.world = {
type: "flat",
Expand Down
5 changes: 5 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var Player = module.exports = function Player(client, game, name, position) {
this.game = game;
this.eid = game.get_eid();
this.name = name;
this.admin = false;

this.position = {
x: 0,
Expand Down Expand Up @@ -50,3 +51,7 @@ Player.prototype.kill = function() {
Player.prototype.respawn = function() {
this.client.emit("data", {pid: 0x09, dimension: this.game.world.dimension, difficulty: this.game.difficulty, game_mode: this.game.mode, world_height: 256, level_type: this.game.world.type});
};

Player.prototype.isAdmin = function() {
return this.admin;
};
5 changes: 5 additions & 0 deletions lib/serialiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ Serialiser.prototype.write = function write(packet) {
break;
}

case 0x46: {
this.emit("data", S().uint8(packet.pid).uint8(packet.reason).uint8(packet.game_mode).result());
break;
}

case 0x64: {
this.emit("data", S().uint8(packet.pid).uint8(packet.window).uint8(packet.type).mcstring16(packet.title).uint8(packet.slots).result());
break;
Expand Down
56 changes: 56 additions & 0 deletions plugins/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module.exports = function() {
return function(game) {
game.on("player:join", function(player) {
player.client.on("packet:03", function(packet) {

if(player.isAdmin()) {

var m = packet.message.split(' ');

switch(m[0]) {

case "/kick": {
game.players.forEach(function(player) {
if(player.name==m[1]) {
player.kick(m[2]);
}
});
break;
};

case "/ban": {
game.players.forEach(function(player) {
if(player.name==m[1]) {
player.kick(m[2]);
}
});
game.banned.push(m[1]);
break;
};

case "/op": {
game.players.forEach(function(player) {
if(player.name==m[1]) {
player.admin = true;
player.message("You are now op!");
}
});
break;
}

case "/deop": {
game.players.forEach(function(player) {
if(player.name==m[1]) {
player.admin = false;
player.message("You are no longer op!");
}
});
break;
}

}
}
});
});
};
};
11 changes: 11 additions & 0 deletions plugins/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ module.exports = function() {
for (y = 255; y > 0 && chunk.get_block_type(0, 0, y) === 0; --y) {}
y += 2;

if(~game.banned.indexOf(packet.username)) {
client.emit("data", {
pid: 0xff,
message: "Banned."
});
return;
}

var player = new Player(client, game, packet.username, {x: 0, y: y, z: 0, stance: y + 1.62, yaw: 0, pitch: 0});

if(~game.admins.indexOf(packet.username))
player.admin = true;

console.log("created player " + player.name + " and spawning at " + [player.position.x, player.position.y, player.position.z].join(","));

console.log("logging player in");
Expand Down
6 changes: 6 additions & 0 deletions server.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ var game = new Game({
mode: nconf.get("game:mode"),
max_players: nconf.get("game:max_players"),
difficulty: nconf.get("game:difficulty"),
admins: nconf.get("game:admins"),
banned: nconf.get("game:banned"),
map: map,
});

Expand Down Expand Up @@ -140,6 +142,10 @@ game.use(DespawnPlugin());
var SaveMapPlugin = require("./plugins/save-map");
game.use(SaveMapPlugin(__dirname + "/map"));

// Provides administrative commands `/ban` `/op` etc.
var AdminPlugin = require('./plugins/admin');
game.use(AdminPlugin());

// The server object is basically a wrapper around `net.Server` that constructs
// `Client` objects as they connect.

Expand Down

0 comments on commit 3ff510a

Please sign in to comment.