-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
199 lines (165 loc) · 6.07 KB
/
Copy pathinit.lua
File metadata and controls
199 lines (165 loc) · 6.07 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
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
gequip = {}
gequip.types = {}
-- Register a type of equipment.
function gequip.register_type(name, def)
def = b.t.combine({
-- Human readable description.
description = "?",
-- Maximum number of equipment in the inventory.
slots = 1,
-- Inventory list name.
list_name = "gequip:list_" .. name,
-- Default definition of individual equipment.
-- Defaults below.
defaults = {},
}, def)
def.defaults = b.t.combine({
}, def.defaults)
function def.on_player_inventory_action(player, action, inv, info)
if (action == "move" and (info.to_list == def.list_name or info.from_list == def.list_name)) or (action == "put" and info.listname == def.list_name) or (action == "take" and info.listname == def.list_name) then
gequip.refresh(player)
return
end
end
function def.allow_player_inventory_action(player, action, inv, info)
local stack
if action == "move" and info.to_list == def.list_name and info.from_list ~= def.list_name then
stack = ItemStack(player:get_inventory():get_list(info.from_list)[info.from_index])
stack:set_count(info.count)
elseif action == "put" and info.listname == def.list_name then
stack = info.stack
else
return nil
end
-- Invalid items can't be inserted.
if stack:get_definition()._eqtype ~= name then
return 0
end
return nil
end
minetest.register_on_player_inventory_action(def.on_player_inventory_action)
minetest.register_allow_player_inventory_action(def.allow_player_inventory_action)
gequip.types[name] = def
end
minetest.register_on_joinplayer(function(player)
for _,def in pairs(gequip.types) do
player:get_inventory():set_size(def.list_name, def.slots)
end
gequip.refresh(player)
end)
gequip.actions = {}
function gequip.register_action(name, def)
gequip.actions[name] = b.t.combine({
-- State is a table for arbitrary data storage between adds.
-- State is shared between all actions, use a unique sub key.
init = function(state) end,
-- Add an item's equipment def to the state.
add = function(state, eqdef, stack) end,
-- Apply the state to a player.
apply = function(state, player) end,
}, def)
end
gequip.eqdef_inits = {}
-- Register init function.
-- Passed static eqdef and stack.
function gequip.register_eqdef_init(func)
table.insert(gequip.eqdef_inits, func)
end
-- Get the eqdef of a stack.
-- Table is deep copied and can be modified freely.
function gequip.get_eqdef(stack, skip_meta)
local def = stack:get_definition()
local typedef = gequip.types[def._eqtype]
local eqdef_static = b.t.combine(typedef.defaults, def._eqdef or {})
for _,func in ipairs(gequip.eqdef_inits) do
func(eqdef_static, stack)
end
local metadef = (stack:get_meta():contains("eqdef") and not skip_meta) and minetest.deserialize(stack:get_meta():get_string("eqdef")) or {}
return b.t.combine(table.copy(eqdef_static), metadef)
end
-- Compile a gequip state from a list of ItemStacks.
function gequip.compile(items)
local state = {}
for _,action in pairs(gequip.actions) do
action.init(state)
end
for _,stack in ipairs(items) do
if stack:get_count() > 0 then
local eqdef = gequip.get_eqdef(stack)
for _,action in pairs(gequip.actions) do
action.add(state, eqdef, stack)
end
end
end
return state
end
-- Get a list of all items that should apply gequip actions to the player.
-- Override in other mods to provide more slots.
function gequip.get_items(player)
local items = {}
for _,type in pairs(gequip.types) do
for _,stack in ipairs(player:get_inventory():get_list(type.list_name)) do
if not stack:is_empty() and gequip.types[stack:get_definition()._eqtype] then
table.insert(items, stack)
end
end
end
return items
end
-- Apply all equipment to the player.
function gequip.refresh(player)
local items = gequip.get_items(player)
local state = gequip.compile(items)
for _,action in pairs(gequip.actions) do
action.apply(state, player)
end
end
gequip.DELEGATE = "gequip:special_delegate"
minetest.register_on_joinplayer(function(player)
player:get_inventory():set_size(gequip.DELEGATE, 1)
-- Ensure delegate inventory is clear.
player:get_inventory():set_list(gequip.DELEGATE, {})
end)
minetest.register_allow_player_inventory_action(function(player, action, inv, info)
local stack
if action == "move" and info.to_list == gequip.DELEGATE and info.from_list ~= gequip.DELEGATE then
stack = ItemStack(player:get_inventory():get_list(info.from_list)[info.from_index])
stack:set_count(info.count)
elseif action == "put" and info.listname == gequip.DELEGATE then
stack = info.stack
else
return nil
end
for type,def in pairs(gequip.types) do
if player:get_inventory():room_for_item(def.list_name, stack) and (def.allow_player_inventory_action(player, "put", player:get_inventory(), {stack = stack, listname = def.list_name}) or 1) > 0 then
return 1
end
end
return 0
end)
minetest.register_on_player_inventory_action(function(player, action, inv, info)
local stack
if action == "move" and info.to_list == gequip.DELEGATE then
stack = ItemStack(player:get_inventory():get_list(info.to_list)[info.to_index])
stack:set_count(info.count)
elseif action == "put" and info.listname == gequip.DELEGATE then
stack = info.stack
else
return
end
local def = gequip.types[stack:get_definition()._eqtype]
player:get_inventory():set_list(gequip.DELEGATE, {})
player:get_inventory():add_item(def.list_name, stack)
def.on_player_inventory_action(player, "put", player:get_inventory(), {stack = stack, listname = def.list_name})
end)
-- On use callback, will equip item if possible when item is used.
function gequip.on_use(itemstack, player)
local def = itemstack:get_definition()
local type_def = gequip.types[def._eqtype]
if type_def and player:get_inventory():room_for_item(type_def.list_name, itemstack) and (type_def.allow_player_inventory_action(player, "put", player:get_inventory(), {stack = itemstack, listname = type_def.list_name}) or 1) > 0 then
player:get_inventory():add_item(type_def.list_name, itemstack)
type_def.on_player_inventory_action(player, "put", player:get_inventory(), {stack = itemstack, listname = type_def.list_name})
itemstack:take_item()
end
return itemstack
end