aboutsummaryrefslogtreecommitdiff
path: root/builtin/common/chatcommands.lua
blob: c945e7bdb2926733dfecb72dbb8f8a979e3118e9 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
-- Minetest: builtin/common/chatcommands.lua

-- For server-side translations (if INIT == "game")
-- Otherwise, use core.gettext
local S = core.get_translator("__builtin")

core.registered_chatcommands = {}

function core.register_chatcommand(cmd, def)
	def = def or {}
	def.params = def.params or ""
	def.description = def.description or ""
	def.privs = def.privs or {}
	def.mod_origin = core.get_current_modname() or "??"
	core.registered_chatcommands[cmd] = def
end

function core.unregister_chatcommand(name)
	if core.registered_chatcommands[name] then
		core.registered_chatcommands[name] = nil
	else
		core.log("warning", "Not unregistering chatcommand " ..name..
			" because it doesn't exist.")
	end
end

function core.override_chatcommand(name, redefinition)
	local chatcommand = core.registered_chatcommands[name]
	assert(chatcommand, "Attempt to override non-existent chatcommand "..name)
	for k, v in pairs(redefinition) do
		rawset(chatcommand, k, v)
	end
	core.registered_chatcommands[name] = chatcommand
end

local function do_help_cmd(name, param)
	local function format_help_line(cmd, def)
		local cmd_marker = "/"
		if INIT == "client" then
			cmd_marker = "."
		end
		local msg = core.colorize("#00ffff", cmd_marker .. cmd)
		if def.params and def.params ~= "" then
			msg = msg .. " " .. def.params
		end
		if def.description and def.description ~= "" then
			msg = msg .. ": " .. def.description
		end
		return msg
	end
	if param == "" then
		local cmds = {}
		for cmd, def in pairs(core.registered_chatcommands) do
			if INIT == "client" or core.check_player_privs(name, def.privs) then
				cmds[#cmds + 1] = cmd
			end
		end
		table.sort(cmds)
		local msg
		if INIT == "game" then
			msg = S("Available commands: @1",
				table.concat(cmds, " ")) .. "\n"
				.. S("Use '/help <cmd>' to get more "
				.. "information, or '/help all' to list "
				.. "everything.")
		else
			msg = core.gettext("Available commands: ")
				.. table.concat(cmds, " ") .. "\n"
				.. core.gettext("Use '.help <cmd>' to get more "
				.. "information, or '.help all' to list "
				.. "everything.")
		end
		return true, msg
	elseif param == "all" then
		local cmds = {}
		for cmd, def in pairs(core.registered_chatcommands) do
			if INIT == "client" or core.check_player_privs(name, def.privs) then
				cmds[#cmds + 1] = format_help_line(cmd, def)
			end
		end
		table.sort(cmds)
		local msg
		if INIT == "game" then
			msg = S("Available commands:")
		else
			msg = core.gettext("Available commands:")
		end
		return true, msg.."\n"..table.concat(cmds, "\n")
	elseif INIT == "game" and param == "privs" then
		local privs = {}
		for priv, def in pairs(core.registered_privileges) do
			privs[#privs + 1] = priv .. ": " .. def.description
		end
		table.sort(privs)
		return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
	else
		local cmd = param
		local def = core.registered_chatcommands[cmd]
		if not def then
			local msg
			if INIT == "game" then
				msg = S("Command not available: @1", cmd)
			else
				msg = core.gettext("Command not available: ") .. cmd
			end
			return false, msg
		else
			return true, format_help_line(cmd, def)
		end
	end
end

if INIT == "client" then
	core.register_chatcommand("help", {
		params = core.gettext("[all | <cmd>]"),
		description = core.gettext("Get help for commands"),
		func = function(param)
			return do_help_cmd(nil, param)
		end,
	})
else
	core.register_chatcommand("help", {
		params = S("[all | privs | <cmd>]"),
		description = S("Get help for commands or list privileges"),
		func = do_help_cmd,
	})
end
n>, "default:ladder"); nimap->set(32, "default:lava_flowing"); nimap->set(33, "default:lava_source"); nimap->set(0x800, "default:dirt_with_grass"); nimap->set(0x801, "default:tree"); nimap->set(0x802, "default:leaves"); nimap->set(0x803, "default:dirt_with_grass_footsteps"); nimap->set(0x804, "default:mese"); nimap->set(0x805, "default:dirt"); nimap->set(0x806, "default:cloud"); nimap->set(0x807, "default:coalstone"); nimap->set(0x808, "default:wood"); nimap->set(0x809, "default:sand"); nimap->set(0x80a, "default:cobble"); nimap->set(0x80b, "default:steelblock"); nimap->set(0x80c, "default:glass"); nimap->set(0x80d, "default:mossycobble"); nimap->set(0x80e, "default:gravel"); nimap->set(0x80f, "default:sandstone"); nimap->set(0x810, "default:cactus"); nimap->set(0x811, "default:brick"); nimap->set(0x812, "default:clay"); nimap->set(0x813, "default:papyrus"); nimap->set(0x814, "default:bookshelf"); nimap->set(0x815, "default:jungletree"); nimap->set(0x816, "default:junglegrass"); nimap->set(0x817, "default:nyancat"); nimap->set(0x818, "default:nyancat_rainbow"); nimap->set(0x819, "default:apple"); nimap->set(0x820, "default:sapling"); // Static types nimap->set(CONTENT_IGNORE, "ignore"); nimap->set(CONTENT_AIR, "air"); } class NewNameGetter { public: NewNameGetter() { old_to_new["CONTENT_STONE"] = "default:stone"; old_to_new["CONTENT_WATER"] = "default:water_flowing"; old_to_new["CONTENT_TORCH"] = "default:torch"; old_to_new["CONTENT_WATERSOURCE"] = "default:water_source"; old_to_new["CONTENT_SIGN_WALL"] = "default:sign_wall"; old_to_new["CONTENT_CHEST"] = "default:chest"; old_to_new["CONTENT_FURNACE"] = "default:furnace"; old_to_new["CONTENT_LOCKABLE_CHEST"] = "default:locked_chest"; old_to_new["CONTENT_FENCE"] = "default:wooden_fence"; old_to_new["CONTENT_RAIL"] = "default:rail"; old_to_new["CONTENT_LADDER"] = "default:ladder"; old_to_new["CONTENT_LAVA"] = "default:lava_flowing"; old_to_new["CONTENT_LAVASOURCE"] = "default:lava_source"; old_to_new["CONTENT_GRASS"] = "default:dirt_with_grass"; old_to_new["CONTENT_TREE"] = "default:tree"; old_to_new["CONTENT_LEAVES"] = "default:leaves"; old_to_new["CONTENT_GRASS_FOOTSTEPS"] = "default:dirt_with_grass_footsteps"; old_to_new["CONTENT_MESE"] = "default:mese"; old_to_new["CONTENT_MUD"] = "default:dirt"; old_to_new["CONTENT_CLOUD"] = "default:cloud"; old_to_new["CONTENT_COALSTONE"] = "default:coalstone"; old_to_new["CONTENT_WOOD"] = "default:wood"; old_to_new["CONTENT_SAND"] = "default:sand"; old_to_new["CONTENT_COBBLE"] = "default:cobble"; old_to_new["CONTENT_STEEL"] = "default:steel"; old_to_new["CONTENT_GLASS"] = "default:glass"; old_to_new["CONTENT_MOSSYCOBBLE"] = "default:mossycobble"; old_to_new["CONTENT_GRAVEL"] = "default:gravel"; old_to_new["CONTENT_SANDSTONE"] = "default:sandstone"; old_to_new["CONTENT_CACTUS"] = "default:cactus"; old_to_new["CONTENT_BRICK"] = "default:brick"; old_to_new["CONTENT_CLAY"] = "default:clay"; old_to_new["CONTENT_PAPYRUS"] = "default:papyrus"; old_to_new["CONTENT_BOOKSHELF"] = "default:bookshelf"; old_to_new["CONTENT_JUNGLETREE"] = "default:jungletree"; old_to_new["CONTENT_JUNGLEGRASS"] = "default:junglegrass"; old_to_new["CONTENT_NC"] = "default:nyancat"; old_to_new["CONTENT_NC_RB"] = "default:nyancat_rainbow"; old_to_new["CONTENT_APPLE"] = "default:apple"; old_to_new["CONTENT_SAPLING"] = "default:sapling"; // Just in case old_to_new["CONTENT_IGNORE"] = "ignore"; old_to_new["CONTENT_AIR"] = "air"; } std::string get(const std::string &old) { std::map<std::string, std::string>::const_iterator i; i = old_to_new.find(old); if(i == old_to_new.end()) return ""; return i->second; } private: std::map<std::string, std::string> old_to_new; }; NewNameGetter newnamegetter; std::string content_mapnode_get_new_name(const std::string &oldname) { return newnamegetter.get(oldname); } content_t legacy_get_id(const std::string &oldname, INodeDefManager *ndef) { std::string newname = content_mapnode_get_new_name(oldname); if(newname == "") return CONTENT_IGNORE; content_t id; bool found = ndef->getId(newname, id); if(!found) return CONTENT_IGNORE; return id; }