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()
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_guest_privs = minetest.settings:get("toggle_areas.guest_privs") or "-fly, -fast"
default_privs = minetest.settings:get("toggle_areas.default_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("switch_creative.guest_privs") or "-fly, -fast"
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 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))
toggle_areas:set_area_privs(astore_id, "+creative, +fast, +fly")
switch_creative:set_area_privs(astore_id, "+creative, +fast, +fly")
end
function toggle_areas:save_areas()
function switch_creative:save_areas()
local datastr = minetest.serialize(self.astore)
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
end
mod_storage:set_string("astore", datastr)
end
function toggle_areas:set_area_privs(id, privs_string)
toggle_areas.area_privs[id] = privs_string
function switch_creative:set_area_privs(id, privs_string)
switch_creative.area_privs[id] = privs_string
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()
assert(type(str) == "string")
local delim = ','
@ -55,13 +55,13 @@ function toggle_areas:decode_privs_string(str)
return privs_grant, privs_revoke
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 a_id = tonumber( string.sub(id, 3) )
return is_areas_id, a_id
end
function toggle_areas:get_area_size(astore_area)
function switch_creative:get_area_size(astore_area)
local pos1 = astore_area.min
local pos2 = astore_area.max
local x = math.abs( pos2.x - pos1.x )
@ -69,24 +69,24 @@ function toggle_areas:get_area_size(astore_area)
return x*z
end
function toggle_areas:save_player_privs(player)
function switch_creative: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("toggle_areas_privs", minetest.serialize(privs))
pmeta:set_string("switch_creative.saved_privs", minetest.serialize(privs))
end
function toggle_areas:restore_player_privs(player)
function switch_creative:restore_player_privs(player)
local name = player:get_player_name()
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
minetest.set_player_privs(name, privs)
pmeta:set_string("toggle_areas_privs", nil)
pmeta:set_string("switch_creative.saved_privs", nil)
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 pmeta = player:get_meta()
local inv = player:get_inventory()
@ -94,7 +94,7 @@ function toggle_areas:save_player_inventory(player, listname)
inv:set_list(listname, player_items)
end
function toggle_areas:restore_player_inventory(player, listname)
function switch_creative:restore_player_inventory(player, listname)
local name = player:get_player_name()
local pmeta = player:get_meta()
local inv = player:get_inventory()
@ -102,23 +102,23 @@ function toggle_areas:restore_player_inventory(player, listname)
inv:set_list("main", player_items)
end
function toggle_areas:player_enter_area(player, privstring)
function switch_creative: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 = 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
minetest.chat_send_player(name, "You entered creative mode")
toggle_areas:save_player_inventory(player, "saved_survival")
toggle_areas:restore_player_inventory(player, "saved_creative")
switch_creative:save_player_inventory(player, "saved_survival")
switch_creative: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")
toggle_areas:save_player_inventory(player, "saved_creative")
toggle_areas:restore_player_inventory(player, "saved_survival")
switch_creative:save_player_inventory(player, "saved_creative")
switch_creative:restore_player_inventory(player, "saved_survival")
end
local has_privs = minetest.get_player_privs(name)
@ -133,13 +133,13 @@ function toggle_areas:player_enter_area(player, privstring)
end
function toggle_areas:update_player(player)
function switch_creative: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("toggle_areas")
local player_last_active_areas = pmeta:get_string("toggle_areas.active_areas") or "-1"
-- pmeta:mark_as_private("switch_creative.active_areas")
local player_last_active_areas = pmeta:get_string("switch_creative.active_areas") or "-1"
local player_active_areas = "-1"
local areas_owner = false
local areas_guest = false
@ -155,7 +155,7 @@ function toggle_areas: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 = 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
smallest_area.id = astore_id
smallest_area.size = areasize
@ -185,8 +185,8 @@ function toggle_areas:update_player(player)
end
local privstring = default_privs
if smallest_area.id and toggle_areas.area_privs[smallest_area.id] then
privstring = toggle_areas.area_privs[smallest_area.id]
if smallest_area.id and switch_creative.area_privs[smallest_area.id] then
privstring = switch_creative.area_privs[smallest_area.id]
end
if areas_owner or areas_open then
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
a = a:trim()
if string.sub(a, 1, 1) == "a" then
if toggle_areas.area_privs[a] then
privstring = privstring .. ", " .. toggle_areas.area_privs[a]
if switch_creative.area_privs[a] then
privstring = privstring .. ", " .. switch_creative.area_privs[a]
end
end
end
end
if player_active_areas ~= player_last_active_areas then
toggle_areas:player_enter_area(player, privstring)
pmeta:set_string("toggle_areas.active_areas", player_active_areas)
switch_creative:player_enter_area(player, privstring)
pmeta:set_string("switch_creative.active_areas", player_active_areas)
end
-- print(dump(pmeta:to_table()))
end
toggle_areas:load_areas()
switch_creative: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
toggle_areas:update_player(player)
switch_creative:update_player(player)
end
end
end)
@ -225,7 +225,7 @@ end)
-- areas mod hud
if minetest.get_modpath("areas") then
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
if astore_id then
table.insert(areas, {

2
mod.conf

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

6
settingtypes.txt

@ -1,8 +1,8 @@
# 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
toggle_areas.guest_privs (Guest privs) string "-fly, -fast"
switch_creative.guest_privs (Guest privs) string "-fly, -fast"
# 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