From 9af94a8bc60754ac72aff490370541f6d85c554a Mon Sep 17 00:00:00 2001 From: Deathwing777 <160651137+Deathwing777@users.noreply.github.com> Date: Tue, 20 Feb 2024 14:53:14 -0800 Subject: [PATCH] Code cleanup and new global/chat function. - Added a new global function to the nether table that lets external mods tell us to update a player's Nether status based on the player's position. This function has been exposed to the console via a registered chat function. - Switched from NETHER as the global table to nether as the global table after discovering that nether was already being used in init.lua. - Switched from accessing function calls in the global table from table syntax to object syntax; as it looks cleaner. --- nether/portal.lua | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/nether/portal.lua b/nether/portal.lua index fa210ba..18dc33c 100644 --- a/nether/portal.lua +++ b/nether/portal.lua @@ -13,9 +13,6 @@ end) local save_path = minetest.get_worldpath() .. "/nether_players" local players_in_nether = {} --- Global table to store things that nether aware mods might need to access. -NETHER = {} - -- Load the list of players which are trapped in the nether -- (or would be trapped if nether.trap_players was true) do @@ -33,7 +30,7 @@ do end -- Nether aware mods will need to know if a player is in the nether. -NETHER['is_player_in_nether'] = function (player) +nether['is_player_in_nether'] = function (player) local pname = player:get_player_name() if players_in_nether[pname] == nil then return false @@ -41,7 +38,17 @@ NETHER['is_player_in_nether'] = function (player) return true end end -local is_player_in_nether = NETHER['is_player_in_nether'] + +-- Nether aware mods may have other means of moving players between the Nether and Overworld, and if so, they should tell us about it so we can keep track of the player state. +nether['external_nether_teleport'] = function (player) + local pos = player:get_pos() + local pname = player:get_player_name() + if (pos.y < nether.start) and (pos.y >= nether.bottom) then + players_in_nether[pname] = true + else + players_in_nether[pname] = nil + end +end local function save_nether_players() local playernames,n = {},1 @@ -210,7 +217,35 @@ minetest.register_chatcommand("in_hell", { end status = pname.." is in the " - if is_player_in_nether(player) then + if nether.is_player_in_nether(player) then + status = status.."NETHER!" + else + status = status.."OVERWORLD!" + end + + return true, status + end +}) + +minetest.register_chatcommand("update_hells_registry", { + params = "[]", + description = "Update player state if they got to or from the nether in another way.", + func = function(name, pname) + if not minetest.check_player_privs(name, {nether=true}) then + return false, + "You need the nether priv to execute this chatcommand." + end + if not player_exists(pname) then + pname = name + end + local player = minetest.get_player_by_name(pname) + if not player then + return false, "Something went wrong." + end + + nether.external_nether_teleport(player) + status = pname.." is in the " + if nether.is_player_in_nether(player) then status = status.."NETHER!" else status = status.."OVERWORLD!"