2018-04-01 08:31:55 +02:00
|
|
|
--
|
|
|
|
-- Minetest player cloaking mod: Core functions
|
|
|
|
--
|
2019-02-10 01:49:56 +01:00
|
|
|
-- © 2019 by luk3yx
|
2018-04-01 08:31:55 +02:00
|
|
|
--
|
|
|
|
|
|
|
|
cloaking = {}
|
|
|
|
|
|
|
|
-- Expose the real get_connected_players and get_player_by_name for mods that
|
|
|
|
-- can use them.
|
2018-04-02 02:00:02 +02:00
|
|
|
cloaking.get_player_by_name = minetest.get_player_by_name
|
|
|
|
cloaking.get_objects_inside_radius = minetest.get_objects_inside_radius
|
|
|
|
cloaking.get_server_status = minetest.get_server_status
|
2018-04-01 08:31:55 +02:00
|
|
|
|
|
|
|
local cloaked_players = {}
|
2018-04-01 22:01:04 +02:00
|
|
|
local chatcommands_modified = false
|
2018-04-01 08:31:55 +02:00
|
|
|
|
|
|
|
-- Override built-in functions
|
2019-02-10 01:49:56 +01:00
|
|
|
function minetest.get_player_by_name(player)
|
2018-04-01 08:31:55 +02:00
|
|
|
if cloaked_players[player] then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return cloaking.get_player_by_name(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-10 01:49:56 +01:00
|
|
|
function minetest.get_objects_inside_radius(pos, radius)
|
2018-04-02 02:00:02 +02:00
|
|
|
local objs = {}
|
|
|
|
for _, obj in ipairs(cloaking.get_objects_inside_radius(pos, radius)) do
|
|
|
|
if not cloaked_players[obj:get_player_name()] then
|
|
|
|
table.insert(objs, obj)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return objs
|
|
|
|
end
|
|
|
|
|
2019-02-10 01:49:56 +01:00
|
|
|
function minetest.get_server_status()
|
2018-04-01 20:57:11 +02:00
|
|
|
local status = cloaking.get_server_status()
|
2018-10-09 03:14:53 +02:00
|
|
|
local motd = status:sub(status:find('}', 1, true) + 0)
|
2018-04-01 20:57:11 +02:00
|
|
|
status = status:sub(1, status:find('{', 1, true))
|
|
|
|
local players = {}
|
|
|
|
for _, player in ipairs(minetest.get_connected_players()) do
|
|
|
|
table.insert(players, player:get_player_name())
|
2018-04-01 08:31:55 +02:00
|
|
|
end
|
2018-04-01 20:57:11 +02:00
|
|
|
players = table.concat(players, ', ')
|
2018-10-09 03:14:53 +02:00
|
|
|
status = status .. players .. motd
|
2018-04-01 20:57:11 +02:00
|
|
|
return status
|
|
|
|
end
|
2018-04-01 08:31:55 +02:00
|
|
|
|
2018-04-02 04:32:32 +02:00
|
|
|
-- Change the on-join status
|
|
|
|
minetest.settings:set_bool('show_statusline_on_connect', false)
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
minetest.chat_send_player(name, minetest.get_server_status())
|
|
|
|
end)
|
|
|
|
|
2018-04-02 01:30:47 +02:00
|
|
|
-- Don't allow chat or chatcommands in all commands that don't have the
|
|
|
|
-- allow_while_cloaked parameter set.
|
2018-04-01 22:01:04 +02:00
|
|
|
local override_chatcommands = function()
|
|
|
|
for name, def in pairs(minetest.chatcommands) do
|
2018-10-09 00:40:05 +02:00
|
|
|
if not def.allow_while_cloaked and not def._allow_while_cloaked then
|
2018-04-01 22:01:04 +02:00
|
|
|
local real_cmd = def.func
|
|
|
|
minetest.chatcommands[name].func = function(name, param)
|
|
|
|
if cloaked_players[name] then
|
2018-10-27 10:11:49 +02:00
|
|
|
local pass, r1, r2
|
|
|
|
if def._disallow_while_cloaked then
|
|
|
|
pass = false
|
|
|
|
else
|
|
|
|
pass, r1, r2 = pcall(real_cmd, name, param)
|
|
|
|
end
|
2018-10-27 10:06:43 +02:00
|
|
|
if pass then
|
|
|
|
return r1, r2
|
|
|
|
else
|
|
|
|
return false, "You may not execute this chatcommand " ..
|
|
|
|
"while cloaked. Please use /uncloak if you want " ..
|
|
|
|
"to execute this chatcommand."
|
|
|
|
end
|
2018-04-01 22:01:04 +02:00
|
|
|
else
|
|
|
|
return real_cmd(name, param)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-09 00:40:05 +02:00
|
|
|
|
2018-04-03 21:16:15 +02:00
|
|
|
local c = 0
|
|
|
|
for _, func in ipairs(minetest.registered_on_leaveplayers) do
|
|
|
|
c = c + 1
|
|
|
|
local f = func
|
2018-08-23 00:13:46 +02:00
|
|
|
if f ~= cloaking.delayed_uncloak then
|
2018-04-03 21:20:15 +02:00
|
|
|
minetest.registered_on_leaveplayers[c] = function(p, t, cloaked)
|
2018-04-13 23:02:20 +02:00
|
|
|
if cloaked ~= 'cloaking' and
|
|
|
|
cloaked_players[p:get_player_name()] then
|
|
|
|
return
|
2018-04-03 21:20:15 +02:00
|
|
|
end
|
|
|
|
return f(p, t)
|
2018-04-03 21:16:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-01 22:01:04 +02:00
|
|
|
end
|
|
|
|
|
2019-02-10 01:49:56 +01:00
|
|
|
-- Handle chat messages
|
|
|
|
function cloaking.on_chat_message(name, message)
|
2018-07-25 21:29:19 +02:00
|
|
|
if message:sub(1, 1) ~= "/" and cloaked_players[name] then
|
2018-04-02 01:30:47 +02:00
|
|
|
minetest.chat_send_player(name, "You cannot use chat while cloaked." ..
|
|
|
|
" Please use /uncloak if you want to use chat.")
|
|
|
|
return true
|
|
|
|
end
|
2018-10-11 01:03:29 +02:00
|
|
|
end
|
|
|
|
|
2019-02-10 01:49:56 +01:00
|
|
|
table.insert(minetest.registered_on_chat_messages, 1, cloaking.on_chat_message)
|
|
|
|
minetest.callback_origins[cloaking.on_chat_message] = {
|
|
|
|
mod = 'cloaking',
|
|
|
|
name = 'on_chat_message'
|
|
|
|
}
|
2018-04-02 01:30:47 +02:00
|
|
|
|
2018-10-27 10:11:49 +02:00
|
|
|
-- Disallow some built-in commands.
|
2018-10-27 10:13:04 +02:00
|
|
|
for _, cmd in ipairs({'me', 'msg'}) do
|
2018-10-27 10:11:49 +02:00
|
|
|
if minetest.chatcommands[cmd] then
|
|
|
|
minetest.override_chatcommand(cmd, {
|
|
|
|
_disallow_while_cloaked = true
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-01 08:31:55 +02:00
|
|
|
-- The cloak and uncloak functions
|
2019-02-10 01:49:56 +01:00
|
|
|
local use_areas = minetest.get_modpath('areas')
|
|
|
|
function cloaking.cloak(player)
|
2018-04-01 22:01:04 +02:00
|
|
|
if not chatcommands_modified then override_chatcommands() end
|
2018-04-01 08:31:55 +02:00
|
|
|
if type(player) == "string" then
|
|
|
|
player = cloaking.get_player_by_name(player)
|
|
|
|
end
|
2018-04-13 23:02:20 +02:00
|
|
|
local victim = player:get_player_name()
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-07-25 21:29:19 +02:00
|
|
|
if cloaked_players[victim] then
|
|
|
|
return
|
|
|
|
end
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-04-01 08:31:55 +02:00
|
|
|
player:set_properties({visual_size = {x = 0, y = 0}, collisionbox = {0,0,0,0,0,0}})
|
2018-04-13 23:02:20 +02:00
|
|
|
player:set_nametag_attributes({text = " "})
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-04-02 22:28:09 +02:00
|
|
|
local t = nil
|
2019-02-10 01:49:56 +01:00
|
|
|
if use_areas and areas.hud[victim] then
|
2018-04-02 22:28:09 +02:00
|
|
|
t = areas.hud[victim]
|
2018-04-01 08:31:55 +02:00
|
|
|
end
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-04-02 22:28:09 +02:00
|
|
|
for _, f in ipairs(minetest.registered_on_leaveplayers) do
|
2018-08-23 00:08:42 +02:00
|
|
|
if f ~= cloaking.delayed_uncloak then
|
2018-04-13 23:02:20 +02:00
|
|
|
f(player, false, 'cloaking')
|
2018-04-02 22:28:09 +02:00
|
|
|
end
|
|
|
|
end
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-04-13 22:33:35 +02:00
|
|
|
cloaked_players[victim] = true
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-04-02 22:28:09 +02:00
|
|
|
if t then
|
|
|
|
areas.hud[victim] = t
|
2018-04-13 23:02:20 +02:00
|
|
|
player:hud_change(areas.hud[victim].areasId, "text", "Cloaked")
|
2018-09-14 06:30:17 +02:00
|
|
|
areas.hud[victim].oldAreas = ""
|
2018-04-01 22:38:36 +02:00
|
|
|
end
|
2019-02-10 01:49:56 +01:00
|
|
|
|
|
|
|
minetest.log('verbose', victim .. ' was cloaked.')
|
2018-04-01 08:31:55 +02:00
|
|
|
end
|
|
|
|
|
2019-02-10 01:49:56 +01:00
|
|
|
function cloaking.uncloak(player)
|
2018-04-01 08:31:55 +02:00
|
|
|
if type(player) == "string" then
|
|
|
|
player = cloaking.get_player_by_name(player)
|
|
|
|
end
|
2018-04-13 23:02:20 +02:00
|
|
|
local victim = player:get_player_name()
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-07-25 21:29:19 +02:00
|
|
|
if not cloaked_players[victim] then
|
|
|
|
return
|
|
|
|
end
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-10-09 00:40:05 +02:00
|
|
|
player:set_properties({visual_size = {x = 1, y = 1},
|
|
|
|
collisionbox = {-0.25,-0.85,-0.25,0.25,0.85,0.25}})
|
2018-04-13 23:02:20 +02:00
|
|
|
player:set_nametag_attributes({text = victim})
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-08-23 00:08:42 +02:00
|
|
|
cloaked_players[victim] = nil
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-07-25 21:29:19 +02:00
|
|
|
-- In singleplayer, there is no joined the game message by default.
|
2018-04-03 21:16:15 +02:00
|
|
|
if victim == "singleplayer" then
|
2018-04-02 23:00:27 +02:00
|
|
|
minetest.chat_send_all("*** " .. victim .. " joined the game.")
|
|
|
|
end
|
2018-09-14 06:30:17 +02:00
|
|
|
|
2018-04-02 22:28:09 +02:00
|
|
|
for _, f in ipairs(minetest.registered_on_joinplayers) do
|
2018-08-23 00:13:46 +02:00
|
|
|
f(player)
|
2018-04-01 08:31:55 +02:00
|
|
|
end
|
2019-02-10 01:49:56 +01:00
|
|
|
|
|
|
|
minetest.log('verbose', victim .. ' was uncloaked.')
|
2018-04-01 08:31:55 +02:00
|
|
|
end
|
|
|
|
|
2018-04-03 22:04:27 +02:00
|
|
|
-- API functions
|
2019-02-10 01:49:56 +01:00
|
|
|
function cloaking.auto_uncloak(player)
|
2018-04-01 08:31:55 +02:00
|
|
|
if type(player) ~= "string" then
|
|
|
|
player = player:get_player_name()
|
|
|
|
end
|
|
|
|
if cloaked_players[player] then
|
|
|
|
cloaking.uncloak(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-10 01:49:56 +01:00
|
|
|
function cloaking.delayed_uncloak(player)
|
2018-04-13 23:02:20 +02:00
|
|
|
local victim = player:get_player_name()
|
2018-07-25 21:29:19 +02:00
|
|
|
if cloaked_players[victim] then
|
2018-04-13 23:02:20 +02:00
|
|
|
minetest.after(0.5, function()
|
|
|
|
cloaked_players[victim] = nil
|
|
|
|
if areas and areas.hud and areas.hud[victim] then
|
|
|
|
areas.hud[victim] = nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-09 00:40:05 +02:00
|
|
|
-- Register cloaking.delayed_uncloak "manually" so that the profiler can't
|
|
|
|
-- hijack it, preventing it from running.
|
|
|
|
minetest.registered_on_leaveplayers[#minetest.registered_on_leaveplayers + 1]
|
|
|
|
= cloaking.delayed_uncloak
|
|
|
|
minetest.callback_origins[cloaking.delayed_uncloak] = {
|
|
|
|
mod = 'cloaking',
|
|
|
|
name = 'delayed_uncloak'
|
|
|
|
}
|
2018-04-13 23:02:20 +02:00
|
|
|
|
2018-04-13 23:17:20 +02:00
|
|
|
-- The cloaking mod is so good it fools the built-in get_connected_players, so
|
|
|
|
-- overlay that with one that adds cloaked players in.
|
2019-02-10 01:49:56 +01:00
|
|
|
function cloaking.get_connected_players()
|
2018-04-13 23:17:20 +02:00
|
|
|
local a = minetest.get_connected_players()
|
|
|
|
for player, cloaked in pairs(cloaked_players) do
|
|
|
|
if cloaked then
|
|
|
|
table.insert(a, cloaking.get_player_by_name(player))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
|
2019-02-10 01:49:56 +01:00
|
|
|
function cloaking.get_cloaked_players()
|
2018-04-01 08:31:55 +02:00
|
|
|
local players = {}
|
|
|
|
for player, cloaked in pairs(cloaked_players) do
|
|
|
|
if cloaked then
|
|
|
|
table.insert(players, player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return players
|
|
|
|
end
|
|
|
|
|
2019-02-10 01:49:56 +01:00
|
|
|
-- Allow mods to get a list of cloaked and uncloaked player names.
|
|
|
|
function cloaking.get_connected_names()
|
|
|
|
local a = cloaking.get_cloaked_players()
|
|
|
|
for _, player in ipairs(minetest.get_connected_players()) do
|
|
|
|
table.insert(a, player:get_player_name())
|
|
|
|
end
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
|
|
|
|
function cloaking.is_cloaked(player)
|
2018-04-01 08:31:55 +02:00
|
|
|
if type(player) ~= "string" then player = player:get_player_name() end
|
|
|
|
return cloaked_players[player] and true or false
|
|
|
|
end
|