Skip to content

Commit fa4ef14

Browse files
authored
Truncate long area names, store previous owner separately (#4)
1 parent a79d56b commit fa4ef14

File tree

6 files changed

+71
-6
lines changed

6 files changed

+71
-6
lines changed

.luacheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ read_globals = {
1010
"AreaStore",
1111
"default",
1212
"mesecon",
13+
"utf8",
1314
table = { fields = { "copy", "getn", "indexof" } }
1415
}
1516

chatcommands.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local S = areas.S
22

3+
local sub8 = utf8.sub
34
local anticurse_exists = minetest.global_exists("chat_anticurse")
45

56
minetest.register_chatcommand("protect", {
@@ -22,6 +23,8 @@ minetest.register_chatcommand("protect", {
2223
end
2324
end
2425

26+
param = sub8(param, 1, areas.config.max_area_name_length)
27+
2528
minetest.log("action", "/protect invoked, owner="..name..
2629
" AreaName="..param..
2730
" StartPos="..minetest.pos_to_string(pos1)..
@@ -112,6 +115,8 @@ minetest.register_chatcommand("add_owner", {
112115
end
113116
end
114117

118+
areaName = sub8(param, 1, areas.config.max_area_name_length)
119+
115120
minetest.log("action", name.." runs /add_owner. Owner = "..ownerName..
116121
" AreaName = "..areaName.." ParentID = "..pid..
117122
" StartPos = "..pos1.x..","..pos1.y..","..pos1.z..
@@ -125,7 +130,7 @@ minetest.register_chatcommand("add_owner", {
125130
end
126131

127132
local id = areas:add(ownerName, areaName, pos1, pos2, pid)
128-
areas.areas[id].name = areaName .. " " .. S("(by @1)", name)
133+
areas.areas[id].prev_owner = name
129134
areas:save()
130135

131136
minetest.chat_send_player(ownerName,
@@ -161,7 +166,10 @@ minetest.register_chatcommand("rename_area", {
161166
end
162167
end
163168

169+
newName = sub8(newName, 1, areas.config.max_area_name_length)
170+
164171
areas.areas[id].name = newName
172+
areas.areas[id].prev_owner = nil
165173
areas:save()
166174
return true, S("Area renamed.")
167175
end
@@ -302,7 +310,7 @@ minetest.register_chatcommand("change_owner", {
302310
.." or is not owned by you.", id)
303311
end
304312
areas.areas[id].owner = newOwner
305-
areas.areas[id].name = areas.areas[id].name .. " " .. S("(by @1)", name)
313+
areas.areas[id].prev_owner = name
306314
areas:save()
307315
minetest.chat_send_player(newOwner,
308316
S("@1 has given you control over the area \"@2\" (ID @3).",

hud.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ areas.hud = {}
66

77
local vround = vector.round
88
local tconcat, tinsert = table.concat, table.insert
9+
local sub8 = utf8.sub
910
local creative_mode = minetest.settings:get_bool("creative_mode")
1011

12+
local function trim_area_name(name)
13+
return sub8(name, 1, areas.config.max_area_name_length)
14+
end
15+
1116
local function createAreaString(area, id)
12-
local parts = {"🛡️ ", area.name, " [", id, "] (", area.owner, ")"}
17+
local parts = {"🛡️ ", trim_area_name(area.name), " [", id, "] (", area.owner, ")"}
18+
if area.prev_owner then
19+
tinsert(parts, 3, " " .. S("(by @1)", area.prev_owner))
20+
end
21+
1322
if area.open then
1423
tinsert(parts, " [" .. S("Open") .. "]")
1524
end
@@ -25,7 +34,7 @@ local function createAreaString(area, id)
2534
end
2635
end
2736

28-
return tconcat(parts):trim()
37+
return tconcat(parts)
2938
end
3039

3140
local function updateHud(player, name, pos)

internal.lua

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,49 @@ end
99

1010
-- Save the areas table to a file
1111
function areas:save()
12-
local datastr = minetest.write_json(self.areas, true)
12+
-- HACK: Add the version code at the end so that areas can be downgraded
13+
-- without issue, minetest.parse_json ignores extra data at the end of the
14+
-- string.
15+
local datastr = minetest.write_json(self.areas, true) .. "\nv2"
1316
if not datastr then
1417
minetest.log("error", "[areas] Failed to serialize area data!")
1518
return
1619
end
1720
return minetest.safe_file_write(self.config.filename, datastr)
1821
end
1922

23+
local function migrate_by_strings(self)
24+
local migrated = 0
25+
for _, area in pairs(self.areas) do
26+
-- Search without a pattern (the "true" argument) as it is much faster
27+
local position = area.name:find("\27(T@areas)", 1, true)
28+
if position then
29+
-- Parse the "(by <name>)" suffix and store in the "prev_owner" field
30+
if not area.prev_owner then
31+
area.prev_owner = area.name:match("\27%(T@areas%)%(by \27F([A-Za-z0-9_%-]-)\27E%)\27E$")
32+
end
33+
34+
-- Remove the translation escape sequence from the area name
35+
area.name = area.name:sub(1, position - 1):gsub(" $", "")
36+
37+
migrated = migrated + 1
38+
end
39+
40+
-- Remove broken by strings
41+
position = area.name:find(" (by ", 1, true)
42+
if position then
43+
area.name = area.name:sub(1, position - 1)
44+
end
45+
end
46+
47+
if migrated > 0 then
48+
minetest.log("action", "[areas] Migrated " .. migrated ..
49+
" \"(by <player>)\" strings in area names")
50+
51+
self:save()
52+
end
53+
end
54+
2055
-- Load the areas table from the save file
2156
function areas:load()
2257
local file, err = io.open(self.config.filename, "r")
@@ -25,7 +60,12 @@ function areas:load()
2560
return err
2661
end
2762
local data = file:read("*a")
63+
local need_migration = true
2864
if data:sub(1, 1) == "[" then
65+
if data:sub(-3) == "\nv2" then
66+
data = data:sub(1, -4)
67+
need_migration = false
68+
end
2969
self.areas, err = minetest.parse_json(data)
3070
else
3171
self.areas, err = minetest.deserialize(data)
@@ -38,6 +78,9 @@ function areas:load()
3878
tostring(err))
3979
end
4080
file:close()
81+
if need_migration then
82+
migrate_by_strings(self)
83+
end
4184
self:populateStore()
4285
end
4386

settings.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ end
2727
-- Settings --
2828
--------------
2929

30-
setting("string", "filename", world_path.."/areas.dat")
30+
setting("string", "filename", world_path.."/areas.dat")
3131
setting("boolean", "pvp_by_default", false)
32+
setting("number", "max_area_name_length", 40)
3233

3334
-- Allow players with a privilege create their own areas
3435
-- within the maximum size and number.

settingtypes.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# Turn on PvP everywhere
77
areas.pvp_by_default (PvP by default) bool false
88

9+
# Maximum length of the area name
10+
max_area_name_length (max area name length) int 40
11+
912
# Allow players with a privilege create their own areas using /protect
1013
# within the specified size and amount limits.
1114
areas.self_protection (Self protection) bool false

0 commit comments

Comments
 (0)