From 0aae8c6c8ec7f1784efa0b1bd83e897acb24c497 Mon Sep 17 00:00:00 2001 From: DemiPixel Date: Wed, 9 Sep 2015 00:44:48 -0700 Subject: [PATCH] Fixing bugs, added world-guard example, updated API a bit --- doc/api.md | 4 +- examples/plugins/world-guard/index.js | 158 ++++++++++++++++++++++ examples/plugins/world-guard/package.json | 11 ++ lib/playerPlugins/animations.js | 2 + lib/playerPlugins/chat.js | 2 + lib/playerPlugins/digging.js | 5 +- lib/serverPlugins/login.js | 2 +- 7 files changed, 179 insertions(+), 5 deletions(-) create mode 100644 examples/plugins/world-guard/index.js create mode 100644 examples/plugins/world-guard/package.json diff --git a/doc/api.md b/doc/api.md index 7128894..1665d4d 100644 --- a/doc/api.md +++ b/doc/api.md @@ -40,7 +40,7 @@ - ["disconnected"](#disconnected) - ["error" (error)](#error-error-1) - ["kicked" (kicker,reason)](#kicked-kickerreason) - - [Cancelable Events](#cancelable-events) + - [Cancelable Behaviors](#cancelable-behaviors) - ["chatMessage"](#chatmessage) - ["chat"](#chat) - ["command"](#command) @@ -215,7 +215,7 @@ Fires when there is an error. `kicker` kick the player with `reason` -### Cancelable Events +### Cancelable Behaviors This type of event is emitted by the the player with the option to cancel a default. It is primarily used by external plugins. This type of event is emitted twice. For example, if a player digs a block, both digBlock\_cancel and digBlock are emitted. diff --git a/examples/plugins/world-guard/index.js b/examples/plugins/world-guard/index.js new file mode 100644 index 0000000..2201c15 --- /dev/null +++ b/examples/plugins/world-guard/index.js @@ -0,0 +1,158 @@ +module.exports = inject; + +function inject(serv, player, self) { + + player.plugins[self.id].pos = { + 1: null, + 2: null + }; + player.plugins[self.id].op = false; + self.areas = []; + + player = player; + + player.on('command_cancel', function(e, cancel) { + var player = this; + var text = e.message; + var split = text.split(' '); + if (split[0] != 'wg') return; + else cancel(); + var opped = player.plugins[self.id].op; + + if (split[1] == 'pos1' && opped) { + self.setPosition(player, 1, player.entity.position.x>>5, player.entity.position.z>>5); + player.chat('Set pos1 at ' + (player.entity.position.x>>5) + ',' + (player.entity.position.z>>5)); + } + else if (split[1] == 'pos2' && opped) { + self.setPosition(player, 2, player.entity.position.x>>5, player.entity.position.z>>5); + player.chat('Set pos2 at ' + (player.entity.position.x>>5) + ',' + (player.entity.position.z>>5)); + } + else if (split[1] == 'set' && opped) { + var success = self.setArea(player.plugins[self.id].pos); + if (!success) { + player.chat('You need to set two positions to set an area. Move to the location you want and use:'); + player.chat('"/wg pos1" or "/wg pos2"'); + } else { + player.chat('Successfully set area! (Size: ' + success.width + 'x' + success.length + ')'); + self.clearPosition(player, 1); + self.clearPosition(player, 2); + } + } else if (split[1] == 'clear' && opped) { + self.clearPosition(player, 1); + self.clearPosition(player, 2); + player.chat('Cleared positions'); + } else if (split[1] == 'op') { + var playerTarget = serv.getPlayer(split[2]); + if (!playerTarget) { + player.chat('No such player "' + split[2] + '"'); + } else { + playerTarget.plugins[self.id].op = true; + player.chat('WG Opped ' + split[2]); + } + } else if (split[1] == 'deop' && opped) { + var playerTarget = serv.getPlayer(split[2]); + if (!playerTarget) { + player.chat('No such player "' + split[2] + '"'); + } else { + playerTarget.plugins[self.id].op = false; + player.chat('WG Deopped ' + split[2]); + } + } else if (split[1] == 'help') { + var messages = [ + 'World Guard is used to restrict building areas!', + 'Use /wg pos1 or /wg pos2 to set your two positions.', + 'Use /wg set to confirm the positions.', + 'Use /wg op to allow a user to set positions or build/mine in restricted areas', + 'Use /wg deop to remove op.', + 'Use /wg clear to clear positions', + 'Use /wg list to list all restricted areas you are in (and their IDs)', + 'Use /wg delete to delete a restricted area' + ]; + for (var m in messages) { + player.chat(messages[m]); + } + } else if (split[1] == 'list' && opped) { + var areas = self.areasInPosition(player.entity.position.x>>5, player.entity.position.z>>5); + if (!areas.length) { + player.chat('You are not in any restricted areas!'); + } else player.chat('==== LIST OF AREAS ===='); + for (var a in areas) { + var ar = areas[a]; + var str = ar[0] + '] ' + ar[1][1].x + ',' + ar[1][1].z + ' to ' + ar[1][2].x + ',' + ar[1][2].z; + str += ' (' + (Math.abs(ar[1][1].x - ar[1][2].x)+1) + 'x' + (Math.abs(ar[1][1].z - ar[1][2].z)+1) + ')'; + player.chat(str); + } + } else if (split[1] == 'delete' && opped) { + if (!self.areas[split[2]]) { + player.chat('Area with id ' + split[2] + ' does not exist!'); + } else { + self.deleteArea(split[2]); + player.chat('Deleted area (id ' + split[2] + ')'); + } + } else { + player.chat('Not a valid World Guard command! Use /wg help'); + } + }); + + player.on('placeBlock_cancel', function(e, cancel) { + if (player.plugins[self.id].op === true) return; + if (self.areasInPosition(e.position.x, e.position.z).length) { + cancel(); + player.sendBlock(e.position, 0); + } + }); + + player.on('finishDig_cancel', function(e, cancel) { + if (player.plugins[self.id].op === true) return; + if (self.areasInPosition(e.position.x, e.position.z).length) { + cancel(); + player.sendBlock(e.position, e.block.type); + } + }); + + self.setPosition = function(player, which, x, z) { + if (which != 1 && which != 2) return; + + player.plugins[self.id].pos[which] = { x: x, z: z} + }; + + self.clearPosition = function(player, which) { + if (which != 1 && which != 2) return; + player.plugins[self.id].pos[which] = null; + } + + self.setArea = function(pos) { + if (!pos[1] || !pos[2]) return false; + + self.areas.push({ + 1: { + x: pos[1].x, + z: pos[1].z + }, + 2: { + x: pos[2].x, + z: pos[2].z + } + }); + return { + width: Math.abs(pos[1].x-pos[2].x) + 1, + length: Math.abs(pos[1].z-pos[2].z) + 1 + } + } + + self.deleteArea = function(id) { + self.areas[id] = null; + } + + self.areasInPosition = function(x, z) { + var inside = []; + for (var a in self.areas) { + if (!self.areas[a]) continue; + var x1 = self.areas[a][1].x, x2 = self.areas[a][2].x, z1 = self.areas[a][1].z, z2 = self.areas[a][2].z; + if (x >= Math.min(x1,x2) && x <= Math.max(x1,x2) && z >= Math.min(z1,z2) && z <= Math.max(z1, z2)) { + inside.push([a, self.areas[a]]); + } + } + return inside; + } +} \ No newline at end of file diff --git a/examples/plugins/world-guard/package.json b/examples/plugins/world-guard/package.json new file mode 100644 index 0000000..5b54599 --- /dev/null +++ b/examples/plugins/world-guard/package.json @@ -0,0 +1,11 @@ +{ + "name": "world-guard", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "DemiPixel ", + "license": "ISC" +} diff --git a/lib/playerPlugins/animations.js b/lib/playerPlugins/animations.js index f1dced0..7b47d88 100644 --- a/lib/playerPlugins/animations.js +++ b/lib/playerPlugins/animations.js @@ -1,3 +1,5 @@ +var cancelEmit = require("../cancelEvent"); + module.exports=inject; function inject(serv, player) diff --git a/lib/playerPlugins/chat.js b/lib/playerPlugins/chat.js index 0cf0f1c..319cdd4 100644 --- a/lib/playerPlugins/chat.js +++ b/lib/playerPlugins/chat.js @@ -1,3 +1,5 @@ +var cancelEmit = require("../cancelEvent"); + module.exports=inject; function inject(serv, player) diff --git a/lib/playerPlugins/digging.js b/lib/playerPlugins/digging.js index 1daf3f7..8f6885a 100644 --- a/lib/playerPlugins/digging.js +++ b/lib/playerPlugins/digging.js @@ -115,9 +115,10 @@ function inject(serv,player) { var doDefault = cancelEmit(player, "finishDig", { time: 0, - position: pos, - block: serv.world.getBlock(pos) + position: location, + block: serv.world.getBlock(location) }); + if (!doDefault) return; player.changeBlock(location,0); } diff --git a/lib/serverPlugins/login.js b/lib/serverPlugins/login.js index 2d6f364..ce9ee05 100644 --- a/lib/serverPlugins/login.js +++ b/lib/serverPlugins/login.js @@ -18,8 +18,8 @@ function inject(serv,options) player.plugins = Array(); for(var pluginName in serv.plugins) { // External plugins - require(serv.plugins[pluginName].path)(serv, player, serv.plugins[pluginName], options); player.plugins[serv.plugins[pluginName].id] = {}; // Give object to save data per plugin per player, referenced by plugin ID + require(serv.plugins[pluginName].path)(serv, player, serv.plugins[pluginName], options); } }); } \ No newline at end of file