Prevent chatcommands being executed while cloaked

Prevents chatcommands being executed while cloaked. Currently the best way of doing it is iterating over every command and modifying it. Mods may prevent this from happening by setting allow_while_cloaked in the chatcommand definition.
This commit is contained in:
luk3yx 2018-04-02 08:01:04 +12:00
parent 3df9c5cdd7
commit b91825abd1
2 changed files with 23 additions and 1 deletions

View File

@ -7,6 +7,7 @@
minetest.register_chatcommand("cloak", { minetest.register_chatcommand("cloak", {
params = "[victim]", params = "[victim]",
description = "Cloak a player so they are not visible.", description = "Cloak a player so they are not visible.",
allow_while_cloaked = true,
privs = {privs = true}, privs = {privs = true},
func = function(player, victim) func = function(player, victim)
if not victim or victim == '' then if not victim or victim == '' then
@ -30,6 +31,7 @@ minetest.register_chatcommand("cloak", {
minetest.register_chatcommand("uncloak", { minetest.register_chatcommand("uncloak", {
params = "[victim]", params = "[victim]",
description = "Uncloak a player so they are visible.", description = "Uncloak a player so they are visible.",
allow_while_cloaked = true,
func = function(player, victim) func = function(player, victim)
if not victim or victim == '' then if not victim or victim == '' then
victim = player victim = player

View File

@ -13,6 +13,7 @@ cloaking.get_player_by_name = minetest.get_player_by_name
cloaking.get_server_status = minetest.get_server_status cloaking.get_server_status = minetest.get_server_status
local cloaked_players = {} local cloaked_players = {}
local chatcommands_modified = false
-- Override built-in functions -- Override built-in functions
minetest.get_connected_players = function() minetest.get_connected_players = function()
@ -45,8 +46,27 @@ minetest.get_server_status = function()
return status return status
end end
-- Override every chatcommand
local override_chatcommands = function()
for name, def in pairs(minetest.chatcommands) do
if not def.allow_while_cloaked then
local real_cmd = def.func
minetest.chatcommands[name].func = function(name, param)
if cloaked_players[name] then
return false, "You may not execute chatcommands while " ..
"cloaked. Please use /uncloak if you want to " ..
"execute a chatcommand."
else
return real_cmd(name, param)
end
end
end
end
end
-- The cloak and uncloak functions -- The cloak and uncloak functions
cloaking.cloak = function(player) cloaking.cloak = function(player)
if not chatcommands_modified then override_chatcommands() end
if type(player) == "string" then if type(player) == "string" then
player = cloaking.get_player_by_name(player) player = cloaking.get_player_by_name(player)
end end