-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.lua
265 lines (237 loc) · 9.27 KB
/
create.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
-- SPDX-License-Identifier: AGPL-3.0-only
-- Copyright © 2023 Corp Por LTD
Create = {}
Create.Custom = {}
Create.Extended = {}
Create.Stack = {}
Create.Temp = {}
Create.CustomTemp = {}
Create.OnCreateSuccessHook = function(obj, template)
--SetItemTooltip(obj, template)
end
--- Create a template at location
-- @param template
-- @param loc - location to create at
-- @param cb - function(obj) callback
Create.AtLoc = function(template, loc, cb)
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then
Object.Decay(obj)
Create.OnCreateSuccessHook(obj, template)
end
if ( cb ) then cb(success and obj or nil) end
end)
CreateObj(template, loc, id)
end
--- Create a template on a mobile's equipment
-- @param template
-- @param mobile - mobile to created equipped object on
-- @param cb - function(obj) callback
Create.Equipped = function(template, mobile, cb)
if ( mobile == nil or not mobile:IsValid() ) then
if ( cb ) then cb(nil) end
LuaDebugCallStack("[Create.Equipped] invalid mobile provided")
return
end
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then Create.OnCreateSuccessHook(obj, template) end
if ( cb ) then cb(success and obj or nil) end
end)
CreateEquippedObj(template, mobile, id)
end
--- Create a custom template at location
-- @param template
-- @param loc - location to create at
-- @param data - Template Data (GetTemplateData)
-- @param cb - function(obj) callback
Create.Custom.AtLoc = function(template, data, loc, cb)
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then
Object.Decay(obj)
Create.OnCreateSuccessHook(obj, template)
end
if ( cb ) then cb(success and obj or nil) end
end)
CreateCustomObj(template, data, loc, id)
end
Create.Extended.AtLoc = function(template, loc, rot, scale, cb)
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then
Object.Decay(obj)
Create.OnCreateSuccessHook(obj, template)
end
if ( cb ) then cb(success and obj or nil) end
end)
CreateObjExtended(template, nil, loc, rot, scale, id)
end
--- Create a template in a container
-- @param template
-- @param container - container gameObj
-- @param containerloc - (optional) location in container
-- @param cb - function(obj) callback
Create.InContainer = function(template, container, containerloc, cb)
if ( container == nil or not container:IsValid() ) then
if ( cb ) then cb(nil) end
LuaDebugCallStack("[Create.InContainer] container mobile provided")
return
end
if not( containerloc ) then
containerloc = Container.NextEmptySlot(container)
if ( containerloc == nil ) then
if ( cb ) then cb(nil, "full") end
return
end
end
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then Create.OnCreateSuccessHook(obj, template) end
if ( cb ) then cb(success and obj or nil) end
end)
CreateObjInContainer(template, container, containerloc, id)
end
--- Create a template in a container
-- @param template
-- @param container - container gameObj
-- @param containerloc - (optional) location in container
-- @param data - Template Data (GetTemplateData)
-- @param cb - function(obj) callback
Create.Custom.InContainer = function(template, data, container, containerloc, cb)
if not( containerloc ) then
containerloc = Container.NextEmptySlot(container)
if ( containerloc == nil ) then
if ( cb ) then cb(nil, "full") end
return
end
end
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then Create.OnCreateSuccessHook(obj, template) end
if ( cb ) then cb(success and obj or nil) end
end)
CreateCustomObjInContainer(template, data, container, containerloc, id)
end
--- Create a template in a mobile's backpack
-- @param template
-- @param mobile - mobileObj
-- @param containerloc - (optional) location in container
-- @param cb - function(obj) callback
Create.InBackpack = function(template, mobile, containerloc, cb)
local backpack = Backpack.Get(mobile)
if ( backpack == nil ) then
LuaDebugCallStack("Mobile has no backpack.")
if ( cb ) then cb(nil, "nobackpack") end
return
end
Create.InContainer(template, backpack, containerloc, cb)
end
--- Create a template at a location, assigning StackCount before creation
-- @param template
-- @param count - stack count
-- @param loc - location in world
-- @param cb - function(obj) callback
Create.Stack.AtLoc = function(template, count, loc, cb)
if ( count == nil or count < 1 ) then count = 1 end
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then
Object.Decay(obj)
Create.OnCreateSuccessHook(obj, template)
end
if ( cb ) then cb(success and obj or nil) end
end)
-- set the stack count before creating
local templateData = GetTemplateData(template)
if ( templateData.SharedObjectProperties == nil ) then templateData.SharedObjectProperties = {} end
if ( templateData.SharedObjectProperties.StackCount == nil or templateData.SharedObjectProperties.StackCount < 1 ) then
templateData.SharedObjectProperties.StackCount = count
end
CreateCustomObj(template, templateData, loc, id)
end
--- Create a template in a container, assigning StackCount before creation
-- @param template
-- @param container - container gameObj
-- @param count - stack count
-- @param containerloc - (optional) location in container
-- @param cb - function(obj) callback
Create.Stack.InContainer = function(template, container, count, containerloc, cb)
if ( count == nil or count < 1 ) then count = 1 end
-- get the template id
local templateData = GetTemplateData(template)
if not(templateData) then
LuaDebugCallStack("[Create.Stack.InContainer] ERROR: Invalid template specified. "..tostring(template))
return
end
-- if this template isn't stackable then create a single one of them
if not( Stackable.Is(nil, template) ) then
Create.InContainer(template, container, containerloc, cb)
return
end
if not( containerloc ) then
containerloc = Container.NextEmptySlot(container)
if ( containerloc == nil ) then
-- early exit, container is full
if ( cb ) then cb(nil, "full") end
return
end
end
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then Create.OnCreateSuccessHook(obj, template) end
if ( cb ) then cb(success and obj or nil) end
end)
-- set the stack count before creating
if ( templateData.SharedObjectProperties == nil ) then templateData.SharedObjectProperties = {} end
if ( templateData.SharedObjectProperties.StackCount == nil or templateData.SharedObjectProperties.StackCount < 1 ) then
templateData.SharedObjectProperties.StackCount = count
end
CreateCustomObjInContainer(template, templateData, container, containerloc, id)
end
--- Create a template in a mobile's backpack, assigning StackCount before creation
-- @param template
-- @param mobile - mobileObj
-- @param count - stack count
-- @param containerloc - (optional) location in container
-- @param cb - function(obj) callback
Create.Stack.InBackpack = function(template, mobile, count, containerloc, cb)
local backpack = Backpack.Get(mobile)
if ( backpack == nil ) then
if ( cb ) then cb(nil, "nobackpack") end
return
end
Create.Stack.InContainer(template, backpack, count, containerloc, cb)
end
--- Create a TEMPORARY (no backup) template at location
-- @param template
-- @param loc - location to create at
-- @param cb - function(obj) callback
Create.Temp.AtLoc = function(template, loc, cb)
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then
Object.Decay(obj)
Create.OnCreateSuccessHook(obj, template)
end
if ( cb ) then cb(success and obj or nil) end
end)
CreateTempObj(template, loc, id)
end
--- Create a custom TEMPORARY (no backup) template at location
-- @param template
-- @param loc - location to create at
-- @param data - Template Data (GetTemplateData)
-- @param cb - function(obj) callback
Create.CustomTemp.AtLoc = function(template, data, loc, cb)
local id = template..uuid()
RegisterSingleEventHandler(EventType.CreatedObject, id, function(success, obj)
if ( success ) then
Object.Decay(obj)
Create.OnCreateSuccessHook(obj, template)
end
if ( cb ) then cb(success and obj or nil) end
end)
CreateCustomTempObj(template, data, loc, id)
end