summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2013-06-06 19:10:45 +0200
committermadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2013-06-06 19:10:45 +0200
commit78462062f345e4e46b37ea6ee208cea3b2688f34 (patch)
tree1edc1c9c91438c94f97deeeb9f4aa45345b0fb21
parentAdded LOGWARNING() function to Lua API, as a synonym to LOGWARN() (diff)
downloadcuberite-78462062f345e4e46b37ea6ee208cea3b2688f34.tar
cuberite-78462062f345e4e46b37ea6ee208cea3b2688f34.tar.gz
cuberite-78462062f345e4e46b37ea6ee208cea3b2688f34.tar.bz2
cuberite-78462062f345e4e46b37ea6ee208cea3b2688f34.tar.lz
cuberite-78462062f345e4e46b37ea6ee208cea3b2688f34.tar.xz
cuberite-78462062f345e4e46b37ea6ee208cea3b2688f34.tar.zst
cuberite-78462062f345e4e46b37ea6ee208cea3b2688f34.zip
-rw-r--r--MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua14
-rw-r--r--MCServer/Plugins/ProtectionAreas/Storage.lua105
2 files changed, 118 insertions, 1 deletions
diff --git a/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua b/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua
index d816f578f..603b7a87f 100644
--- a/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua
+++ b/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua
@@ -6,11 +6,23 @@
+--- Prefix for all messages logged to the server console
+PluginPrefix = "ProtectionAreas: ";
+
+
+
+
+
+--- Called by MCS when the plugin loads
+-- Returns true if initialization successful, false otherwise
function Initialize(a_Plugin)
a_Plugin:SetName("ProtectionAreas");
a_Plugin:SetVersion(1);
- InitializeStorage();
+ if (not(InitializeStorage())) then
+ LOGWARNING(PluginPrefix .. "failed to initialize Storage, plugin is disabled");
+ return false;
+ end
InitializeHooks(a_Plugin);
InitializeCommandHandlers();
diff --git a/MCServer/Plugins/ProtectionAreas/Storage.lua b/MCServer/Plugins/ProtectionAreas/Storage.lua
index 8aebae0c7..21c147300 100644
--- a/MCServer/Plugins/ProtectionAreas/Storage.lua
+++ b/MCServer/Plugins/ProtectionAreas/Storage.lua
@@ -23,8 +23,14 @@ g_Storage = {};
--- Initializes the storage subsystem, creates the g_Storage object
+-- Returns true if successful, false if not
function InitializeStorage()
g_Storage = cStorage:new();
+ if (not(g_Storage:OpenDB())) then
+ return false;
+ end
+
+ return true;
end
@@ -54,3 +60,102 @@ end
+
+--- Opens the DB and makes sure it has all the columns needed
+-- Returns true if successful, false otherwise
+function cStorage:OpenDB()
+ local ErrCode, ErrMsg;
+ self.DB, ErrCode, ErrMsg = sqlite3.open("ProtectionAreas.sqlite");
+ if (self.DB == nil) then
+ LOGWARNING(PluginPrefix .. "Cannot open ProtectionAreas.sqlite, error " .. ErrCode .. " (" .. ErrMsg ..")");
+ return false;
+ end
+
+ if (
+ not(self:CreateTable("Area", {"ID INTEGER PRIMARY KEY AUTOINCREMENT", "MinX", "MaxX", "MinZ", "MaxZ", "CreatorUserName"})) or
+ not(self:CreateTable("AllowedUsers", {"AreaID", "UserName"}))
+ ) then
+ LOGWARNING(PluginPrefix .. "Cannot create DB tables!");
+ return false;
+ end
+
+ return true;
+end
+
+
+
+
+
+--- Creates the table of the specified name and columns[]
+-- If the table exists, any columns missing are added; existing data is kept
+function cStorage:CreateTable(a_TableName, a_Columns)
+
+ local sql = "CREATE TABLE IF NOT EXISTS '" .. a_TableName .. "' (";
+ sql = sql .. table.concat(a_Columns, ", ");
+ sql = sql .. ")";
+ local ErrCode = self.DB:exec(sql);
+ if (ErrCode ~= sqlite3.OK) then
+ LOGWARNING(PluginPrefix .. "Cannot create DB Table, error " .. ErrCode .. " (" .. self.DB:errmsg() .. ")");
+ return false;
+ end
+
+ -- Check each column whether it exists
+ -- Remove all the existing columns from a_Columns:
+ local RemoveExistingColumn = function(UserData, NumCols, Values, Names)
+ -- Remove the received column from a_Columns. Search for column name in the Names[] / Values[] pairs
+ for i = 1, NumCols do
+ if (Names[i] == "name") then
+ local ColumnName = Values[i]:lower();
+ -- Search the a_Columns if they have that column:
+ for j = 1, #a_Columns do
+ -- Cut away all column specifiers (after the first space), if any:
+ local SpaceIdx = string.find(a_Columns[j], " ");
+ if (SpaceIdx ~= nil) then
+ SpaceIdx = SpaceIdx - 1;
+ end
+ local ColumnTemplate = string.lower(string.sub(a_Columns[j], 1, SpaceIdx));
+ -- If it is a match, remove from a_Columns:
+ if (ColumnTemplate == ColumnName) then
+ table.remove(a_Columns, j);
+ break; -- for j
+ end
+ end -- for j - a_Columns[]
+ end
+ end -- for i - Names[] / Values[]
+ return 0;
+ end
+ local ErrCode = self.DB:exec("PRAGMA table_info(" .. a_TableName .. ")", RemoveExistingColumn);
+ if (ErrCode ~= sqlite3.OK) then
+ LOGWARNING(PluginPrefix .. "Cannot query DB table structure, error " .. ErrCode .. " (" .. self.DB:errmsg() ..")");
+ return false;
+ end
+
+ -- Create the missing columns
+ -- a_Columns now contains only those columns that are missing in the DB
+ if (#a_Columns > 0) then
+ LOGINFO(PluginPrefix .. "Database table \"" .. a_TableName .. "\" is missing " .. #a_Columns .. " columns, fixing now.");
+ for idx, ColumnName in ipairs(a_Columns) do
+ local ErrCode = self.DB:exec("ALTER TABLE '" .. a_TableName .. "' ADD COLUMN " .. ColumnName);
+ if (ErrCode ~= sqlite3.OK) then
+ LOGWARNING(PluginPrefix .. "Cannot add DB table \"" .. a_TableName .. "\" column \"" .. ColumnName .. "\", error " .. ErrCode .. " (" .. self.DB:errmsg() ..")");
+ return false;
+ end
+ end
+ LOGINFO(PluginPrefix .. "Database table \"" .. a_TableName .. "\" columns fixed.");
+ end
+
+ return true;
+end
+
+
+
+
+
+
+function cStorage:AddArea(a_Cuboid, a_Creator, a_AllowedPlayers)
+ -- TODO
+end
+
+
+
+