summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2013-06-06 22:42:42 +0200
committermadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2013-06-06 22:42:42 +0200
commit6cb76856ff95bdc56d15b9dee6dc80dfbc755fd9 (patch)
tree0012765f0fc3d86719b956e57b79db5aa33da5b4
parentProtectionAreas: Added cStorage initialization (diff)
downloadcuberite-6cb76856ff95bdc56d15b9dee6dc80dfbc755fd9.tar
cuberite-6cb76856ff95bdc56d15b9dee6dc80dfbc755fd9.tar.gz
cuberite-6cb76856ff95bdc56d15b9dee6dc80dfbc755fd9.tar.bz2
cuberite-6cb76856ff95bdc56d15b9dee6dc80dfbc755fd9.tar.lz
cuberite-6cb76856ff95bdc56d15b9dee6dc80dfbc755fd9.tar.xz
cuberite-6cb76856ff95bdc56d15b9dee6dc80dfbc755fd9.tar.zst
cuberite-6cb76856ff95bdc56d15b9dee6dc80dfbc755fd9.zip
-rw-r--r--MCServer/Plugins/ProtectionAreas/CommandHandlers.lua33
-rw-r--r--MCServer/Plugins/ProtectionAreas/CommandState.lua4
-rw-r--r--MCServer/Plugins/ProtectionAreas/Storage.lua54
3 files changed, 85 insertions, 6 deletions
diff --git a/MCServer/Plugins/ProtectionAreas/CommandHandlers.lua b/MCServer/Plugins/ProtectionAreas/CommandHandlers.lua
index 21049a517..cc2e18b21 100644
--- a/MCServer/Plugins/ProtectionAreas/CommandHandlers.lua
+++ b/MCServer/Plugins/ProtectionAreas/CommandHandlers.lua
@@ -25,7 +25,38 @@ function HandleAddArea(a_Split, a_Player)
return true;
end
- -- TODO: Add the area to the storage and reload all currently logged in players
+ -- Get the cuboid that the player had selected
+ local CmdState = GetCommandStateForPlayer(a_Player);
+ if (CmdState == nil) then
+ a_Player:SendMessage("Cannot add area, internal plugin error (CmdState == nil)");
+ return true;
+ end
+ local Cuboid = CmdState:GetCurrentCuboid();
+ if (Cuboid == nil) then
+ a_Player:SendMessage("Cannot add area, internal plugin error (Cuboid == nil)");
+ return true;
+ end
+
+ -- If the cuboid hasn't been assigned, give the player an error message and bail out
+ if (
+ (Cuboid.p1.x == 0) and (Cuboid.p1.y == 0) and (Cuboid.p1.z == 0) and
+ (Cuboid.p1.x == 0) and (Cuboid.p1.y == 0) and (Cuboid.p1.z == 0)
+ ) then
+ a_Player:SendMessage("Cannot add area, no area has been selected. Use a ProtWand lclk / rclk to select area first");
+ return true;
+ end
+
+ -- Put all allowed players into a table:
+ AllowedNames = {};
+ for i = 2, #a_Split do
+ table.insert(AllowedNames, a_Split[i]);
+ end
+
+ -- Add the area to the storage
+ g_Storage:AddArea(Cuboid, a_Player:GetName(), AllowedNames);
+ a_Player:SendMessage("Area added");
+
+ -- TODO: Reload all currently logged in players
return true;
end
diff --git a/MCServer/Plugins/ProtectionAreas/CommandState.lua b/MCServer/Plugins/ProtectionAreas/CommandState.lua
index 3df1259cc..8feae8f8c 100644
--- a/MCServer/Plugins/ProtectionAreas/CommandState.lua
+++ b/MCServer/Plugins/ProtectionAreas/CommandState.lua
@@ -42,8 +42,8 @@ end
--- Returns the current coord pair as a cCuboid object
function cCommandState:GetCurrentCuboid()
local res = cCuboid(
- self.Coords1.x, self.Coords1.y, self.Coords1.z,
- self.Coords2.x, self.Coords2.y, self.Coords2.z
+ self.m_Coords1.x, self.m_Coords1.y, self.m_Coords1.z,
+ self.m_Coords2.x, self.m_Coords2.y, self.m_Coords2.z
);
res:Sort();
return res;
diff --git a/MCServer/Plugins/ProtectionAreas/Storage.lua b/MCServer/Plugins/ProtectionAreas/Storage.lua
index 21c147300..307233eac 100644
--- a/MCServer/Plugins/ProtectionAreas/Storage.lua
+++ b/MCServer/Plugins/ProtectionAreas/Storage.lua
@@ -72,7 +72,7 @@ function cStorage:OpenDB()
end
if (
- not(self:CreateTable("Area", {"ID INTEGER PRIMARY KEY AUTOINCREMENT", "MinX", "MaxX", "MinZ", "MaxZ", "CreatorUserName"})) or
+ not(self:CreateTable("Areas", {"ID INTEGER PRIMARY KEY AUTOINCREMENT", "MinX", "MaxX", "MinZ", "MaxZ", "CreatorUserName"})) or
not(self:CreateTable("AllowedUsers", {"AreaID", "UserName"}))
) then
LOGWARNING(PluginPrefix .. "Cannot create DB tables!");
@@ -151,11 +151,59 @@ end
+--- Adds a new area into the DB. a_AllowedNames is a table listing all the players that are allowed in the area
+function cStorage:AddArea(a_Cuboid, a_CreatorName, a_AllowedNames)
+ -- Store the area in the DB
+ local ID = -1;
+ local function RememberID(UserData, NumCols, Values, Names)
+ for i = 1, NumCols do
+ if (Names[i] == "ID") then
+ ID = Values[i];
+ end
+ end
+ return 0;
+ end
+ local sql =
+ "INSERT INTO Areas (ID, MinX, MaxX, MinZ, MaxZ, CreatorUserName) VALUES (NULL, " ..
+ a_Cuboid.p1.x .. ", " .. a_Cuboid.p2.x .. ", " .. a_Cuboid.p1.z .. ", " .. a_Cuboid.p2.z .. ", '" .. a_CreatorName ..
+ "'); SELECT last_insert_rowid() as ID";
+ if (not(self:DBExec(sql, RememberID))) then
+ LOGWARNING(PluginPrefix .. "SQL Error while inserting new area");
+ return false;
+ end
+ if (ID == -1) then
+ LOGWARNING(PluginPrefix .. "SQL Error while retrieving INSERTion ID");
+ return false;
+ end
+
+ -- Store each allowed player in the DB
+ for idx, Name in ipairs(a_AllowedNames) do
+ local sql = "INSERT INTO AllowedUsers (AreaID, UserName) VALUES (" .. ID .. ", '" .. Name .. "')";
+ if (not(self:DBExec(sql))) then
+ LOGWARNING(PluginPrefix .. "SQL Error while inserting new area's allowed player " .. Name);
+ end;
+ end
+end
+
+
+
+
-function cStorage:AddArea(a_Cuboid, a_Creator, a_AllowedPlayers)
- -- TODO
+--- Executes the SQL command given, calling the a_Callback for each result
+-- If the SQL command fails, prints it out on the server console and returns false
+-- Returns true on success
+function cStorage:DBExec(a_SQL, a_Callback, a_CallbackParam)
+ local ErrCode = self.DB:exec(a_SQL, a_Callback, a_CallbackParam);
+ if (ErrCode ~= sqlite3.OK) then
+ LOGWARNING(PluginPrefix .. "Error " .. ErrCode .. " (" .. self.DB:errmsg() ..
+ ") while processing SQL command >>" .. a_SQL .. "<<"
+ );
+ return false;
+ end
+ return true;
end
+