Make cloaking.unhide_player() restore original player properties.

This commit is contained in:
luk3yx 2019-03-10 18:38:30 +13:00
parent 3e9a7a6ea2
commit f415fc33e7
1 changed files with 44 additions and 20 deletions

View File

@ -130,9 +130,9 @@ local function player_and_name(player, n)
local victim = player:get_player_name() local victim = player:get_player_name()
if n then if n then
if cloaked_players[victim] then return end if cloaked_players[victim] then return false end
elseif n ~= nil then elseif n ~= nil then
if not cloaked_players[victim] then return end if not cloaked_players[victim] then return false end
end end
return player, victim return player, victim
@ -145,12 +145,22 @@ if not minetest.features.object_independent_selectionbox then
end end
-- "Hide" players -- "Hide" players
function cloaking.hide_player(player) local hidden = {}
function cloaking.hide_player(player, preserve_attrs)
-- Sanity check -- Sanity check
if type(player) == "string" then local victim
local _ local player, victim = player_and_name(player, true)
player, _ = player_and_name(player, true)
if not player then return end 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 end
-- Hide the player -- Hide the player
@ -162,26 +172,39 @@ function cloaking.hide_player(player)
player:set_nametag_attributes({text = " "}) player:set_nametag_attributes({text = " "})
end end
-- Remove original attributes when players leave
minetest.register_on_leaveplayer(function(player)
hidden[player:get_player_name()] = nil
end)
-- "Unhide" players -- "Unhide" players
function cloaking.unhide_player(player) function cloaking.unhide_player(player)
-- Sanity check -- Sanity check
local player, victim = player_and_name(player, true) 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 -- 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 local box = false
if minetest.features.object_independent_selectionbox then if minetest.features.object_independent_selectionbox then
box = player:get_properties().collisionbox box = player:get_properties().collisionbox
end end
box = box or {-0.3,-1,-0.3,0.3,0.75,0.3} box = box or {-0.3,-1,-0.3,0.3,0.75,0.3}
-- Make the player visible data = {{
player:set_properties({
visual_size = {x = 1, y = 2, z = 1}, visual_size = {x = 1, y = 2, z = 1},
[selectionbox] = box, [selectionbox] = box,
makes_footstep_sound = true, makes_footstep_sound = true,
}) }}
player:set_nametag_attributes({text = victim}) end
-- Make the player visible
player:set_properties(data[1])
player:set_nametag_attributes(data[2] or {text = victim})
end end
-- The cloak and uncloak functions -- The cloak and uncloak functions
@ -192,7 +215,7 @@ function cloaking.cloak(player)
local player, victim = player_and_name(player, true) local player, victim = player_and_name(player, true)
if not player then return end if not player then return end
cloaking.hide_player(player) cloaking.hide_player(player, false)
local t = nil local t = nil
if use_areas and areas.hud[victim] then if use_areas and areas.hud[victim] then
@ -229,6 +252,7 @@ function cloaking.uncloak(player)
if not player then return end if not player then return end
cloaked_players[victim] = nil cloaked_players[victim] = nil
hidden[victim] = false
cloaking.unhide_player(player) cloaking.unhide_player(player)
-- In singleplayer, there is no joined the game message by default. -- In singleplayer, there is no joined the game message by default.