aboutsummaryrefslogtreecommitdiff
path: root/builtin/common/chatcommands.lua
blob: 52edda659baab0a89ede36a09b6c45faf6877641 (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
-- Minetest: builtin/common/chatcommands.lua

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 cmd_marker = "/"

local function gettext(...)
	return ...
end

local function gettext_replace(text, replace)
	return text:gsub("$1", replace)
end


if INIT == "client" then
	cmd_marker = "."
	gettext = core.gettext
	gettext_replace = fgettext_ne
end

local function do_help_cmd(name, param)
	local function format_help_line(cmd, def)
		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)
		return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n"
				.. gettext_replace("Use '$1help <cmd>' to get more information,"
				.. " or '$1help all' to list everything.", cmd_marker)
	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)
		return true, gettext("Available commands:").."\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, "Available privileges:\n"..table.concat(privs, "\n")
	else
		local cmd = param
		local def = core.registered_chatcommands[cmd]
		if not def then
			return false, gettext("Command not available: ")..cmd
		else
			return true, format_help_line(cmd, def)
		end
	end
end

if INIT == "client" then
	core.register_chatcommand("help", {
		params = gettext("[all | <cmd>]"),
		description = gettext("Get help for commands"),
		func = function(param)
			return do_help_cmd(nil, param)
		end,
	})
else
	core.register_chatcommand("help", {
		params = "[all | privs | <cmd>]",
		description = "Get help for commands or list privileges",
		func = do_help_cmd,
	})
end
lass="hl opt">/2, screensize.X/2 + 580/2, screensize.Y/2 + 300/2 ); DesiredRect = rect; recalculateAbsolutePosition(false); v2s32 size = rect.getSize(); v2s32 topleft = v2s32(10+80, 10+70); /* Add stuff */ { core::rect<s32> rect(0, 0, 100, 20); rect += v2s32(0, 5) + topleft; wchar_t* text = wgettext("World name"); Environment->addStaticText(text, rect, false, true, this, -1); delete[] text; } { core::rect<s32> rect(0, 0, 300, 30); rect = rect + v2s32(100, 0) + topleft; gui::IGUIElement *e = Environment->addEditBox(name.c_str(), rect, true, this, GUI_ID_NAME_INPUT); Environment->setFocus(e); irr::SEvent evt; evt.EventType = EET_KEY_INPUT_EVENT; evt.KeyInput.Key = KEY_END; evt.KeyInput.PressedDown = true; e->OnEvent(evt); } { core::rect<s32> rect(0, 0, 100, 20); rect += v2s32(0, 40+5) + topleft; wchar_t* text = wgettext("Game"); Environment->addStaticText(text, rect, false, true, this, -1); delete[] text; } { core::rect<s32> rect(0, 0, 300, 80); rect += v2s32(100, 40) + topleft; gui::IGUIListBox *e = Environment->addListBox(rect, this, GUI_ID_GAME_LISTBOX); e->setDrawBackground(true); for(u32 i=0; i<m_games.size(); i++){ std::wostringstream os(std::ios::binary); os<<narrow_to_wide(m_games[i].name).c_str(); os<<L" ["; os<<narrow_to_wide(m_games[i].id).c_str(); os<<L"]"; e->addItem(os.str().c_str()); } e->setSelected(m_initial_game_i); } changeCtype(""); { core::rect<s32> rect(0, 0, 120, 30); rect = rect + v2s32(170, 140) + topleft; wchar_t* text = wgettext("Create"); Environment->addButton(rect, this, GUI_ID_CREATE, text); delete[] text; } { core::rect<s32> rect(0, 0, 120, 30); rect = rect + v2s32(300, 140) + topleft; wchar_t* text = wgettext("Cancel"); Environment->addButton(rect, this, GUI_ID_CANCEL, text); delete [] text; } changeCtype("C"); } void GUICreateWorld::drawMenu() { gui::IGUISkin* skin = Environment->getSkin(); if (!skin) return; video::IVideoDriver* driver = Environment->getVideoDriver(); video::SColor bgcolor(140,0,0,0); driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect); gui::IGUIElement::draw(); } void GUICreateWorld::acceptInput() { if(m_dest) { int selected = -1; { gui::IGUIElement *e = getElementFromId(GUI_ID_GAME_LISTBOX); if(e != NULL && e->getType() == gui::EGUIET_LIST_BOX) selected = ((gui::IGUIListBox*)e)->getSelected(); } std::wstring name; { gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT); if(e != NULL) name = e->getText(); } if(selected != -1 && name != L"") m_dest->accepted(name, m_games[selected].id); delete m_dest; m_dest = NULL; } } bool GUICreateWorld::OnEvent(const SEvent& event) { if(event.EventType==EET_KEY_INPUT_EVENT) { if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown) { quitMenu(); return true; } if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown) { acceptInput(); quitMenu(); return true; } } if(event.EventType==EET_GUI_EVENT) { if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) { if(!canTakeFocus(event.GUIEvent.Element)) { dstream<<"GUICreateWorld: Not allowing focus change." <<std::endl; // Returning true disables focus change return true; } } bool accept_input = false; if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED){ switch(event.GUIEvent.Caller->getID()){ case GUI_ID_CANCEL: quitMenu(); return true; break; case GUI_ID_CREATE: accept_input = true; break; } } if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER){ switch(event.GUIEvent.Caller->getID()){ case GUI_ID_NAME_INPUT: accept_input = true; break; } } if(accept_input){ acceptInput(); quitMenu(); // quitMenu deallocates menu return true; } } return Parent ? Parent->OnEvent(event) : false; }