From 8ca612e87cdefcb8cbee80373cbaeab6d0351507 Mon Sep 17 00:00:00 2001 From: elite Date: Mon, 10 Jul 2017 17:48:24 -0400 Subject: [PATCH] add privilegeareas mod --- worldmods/privilegeareas/README.md | 7 + worldmods/privilegeareas/description.txt | 1 + worldmods/privilegeareas/gui.lua | 196 ++++++++++++++++ worldmods/privilegeareas/init.lua | 273 +++++++++++++++++++++++ worldmods/privilegeareas/mod.conf | 7 + 5 files changed, 484 insertions(+) create mode 100644 worldmods/privilegeareas/README.md create mode 100644 worldmods/privilegeareas/description.txt create mode 100644 worldmods/privilegeareas/gui.lua create mode 100644 worldmods/privilegeareas/init.lua create mode 100644 worldmods/privilegeareas/mod.conf diff --git a/worldmods/privilegeareas/README.md b/worldmods/privilegeareas/README.md new file mode 100644 index 0000000..733885f --- /dev/null +++ b/worldmods/privilegeareas/README.md @@ -0,0 +1,7 @@ +# Privilege Areas [privilegeareas] + +This mod grants or revokes privileges to players depending on their position. + +Made by rubenwardy (a long time ago) + +License: CC0 or WTFPL diff --git a/worldmods/privilegeareas/description.txt b/worldmods/privilegeareas/description.txt new file mode 100644 index 0000000..a517d7d --- /dev/null +++ b/worldmods/privilegeareas/description.txt @@ -0,0 +1 @@ +Granting and revoking privileges based on position on the map. diff --git a/worldmods/privilegeareas/gui.lua b/worldmods/privilegeareas/gui.lua new file mode 100644 index 0000000..0f9d6f0 --- /dev/null +++ b/worldmods/privilegeareas/gui.lua @@ -0,0 +1,196 @@ +gui = {} + +-- Chat command for opening gui +minetest.register_chatcommand("privareas", { + params = "", + description = "PrivAreas: access a formspec from the privilegeareas mod", + func = function(name, param) + add_gui(name) + end, +}) + +-- The add gui +function add_gui(name) + print("Showing add area form to "..name) + + gui[name]={ + type="box" + } + + minetest.show_formspec(name, "privilegeareas:gui_add", "size[6,5]".. + "button[2,0;2,1;type;box]".. + "field[2,2;1,1;x;X;0]".. + "field[3,2;1,1;y;Y;0]".. + "field[4,2;1,1;z;Z;0]".. + "field[2,3;1,1;x2;X;0]".. + "field[3,3;1,1;y2;Y;0]".. + "field[4,3;1,1;z2;Z;0]".. + "button_exit[2,4;2,1;submit;Add]") +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + local name = player:get_player_name() + if (formname~="privilegeareas:gui_add") then + return false + end + + if fields.submit then + -- Do addition stuff + print("submitting") + + if gui[name].type == "box" then + table.insert(privilegeareas.areas,{ + type = gui[name].type, + location = { + x=math.floor(fields.x), + y=math.floor(fields.y), + z=math.floor(fields.z), + }, + location2 = { + x=math.floor(fields.x2), + y=math.floor(fields.y2), + z=math.floor(fields.z2), + }, + actions = { + on_enter = { + }, + on_leave = { + } + } + }) + else + table.insert(privilegeareas.areas,{ + type = gui[name].type, + location = { + x=math.floor(fields.x), + y=math.floor(fields.y), + z=math.floor(fields.z), + radius=math.floor(fields.rad) + }, + actions = { + on_enter = { + grant = {}, + take = {} + + }, + on_leave = { + grant = {}, + take = {} + } + } + }) + end + + gui[name]=nil + + act_gui(name,#privilegeareas.areas) + + return + end + + if fields.type then + -- Do toggle stuff + print("toggling") + if gui[name].type == "box" then + gui[name].type = "radius" + minetest.show_formspec(name, "privilegeareas:gui_add", "size[6,5]".. + "button[2,0;2,1;type;"..gui[name].type.."]".. + "field[2,2;1,1;x;X;"..fields.x.."]".. + "field[3,2;1,1;y;Y;"..fields.y.."]".. + "field[4,2;1,1;z;Z;"..fields.z.."]".. + "field[2,3;1,1;rad;R;0]".. + "button_exit[2,4;2,1;submit;Add]") + else + gui[name].type = "box" + minetest.show_formspec(name, "privilegeareas:gui_add", "size[6,5]".. + "button[2,0;2,1;type;"..gui[name].type.."]".. + "field[2,2;1,1;x;X;"..fields.x.."]".. + "field[3,2;1,1;y;Y;"..fields.y.."]".. + "field[4,2;1,1;z;Z;"..fields.z.."]".. + "field[2,3;1,1;x2;X;0]".. + "field[3,3;1,1;y2;Y;0]".. + "field[4,3;1,1;z2;Z;0]".. + "button_exit[2,4;2,1;submit;Add]") + end + end +end) + +function act_gui(name,id) + print("Showing add action form for area "..id) + + print(dump(privilegeareas.areas[id])) + + if not gui[name] or not gui[name].trigger or not gui[name].type or not gui[name].value then + gui[name]={ + trigger = "on_enter", + type = "grant", + value = "", + } + end + + gui[name].id = id + + minetest.show_formspec(name, "privilegeareas:gui_act", "size[7,4]".. + "field[2,1;4,1;name;Data;"..gui[name].value.."]".. + "button[1,2;2,1;trigger;"..gui[name].trigger.."]".. + "button[4,2;2,1;type;"..gui[name].type.."]".. + "button[1,3;2,1;submit;Add]".. + "button_exit[4,3;2,1;close;Close]") +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if (formname~="privilegeareas:gui_act") then + return false + end + + local name = player:get_player_name() + gui[name].value = fields.name + + if fields.close then + return + end + + if fields.submit then + -- Do addition stuff + print("submitting") + + if (gui[name].id and gui[name].trigger and fields.name) then + -- this will doubtlessly be buggy + table.insert(privilegeareas.areas[gui[name].id].actions[gui[name].trigger][gui[name].type],fields.name) + + -- Alert user of success + minetest.chat_send_player(name, "Added data '"..fields.name.."' to '"..gui[name].type.."' in trigger "..gui[name].trigger.." in area "..gui[name].id) + + -- Delete field + local id = gui[name].id + gui[name] = nil + + -- Update + act_gui(name,id) + + return + end + end + + if fields.type then + -- Do toggle stuff + print("toggling type") + if gui[player:get_player_name()].type == "grant" then + gui[player:get_player_name()].type = "take" + else + gui[player:get_player_name()].type = "grant" + end + end + + if fields.trigger then + -- Do toggle stuff + print("toggling trigger") + if gui[player:get_player_name()].trigger == "on_enter" then + gui[player:get_player_name()].trigger = "on_leave" + else + gui[player:get_player_name()].trigger = "on_enter" + end + end + + act_gui(name,gui[name].id) +end) \ No newline at end of file diff --git a/worldmods/privilegeareas/init.lua b/worldmods/privilegeareas/init.lua new file mode 100644 index 0000000..1e5414d --- /dev/null +++ b/worldmods/privilegeareas/init.lua @@ -0,0 +1,273 @@ +privilegeareas = { + + players = nil, + userdata = {}, + + areas = { + }, + + createPlayerTable = function(player) + if not privilegeareas.players then + load_data() + end + + if not player or not player:get_player_name() then + print("[PrivilegeAreas] Player does not exist!") + return; + end + + local name = player:get_player_name() + + print("[PrivilegeAreas] writing player table for "..name) + + if (name=="") then + return; + end + + privilegeareas.players[name] = {} + privilegeareas.userdata[name] = player + + if not privilegeareas.players[name].areas then + privilegeareas.players[name].areas = {} + end + end, + + calculate_current_areas = function(player) + local name = player:get_player_name() + + if (name=="") then + return; + end + + if (not privilegeareas.players[name]) then + createPlayerTable(player) + end + + for i=1,# privilegeareas.areas do + if privilegeareas.areas[i].type == "radius" then + if distance(player:getpos(),privilegeareas.areas[i].location) < privilegeareas.areas[i].location.radius then + if (not privilegeareas.players[name].areas or not privilegeareas.players[name].areas[i] or privilegeareas.players[name].areas[i]==false) then + privilegeareas.enter_area(player,i) + end + else + if (privilegeareas.players[name].areas[i]==true) then + privilegeareas.leave_area(player,i) + end + end + elseif privilegeareas.areas[i].type == "box" then + if (vector_is_in(privilegeareas.areas[i].location,privilegeareas.areas[i].location2,player:getpos())) then + if (not privilegeareas.players[name].areas or not privilegeareas.players[name].areas[i] or privilegeareas.players[name].areas[i]==false) then + privilegeareas.enter_area(player,i) + end + else + if (privilegeareas.players[name].areas[i]==true) then + privilegeareas.leave_area(player,i) + end + end + end + end + end, + + enter_area = function(player,i) + local name = player:get_player_name() + privilegeareas.players[name].areas[i]=true + minetest.chat_send_player(name, "You have entered area "..i) + print ("[PrivilegeAreas] "..name.." has entered area "..i) + + -- Get privs + local privs = minetest.get_player_privs(name) + + if not privs then + print("[PrivilegeAreas] player does not exist error!") + end + + -- loop grants + local tmpv = false + if privilegeareas.areas[i].actions.on_enter.grant then + for a=1,# privilegeareas.areas[i].actions.on_enter.grant do + if tmpv == false then + tmpv = true + minetest.chat_send_player(name, "You have been given the following privs:") + print("[PrivilegeAreas] "..name.." has been given the following privs:") + end + + privs[privilegeareas.areas[i].actions.on_enter.grant[a]]=true; + minetest.chat_send_player(name, "-- "..privilegeareas.areas[i].actions.on_enter.grant[a]) + print("[PrivilegeAreas] -- "..privilegeareas.areas[i].actions.on_enter.grant[a]) + end + end + + -- Loop though takes + tmpv = false + if privilegeareas.areas[i].actions.on_enter.take then + + for a=1,# privilegeareas.areas[i].actions.on_enter.take do + if tmpv == false then + tmpv = true + minetest.chat_send_player(name, "You have lost the following privs:") + print("[PrivilegeAreas] "..name.." has lost the following privs:") + end + + privs[privilegeareas.areas[i].actions.on_enter.take[a]]=false; + minetest.chat_send_player(name, "-- "..privilegeareas.areas[i].actions.on_enter.take[a]) + print("[PrivilegeAreas] -- "..privilegeareas.areas[i].actions.on_enter.take[a]) + end + end + + -- Set privs + minetest.set_player_privs(name, privs) + + -- save data + save_data() + end, + + leave_area = function(player,i) + local name = player:get_player_name() + privilegeareas.players[name].areas[i]=false + minetest.chat_send_player(name, "You have left area "..i) + print ("[PrivilegeAreas] "..name.." has left area "..i) + + -- Get privs + local privs = minetest.get_player_privs(name) + + if not privs then + print("[PrivilegeAreas] player does not exist error!") + end + + -- loop grants + local tmp = false + if privilegeareas.areas[i].actions.on_leave.grant then + for a=1,# privilegeareas.areas[i].actions.on_leave.grant do + if tmp == false then + tmp = true + minetest.chat_send_player(name, "You have been given the following privs:") + print("[PrivilegeAreas] "..name.." has been given the following privs:") + end + + privs[privilegeareas.areas[i].actions.on_leave.grant[a]]=true; + minetest.chat_send_player(name, "-- "..privilegeareas.areas[i].actions.on_leave.grant[a]) + print("[PrivilegeAreas] -- "..privilegeareas.areas[i].actions.on_leave.grant[a]) + end + end + + -- Loop though takes + tmp = false + if privilegeareas.areas[i].actions.on_leave.take then + + for a=1,# privilegeareas.areas[i].actions.on_leave.take do + if tmp == false then + tmp = true + minetest.chat_send_player(name, "You have lost the following privs:") + print("[PrivilegeAreas] "..name.." has lost the following privs:") + end + + privs[privilegeareas.areas[i].actions.on_leave.take[a]]=false; + minetest.chat_send_player(name, "-- "..privilegeareas.areas[i].actions.on_leave.take[a]) + print("[PrivilegeAreas] -- "..privilegeareas.areas[i].actions.on_leave.take[a]) + end + end + + -- Set privs + minetest.set_player_privs(name, privs) + + -- save data + save_data() + end, +} + +-- Table Save Load Functions +function save_data() + if privilegeareas.players == nil then + return + end + + print("[PrivilegeAreas] Saving data") + + local file = io.open(minetest.get_worldpath().."/privareas.txt", "w") + if file then + file:write(minetest.serialize(privilegeareas.players)) + file:close() + end + + local file = io.open(minetest.get_worldpath().."/privareas_areas.txt", "w") + if file then + file:write(minetest.serialize(privilegeareas.areas)) + file:close() + end +end + +function _load_data() + local file = io.open(minetest.get_worldpath().."/privareas_areas.txt", "r") + if file then + local table = minetest.deserialize(file:read("*all")) + if type(table) == "table" then + privilegeareas.areas = table + return + end + end +end + +function load_data() + print("[PrivilegeAreas] Loading data") + _load_data() + if privilegeareas.players == nil then + local file = io.open(minetest.get_worldpath().."/privareas.txt", "r") + if file then + local table = minetest.deserialize(file:read("*all")) + if type(table) == "table" then + privilegeareas.players = table + return + end + end + end + privilegeareas.players = {} +end + +load_data() + +minetest.register_on_shutdown(function() + -- save data + save_data() +end) + +function vector_is_in(hay,box,needle) + if (needle.x > hay.x and needle.x < box.x) then + if (needle.y > hay.y and needle.y < box.y) then + if (needle.z > hay.z and needle.z = 1 then + timer=0 + for _, plr in pairs(privilegeareas.userdata) do + privilegeareas.calculate_current_areas(plr) + end + + end +end) + +minetest.register_on_joinplayer(function(player) + privilegeareas.createPlayerTable(player) +end) + +minetest.register_on_leaveplayer(function(player) + privilegeareas.userdata[player:get_player_name()]=nil +end) + +dofile(minetest.get_modpath("privilegeareas").."/gui.lua") \ No newline at end of file diff --git a/worldmods/privilegeareas/mod.conf b/worldmods/privilegeareas/mod.conf new file mode 100644 index 0000000..61a9750 --- /dev/null +++ b/worldmods/privilegeareas/mod.conf @@ -0,0 +1,7 @@ +name = privilegeareas +title = Privilege Areas +author = rubenwardy +description = Grant and revoke privileges based on position of a player on the map. +license = WTFPL +forum = https://forum.minetest.net/viewtopic.php?t=7553 +version = 1.1.0