Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 3320fbd

Browse files
committed
Full backward compat with MT 0.4.17
1 parent 19fb5e2 commit 3320fbd

File tree

1 file changed

+62
-33
lines changed

1 file changed

+62
-33
lines changed

init.lua

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
craftguide = {}
22

3+
local CORE_VERSION = core.get_version().string
4+
CORE_VERSION = CORE_VERSION:match("[^%-]*"):gsub("%.", "")
5+
CORE_VERSION = tonumber(CORE_VERSION)
6+
37
-- Caches
48
local pdata = {}
59
local init_items = {}
@@ -28,13 +32,22 @@ local on_joinplayer = core.register_on_joinplayer
2832
local get_all_recipes = core.get_all_craft_recipes
2933
local register_command = core.register_chatcommand
3034
local get_player_by_name = core.get_player_by_name
35+
local slz, dslz = core.serialize, core.deserialize
3136
local on_mods_loaded = core.register_on_mods_loaded
3237
local on_leaveplayer = core.register_on_leaveplayer
33-
local serialize, deserialize = core.serialize, core.deserialize
3438
local on_receive_fields = core.register_on_player_receive_fields
3539

3640
local ESC = core.formspec_escape
37-
local S = core.get_translator("craftguide")
41+
local S = CORE_VERSION >= 500 and core.get_translator("craftguide") or
42+
function(...)
43+
local args = {...}
44+
local i = 1
45+
46+
return args[1]:gsub("@%d+", function()
47+
i = i + 1
48+
return args[i]
49+
end)
50+
end
3851

3952
local maxn, sort, concat, copy, insert =
4053
table.maxn, table.sort, table.concat, table.copy, table.insert
@@ -55,10 +68,6 @@ local WH_LIMIT = 8
5568
local XOFFSET = sfinv_only and 3.83 or 4.66
5669
local YOFFSET = sfinv_only and 6 or 6.6
5770

58-
local CORE_VERSION = core.get_version().string
59-
CORE_VERSION = match(CORE_VERSION, "[^%-]*"):gsub("%.", "")
60-
CORE_VERSION = tonumber(CORE_VERSION)
61-
6271
craftguide.background = "craftguide_bg_full.png#10"
6372

6473
local PNG = {
@@ -550,8 +559,9 @@ local function get_output_fs(fs, L)
550559
local tooltip = custom_recipe and custom_recipe.description or
551560
L.shapeless and S("Shapeless") or S("Cooking")
552561

553-
fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
554-
pos_x, pos_y, 0.5, 0.5, ESC(tooltip))
562+
if CORE_VERSION >= 500 then
563+
fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y, 0.5, 0.5, ESC(tooltip))
564+
end
555565
end
556566

557567
local arrow_X = L.rightest + (L.s_btn_size or 1.1)
@@ -862,25 +872,6 @@ local function search(data)
862872
data.items = filtered_list
863873
end
864874

865-
local function init_data(name)
866-
pdata[name] = {
867-
filter = "",
868-
pagenum = 1,
869-
items = init_items,
870-
items_raw = init_items,
871-
}
872-
end
873-
874-
local function reset_data(data)
875-
data.filter = ""
876-
data.pagenum = 1
877-
data.rnum = 1
878-
data.query_item = nil
879-
data.show_usages = nil
880-
data.recipes = nil
881-
data.items = data.items_raw
882-
end
883-
884875
local old_register_alias = core.register_alias
885876

886877
core.register_alias = function(old, new)
@@ -1006,7 +997,31 @@ local function get_init_items()
1006997
sort(init_items)
1007998
end
1008999

1009-
on_mods_loaded(get_init_items)
1000+
local function init_data(name)
1001+
local items = CORE_VERSION >= 500 and init_items or
1002+
(#init_items == 0 and get_init_items() or init_items)
1003+
1004+
pdata[name] = {
1005+
filter = "",
1006+
pagenum = 1,
1007+
items = items,
1008+
items_raw = items,
1009+
}
1010+
end
1011+
1012+
local function reset_data(data)
1013+
data.filter = ""
1014+
data.pagenum = 1
1015+
data.rnum = 1
1016+
data.query_item = nil
1017+
data.show_usages = nil
1018+
data.recipes = nil
1019+
data.items = data.items_raw
1020+
end
1021+
1022+
if CORE_VERSION >= 500 then
1023+
on_mods_loaded(get_init_items)
1024+
end
10101025

10111026
on_joinplayer(function(player)
10121027
local name = player:get_player_name()
@@ -1419,12 +1434,17 @@ if progressive_mode then
14191434
on_joinplayer(function(player)
14201435
PLAYERS = get_players()
14211436

1422-
local meta = player:get_meta()
14231437
local name = player:get_player_name()
14241438
local data = pdata[name]
14251439

1426-
data.inv_items = deserialize(meta:get_string("inv_items")) or {}
1427-
data.known_recipes = deserialize(meta:get_string("known_recipes")) or 0
1440+
if CORE_VERSION >= 500 then
1441+
local meta = player:get_meta()
1442+
data.inv_items = dslz(meta:get_string("inv_items")) or {}
1443+
data.known_recipes = dslz(meta:get_string("known_recipes")) or 0
1444+
else
1445+
data.inv_items = dslz(player:get_attribute("inv_items")) or {}
1446+
data.known_recipes = dslz(player:get_attribute("known_recipes")) or 0
1447+
end
14281448

14291449
data.hud = {
14301450
bg = player:hud_add({
@@ -1459,13 +1479,22 @@ if progressive_mode then
14591479
}
14601480

14611481
local function save_meta(player)
1462-
local meta = player:get_meta()
1482+
local meta
14631483
local name = player:get_player_name()
14641484
local data = pdata[name]
14651485

1486+
if CORE_VERSION >= 500 then
1487+
meta = player:get_meta()
1488+
end
1489+
14661490
for i = 1, #to_save do
14671491
local meta_name = to_save[i]
1468-
meta:set_string(meta_name, serialize(data[meta_name]))
1492+
1493+
if CORE_VERSION >= 500 then
1494+
meta:set_string(meta_name, slz(data[meta_name]))
1495+
else
1496+
player:set_attribute(meta_name, slz(data[meta_name]))
1497+
end
14691498
end
14701499
end
14711500

0 commit comments

Comments
 (0)