-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
182 lines (159 loc) · 6.23 KB
/
init.lua
File metadata and controls
182 lines (159 loc) · 6.23 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
--[[
chat_exporter - Log the chat messages received for translation
License: 3-clause BSD (see https://opensource.org/licenses/BSD-3-Clause)
Author: Hubert Tournier
--]]
local mod_name = assert(core.get_current_modname())
local mod_storage = core.get_mod_storage()
local chat_messages_allowed = true
local logging_allowed = true
local action_log_level = false
local server_name = ""
local server_language = ""
local player_name = ""
local player_language = ""
function log(message)
-- Log a message and send a return code depending on the method used
if action_log_level then
core.log("action", message)
return false -- resume normal processing
else
print(message)
return true -- interrupt normal processing (prevent echoing the message twice)
end
end
function is_a_language_code(input)
-- Test if the input is a possible ISO 639-1 language code (with Google Translate extensions such as "zh-CN")
local function is_letter(character)
return (character >= 'a' and character <= 'z') or (character >= 'A' and character <= 'Z')
end
for character in input:gmatch(".") do
if not (is_letter(character) or character == "-") then
return false
end
end
return true
end
core.register_on_mods_loaded(function()
-- Check restrictions on client-side mods usage
local csm_restrictions = core.get_csm_restrictions()
if csm_restrictions.load_client_mods then
print(core.colorize("red", "Loading client-side mods is disabled by the server"))
else
if csm_restrictions.chat_messages then
chat_messages_allowed = false
print(core.colorize("orange", "Client-side mod '" .. mod_name .. "' loaded but server restricts receiving chat messages"))
else
-- Get the log level configuration
local debug_log_level = core.settings:get("debug_log_level")
if debug_log_level then
if debug_log_level == "" then
logging_allowed = false
print(core.colorize("orange", "Client-side mod '" .. mod_name .. "' loaded but server restricts logging"))
elseif debug_log_level == "action" or debug_log_level == "info" or debug_log_level == "verbose" or debug_log_level == "trace" then
action_log_level = true
print(core.colorize("palegreen", "Client-side mod '" .. mod_name .. "' loaded. Logging to debug.txt"))
else
print(core.colorize("palegreen", "Client-side mod '" .. mod_name .. "' loaded. Printing to debug.txt"))
end
else
logging_allowed = false
print(core.colorize("orange", "Client-side mod '" .. mod_name .. "' loaded but server restricts logging"))
end
end
end
-- Get the server name and (if defined) the server language
local server_info = core.get_server_info()
server_name = server_info.address .. ":" .. server_info.port
log("§# <" .. server_name .. "> Server gaming session started")
server_language = mod_storage:get_string(server_name .. "_lang")
if server_language ~= "" then
log("§>" .. server_language .. " <" .. server_name .. "> Server target language initialized")
end
end)
-- Wait for core.localplayer initialization
for i=1,10 do
core.after(i, function()
if player_name == "" and core.localplayer then
player_name = core.localplayer:get_name()
player_language = mod_storage:get_string(player_name .. "_lang")
if player_language == "" then
local gettext_locale = ""
gettext_locale, player_language = core.get_language()
end
log("§<" .. player_language .. " <" .. player_name .. "> Player target language initialized")
end
end)
end
core.register_on_receiving_chat_message(function(message)
-- Process chat messages
if chat_messages_allowed and logging_allowed then
message = core.strip_colors(message)
return log("§ " .. message)
end
return false -- resume normal processing
end)
core.register_on_modchannel_message(function(channel, sender, message)
-- Process channel messages
if chat_messages_allowed and logging_allowed then
message = core.strip_colors(message)
log("§" .. channel .. " <" .. sender .. "> " .. message)
end
end)
core.register_chatcommand("tr", {
-- Chat command for requesting a translation
params = ".tr LANG MESSAGE",
description = "Request the translation of a message in the specified target language",
func = function(param)
param = string.trim(param)
local words = string.split(param, " ")
if #words < 2 or not is_a_language_code(words[1]) then
print("ERROR: usage: .tr LANG MESSAGE (where LANG is an ISO 639-1 language code)")
return false
end
if chat_messages_allowed and logging_allowed then
-- The first parameter is supposed to be an ISO 639-1 language code
-- See https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes
-- The other ones form the message to translate
local message = string.gsub(param, words[1] .. " ", "", 1)
log("§?" .. words[1] .. " <" .. player_name .. "> " .. message)
end
return true
end
})
core.register_chatcommand("lang", {
-- Chat command for defining the player's target language
params = ".lang LANG",
description = "Define player's target language",
func = function(param)
param = string.trim(param)
local words = string.split(param, " ")
if #words ~= 1 or not is_a_language_code(words[1]) then
print("ERROR: usage: .lang LANG (where LANG is an ISO 639-1 language code)")
return false
end
mod_storage:set_string(player_name .. "_lang", words[1])
if chat_messages_allowed and logging_allowed then
log("§<" .. words[1] .. " <" .. player_name .. "> Player target language redefined")
end
return true
end
})
core.register_chatcommand("slang", {
-- Chat command for defining the server's target language
params = ".slang LANG",
description = "Define server's target language",
func = function(param)
param = string.trim(param)
local words = string.split(param, " ")
if #words ~= 1 or not is_a_language_code(words[1]) then
print("ERROR: usage: .slang LANG (where LANG is an ISO 639-1 language code)")
return false
end
mod_storage:set_string(server_name .. "_lang", words[1])
if chat_messages_allowed and logging_allowed then
log("§>" .. words[1] .. " <" .. server_name .. "> Server target language redefined")
end
return true
end
})