summaryrefslogtreecommitdiff
path: root/init.lua
blob: 663c0312c55f895e6ad77c58bfc634ae9cbf6cf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
local state = "passthrough"
local dumpcalls = false

local old_is_protected = minetest.is_protected

minetest.register_chatcommand("isprot_override",{
	params = "< prot | unprot | passthrough >",
	description = "Override minetest.is_protected() results. DANGER - 'unprot' option effectively disables area protection for the entire server!",
	privs = {server = true},
	func = function(name,param)
		if param == "prot" or param == "protected" or param == "true" then
			minetest.chat_send_player(name,"minetest.is_protected() will now always return "..minetest.colorize("#55ffff","true")..".")
			state = "true"
		elseif param == "unprot" or param == "unprotected" or param == "false" then
			minetest.chat_send_player(name,"minetest.is_protected() will now always return "..minetest.colorize("#55ffff","false")..". This means that area protection is "..minetest.colorize("#ff5555","effectively disabled").."!")
			state = "false"
		elseif param == "passthrough" then
			minetest.chat_send_player(name,"minetest.is_protected() will now "..minetest.colorize("#55ffff","run the existing function")..".")
			state = "passthrough"
		elseif param == "" or not param then
			local states = {
				["passthrough"] = "passing calls to the existing function",
				["true"] = "always returning true",
				["false"] = "always returning false",
			}
			minetest.chat_send_player(name,"minetest.is_protected() is currently "..states[state])
		else
			minetest.chat_send_player(name,minetest.colorize("#ff5555","Error: ").."Unrecognized mode. Valid modes are: prot, unprot, passthrough")
		end
	end,
})

minetest.register_chatcommand("isprot_dumpcalls",{
	params = "< on | off >",
	description = "Enables/disables sending chat messages to the entire server every time minetest.is_protected() or minetest.record_protection_violation() is called.",
	privs = {server = true},
	func = function(name,param)
		if param == "on" or param == "true" or param == "enable" then
			minetest.chat_send_player(name,"minetest.is_protected() / minetest.record_protection_violation() debugging "..minetest.colorize("#55ffff","enabled")..".")
			dumpcalls = true
		elseif param == "off" or param == "false" or param == "disable" then
			minetest.chat_send_player(name,"minetest.is_protected() / minetest.record_protection_violation() debugging "..minetest.colorize("#55ffff","disabled")..".")
			dumpcalls = false
		elseif param == "" or not param then
			minetest.chat_send_player(name,"minetest.is_protected() / minetest.record_protection_violation() debugging is currently "..(dumpcalls and "enabled" or "disabled")..".")
		else
			minetest.chat_send_player(name,minetest.colorize("#ff5555","Error: ").."Unrecognized mode. Valid options are: on, off")
		end
	end,
})

minetest.is_protected = function(pos,name)
	if state == "true" then
		if dumpcalls then minetest.chat_send_all(string.format("minetest.is_protected() called for %s at %d,%d,%d. Returning true due to override.",name,pos.x,pos.y,pos.z)) end
		return true
	elseif state == "false" then
		if dumpcalls then minetest.chat_send_all(string.format("minetest.is_protected() called for %s at %d,%d,%d. Returning false due to override.",name,pos.x,pos.y,pos.z)) end
		return false
	elseif state == "passthrough" then
		local ret = old_is_protected(pos,name)
		if dumpcalls then minetest.chat_send_all(string.format("minetest.is_protected() called for %s at %d,%d,%d. Existing function returned %s.",name,pos.x,pos.y,pos.z,ret)) end
		return ret
	end
end

minetest.register_on_protection_violation(function(pos,name)
	if dumpcalls then minetest.chat_send_all(string.format("minetest.record_protection_violation() called for %s at %d,%d,%d.",name,pos.x,pos.y,pos.z)) end
end)