Browse Source

refactor: rename mod

main
Hendrik Langer 5 years ago
parent
commit
ea0376d0e3
  1. 84
      init.lua
  2. 2
      mod.conf
  3. 6
      settingtypes.txt

84
init.lua

@ -1,40 +1,40 @@
-- toggle_areas mod by h3ndrik -- switch_creative mod by h3ndrik
toggle_areas = {} switch_creative = {}
local mod_storage = minetest.get_mod_storage() local mod_storage = minetest.get_mod_storage()
toggle_areas.astore = AreaStore() switch_creative.astore = AreaStore()
toggle_areas.area_privs = {} switch_creative.area_privs = {}
areas_owner_privs = minetest.settings:get("toggle_areas.owner_privs") or "+creative, +fast, +fly" areas_owner_privs = minetest.settings:get("switch_creative.owner_privs") or "+creative, +fast, +fly"
areas_guest_privs = minetest.settings:get("toggle_areas.guest_privs") or "-fly, -fast" areas_guest_privs = minetest.settings:get("switch_creative.guest_privs") or "-fly, -fast"
default_privs = minetest.settings:get("toggle_areas.default_privs") or "-creative, +fast, -fly" default_privs = minetest.settings:get("switch_creative.default_privs") or "-creative, +fast, -fly"
function toggle_areas:load_areas() function switch_creative:load_areas()
local test = minetest.deserialize(mod_storage:get_string("astore")) local test = minetest.deserialize(mod_storage:get_string("astore"))
local edge1 = { x=-10, y=-10, z=-10 } local edge1 = { x=-10, y=-10, z=-10 }
local edge2 = { x=10, y=10, z=10 } local edge2 = { x=10, y=10, z=10 }
local data = "Testarea" local data = "Testarea"
local astore_id = self.astore:insert_area(edge1, edge2, tostring(data)) local astore_id = self.astore:insert_area(edge1, edge2, tostring(data))
toggle_areas:set_area_privs(astore_id, "+creative, +fast, +fly") switch_creative:set_area_privs(astore_id, "+creative, +fast, +fly")
end end
function toggle_areas:save_areas() function switch_creative:save_areas()
local datastr = minetest.serialize(self.astore) local datastr = minetest.serialize(self.astore)
if not datastr then if not datastr then
minetest.log("error", "[toggle_areas] Failed to serialize area data!") minetest.log("error", "[switch_creative] Failed to serialize area data!")
return return
end end
mod_storage:set_string("astore", datastr) mod_storage:set_string("astore", datastr)
end end
function toggle_areas:set_area_privs(id, privs_string) function switch_creative:set_area_privs(id, privs_string)
toggle_areas.area_privs[id] = privs_string switch_creative.area_privs[id] = privs_string
end end
function toggle_areas:decode_privs_string(str) function switch_creative:decode_privs_string(str)
-- minetest/builtin/common/misc_helpers.lua:core.string_to_privs() -- minetest/builtin/common/misc_helpers.lua:core.string_to_privs()
assert(type(str) == "string") assert(type(str) == "string")
local delim = ',' local delim = ','
@ -55,13 +55,13 @@ function toggle_areas:decode_privs_string(str)
return privs_grant, privs_revoke return privs_grant, privs_revoke
end end
function toggle_areas:is_areas_mod(area_id) function switch_creative:is_areas_mod(area_id)
local is_areas_id = string.sub(id, 1, 2) == "a_" local is_areas_id = string.sub(id, 1, 2) == "a_"
local a_id = tonumber( string.sub(id, 3) ) local a_id = tonumber( string.sub(id, 3) )
return is_areas_id, a_id return is_areas_id, a_id
end end
function toggle_areas:get_area_size(astore_area) function switch_creative:get_area_size(astore_area)
local pos1 = astore_area.min local pos1 = astore_area.min
local pos2 = astore_area.max local pos2 = astore_area.max
local x = math.abs( pos2.x - pos1.x ) local x = math.abs( pos2.x - pos1.x )
@ -69,24 +69,24 @@ function toggle_areas:get_area_size(astore_area)
return x*z return x*z
end end
function toggle_areas:save_player_privs(player) function switch_creative:save_player_privs(player)
local name = player:get_player_name() local name = player:get_player_name()
local pmeta = player:get_meta() local pmeta = player:get_meta()
local privs = minetest.get_player_privs(name) local privs = minetest.get_player_privs(name)
pmeta:set_string("toggle_areas_privs", minetest.serialize(privs)) pmeta:set_string("switch_creative.saved_privs", minetest.serialize(privs))
end end
function toggle_areas:restore_player_privs(player) function switch_creative:restore_player_privs(player)
local name = player:get_player_name() local name = player:get_player_name()
local pmeta = player:get_meta() local pmeta = player:get_meta()
local privs = minetest.deserialize(pmeta:get_string("toggle_areas_privs")) local privs = minetest.deserialize(pmeta:get_string("switch_creative.saved_privs"))
if privs then if privs then
minetest.set_player_privs(name, privs) minetest.set_player_privs(name, privs)
pmeta:set_string("toggle_areas_privs", nil) pmeta:set_string("switch_creative.saved_privs", nil)
end end
end end
function toggle_areas:save_player_inventory(player, listname) function switch_creative:save_player_inventory(player, listname)
local name = player:get_player_name() local name = player:get_player_name()
local pmeta = player:get_meta() local pmeta = player:get_meta()
local inv = player:get_inventory() local inv = player:get_inventory()
@ -94,7 +94,7 @@ function toggle_areas:save_player_inventory(player, listname)
inv:set_list(listname, player_items) inv:set_list(listname, player_items)
end end
function toggle_areas:restore_player_inventory(player, listname) function switch_creative:restore_player_inventory(player, listname)
local name = player:get_player_name() local name = player:get_player_name()
local pmeta = player:get_meta() local pmeta = player:get_meta()
local inv = player:get_inventory() local inv = player:get_inventory()
@ -102,23 +102,23 @@ function toggle_areas:restore_player_inventory(player, listname)
inv:set_list("main", player_items) inv:set_list("main", player_items)
end end
function toggle_areas:player_enter_area(player, privstring) function switch_creative:player_enter_area(player, privstring)
local name = player:get_player_name() local name = player:get_player_name()
local has_creative = minetest.check_player_privs(name, {creative=true}) local has_creative = minetest.check_player_privs(name, {creative=true})
local privs_grant, privs_revoke = toggle_areas:decode_privs_string(privstring) local privs_grant, privs_revoke = switch_creative:decode_privs_string(privstring)
if not has_creative and privs_grant.creative and not privs_revoke.creative then if not has_creative and privs_grant.creative and not privs_revoke.creative then
minetest.chat_send_player(name, "You entered creative mode") minetest.chat_send_player(name, "You entered creative mode")
toggle_areas:save_player_inventory(player, "saved_survival") switch_creative:save_player_inventory(player, "saved_survival")
toggle_areas:restore_player_inventory(player, "saved_creative") switch_creative:restore_player_inventory(player, "saved_creative")
if minetest.get_modpath("unified_inventory") then if minetest.get_modpath("unified_inventory") then
unified_inventory.set_inventory_formspec(player, unified_inventory.default) unified_inventory.set_inventory_formspec(player, unified_inventory.default)
end end
elseif has_creative and privs_revoke.creative then elseif has_creative and privs_revoke.creative then
minetest.chat_send_player(name, "You entered survival mode") minetest.chat_send_player(name, "You entered survival mode")
toggle_areas:save_player_inventory(player, "saved_creative") switch_creative:save_player_inventory(player, "saved_creative")
toggle_areas:restore_player_inventory(player, "saved_survival") switch_creative:restore_player_inventory(player, "saved_survival")
end end
local has_privs = minetest.get_player_privs(name) local has_privs = minetest.get_player_privs(name)
@ -133,13 +133,13 @@ function toggle_areas:player_enter_area(player, privstring)
end end
function toggle_areas:update_player(player) function switch_creative:update_player(player)
local name = player:get_player_name() local name = player:get_player_name()
local pos = vector.round(player:get_pos()) local pos = vector.round(player:get_pos())
local pmeta = player:get_meta() local pmeta = player:get_meta()
-- pmeta:mark_as_private("toggle_areas") -- pmeta:mark_as_private("switch_creative.active_areas")
local player_last_active_areas = pmeta:get_string("toggle_areas.active_areas") or "-1" local player_last_active_areas = pmeta:get_string("switch_creative.active_areas") or "-1"
local player_active_areas = "-1" local player_active_areas = "-1"
local areas_owner = false local areas_owner = false
local areas_guest = false local areas_guest = false
@ -155,7 +155,7 @@ function toggle_areas:update_player(player)
-- enter area -- enter area
local current_areas = self.astore:get_areas_for_pos(pos, true, true) local current_areas = self.astore:get_areas_for_pos(pos, true, true)
for astore_id, astore_area in pairs( current_areas ) do for astore_id, astore_area in pairs( current_areas ) do
local areasize = toggle_areas:get_area_size(astore_area) local areasize = switch_creative:get_area_size(astore_area)
if not smallest_area.size or areasize < smallest_area.size then if not smallest_area.size or areasize < smallest_area.size then
smallest_area.id = astore_id smallest_area.id = astore_id
smallest_area.size = areasize smallest_area.size = areasize
@ -185,8 +185,8 @@ function toggle_areas:update_player(player)
end end
local privstring = default_privs local privstring = default_privs
if smallest_area.id and toggle_areas.area_privs[smallest_area.id] then if smallest_area.id and switch_creative.area_privs[smallest_area.id] then
privstring = toggle_areas.area_privs[smallest_area.id] privstring = switch_creative.area_privs[smallest_area.id]
end end
if areas_owner or areas_open then if areas_owner or areas_open then
privstring = privstring .. ", " .. areas_owner_privs privstring = privstring .. ", " .. areas_owner_privs
@ -195,29 +195,29 @@ function toggle_areas:update_player(player)
for _, a in pairs(string.split(player_active_areas, ",")) do for _, a in pairs(string.split(player_active_areas, ",")) do
a = a:trim() a = a:trim()
if string.sub(a, 1, 1) == "a" then if string.sub(a, 1, 1) == "a" then
if toggle_areas.area_privs[a] then if switch_creative.area_privs[a] then
privstring = privstring .. ", " .. toggle_areas.area_privs[a] privstring = privstring .. ", " .. switch_creative.area_privs[a]
end end
end end
end end
end end
if player_active_areas ~= player_last_active_areas then if player_active_areas ~= player_last_active_areas then
toggle_areas:player_enter_area(player, privstring) switch_creative:player_enter_area(player, privstring)
pmeta:set_string("toggle_areas.active_areas", player_active_areas) pmeta:set_string("switch_creative.active_areas", player_active_areas)
end end
-- print(dump(pmeta:to_table())) -- print(dump(pmeta:to_table()))
end end
toggle_areas:load_areas() switch_creative:load_areas()
local timer = 0 local timer = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
timer = timer + dtime timer = timer + dtime
if timer >= 1 then if timer >= 1 then
for _, player in pairs(minetest.get_connected_players()) do for _, player in pairs(minetest.get_connected_players()) do
toggle_areas:update_player(player) switch_creative:update_player(player)
end end
end end
end) end)
@ -225,7 +225,7 @@ end)
-- areas mod hud -- areas mod hud
if minetest.get_modpath("areas") then if minetest.get_modpath("areas") then
local function areas_hud_handler(pos, areas) local function areas_hud_handler(pos, areas)
local current_areas = toggle_areas.astore:get_areas_for_pos(pos, false, true) local current_areas = switch_creative.astore:get_areas_for_pos(pos, false, true)
for astore_id, astore_area in pairs( current_areas ) do for astore_id, astore_area in pairs( current_areas ) do
if astore_id then if astore_id then
table.insert(areas, { table.insert(areas, {

2
mod.conf

@ -1,4 +1,4 @@
name = toggle_areas name = switch_creative
description = Toggle privs in areas description = Toggle privs in areas
depends = default, sfinv depends = default, sfinv
optional_depends = areas, unified_inventory, hunger_ng optional_depends = areas, unified_inventory, hunger_ng

6
settingtypes.txt

@ -1,8 +1,8 @@
# Privs of an areas mod owner # Privs of an areas mod owner
toggle_areas.owner_privs (Owner privs) string "+creative, +fast, +fly" switch_creative.owner_privs (Owner privs) string "+creative, +fast, +fly"
# Privs of an areas mod guest # Privs of an areas mod guest
toggle_areas.guest_privs (Guest privs) string "-fly, -fast" switch_creative.guest_privs (Guest privs) string "-fly, -fast"
# Default privs # Default privs
toggle_areas.guest_privs (Default privs) string "-creative, -fast, -fly" switch_creative.guest_privs (Default privs) string "-creative, -fast, -fly"

Loading…
Cancel
Save