creative_areas/init.lua

142 lines
4.7 KiB
Lua
Raw Normal View History

2018-02-19 20:11:00 +01:00
local storage = minetest.get_mod_storage()
local cr_areas = minetest.deserialize(storage:get_string("cr_areas")) or {}
2018-02-19 07:01:53 +01:00
---------------
-- Functions
---------------
2018-02-19 20:11:42 +01:00
-- Add creative area to list.
local function make_cr_area(name, areaID)
2018-02-14 23:19:10 +01:00
local id = tonumber(areaID)
if areas.areas[id] ~= nil then
if cr_areas ~= {} then
for i = 1, #cr_areas do
if cr_areas[i] == id then
return minetest.chat_send_player(name, "Area " ..id.." is already a creative area.")
end
end
end
2018-02-14 23:19:10 +01:00
table.insert(cr_areas, id)
2018-02-19 20:11:00 +01:00
storage:set_string("cr_areas", minetest.serialize(cr_areas))
minetest.chat_send_player(name, "Area added to Creative Areas!")
else minetest.chat_send_player(name, "Not a valid area ID")
2018-02-14 23:19:10 +01:00
end
end
--Removes Creative Area
2018-02-19 20:11:42 +01:00
local function rm_cr_area(name, areaID)
local id = tonumber(areaID)
for i = 1, #cr_areas do
if cr_areas[i] == id then
table.remove(cr_areas, i)
2018-02-19 20:11:42 +01:00
storage:set_string("cr_areas", minetest.serialize(cr_areas))
return minetest.chat_send_player(name, "Creative area removed!")
end
end
return minetest.chat_send_player(name, "Not a creative area ID")
end
2018-02-19 07:01:53 +01:00
-- Checks players location against listed creative areas.
2018-02-14 23:19:10 +01:00
function check_cr_area(player)
local pos = player:get_pos()
local area_at_pos = areas:getAreasAtPos(pos)
local status = false
2018-02-19 21:16:36 +01:00
if cr_areas ~= {} then
for i = 1, #cr_areas do
local areaID = cr_areas[i]
-- Clean up creative areas which are have been deleted from Areas mod
if areas.areas[areaID] == nil then
table.remove(cr_areas, i)
2018-02-19 20:11:42 +01:00
storage:set_string("cr_areas", minetest.serialize(cr_areas))
end
-- Compare Areas which player are in with Creative Area. Grant/revoke creative priv accordingly."
for _, in_area in pairs(area_at_pos) do
if in_area["pos1"] == areas.areas[areaID]["pos1"] --make sure the areas are not just the same name.
and in_area["name"] == areas.areas[areaID]["name"] then
2018-02-14 23:19:10 +01:00
status = true
end
end
end
end
return status
2018-02-14 23:19:10 +01:00
end
2018-02-19 20:11:42 +01:00
--------------------
2018-02-14 23:19:10 +01:00
-- Chat Commands
2018-02-19 20:11:42 +01:00
-------------------
2018-02-14 23:19:10 +01:00
minetest.register_chatcommand("creative_area", {
description = "Sets area to grant players creative priv while inside it",
params = "<AreaID>",
privs = {privs = true},
2018-02-14 23:19:10 +01:00
func = function(name, param)
make_cr_area(name, param)
end
})
minetest.register_chatcommand("rm_creative_area", {
description = "Revokes area from list of creative areas",
params = "<AreaID>",
privs = {privs = true},
func = function(name, param)
rm_cr_area(name, param)
end
})
2018-02-19 21:16:36 +01:00
minetest.register_chatcommand("ls_creative_areas", {
description = "List creative areas and IDs",
params = "",
func = function(name, params)
local list = ""
if cr_areas ~= {} then
for i = 1, #cr_areas do
local id = tonumber(cr_areas[i])
local area_name = areas.areas[ id ]["name"]
list = list .. " " .. area_name .. " (ID="..id..")"
end
minetest.chat_send_player(name, "Creative Area (ID): "..list)
else minetest.chat_send_player(name, "No creative areas found")
end
end
})
2018-02-19 20:11:42 +01:00
-------------------------------------------------
2018-02-14 23:19:10 +01:00
-- Check location and Grant/revoke creative priv
2018-02-19 20:11:42 +01:00
-------------------------------------------------
2018-02-14 23:19:10 +01:00
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer >= math.random(1,3) then
2018-02-14 23:19:10 +01:00
for _, player in ipairs(minetest.get_connected_players()) do
local pname = player:get_player_name()
2018-02-19 20:11:00 +01:00
local privs = minetest.get_player_privs(pname)
local inv = minetest.get_inventory({type="player", name=pname})
2018-02-19 20:11:42 +01:00
if minetest.get_player_privs(pname).privs == nil then --Players with the "privs" priv will not have privileges effected.
2018-02-14 23:19:10 +01:00
if check_cr_area(player) == true then
if not minetest.check_player_privs(pname, {creative = true}) then
privs.creative = true
minetest.set_player_privs(pname, privs)
2018-02-19 07:01:53 +01:00
local context = {page = sfinv.get_homepage_name(player)}--minetest.get_inventory{{type="detached", name="creative_"..pname}}--{page = sfinv.pages["creative_"..pname]}
sfinv.set_player_inventory_formspec(player, context)
2018-02-19 20:11:00 +01:00
local invlist = inv:get_list("main")
inv:set_list("saved", invlist)
local list = ""
for i = 1, #invlist do
list = list .." "..dump(invlist[i])
end
--write_file(inv_file,)
minetest.chat_send_player(pname, "You are in creative area.")
2018-02-19 20:11:00 +01:00
end
2018-02-14 23:19:10 +01:00
else
if minetest.check_player_privs(pname, {creative=true}) then
privs.creative = nil
minetest.set_player_privs(pname, privs)
2018-02-19 20:11:00 +01:00
local saved = inv:get_list("saved")
if saved ~= nil then
inv:set_list("main", saved)
end
local context = {page = sfinv.get_homepage_name(player)}
sfinv.set_player_inventory_formspec(player, context)
minetest.chat_send_player(pname, "You have left creative area.")
end
2018-02-14 23:19:10 +01:00
end
2018-02-19 20:11:42 +01:00
end
2018-02-14 23:19:10 +01:00
end
timer = 0
end
end)