-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
94 lines (79 loc) · 2.59 KB
/
init.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
if not minetest.register_allow_player_inventory_action then
error("[upgrade_packs] This mod requires at least Minetest 5.0.0-dev")
end
upgrade_packs = {}
upgrade_packs.health_items = {}
upgrade_packs.breath_items = {}
upgrade_packs.translator = minetest.get_translator("upgrade_packs")
local modpath = minetest.get_modpath("upgrade_packs")
dofile(modpath .. "/api.lua")
dofile(modpath .. "/packs.lua")
if minetest.get_modpath("unified_inventory")
and not unified_inventory.sfinv_compat_layer then
dofile(modpath .. "/gui_unified_inventory.lua")
elseif minetest.get_modpath("sfinv") then
dofile(modpath .. "/gui_sfinv.lua")
else
dofile(modpath .. "/gui_plain.lua")
end
-- Cache items which are interesting for this mod
minetest.after(0, function()
local items = minetest.registered_items
local health_items = {}
local breath_items = {}
for name, def in pairs(items) do
local groups = def.groups or {}
if groups.upgrade_health
and groups.upgrade_health ~= 0 then
health_items[name] = groups.upgrade_health
end
if groups.upgrade_breath
and groups.upgrade_breath ~= 0 then
breath_items[name] = groups.upgrade_breath
end
end
upgrade_packs.health_items = health_items
upgrade_packs.breath_items = breath_items
end)
-- Hacky: Set the hp_max and breath_max value first
table.insert(minetest.registered_on_joinplayers, 1, function(player)
upgrade_packs.meta_to_inv(player)
upgrade_packs.update_player(player)
end)
minetest.register_on_leaveplayer(upgrade_packs.inv_to_meta)
minetest.register_on_item_eat(function(hp_change, replace_with_item, itemstack, user)
if hp_change == 0 then
return
end
-- Undo some of the wear when eating instead of dying
upgrade_packs.add_wear(user, "health", hp_change * -100)
end)
minetest.register_on_player_hpchange(function(player, hp_change, reason)
if hp_change >= 0 then
return hp_change
end
if reason.type == "drown" then
upgrade_packs.add_wear(player, "breath", hp_change * -2000)
else
upgrade_packs.add_wear(player, "health", hp_change * -200)
end
return hp_change
end, true)
minetest.register_allow_player_inventory_action(function(player, action, inv, data)
if data.to_list ~= "ugpacks" then
return -- Not interesting for this mod
end
local stack = inv:get_stack(data.from_list, data.from_index)
if upgrade_packs.health_items[stack:get_name()] then
return 1
end
if upgrade_packs.breath_items[stack:get_name()] then
return 1
end
return 0
end)
minetest.register_on_player_inventory_action(function(player, action, inv, data)
if data.to_list == "ugpacks" or data.from_list == "ugpacks" then
upgrade_packs.update_player(player)
end
end)