forked from OgelGames/bones
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventories.lua
More file actions
113 lines (106 loc) · 2.61 KB
/
inventories.lua
File metadata and controls
113 lines (106 loc) · 2.61 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
bones.registered_inventories = {}
function bones.register_inventory(id, def)
if type(id) ~= "string" then
core.log("error", "[bones] Invalid call to bones.register_inventory.")
return
end
bones.registered_inventories[id] = def or {
has_items = function(player)
return not player:get_inventory():is_empty(id)
end,
take_items = function(player)
local inv = player:get_inventory()
local items = inv:get_list(id)
inv:set_list(id, {})
return items
end,
restore_items = function(player, items)
local player_inv = player:get_inventory()
if player_inv:is_empty(id) then
player_inv:set_list(id, items)
return {}
end
local list = player_inv:get_list(id)
for i, stack in ipairs(list) do
if items[i] and stack:is_empty() then
list[i], items[i] = items[i], list[i]
end
end
player_inv:set_list(id, list)
for i, stack in ipairs(items) do
if not stack:is_empty() then
items[i] = player_inv:add_item(id, stack)
end
end
return items
end
}
end
local function is_list_empty(list)
for _,stack in ipairs(list) do
if not stack:is_empty() then
return false
end
end
return true
end
function bones.has_any_items(player)
for _,inv in pairs(bones.registered_inventories) do
if inv.has_items(player) then
return true
end
end
return false
end
function bones.take_all_items(player)
local inv_lists = {}
for id, inv in pairs(bones.registered_inventories) do
if inv.has_items(player) then
inv_lists[id] = inv.take_items(player)
end
end
return inv_lists
end
function bones.restore_all_items(player, inv_lists)
local unknown_lists = {}
for id, list in pairs(inv_lists) do
local inv = bones.registered_inventories[id]
if inv then
inv_lists[id] = inv.restore_items(player, list)
if is_list_empty(inv_lists[id]) then
inv_lists[id] = nil
end
else
unknown_lists[id] = list
end
end
-- Dump items in any unknown lists into the main inventory
if next(unknown_lists) then
local player_inv = player:get_inventory()
for id, list in pairs(unknown_lists) do
for i, stack in ipairs(list) do
if not stack:is_empty() then
list[i] = player_inv:add_item("main", stack)
end
end
if is_list_empty(list) then
inv_lists[id] = nil
end
end
end
return next(inv_lists) == nil
end
function bones.add_all_items(player, inv_lists)
local player_inv = player:get_inventory()
for id, list in pairs(inv_lists) do
for i, stack in ipairs(list) do
if not stack:is_empty() then
list[i] = player_inv:add_item("main", stack)
end
end
if is_list_empty(list) then
inv_lists[id] = nil
end
end
return next(inv_lists) == nil
end