From f415fc33e7381e8b5185ade61f4a1e67f5e84328 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Sun, 10 Mar 2019 18:38:30 +1300 Subject: [PATCH] Make cloaking.unhide_player() restore original player properties. --- core.lua | 64 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/core.lua b/core.lua index d45ed83..81d364a 100644 --- a/core.lua +++ b/core.lua @@ -130,9 +130,9 @@ local function player_and_name(player, n) local victim = player:get_player_name() if n then - if cloaked_players[victim] then return end + if cloaked_players[victim] then return false end elseif n ~= nil then - if not cloaked_players[victim] then return end + if not cloaked_players[victim] then return false end end return player, victim @@ -145,12 +145,22 @@ if not minetest.features.object_independent_selectionbox then end -- "Hide" players -function cloaking.hide_player(player) +local hidden = {} +function cloaking.hide_player(player, preserve_attrs) -- Sanity check - if type(player) == "string" then - local _ - player, _ = player_and_name(player, true) - if not player then return end + local victim + local player, victim = player_and_name(player, true) + if not player then return end + + -- Save existing attributes + if preserve_attrs or preserve_attrs == nil then + if hidden[victim] then return end + hidden[victim] = { + player:get_properties(), + player:get_nametag_attributes() + } + else + hidden[victim] = nil end -- Hide the player @@ -162,26 +172,39 @@ function cloaking.hide_player(player) player:set_nametag_attributes({text = " "}) end +-- Remove original attributes when players leave +minetest.register_on_leaveplayer(function(player) + hidden[player:get_player_name()] = nil +end) + -- "Unhide" players function cloaking.unhide_player(player) -- Sanity check local player, victim = player_and_name(player, true) - if not player then return end + if not player or hidden[victim] == nil then return end - -- Get the new selectionbox - local box = false - if minetest.features.object_independent_selectionbox then - box = player:get_properties().collisionbox + -- Get the data + local data = hidden[victim] or {} + hidden[victim] = nil + + -- Use sensible defaults if the data does not exist. + if not data[1] then + local box = false + if minetest.features.object_independent_selectionbox then + box = player:get_properties().collisionbox + end + box = box or {-0.3,-1,-0.3,0.3,0.75,0.3} + + data = {{ + visual_size = {x = 1, y = 2, z = 1}, + [selectionbox] = box, + makes_footstep_sound = true, + }} end - box = box or {-0.3,-1,-0.3,0.3,0.75,0.3} -- Make the player visible - player:set_properties({ - visual_size = {x = 1, y = 2, z = 1}, - [selectionbox] = box, - makes_footstep_sound = true, - }) - player:set_nametag_attributes({text = victim}) + player:set_properties(data[1]) + player:set_nametag_attributes(data[2] or {text = victim}) end -- The cloak and uncloak functions @@ -192,7 +215,7 @@ function cloaking.cloak(player) local player, victim = player_and_name(player, true) if not player then return end - cloaking.hide_player(player) + cloaking.hide_player(player, false) local t = nil if use_areas and areas.hud[victim] then @@ -229,6 +252,7 @@ function cloaking.uncloak(player) if not player then return end cloaked_players[victim] = nil + hidden[victim] = false cloaking.unhide_player(player) -- In singleplayer, there is no joined the game message by default.