From f746d17424a071c15fd5b92bc521ce832ba15a07 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 4 Jun 2013 21:05:33 +0000 Subject: ProtectionAreas: Actual protection is now working, areas are hard-coded (10,10) - (20,20) git-svn-id: http://mc-server.googlecode.com/svn/trunk@1557 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- MCServer/Plugins/ProtectionAreas/CommandState.lua | 2 +- MCServer/Plugins/ProtectionAreas/HookHandlers.lua | 26 ++++++++---- MCServer/Plugins/ProtectionAreas/PlayerAreas.lua | 6 +-- .../Plugins/ProtectionAreas/ProtectionAreas.lua | 1 + MCServer/Plugins/ProtectionAreas/Storage.lua | 49 +++++++++++++++++++++- 5 files changed, 72 insertions(+), 12 deletions(-) diff --git a/MCServer/Plugins/ProtectionAreas/CommandState.lua b/MCServer/Plugins/ProtectionAreas/CommandState.lua index f9d0e28db..3df1259cc 100644 --- a/MCServer/Plugins/ProtectionAreas/CommandState.lua +++ b/MCServer/Plugins/ProtectionAreas/CommandState.lua @@ -40,7 +40,7 @@ end --- Returns the current coord pair as a cCuboid object -function cCommandState.GetCurrentCuboid() +function cCommandState:GetCurrentCuboid() local res = cCuboid( self.Coords1.x, self.Coords1.y, self.Coords1.z, self.Coords2.x, self.Coords2.y, self.Coords2.z diff --git a/MCServer/Plugins/ProtectionAreas/HookHandlers.lua b/MCServer/Plugins/ProtectionAreas/HookHandlers.lua index 4f1e3755e..d0748a8a4 100644 --- a/MCServer/Plugins/ProtectionAreas/HookHandlers.lua +++ b/MCServer/Plugins/ProtectionAreas/HookHandlers.lua @@ -21,7 +21,7 @@ end function OnDisconnect(a_Player, a_Reason) -- Remove the player's cProtectionArea object -- TODO: What if there are two players with the same name? need to check - g_PlayerAreas[a_Player:GetName()] = nil; + g_PlayerAreas[a_Player:GetUniqueID()] = nil; -- If the player is a VIP, they had a command state, remove that as well g_CommandStates[a_Player:GetUniqueID()] = nil; @@ -34,10 +34,12 @@ end; function OnPlayerJoined(a_Player) - -- Create a new cProtectionArea for this player - g_PlayerAreas[a_Player:GetName()] = cPlayerAreas:new(); - - -- TODO: Load the protection areas for this player + -- Create a new cPlayerAreas object for this player + local PlayerName = a_Player:GetName(); + local PlayerID = a_Player:GetUniqueID(); + if (g_PlayerAreas[PlayerID] == nil) then + g_PlayerAreas[PlayerID] = g_Storage:LoadPlayerAreas(PlayerName); + end; return false; end @@ -63,8 +65,13 @@ function OnPlayerLeftClick(a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, return true; end; - -- TODO: Check the player areas to see whether to disable this action + -- Check the player areas to see whether to disable this action + local Areas = g_PlayerAreas[a_Player:GetUniqueID()]; + if not(Areas:CanInteractWithBlock(a_BlockX, a_BlockY, a_BlockZ)) then + return true; + end + -- Allow interaction return false; end @@ -89,8 +96,13 @@ function OnPlayerRightClick(a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, return true; end; - -- TODO: Check the player areas to see whether to disable this action + -- Check the player areas to see whether to disable this action + local Areas = g_PlayerAreas[a_Player:GetUniqueID()]; + if not(Areas:CanInteractWithBlock(a_BlockX, a_BlockY, a_BlockZ)) then + return true; + end + -- Allow interaction return false; end diff --git a/MCServer/Plugins/ProtectionAreas/PlayerAreas.lua b/MCServer/Plugins/ProtectionAreas/PlayerAreas.lua index 472a4c59b..5d054ea15 100644 --- a/MCServer/Plugins/ProtectionAreas/PlayerAreas.lua +++ b/MCServer/Plugins/ProtectionAreas/PlayerAreas.lua @@ -11,7 +11,7 @@ A player can interact with a block if either one of these is true: 2, There is at least one area covering the block with IsAllowed set to true The OOP class implementation follows the PiL 16.1 -Also, a global table g_PlayerAreas is the actual map of PlayerName -> cPlayerAreas +Also, a global table g_PlayerAreas is the actual map of PlayerID -> cPlayerAreas --]] @@ -37,7 +37,7 @@ end -- Adds a new cuboid to the area list, where the player is either allowed or not, depending on the IsAllowed param function cPlayerAreas:AddArea(a_Cuboid, a_IsAllowed) - table.add(self, {Cuboid = a_Cuboid, IsAllowed = a_IsAllowed}); + table.insert(self, {Cuboid = a_Cuboid, IsAllowed = a_IsAllowed}); end @@ -45,7 +45,7 @@ end --- returns true if the player owning this object can interact with the specified block -function cPlayerAreas:CanInteract(a_BlockX, a_BlockY, a_BlockZ) +function cPlayerAreas:CanInteractWithBlock(a_BlockX, a_BlockY, a_BlockZ) -- iterate through all the stored areas: local IsInsideAnyArea = false; for idx, Area in ipairs(self) do diff --git a/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua b/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua index 36256c5f3..d816f578f 100644 --- a/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua +++ b/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua @@ -10,6 +10,7 @@ function Initialize(a_Plugin) a_Plugin:SetName("ProtectionAreas"); a_Plugin:SetVersion(1); + InitializeStorage(); InitializeHooks(a_Plugin); InitializeCommandHandlers(); diff --git a/MCServer/Plugins/ProtectionAreas/Storage.lua b/MCServer/Plugins/ProtectionAreas/Storage.lua index 260f895c2..8aebae0c7 100644 --- a/MCServer/Plugins/ProtectionAreas/Storage.lua +++ b/MCServer/Plugins/ProtectionAreas/Storage.lua @@ -2,8 +2,55 @@ -- Storage.lua -- Implements the storage access object, shielding the rest of the code away from the DB +--[[ +The cStorage class is the interface to the underlying storage, the SQLite database. +This class knows how to load player areas from the DB, how to add or remove areas in the DB +and other such operations. + +Also, a g_Storage global variable is declared, it holds the single instance of the storage. +--]] + + + + + +cStorage = {}; + +g_Storage = {}; + + + + + +--- Initializes the storage subsystem, creates the g_Storage object +function InitializeStorage() + g_Storage = cStorage:new(); +end + + + + + +function cStorage:new(obj) + obj = obj or {}; + setmetatable(obj, self); + self.__index = self; + return obj; +end + + + + +--- Loads cPlayerAreas for the specified player from the DB. Returns a cPlayerAreas object +function cStorage:LoadPlayerAreas(PlayerName) + local res = cPlayerAreas:new(); + -- TODO: Load the areas from the DB, based on the player's location + + -- DEBUG: Insert a dummy area for testing purposes: + res:AddArea(cCuboid(10, 0, 10, 20, 255, 20), false); + return res; +end --- TODO -- cgit v1.2.3