From 53497817a75cc827f17bea8c340e4a5e36e07ea3 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sat, 27 Feb 2021 21:45:21 +0100 Subject: [PATCH] Add database system, split plot mod into plot and plotmg --- .gitignore | 2 + mods/elidragon/init.lua | 14 +++++- mods/elidragon/mod.conf | 2 +- mods/elidragon_class/init.lua | 15 ++++++ mods/elidragon_creative/init.lua | 23 +++++----- mods/elidragon_creative/mod.conf | 2 +- mods/elidragon_db/init.lua | 46 +++++++++++++++++++ mods/elidragon_db/mod.conf | 4 ++ mods/elidragon_playerdb/init.lua | 26 +++++++++++ mods/elidragon_playerdb/mod.conf | 4 ++ mods/elidragon_plot/init.lua | 78 ++------------------------------ mods/elidragon_plot/mod.conf | 2 +- mods/elidragon_plotmg/init.lua | 76 +++++++++++++++++++++++++++++++ mods/elidragon_plotmg/mod.conf | 4 ++ mods/elidragon_skyblock/init.lua | 31 ++++++------- mods/elidragon_skyblock/mod.conf | 2 +- worlds/creative/world.mt | 4 ++ worlds/lobby/world.mt | 4 ++ worlds/skyblock/world.mt | 4 ++ worlds/survival/world.mt | 4 ++ 20 files changed, 240 insertions(+), 107 deletions(-) create mode 100644 mods/elidragon_class/init.lua create mode 100644 mods/elidragon_db/init.lua create mode 100644 mods/elidragon_db/mod.conf create mode 100644 mods/elidragon_playerdb/init.lua create mode 100644 mods/elidragon_playerdb/mod.conf create mode 100644 mods/elidragon_plotmg/init.lua create mode 100644 mods/elidragon_plotmg/mod.conf diff --git a/.gitignore b/.gitignore index 6beaa5b..debf00b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ cache/ storage/ +players/ +!players/.gitkeep diff --git a/mods/elidragon/init.lua b/mods/elidragon/init.lua index b3f68a1..3dd48db 100644 --- a/mods/elidragon/init.lua +++ b/mods/elidragon/init.lua @@ -1 +1,13 @@ -elidragon = {} +local proxy = { + WORLDS = {"lobby", "creative", "survival", "skyblock"}, +} + +elidragon = setmetatable({}, { + __index = function(t, k) + return proxy[k] + end, + __newindex = function(t, k, v) + assert(not proxy[k]) + proxy[k] = v + end, +}) diff --git a/mods/elidragon/mod.conf b/mods/elidragon/mod.conf index 18cda9c..a9ee149 100644 --- a/mods/elidragon/mod.conf +++ b/mods/elidragon/mod.conf @@ -1,3 +1,3 @@ name = elidragon author = Fleckenstein -description = Initalizer for the Elidragon v2 API +description = Safe API Initalizer for the Elidragon v2, makes sure that every module is only defined once. Also contains constants diff --git a/mods/elidragon_class/init.lua b/mods/elidragon_class/init.lua new file mode 100644 index 0000000..fac9209 --- /dev/null +++ b/mods/elidragon_class/init.lua @@ -0,0 +1,15 @@ +local function class(classdef) + local metatable = { + __index = classdef, + } + + return function (...) + local object = setmetatable({}, metatable) + if object.constructor then + object:constructor(...) + end + return object + end +end + +elidragon.class = class diff --git a/mods/elidragon_creative/init.lua b/mods/elidragon_creative/init.lua index 0e0f6c4..23d3f55 100644 --- a/mods/elidragon_creative/init.lua +++ b/mods/elidragon_creative/init.lua @@ -1,19 +1,18 @@ -local plot, schems = elidragon.plot, elidragon.schems +local plot, plotmg, schems = elidragon.plot, elidragon.plotmg, elidragon.schems plot.config = { gap = 50, road_width = 10, - mapgen = { - min_y = 9, - max_y = 9, - c_border = minetest.get_content_id("mcl_stairs:slab_stonebrick"), - road_schem = "elidragon_creative_road", - road_schem_offset = -3, - }, - claiming = { - enable_autoclaim_command = true, - max_plots = 5, - }, + enable_autoclaim_command = true, + max_plots_per_player = 5, +} + +plotmg.config = { + min_y = 9, + max_y = 9, + c_border = minetest.get_content_id("mcl_stairs:slab_stonebrick"), + road_schem = "elidragon_creative_road", + road_schem_offset = -3, } schems.load("elidragon_creative_road") diff --git a/mods/elidragon_creative/mod.conf b/mods/elidragon_creative/mod.conf index 6da30cf..cc423fd 100644 --- a/mods/elidragon_creative/mod.conf +++ b/mods/elidragon_creative/mod.conf @@ -1,4 +1,4 @@ name = elidragon_creative author = Fleckenstein description = Creative gamemode for Elidragon v2 -depends = elidragon, elidragon_plot, elidragon_schems, mcl_core +depends = elidragon, elidragon_plot, elidragon_plotmg, elidragon_schems, mcl_core diff --git a/mods/elidragon_db/init.lua b/mods/elidragon_db/init.lua new file mode 100644 index 0000000..f4f03e8 --- /dev/null +++ b/mods/elidragon_db/init.lua @@ -0,0 +1,46 @@ +local class = elidragon.class + +local db = {} + +local paths = {} +local worldpath = minetest.get_worldpath() + +function db:constructor(name, initial_data, dir) + paths[self] = dir or worldpath .. "/" .. name .. ".json" + self:load(initial_data or {}) +end + +function db:load(initial_data) + local file = io.open(paths[self], "r") + local data = file and minetest.parse_json(file:read()) or {} + if file then + file:close() + end + for k, v in pairs(data) do + self[k] = v + end + for k, v in pairs(initial_data) do + if not rawget(self, k) then + self[k] = v + end + end +end + +function db:save() + local file = assert(io.open(paths[self], "w")) + file:write(minetest.write_json(self)) + file:close() +end + +function db:close() + self:save() + paths[self] = nil +end + +minetest.register_on_shutdown(function() + for d in pairs(private) do + d:save() + end +end) + +elidragon.db = class(db) diff --git a/mods/elidragon_db/mod.conf b/mods/elidragon_db/mod.conf new file mode 100644 index 0000000..4065b30 --- /dev/null +++ b/mods/elidragon_db/mod.conf @@ -0,0 +1,4 @@ +name = elidragon_db +author = Fleckenstein +description = Easily migratable JSON databases for Elidragon v2, supports custom paths and safe usage of insecure environment +depends = elidragon, elidragon_class diff --git a/mods/elidragon_playerdb/init.lua b/mods/elidragon_playerdb/init.lua new file mode 100644 index 0000000..6f53276 --- /dev/null +++ b/mods/elidragon_playerdb/init.lua @@ -0,0 +1,26 @@ +local db = elidragon.db + +local playerdb = { + initial_data = {}, + players = {}, +} + +local players = playerdb.players +local env = assert(minetest.request_insecure_environment()) + +minetest.register_on_joinplayer(function (player) + local name = player:get_player_name() + if name ~= "rpc" then + players[name] = db(name, playerdb.initial_data, "players", env) + end +end) + +minetest.register_on_leaveplayer(function (player) + local name = player:get_player_name() + if name ~= "rpc" then + players[name]:close() + players[name] = nil + end +end) + +elidragon.playerdb = playerdb diff --git a/mods/elidragon_playerdb/mod.conf b/mods/elidragon_playerdb/mod.conf new file mode 100644 index 0000000..e746514 --- /dev/null +++ b/mods/elidragon_playerdb/mod.conf @@ -0,0 +1,4 @@ +name = elidragon_playerdb +author = Fleckenstein +description = Per-player modular storage shared between worlds for Elidragon v2, multiserver compatible +depends = elidragon, elidragon_db diff --git a/mods/elidragon_plot/init.lua b/mods/elidragon_plot/init.lua index f22d1e3..7942f2a 100644 --- a/mods/elidragon_plot/init.lua +++ b/mods/elidragon_plot/init.lua @@ -1,79 +1,9 @@ local plot = {} -minetest.register_on_generated(function(minp, maxp) - local config = assert(plot.config) - - local mgconfig = config.mapgen - - if not mgconfig then - return - end +local old_is_protected = minetest.is_protected - local min_y, max_y = mgconfig.min_y, mgconfig.max_y - - if maxp.y < min_y or minp.y > max_y then - return - end - - local vm, emin, emax = minetest.get_mapgen_object("voxelmanip") - local data = vm:get_data() - local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax}) - - local void_layer = mgconfig.void_layer - - if void_layer then - for idx in area:iter(minp.x, math.max(minp.y, void_layer.min_y), minp.z, maxp.x, math.min(maxp.y, void_layer.max_y), mapx.z) do - data[idx] = void_layer.c_void - end - end - - local function do_multiples(low, high, base, add, func) - for p = math.ceil(low / base), math.floor(high / base) do - func(p * base + add) - end - end - - local function do_borders(low, high, base, road, func) - do_multiples(low - road, high - road, base, road, func) - do_multiples(low + road, high + road, base, -road, func) - end - - min_y, max_y = math.max(minp.y, min_y), math.min(maxp.y, max_y) - - local gap, road_width = config.gap, config.road_width - local road_width_half = road_width / 2 - - do_borders(minp.x, maxp.x, gap, road_width_half, function(x) - do_multiples(minp.z - gap + road_width_half, maxp.z - road_width_half, gap, road_width_half, function(start_z) - for idx in area:iter(x, min_y, math.max(minp.z, start_z), x, max_y, math.min(maxp.z, start_z + gap - road_width)) do - data[idx] = mgconfig.c_border - end - end) - end) - - do_borders(minp.z, maxp.z, gap, road_width_half, function(z) - do_multiples(minp.x - gap + road_width_half, maxp.x - road_width_half, gap, road_width_half, function(start_x) - for idx in area:iter(math.max(minp.x, start_x), min_y, z, math.min(maxp.x, start_x + gap - road_width), max_y, z) do - data[idx] = mgconfig.c_border - end - end) - end) - - vm:set_data(data) - vm:calc_lighting() - vm:update_liquids() - vm:write_to_map() - - local road_schem = mgconfig.road_schem - - if road_schem and min_y == mgconfig.min_y then - do_multiples(minp.x, maxp.x, gap, 0, function(x) - do_multiples(minp.z, maxp.z, gap, 0, function(z) - elidragon.schems.add(vector.new(x + road_width_half, min_y + mgconfig.road_schem_offset, z - road_width_half + 1), road_schem) - elidragon.schems.add(vector.new(x - road_width_half + 1, min_y + mgconfig.road_schem_offset, z + road_width_half), road_schem .. "_flipped") - end) - end) - end -end) +function minetest.is_protected(pos, name) + return old_is_protected(pos, name) +end elidragon.plot = plot diff --git a/mods/elidragon_plot/mod.conf b/mods/elidragon_plot/mod.conf index 25a6f16..9a574b2 100644 --- a/mods/elidragon_plot/mod.conf +++ b/mods/elidragon_plot/mod.conf @@ -1,4 +1,4 @@ name = elidragon_plot author = Fleckenstein description = Flexible plot system for Elidragon v2 -depends = elidragon, mcl_mapgen_core +depends = elidragon diff --git a/mods/elidragon_plotmg/init.lua b/mods/elidragon_plotmg/init.lua new file mode 100644 index 0000000..eccf652 --- /dev/null +++ b/mods/elidragon_plotmg/init.lua @@ -0,0 +1,76 @@ +local plot, schems = elidragon.plot, elidragon.schems + +local plotmg = {} + +minetest.register_on_generated(function(minp, maxp) + local config = assert(plot.config) + local mgconfig = assert(plotmg.config) + + local min_y, max_y = mgconfig.min_y, mgconfig.max_y + + if maxp.y < min_y or minp.y > max_y then + return + end + + local vm, emin, emax = minetest.get_mapgen_object("voxelmanip") + local data = vm:get_data() + local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax}) + + local void_layer = mgconfig.void_layer + + if void_layer then + for idx in area:iter(minp.x, math.max(minp.y, void_layer.min_y), minp.z, maxp.x, math.min(maxp.y, void_layer.max_y), mapx.z) do + data[idx] = void_layer.c_void + end + end + + local function do_multiples(low, high, base, add, func) + for p = math.ceil(low / base), math.floor(high / base) do + func(p * base + add) + end + end + + local function do_borders(low, high, base, road, func) + do_multiples(low - road, high - road, base, road, func) + do_multiples(low + road, high + road, base, -road, func) + end + + min_y, max_y = math.max(minp.y, min_y), math.min(maxp.y, max_y) + + local gap, road_width = config.gap, config.road_width + local road_width_half = road_width / 2 + + do_borders(minp.x, maxp.x, gap, road_width_half, function(x) + do_multiples(minp.z - gap + road_width_half, maxp.z - road_width_half, gap, road_width_half, function(start_z) + for idx in area:iter(x, min_y, math.max(minp.z, start_z), x, max_y, math.min(maxp.z, start_z + gap - road_width)) do + data[idx] = mgconfig.c_border + end + end) + end) + + do_borders(minp.z, maxp.z, gap, road_width_half, function(z) + do_multiples(minp.x - gap + road_width_half, maxp.x - road_width_half, gap, road_width_half, function(start_x) + for idx in area:iter(math.max(minp.x, start_x), min_y, z, math.min(maxp.x, start_x + gap - road_width), max_y, z) do + data[idx] = mgconfig.c_border + end + end) + end) + + vm:set_data(data) + vm:calc_lighting() + vm:update_liquids() + vm:write_to_map() + + local road_schem = mgconfig.road_schem + + if road_schem and min_y == mgconfig.min_y then + do_multiples(minp.x, maxp.x, gap, 0, function(x) + do_multiples(minp.z, maxp.z, gap, 0, function(z) + schems.add(vector.new(x + road_width_half, min_y + mgconfig.road_schem_offset, z - road_width_half + 1), road_schem) + schems.add(vector.new(x - road_width_half + 1, min_y + mgconfig.road_schem_offset, z + road_width_half), road_schem .. "_flipped") + end) + end) + end +end) + +elidragon.plotmg = plotmg diff --git a/mods/elidragon_plotmg/mod.conf b/mods/elidragon_plotmg/mod.conf new file mode 100644 index 0000000..b1a066c --- /dev/null +++ b/mods/elidragon_plotmg/mod.conf @@ -0,0 +1,4 @@ +name = elidragon_plotmg +author = Fleckenstein +description = Plot map generation for Elidragon v2 +depends = elidragon, elidragon_plot, elidragon_schems, mcl_mapgen_core diff --git a/mods/elidragon_skyblock/init.lua b/mods/elidragon_skyblock/init.lua index 4940ad3..c3ef330 100644 --- a/mods/elidragon_skyblock/init.lua +++ b/mods/elidragon_skyblock/init.lua @@ -1,25 +1,24 @@ -local plot = elidragon.plot +local plot, plotmg = elidragon.plot, elidragon.plotmg plot.config = { gap = 1000, road_width = 100, - --[[min_y = 2000, - max_y = 31000,]]-- - mapgen = { + min_y = 2000, + max_y = 31000, + auto_allocation = true, + on_claim = function() -- create island and move there + end +} + +plotmg.config = { + min_y = 1000, + max_y = 31000, + void_layer = { min_y = 1000, - max_y = 31000, - void_layer = { - min_y = 1000, - max_y = 2000, - c_void = minetest.get_content_id("mcl_core:void"), - }, - c_border = minetest.get_content_id("mcl_core:barrier"), -- ToDo: make world border + max_y = 2000, + c_void = minetest.get_content_id("mcl_core:void"), }, - --[[claiming = { - auto_allocation = true, - on_claim = function() -- create island and move there - end - },]]-- + c_border = minetest.get_content_id("mcl_core:barrier"), -- ToDo: make world border } elidragon.skyblock = {} diff --git a/mods/elidragon_skyblock/mod.conf b/mods/elidragon_skyblock/mod.conf index 35ae635..99d07d8 100644 --- a/mods/elidragon_skyblock/mod.conf +++ b/mods/elidragon_skyblock/mod.conf @@ -1,4 +1,4 @@ name = elidragon_skyblock author = Fleckenstein description = Skyblock gamemode for Elidragon v2 -depends = elidragon, elidragon_plot, mcl_core +depends = elidragon, elidragon_plot, elidragon_plotmg, mcl_core diff --git a/worlds/creative/world.mt b/worlds/creative/world.mt index 31be860..d1c4aa3 100644 --- a/worlds/creative/world.mt +++ b/worlds/creative/world.mt @@ -6,10 +6,14 @@ auth_backend = sqlite3 player_backend = sqlite3 load_mod_default = false load_mod_elidragon = true +load_mod_elidragon_class = true load_mod_elidragon_creative = true +load_mod_elidragon_db = true load_mod_elidragon_grouplist = false load_mod_elidragon_luckyblock = false +load_mod_elidragon_playerdb = true load_mod_elidragon_plot = true +load_mod_elidragon_plotmg = true load_mod_elidragon_random = false load_mod_elidragon_request = true load_mod_elidragon_schems = true diff --git a/worlds/lobby/world.mt b/worlds/lobby/world.mt index 370bb10..4375d0b 100644 --- a/worlds/lobby/world.mt +++ b/worlds/lobby/world.mt @@ -6,10 +6,14 @@ auth_backend = sqlite3 player_backend = sqlite3 load_mod_default = false load_mod_elidragon = true +load_mod_elidragon_class = true load_mod_elidragon_creative = false +load_mod_elidragon_db = false load_mod_elidragon_grouplist = false load_mod_elidragon_luckyblock = false +load_mod_elidragon_playerdb = true load_mod_elidragon_plot = false +load_mod_elidragon_plotmg = false load_mod_elidragon_random = false load_mod_elidragon_request = false load_mod_elidragon_schems = false diff --git a/worlds/skyblock/world.mt b/worlds/skyblock/world.mt index 9c131d8..cfd77d7 100644 --- a/worlds/skyblock/world.mt +++ b/worlds/skyblock/world.mt @@ -6,10 +6,14 @@ auth_backend = sqlite3 player_backend = sqlite3 load_mod_default = true load_mod_elidragon = true +load_mod_elidragon_class = true load_mod_elidragon_creative = false +load_mod_elidragon_db = true load_mod_elidragon_grouplist = true load_mod_elidragon_luckyblock = true +load_mod_elidragon_playerdb = true load_mod_elidragon_plot = true +load_mod_elidragon_plotmg = true load_mod_elidragon_random = false load_mod_elidragon_request = true load_mod_elidragon_schems = false diff --git a/worlds/survival/world.mt b/worlds/survival/world.mt index b3ac373..9de9a2b 100644 --- a/worlds/survival/world.mt +++ b/worlds/survival/world.mt @@ -6,10 +6,14 @@ auth_backend = sqlite3 player_backend = sqlite3 load_mod_default = false load_mod_elidragon = true +load_mod_elidragon_class = true load_mod_elidragon_creative = false +load_mod_elidragon_db = true load_mod_elidragon_grouplist = false load_mod_elidragon_luckyblock = false +load_mod_elidragon_playerdb = true load_mod_elidragon_plot = false +load_mod_elidragon_plotmg = false load_mod_elidragon_random = false load_mod_elidragon_request = true load_mod_elidragon_schems = false -- 2.44.0