forked from Sokomine/cottages
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.lua
328 lines (285 loc) · 7.49 KB
/
util.lua
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
local f = string.format
local util = {}
function util.player_can_use(pos, player)
if not (pos and minetest.is_player(player)) then
return false
end
local player_name = player:get_player_name()
local meta = minetest.get_meta(pos)
if meta:get_string("public") == "public" then
meta:set_int("public", 2)
end
local owner = meta:get_string("owner")
local public = meta:get_int("public")
if owner == player_name then
return true
elseif owner == "" or owner == " " or public == 1 then
return not minetest.is_protected(pos, player_name)
else
return true
end
end
function util.toggle_public(pos, sender)
local sender_name = sender:get_player_name()
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
if (owner == "" or owner == " ") and not minetest.is_protected(pos, sender_name) then
owner = sender_name
meta:set_string("owner", sender_name)
end
if meta:get_string("public") == "public" then
meta:set_int("public", 2)
end
local public = meta:get_int("public")
if public == 0 and owner == sender_name then
-- owner can switch private to protected
meta:set_int("public", 1)
return true
elseif public == 1 and not minetest.is_protected(pos, sender_name) then
-- player of area can switch protected to public
meta:set_int("public", 2)
return true
elseif public == 2 then
if owner == sender_name then
-- owner can switch public to private
meta:set_int("public", 0)
return true
elseif not minetest.is_protected(pos, sender_name) then
-- player of area can switch public to protected
meta:set_int("public", 1)
return true
end
end
return false
end
local function tokenize(s)
local tokens = {}
local i = 1
local j = 1
while true do
if s:sub(j, j) == "" then
if i < j then
table.insert(tokens, s:sub(i, j - 1))
end
return tokens
elseif s:sub(j, j):byte() == 27 then
if i < j then
table.insert(tokens, s:sub(i, j - 1))
end
i = j
local n = s:sub(i + 1, i + 1)
if n == "(" then
local m = s:sub(i + 2, i + 2)
local k = s:find(")", i + 3, true)
if m == "T" then
table.insert(tokens, {
type = "translation",
domain = s:sub(i + 4, k - 1),
})
elseif m == "c" then
table.insert(tokens, {
type = "color",
color = s:sub(i + 4, k - 1),
})
elseif m == "b" then
table.insert(tokens, {
type = "bgcolor",
color = s:sub(i + 4, k - 1),
})
else
error(("couldn't parse %s"):format(s))
end
i = k + 1
j = k + 1
elseif n == "F" then
table.insert(tokens, {
type = "start",
})
i = j + 2
j = j + 2
elseif n == "E" then
table.insert(tokens, {
type = "stop",
})
i = j + 2
j = j + 2
else
error(("couldn't parse %s"):format(s))
end
else
j = j + 1
end
end
end
local function parse(tokens, i, parsed)
parsed = parsed or {}
i = i or 1
while i <= #tokens do
local token = tokens[i]
if type(token) == "string" then
table.insert(parsed, token)
i = i + 1
elseif token.type == "color" or token.type == "bgcolor" then
table.insert(parsed, token)
i = i + 1
elseif token.type == "translation" then
local contents = {
type = "translation",
domain = token.domain,
}
i = i + 1
contents, i = parse(tokens, i, contents)
table.insert(parsed, contents)
elseif token.type == "start" then
local contents = {
type = "escape",
}
i = i + 1
contents, i = parse(tokens, i, contents)
table.insert(parsed, contents)
elseif token.type == "stop" then
i = i + 1
return parsed, i
else
error(("couldn't parse %s"):format(dump(token)))
end
end
return parsed, i
end
local function erase_after_newline(parsed, erasing)
local single_line_parsed = {}
for _, piece in ipairs(parsed) do
if type(piece) == "string" then
if not erasing then
if piece:find("\n") then
erasing = true
local single_line = piece:match("^([^\n]*)\n")
table.insert(single_line_parsed, single_line)
else
table.insert(single_line_parsed, piece)
end
end
elseif piece.type == "bgcolor" or piece.type == "color" then
table.insert(single_line_parsed, piece)
elseif piece.type == "escape" then
table.insert(single_line_parsed, erase_after_newline(piece, erasing))
elseif piece.type == "translation" then
local stuff = erase_after_newline(piece, erasing)
stuff.domain = piece.domain
table.insert(single_line_parsed, stuff)
else
error(("unknown type %s"):format(piece.type))
end
end
return single_line_parsed
end
local function unparse(parsed, parts)
parts = parts or {}
for _, part in ipairs(parsed) do
if type(part) == "string" then
table.insert(parts, part)
else
if part.type == "bgcolor" then
table.insert(parts, ("\27(b@%s)"):format(part.color))
elseif part.type == "color" then
table.insert(parts, ("\27(c@%s)"):format(part.color))
elseif part.domain then
table.insert(parts, ("\27(T@%s)"):format(part.domain))
unparse(part, parts)
table.insert(parts, "\27E")
else
table.insert(parts, "\27F")
unparse(part, parts)
table.insert(parts, "\27E")
end
end
end
return parts
end
function util.get_safe_short_description(item)
item = type(item) == "userdata" and item or ItemStack(item)
local description = item:get_description()
local tokens = tokenize(description)
local parsed = parse(tokens)
local single_line_parsed = erase_after_newline(parsed)
local single_line = table.concat(unparse(single_line_parsed), "")
return single_line
end
function util.resolve_item(item)
local item_stack = ItemStack(item)
local name = item_stack:get_name()
local seen = { [name] = true }
local alias = minetest.registered_aliases[name]
while alias do
name = alias
seen[name] = true
alias = minetest.registered_aliases[name]
if seen[alias] then
error(f("alias cycle on %s", name))
end
end
if minetest.registered_items[name] then
item_stack:set_name(name)
return item_stack:to_string()
end
end
-- https://github.com/minetest/minetest/blob/9fc018ded10225589d2559d24a5db739e891fb31/doc/lua_api.txt#L453-L462
function util.escape_texture(texturestring)
-- store in a variable so we don't return both rvs of gsub
local v = texturestring:gsub("[%^:]", {
["^"] = "\\^",
[":"] = "\\:",
})
return v
end
function util.table_size(t)
local size = 0
for _ in pairs(t) do
size = size + 1
end
return size
end
local function equals(a, b)
local t = type(a)
if t ~= type(b) then
return false
end
if t ~= "table" then
return a == b
elseif a == b then
return true
end
local size_a = 0
for key, value in pairs(a) do
if not equals(value, b[key]) then
return false
end
size_a = size_a + 1
end
return size_a == util.table_size(b)
end
util.equals = equals
if ItemStack().equals then
-- https://github.com/minetest/minetest/pull/12771
function util.items_equals(item1, item2)
item1 = type(item1) == "userdata" and item1 or ItemStack(item1)
item2 = type(item2) == "userdata" and item2 or ItemStack(item2)
return item1 == item2
end
else
function util.items_equals(item1, item2)
item1 = type(item1) == "userdata" and item1 or ItemStack(item1)
item2 = type(item2) == "userdata" and item2 or ItemStack(item2)
return equals(item1:to_table(), item2:to_table())
end
end
local has_stamina = cottages.has.stamina
local has_staminoid = cottages.has.staminoid
function util.exhaust_player(player, amount, reason)
if has_stamina then
stamina.exhaust_player(player, amount, reason)
elseif has_staminoid then
staminoid.exhaust(player, amount / 10, reason)
end
end
cottages.util = util