|
|
@ -1,45 +1,45 @@ |
|
|
|
-- switch_creative mod by h3ndrik |
|
|
|
-- creative_regions mod by h3ndrik |
|
|
|
|
|
|
|
switch_creative = {} |
|
|
|
creative_regions = {} |
|
|
|
|
|
|
|
local mod_storage = minetest.get_mod_storage() |
|
|
|
switch_creative.astore = AreaStore() |
|
|
|
creative_regions.astore = AreaStore() |
|
|
|
|
|
|
|
switch_creative.area_privs = {} |
|
|
|
creative_regions.area_privs = {} |
|
|
|
|
|
|
|
areas_owner_privs = minetest.settings:get("switch_creative.owner_privs") or "+creative, +fast, +fly" |
|
|
|
areas_guest_privs = minetest.settings:get("switch_creative.guest_privs") or "-fly, -fast" |
|
|
|
noarea_privs = minetest.settings:get("switch_creative.noarea_privs") or "-creative, +fast, -fly" |
|
|
|
default_privs = minetest.settings:get("switch_creative.default_privs") or "+creative, +fast, +fly" |
|
|
|
areas_owner_privs = minetest.settings:get("creative_regions.owner_privs") or "+creative, +fast, +fly" |
|
|
|
areas_guest_privs = minetest.settings:get("creative_regions.guest_privs") or "-fly, -fast" |
|
|
|
noarea_privs = minetest.settings:get("creative_regions.noarea_privs") or "-creative, +fast, -fly" |
|
|
|
default_privs = minetest.settings:get("creative_regions.default_privs") or "+creative, +fast, +fly" |
|
|
|
|
|
|
|
function switch_creative:load_areas() |
|
|
|
self.astore:from_file(minetest.get_worldpath().."/switch_creative_areas") |
|
|
|
switch_creative.area_privs = minetest.deserialize(mod_storage:get_string("astore_privs")) or {} |
|
|
|
function creative_regions:load_areas() |
|
|
|
self.astore:from_file(minetest.get_worldpath().."/creative_regions_areas") |
|
|
|
creative_regions.area_privs = minetest.deserialize(mod_storage:get_string("astore_privs")) or {} |
|
|
|
-- local edge1 = { x=-10, y=-10, z=-10 } |
|
|
|
-- local edge2 = { x=10, y=10, z=10 } |
|
|
|
-- local data = "Testarea" |
|
|
|
-- local astore_id = self.astore:insert_area(edge1, edge2, tostring(data)) |
|
|
|
-- |
|
|
|
-- switch_creative:set_area_privs(astore_id, "+creative, +fast, +fly") |
|
|
|
-- creative_regions:set_area_privs(astore_id, "+creative, +fast, +fly") |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:save_areas() |
|
|
|
self.astore:to_file(minetest.get_worldpath().."/switch_creative_areas") |
|
|
|
local datastr = minetest.serialize(switch_creative.area_privs) |
|
|
|
function creative_regions:save_areas() |
|
|
|
self.astore:to_file(minetest.get_worldpath().."/creative_regions_areas") |
|
|
|
local datastr = minetest.serialize(creative_regions.area_privs) |
|
|
|
if not datastr then |
|
|
|
minetest.log("error", "[switch_creative] Failed to serialize area_privs data!") |
|
|
|
minetest.log("error", "[creative_regions] Failed to serialize area_privs data!") |
|
|
|
return |
|
|
|
end |
|
|
|
mod_storage:set_string("astore_privs", datastr) |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:set_area_privs(id, privs_string) |
|
|
|
switch_creative.area_privs[tostring(id)] = privs_string |
|
|
|
function creative_regions:set_area_privs(id, privs_string) |
|
|
|
creative_regions.area_privs[tostring(id)] = privs_string |
|
|
|
end |
|
|
|
|
|
|
|
minetest.register_chatcommand("creative_area", { |
|
|
|
params = "<x> <y> <z>, <x> <y> <z>, <text>", |
|
|
|
description = "Set a switch_creative area from a to b", |
|
|
|
description = "Set a creative_regions area from a to b", |
|
|
|
privs = {server = true}, |
|
|
|
func = function(name, param) |
|
|
|
local player = minetest.get_player_by_name(name) |
|
|
@ -54,11 +54,11 @@ minetest.register_chatcommand("creative_area", { |
|
|
|
local edge1 = { x=tonumber(x1), y=tonumber(y1), z=tonumber(z1) } |
|
|
|
local edge2 = { x=tonumber(x2), y=tonumber(y2), z=tonumber(z2) } |
|
|
|
local data = area_name |
|
|
|
local astore_id = switch_creative.astore:insert_area(edge1, edge2, tostring(data)) |
|
|
|
local astore_id = creative_regions.astore:insert_area(edge1, edge2, tostring(data)) |
|
|
|
if astore_id then |
|
|
|
switch_creative:set_area_privs(tostring(astore_id), nil) |
|
|
|
creative_regions:set_area_privs(tostring(astore_id), nil) |
|
|
|
minetest.chat_send_player(name, "[creative_area] New area set. ID: " .. astore_id) |
|
|
|
switch_creative:save_areas() |
|
|
|
creative_regions:save_areas() |
|
|
|
return true, "Done" |
|
|
|
else |
|
|
|
return false, "Failed" |
|
|
@ -68,7 +68,7 @@ minetest.register_chatcommand("creative_area", { |
|
|
|
|
|
|
|
minetest.register_chatcommand("creative_area_privs", { |
|
|
|
params = "<id>, <text>", |
|
|
|
description = "Set privstring for switch_creative area", |
|
|
|
description = "Set privstring for creative_regions area", |
|
|
|
privs = {server = true}, |
|
|
|
func = function(name, param) |
|
|
|
local player = minetest.get_player_by_name(name) |
|
|
@ -80,9 +80,9 @@ minetest.register_chatcommand("creative_area_privs", { |
|
|
|
return false, "Syntax error" |
|
|
|
end |
|
|
|
if privstring and area_id then |
|
|
|
switch_creative:set_area_privs(tostring(area_id), privstring) |
|
|
|
creative_regions:set_area_privs(tostring(area_id), privstring) |
|
|
|
minetest.chat_send_player(name, "[creative_area] Privstring set for area " .. area_id .. ": " .. privstring) |
|
|
|
switch_creative:save_areas() |
|
|
|
creative_regions:save_areas() |
|
|
|
return true, "Done" |
|
|
|
else |
|
|
|
return false, "Failed" |
|
|
@ -92,7 +92,7 @@ minetest.register_chatcommand("creative_area_privs", { |
|
|
|
|
|
|
|
minetest.register_chatcommand("creative_area_rm", { |
|
|
|
params = "<id>", |
|
|
|
description = "Remove a switch_creative area", |
|
|
|
description = "Remove a creative_regions area", |
|
|
|
privs = {server = true}, |
|
|
|
func = function(name, param) |
|
|
|
local player = minetest.get_player_by_name(name) |
|
|
@ -103,19 +103,19 @@ minetest.register_chatcommand("creative_area_rm", { |
|
|
|
if not found then |
|
|
|
return false, "Syntax error" |
|
|
|
end |
|
|
|
local success = switch_creative.astore:remove_area(tonumber(area_id)) |
|
|
|
local success = creative_regions.astore:remove_area(tonumber(area_id)) |
|
|
|
if success then |
|
|
|
minetest.chat_send_player(name, "[creative_area] Area " .. area_id .. "removed") |
|
|
|
switch_creative:set_area_privs(tostring(area_id), nil) |
|
|
|
creative_regions:set_area_privs(tostring(area_id), nil) |
|
|
|
else |
|
|
|
minetest.chat_send_player(name, "[creative_area] Error removing area " .. area_id) |
|
|
|
end |
|
|
|
switch_creative:save_areas() |
|
|
|
creative_regions:save_areas() |
|
|
|
return true, "Done." |
|
|
|
end, |
|
|
|
}) |
|
|
|
|
|
|
|
function switch_creative:decode_privs_string(str) |
|
|
|
function creative_regions:decode_privs_string(str) |
|
|
|
-- minetest/builtin/common/misc_helpers.lua:core.string_to_privs() |
|
|
|
assert(type(str) == "string") |
|
|
|
local delim = ',' |
|
|
@ -136,14 +136,18 @@ function switch_creative:decode_privs_string(str) |
|
|
|
return privs_grant, privs_revoke |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:is_areas_mod(area_id) |
|
|
|
function creative_regions:add_privs_from_string(table, str) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
function creative_regions:is_areas_mod(area_id) |
|
|
|
area_id = tostring(area_id) |
|
|
|
local is_areas_id = string.sub(id, 1, 2) == "a" |
|
|
|
local a_id = tonumber( string.sub(id, 2) ) |
|
|
|
return is_areas_id, a_id |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:get_area_size(astore_area) |
|
|
|
function creative_regions:get_area_size(astore_area) |
|
|
|
local pos1 = astore_area.min |
|
|
|
local pos2 = astore_area.max |
|
|
|
local x = math.abs( pos2.x - pos1.x ) |
|
|
@ -151,7 +155,7 @@ function switch_creative:get_area_size(astore_area) |
|
|
|
return x*z |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:get_areas_area_size(areas_area) |
|
|
|
function creative_regions:get_areas_area_size(areas_area) |
|
|
|
local pos1 = areas_area.pos1 |
|
|
|
local pos2 = areas_area.pos2 |
|
|
|
local x = math.abs( pos2.x - pos1.x ) |
|
|
@ -159,7 +163,7 @@ function switch_creative:get_areas_area_size(areas_area) |
|
|
|
return x*z |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:initial_stuff() |
|
|
|
function creative_regions:initial_stuff() |
|
|
|
local stuff_string = minetest.settings:get("initial_stuff") or |
|
|
|
"default:pick_steel,default:axe_steel,default:shovel_steel," .. |
|
|
|
"default:torch 99,default:cobble 99" |
|
|
@ -171,24 +175,24 @@ function switch_creative:initial_stuff() |
|
|
|
return itemtable |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:save_player_privs(player) |
|
|
|
function creative_regions:save_player_privs(player) |
|
|
|
local name = player:get_player_name() |
|
|
|
local pmeta = player:get_meta() |
|
|
|
local privs = minetest.get_player_privs(name) |
|
|
|
pmeta:set_string("switch_creative.saved_privs", minetest.serialize(privs)) |
|
|
|
pmeta:set_string("creative_regions.saved_privs", minetest.serialize(privs)) |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:restore_player_privs(player) |
|
|
|
function creative_regions:restore_player_privs(player) |
|
|
|
local name = player:get_player_name() |
|
|
|
local pmeta = player:get_meta() |
|
|
|
local privs = minetest.deserialize(pmeta:get_string("switch_creative.saved_privs")) |
|
|
|
local privs = minetest.deserialize(pmeta:get_string("creative_regions.saved_privs")) |
|
|
|
if privs then |
|
|
|
minetest.set_player_privs(name, privs) |
|
|
|
pmeta:set_string("switch_creative.saved_privs", nil) |
|
|
|
pmeta:set_string("creative_regions.saved_privs", nil) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:save_player_inventory(player, listname) |
|
|
|
function creative_regions:save_player_inventory(player, listname) |
|
|
|
local name = player:get_player_name() |
|
|
|
local pmeta = player:get_meta() |
|
|
|
local inv = player:get_inventory() |
|
|
@ -196,7 +200,7 @@ function switch_creative:save_player_inventory(player, listname) |
|
|
|
inv:set_list(listname, player_items) |
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:restore_player_inventory(player, listname) |
|
|
|
function creative_regions:restore_player_inventory(player, listname) |
|
|
|
local name = player:get_player_name() |
|
|
|
local pmeta = player:get_meta() |
|
|
|
local inv = player:get_inventory() |
|
|
@ -208,7 +212,7 @@ function switch_creative:restore_player_inventory(player, listname) |
|
|
|
end |
|
|
|
inv:set_list("main", player_items) |
|
|
|
if give_initial then |
|
|
|
local initial_items = switch_creative:initial_stuff() |
|
|
|
local initial_items = creative_regions:initial_stuff() |
|
|
|
for _, stack in ipairs(initial_items) do |
|
|
|
inv:add_item("main", stack) |
|
|
|
end |
|
|
@ -216,23 +220,23 @@ function switch_creative:restore_player_inventory(player, listname) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:player_enter_area(player, privstring) |
|
|
|
function creative_regions:player_enter_area(player, privstring) |
|
|
|
local name = player:get_player_name() |
|
|
|
local has_creative = minetest.check_player_privs(name, {creative=true}) |
|
|
|
|
|
|
|
local privs_grant, privs_revoke = switch_creative:decode_privs_string(privstring) |
|
|
|
local privs_grant, privs_revoke = creative_regions:decode_privs_string(privstring) |
|
|
|
|
|
|
|
if not has_creative and privs_grant.creative and not privs_revoke.creative then |
|
|
|
minetest.chat_send_player(name, "You entered creative mode") |
|
|
|
switch_creative:save_player_inventory(player, "saved_survival") |
|
|
|
switch_creative:restore_player_inventory(player, "saved_creative") |
|
|
|
creative_regions:save_player_inventory(player, "saved_survival") |
|
|
|
creative_regions:restore_player_inventory(player, "saved_creative") |
|
|
|
if minetest.get_modpath("unified_inventory") then |
|
|
|
unified_inventory.set_inventory_formspec(player, unified_inventory.default) |
|
|
|
end |
|
|
|
elseif has_creative and privs_revoke.creative then |
|
|
|
minetest.chat_send_player(name, "You entered survival mode") |
|
|
|
switch_creative:save_player_inventory(player, "saved_creative") |
|
|
|
switch_creative:restore_player_inventory(player, "saved_survival") |
|
|
|
creative_regions:save_player_inventory(player, "saved_creative") |
|
|
|
creative_regions:restore_player_inventory(player, "saved_survival") |
|
|
|
end |
|
|
|
|
|
|
|
local has_privs = minetest.get_player_privs(name) |
|
|
@ -247,13 +251,13 @@ function switch_creative:player_enter_area(player, privstring) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
function switch_creative:update_player(player) |
|
|
|
function creative_regions:update_player(player) |
|
|
|
local name = player:get_player_name() |
|
|
|
local pos = vector.round(player:get_pos()) |
|
|
|
|
|
|
|
local pmeta = player:get_meta() |
|
|
|
-- pmeta:mark_as_private("switch_creative.active_areas") |
|
|
|
local player_last_active_areas = pmeta:get_string("switch_creative.active_areas") or "-1" |
|
|
|
-- pmeta:mark_as_private("creative_regions.active_areas") |
|
|
|
local player_last_active_areas = pmeta:get_string("creative_regions.active_areas") or "-1" |
|
|
|
local player_active_areas = "-1" |
|
|
|
local areas_owner = false |
|
|
|
local areas_guest = false |
|
|
@ -270,7 +274,7 @@ function switch_creative:update_player(player) |
|
|
|
-- enter area |
|
|
|
local current_areas = self.astore:get_areas_for_pos(pos, true, true) |
|
|
|
for astore_id, astore_area in pairs( current_areas ) do |
|
|
|
local areasize = switch_creative:get_area_size(astore_area) |
|
|
|
local areasize = creative_regions:get_area_size(astore_area) |
|
|
|
if not smallest_area.size or areasize < smallest_area.size then |
|
|
|
smallest_area.id = tostring(astore_id) |
|
|
|
smallest_area.size = areasize |
|
|
@ -285,7 +289,7 @@ function switch_creative:update_player(player) |
|
|
|
if minetest.get_modpath("areas") then |
|
|
|
a_current_areas = areas:getAreasAtPos(pos) |
|
|
|
for a_id, a_area in pairs( a_current_areas ) do |
|
|
|
local areasize = switch_creative:get_areas_area_size(a_area) |
|
|
|
local areasize = creative_regions:get_areas_area_size(a_area) |
|
|
|
if not smallest_areas_area.size or areasize < smallest_areas_area.size then |
|
|
|
smallest_areas_area.id = tostring(a_id) |
|
|
|
smallest_areas_area.size = areasize |
|
|
@ -306,8 +310,8 @@ function switch_creative:update_player(player) |
|
|
|
|
|
|
|
local privstring = noarea_privs |
|
|
|
if smallest_area.id then |
|
|
|
if switch_creative.area_privs[tostring(smallest_area.id)] then |
|
|
|
privstring = switch_creative.area_privs[tostring(smallest_area.id)] |
|
|
|
if creative_regions.area_privs[tostring(smallest_area.id)] then |
|
|
|
privstring = creative_regions.area_privs[tostring(smallest_area.id)] |
|
|
|
else |
|
|
|
privstring = default_privs |
|
|
|
end |
|
|
@ -319,34 +323,34 @@ function switch_creative:update_player(player) |
|
|
|
for _, a in pairs(string.split(player_active_areas, ",")) do |
|
|
|
a = a:trim() |
|
|
|
if string.sub(a, 1, 1) == "a" then |
|
|
|
if switch_creative.area_privs[a] then |
|
|
|
privstring = privstring .. ", " .. switch_creative.area_privs[a] |
|
|
|
if creative_regions.area_privs[a] then |
|
|
|
privstring = privstring .. ", " .. creative_regions.area_privs[a] |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
if smallest_areas_area.size and smallest_area.size then |
|
|
|
if smallest_area.size <= smallest_areas_area.size then |
|
|
|
privstring = privstring .. ", " .. switch_creative.area_privs[tostring(smallest_area.id)] |
|
|
|
privstring = privstring .. ", " .. creative_regions.area_privs[tostring(smallest_area.id)] |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
if player_active_areas ~= player_last_active_areas then |
|
|
|
switch_creative:player_enter_area(player, privstring) |
|
|
|
pmeta:set_string("switch_creative.active_areas", player_active_areas) |
|
|
|
creative_regions:player_enter_area(player, privstring) |
|
|
|
pmeta:set_string("creative_regions.active_areas", player_active_areas) |
|
|
|
end |
|
|
|
|
|
|
|
-- print(dump(pmeta:to_table())) |
|
|
|
end |
|
|
|
|
|
|
|
switch_creative:load_areas() |
|
|
|
creative_regions:load_areas() |
|
|
|
|
|
|
|
local timer = 0 |
|
|
|
minetest.register_globalstep(function(dtime) |
|
|
|
timer = timer + dtime |
|
|
|
if timer >= 1 then |
|
|
|
for _, player in pairs(minetest.get_connected_players()) do |
|
|
|
switch_creative:update_player(player) |
|
|
|
creative_regions:update_player(player) |
|
|
|
end |
|
|
|
end |
|
|
|
end) |
|
|
@ -354,7 +358,7 @@ end) |
|
|
|
-- areas mod hud |
|
|
|
if minetest.get_modpath("areas") then |
|
|
|
local function areas_hud_handler(pos, areas) |
|
|
|
local current_areas = switch_creative.astore:get_areas_for_pos(pos, false, true) |
|
|
|
local current_areas = creative_regions.astore:get_areas_for_pos(pos, false, true) |
|
|
|
for astore_id, astore_area in pairs( current_areas ) do |
|
|
|
if astore_id then |
|
|
|
table.insert(areas, { |
|
|
|