~jadedctrl/gem-xwx-moe

~jadedctrl/gem-xwx-moe/gemujo_ludo/mods.niaj/fasado/subtitles/chatcommands.lua
 ..
0 --[[
1 Subtitles — adds subtitles to Minetest.
2
3 Copyright © 2022‒2023, Silver Sandstone <@SilverSandstone@craftodon.social>
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
22 ]]
23
24
25 --- Provides the `/subtitles` command.
26
27
28 local S = subtitles.S;
29
30
31 local usage = S('[on|off|modes|footsteps|version|help|<mode>]');
32 local help =
33 {
34 '/subtitles ' .. usage,
35 '',
36 'on ' .. S('Enables subtitles.'),
37 'off ' .. S('Disables subtitles.'),
38 'modes ' .. S('Lists available display modes.'),
39 'footsteps ' .. S('Toggles subtitles for footsteps.'),
40 'version ' .. S('Shows version information.'),
41 'help ' .. S('Shows this help message.'),
42 '<mode> ' .. S('Sets the display mode.'),
43 };
44
45 --- Runs the `/subtitles` command.
46 -- @param name The username of the player running the command.
47 -- @param param The command parameter string.
48 -- @return Success boolean.
49 function subtitles.run_chatcommand(name, param)
50 local function _chat(message)
51 minetest.chat_send_player(name, message);
52 end;
53
54 param = param:trim();
55 local agent = subtitles.get_agent(name);
56 if param == '' then
57 -- /subtitles
58 subtitles.show_menu(name);
59
60 elseif param == 'on' then
61 -- /subtitles on
62 agent:set_enabled(true);
63 _chat(S('Subtitles enabled.'));
64
65 elseif param == 'off' then
66 -- /subtitles off
67 agent:set_enabled(false);
68 _chat(S('Subtitles disabled.'));
69
70 elseif param == 'modes' then
71 -- /subtitles modes
72 _chat(S('Available display modes:'));
73 for __, impl in ipairs(subtitles.SubtitleDisplay.implementations) do
74 _chat(('%-12s%s'):format(impl.NAME, impl.DESCRIPTION));
75 end;
76
77 elseif param == 'footsteps' then
78 -- /subtitles footsteps
79 agent:toggle_footsteps_enabled();
80 _chat(agent:get_footsteps_enabled() and S('Footsteps enabled.') or S('Footsteps disabled.'));
81
82 elseif param == 'version' then
83 -- /subtitles version
84 _chat(subtitles.ABOUT);
85
86 elseif param == 'help' or param == '--help' or param == '-h' then
87 -- /subtitles help
88 for __, line in ipairs(help) do
89 _chat(line);
90 end;
91
92 elseif subtitles.SubtitleDisplay.get_by_name(param) then
93 -- /subtitles <mode>
94 agent:set_display_name(param);
95 _chat(S('Set display mode: @1', agent:get_display().TITLE));
96
97 else
98 -- ☹
99 _chat(S('Invalid option: ‘@1’.', param));
100 _chat(S('Usage: /subtitles @1', usage));
101 return false;
102 end;
103
104 return true;
105 end;
106
107 minetest.register_chatcommand('subtitles',
108 {
109 description = S('Manages your subtitle preferences.');
110 params = usage;
111 func = subtitles.run_chatcommand;
112 });