-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.lua
785 lines (629 loc) · 25.7 KB
/
main.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
CustomAlchemy = {}
local Formulas = require("custom.CustomAlchemy.formulas")
CustomAlchemy.scriptName = "CustomAlchemy"
CustomAlchemy.initialConfig = require("custom.CustomAlchemy.initialConfig")
CustomAlchemy.config = DataManager.loadConfiguration(
CustomAlchemy.scriptName,
CustomAlchemy.initialConfig.default,
CustomAlchemy.initialConfig.keyOrder
)
tableHelper.fixNumericalKeys(CustomAlchemy.config)
CustomAlchemy.initialData = require("custom.CustomAlchemy.initialData")
CustomAlchemy.uniqueIndexCache = {}
CustomAlchemy.skillId = tes3mp.GetSkillId("Alchemy")
function CustomAlchemy.loadData()
CustomAlchemy.data = DataManager.loadData(
CustomAlchemy.scriptName,
CustomAlchemy.initialData.default,
CustomAlchemy.initialData.keyOrder
)
end
function CustomAlchemy.saveData()
DataManager.saveData(CustomAlchemy.scriptName, CustomAlchemy.data)
end
function CustomAlchemy.importESPs()
if espParser == nil then
return
end
-- Load esp files if necessary
local loaded = espParser.isLoaded()
if not loaded then
espParser.loadFiles()
end
-- Parse ingredients
for _, record in pairs(espParser.getAllRecords("INGR")) do
local ingredient = {}
local refId = ""
for _, subRecord in pairs(record.subRecords) do
local data = subRecord.data
if data ~= nil then
if subRecord.name == "NAME" then
refId = espParser.getValue(data, "s", 1):lower()
elseif subRecord.name == "IRDT" then
local pos = 1
ingredient.weight = espParser.getValue(data, "f", pos)
pos = pos + 4
pos = pos + 4 --skip value
ingredient.effects = {}
for i = 1, 4 do
ingredient.effects[i] = espParser.getValue(data, "i", pos)
pos = pos + 4
end
ingredient.skills = {}
for i = 1, 4 do
ingredient.skills[i] = espParser.getValue(data, "i", pos)
pos = pos + 4
end
ingredient.attributes = {}
for i = 1, 4 do
ingredient.attributes[i] = espParser.getValue(data, "i", pos)
pos = pos + 4
end
end
end
end
CustomAlchemy.data.ingredients[refId] = ingredient
end
-- Parse apparatuses
for _, record in pairs(espParser.getAllRecords("APPA")) do
local apparatus = {}
local refId = ""
for _, subRecord in pairs(record.subRecords) do
local data = subRecord.data
if data ~= nil then
if subRecord.name == "NAME" then
refId = espParser.getValue(data, "s", 1):lower()
elseif subRecord.name == "AADT" then
local pos = 1
apparatus.type = espParser.getValue(data, "I", pos)
pos = pos + 4
apparatus.quality = espParser.getValue(data, "f", pos)
end
end
end
CustomAlchemy.data.apparatuses[refId] = apparatus
end
-- Clean after if expected
if not loaded then
espParser.unloadFiles()
end
-- Save freshly loaded classes
DataManager.saveData(CustomAlchemy.scriptName, CustomAlchemy.data, CustomAlchemy.initialData.keyOrder)
end
function CustomAlchemy.updatePlayerSpellbook(pid)
Players[pid]:LoadSpellbook()
end
function CustomAlchemy.sendSpell(pid, id, action)
tes3mp.ClearSpellbookChanges(pid)
tes3mp.SetSpellbookChangesAction(pid, action)
tes3mp.AddSpell(pid, id)
tes3mp.SendSpellbookChanges(pid)
end
function CustomAlchemy.createAlchemyContainerRecord()
local recordStore = RecordStores[CustomAlchemy.config.container.type]
if recordStore.data.permanentRecords[CustomAlchemy.config.container.refId] == nil then
recordStore.data.permanentRecords[CustomAlchemy.config.container.refId] = {
baseId = CustomAlchemy.config.container.baseId,
name = CustomAlchemy.config.container.name
}
recordStore:Save()
end
if CustomAlchemy.data.recordId == nil or ContainerFramework.getRecordData(CustomAlchemy.data.recordId) == nil then
CustomAlchemy.data.recordId = ContainerFramework.createRecord(
CustomAlchemy.config.container.refId,
CustomAlchemy.config.container.packetType
)
end
CustomAlchemy.saveData()
end
function CustomAlchemy.createContainer(pid)
local instanceId = ContainerFramework.createContainer(CustomAlchemy.data.recordId)
CustomAlchemy.data.instances[pid] = instanceId
CustomAlchemy.data.weight[pid] = 0
end
function CustomAlchemy.getContainerInstanceId(pid)
return CustomAlchemy.data.instances[pid]
end
function CustomAlchemy.getContainerInventory(pid)
local instanceId = CustomAlchemy.getContainerInstanceId(pid)
return ContainerFramework.getInventory(instanceId)
end
function CustomAlchemy.setContainerInventory(pid, inventory)
local instanceId = CustomAlchemy.getContainerInstanceId(pid)
ContainerFramework.setInventory(instanceId, inventory)
end
function CustomAlchemy.refreshUI(pid)
logicHandler.RunConsoleCommandOnPlayer(pid, "togglemenus", false)
logicHandler.RunConsoleCommandOnPlayer(pid, "togglemenus", false)
end
function CustomAlchemy.updateContainer(pid)
local instanceId = CustomAlchemy.getContainerInstanceId(pid)
ContainerFramework.updateInventory(pid, instanceId)
CustomAlchemy.refreshUI(pid)
CustomAlchemy.activateContainer(pid)
end
function CustomAlchemy.getContainerWeight(pid)
return CustomAlchemy.data.weight[pid]
end
function CustomAlchemy.updateContainerWeight(pid)
local inventory = CustomAlchemy.getContainerInventory(pid)
local weight = 0
for _, item in pairs(inventory) do
if CustomAlchemy.isIngredient(item.refId) then
weight = weight + CustomAlchemy.data.ingredients[item.refId].weight * item.count
end
end
CustomAlchemy.data.weight[pid] = weight
return CustomAlchemy.data.weight[pid]
end
function CustomAlchemy.isContainerEmpty(pid)
return next(CustomAlchemy.getContainerInventory(pid)) == nil
end
function CustomAlchemy.getContainerUniqueIndex(pid)
if CustomAlchemy.uniqueIndexCache[pid] == nil then
local instanceId = CustomAlchemy.getContainerInstanceId(pid)
local instanceData = ContainerFramework.getInstanceData(instanceId)
local uniqueIndex = instanceData.container.uniqueIndex
CustomAlchemy.uniqueIndexCache[pid] = uniqueIndex
end
return CustomAlchemy.uniqueIndexCache[pid]
end
function CustomAlchemy.emptyContainer(pid)
local inventory = CustomAlchemy.getContainerInventory(pid)
local player = Players[pid]
tes3mp.ClearInventoryChanges(pid)
tes3mp.SetInventoryChangesAction(pid, enumerations.inventory.ADD)
for _, item in pairs(inventory) do
inventoryHelper.addItem(player.data.inventory, item.refId, item.count, item.charge, item.enchantmentCharge, item.soul)
tes3mp.AddItemChange(pid, item.refId, item.count, item.charge, item.enchantmentCharge, item.soul)
end
tes3mp.SendInventoryChanges(pid)
CustomAlchemy.setContainerInventory(pid, {})
local splitIndex = CustomAlchemy.getContainerUniqueIndex(pid):split("-")
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(ContainerFramework.config.storage.cell)
tes3mp.SetObjectRefNum(splitIndex[1])
tes3mp.SetObjectMpNum(splitIndex[2])
tes3mp.SetObjectRefId(CustomAlchemy.config.container.refId)
tes3mp.AddObject()
tes3mp.SetObjectListAction(enumerations.container.SET)
tes3mp.SendContainer()
CustomAlchemy.updateContainerWeight(pid)
CustomAlchemy.applyContainerBurden(pid)
end
function CustomAlchemy.destroyContainer(pid)
CustomAlchemy.emptyContainer(pid)
ContainerFramework.removeContainer(CustomAlchemy.data.instances[pid])
CustomAlchemy.data.instances[pid] = nil
end
function CustomAlchemy.activateContainer(pid)
ContainerFramework.activateContainer(pid, CustomAlchemy.getContainerInstanceId(pid))
end
function CustomAlchemy.filterContainerIngredients(pid, objectIndex)
local nonIngredients = false
tes3mp.ClearInventoryChanges(pid)
tes3mp.SetInventoryChangesAction(pid, enumerations.inventory.ADD)
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(ContainerFramework.config.storage.cell)
local splitIndex = CustomAlchemy.getContainerUniqueIndex(pid):split("-")
tes3mp.SetObjectRefNum(splitIndex[1])
tes3mp.SetObjectMpNum(splitIndex[2])
tes3mp.SetObjectRefId(CustomAlchemy.config.container.refId)
for itemIndex = 0, tes3mp.GetContainerChangesSize(objectIndex) - 1 do
local item = {
refId = tes3mp.GetContainerItemRefId(objectIndex, itemIndex),
count = tes3mp.GetContainerItemCount(objectIndex, itemIndex),
charge = tes3mp.GetContainerItemCharge(objectIndex, itemIndex),
enchantmentCharge = tes3mp.GetContainerItemEnchantmentCharge(objectIndex, itemIndex),
soul = tes3mp.GetContainerItemSoul(objectIndex, itemIndex)
}
if not CustomAlchemy.isIngredient(item.refId) then
nonIngredients = true
inventoryHelper.addItem(Players[pid].data.inventory, item.refId, item.count, item.charge, item.enchantmentCharge, item.soul)
tes3mp.AddItemChange(pid, item.refId, item.count, item.charge, item.enchantmentCharge, item.soul)
tes3mp.SetContainerItemRefId(item.refId)
tes3mp.SetContainerItemCount(item.count)
tes3mp.SetContainerItemCharge(item.charge)
tes3mp.SetContainerItemEnchantmentCharge(item.enchantmentCharge)
tes3mp.SetContainerItemSoul(item.soul)
tes3mp.AddContainerItem()
end
end
tes3mp.AddObject()
tes3mp.SetObjectListAction(enumerations.container.REMOVE)
if nonIngredients then
tes3mp.SendInventoryChanges(pid)
tes3mp.SendContainer()
return true
end
return false
end
function CustomAlchemy.applyContainerBurden(pid, readd)
if readd == nil then
readd = false
end
local id = CustomAlchemy.data.burdenId[pid]
tableHelper.removeValue(Players[pid].data.spellbook, id)
CustomAlchemy.sendSpell(pid, id, enumerations.spellbook.REMOVE)
if readd then
local recordStore = RecordStores["spell"]
tableHelper.removeValue(Players[pid].generatedRecordsReceived, id)
tableHelper.cleanNils(Players[pid].generatedRecordsReceived)
recordStore:LoadGeneratedRecords(pid, recordStore.data.generatedRecords, {id})
table.insert(Players[pid].data.spellbook, id)
CustomAlchemy.sendSpell(pid, id, enumerations.spellbook.ADD)
end
tableHelper.cleanNils(Players[pid].data.spellbook)
end
function CustomAlchemy.createContainerBurden(pid)
local recordStore = RecordStores["spell"]
local id = recordStore:GenerateRecordId()
CustomAlchemy.data.burdenId[pid] = id
local recordTable = {
name = "Weight of ingredients",
subtype = 2,
effects = {{
id = 7,
attribute = -1,
skill = -1,
rangeType = 0,
area = 0,
magnitudeMin = 0,
magnitudeMax = 0
}}
}
recordStore.data.generatedRecords[id] = recordTable
recordStore:AddLinkToPlayer(id, Players[pid])
Players[pid]:AddLinkToRecord("spell", id)
recordStore:Save()
end
function CustomAlchemy.updateContainerBurden(pid)
local weight = math.ceil(CustomAlchemy.getContainerWeight(pid))
local recordStore = RecordStores["spell"]
local id = CustomAlchemy.data.burdenId[pid]
recordStore.data.generatedRecords[id].effects[1].magnitudeMin = weight
recordStore.data.generatedRecords[id].effects[1].magnitudeMax = weight
CustomAlchemy.applyContainerBurden(pid, weight > 0)
end
function CustomAlchemy.destroyContainerBurden(pid)
CustomAlchemy.applyContainerBurden(pid, false)
local id = CustomAlchemy.data.burdenId[pid]
CustomAlchemy.data.burdenId[pid] = nil
local recordStore = RecordStores["spell"]
recordStore.data.generatedRecords[id] = nil
recordStore:RemoveLinkToPlayer(id, Players[pid])
Players[pid]:RemoveLinkToRecord("spell", id)
recordStore:Save()
end
function CustomAlchemy.isApparatus(refId)
return CustomAlchemy.data.apparatuses[refId] ~= nil
end
function CustomAlchemy.isMortar(refId)
return CustomAlchemy.data.apparatuses[refId] ~= nil and CustomAlchemy.data.apparatuses[refId].type == 1
end
function CustomAlchemy.getApparatus(refId)
return CustomAlchemy.data.apparatuses[refId]
end
function CustomAlchemy.isIngredient(refId)
return CustomAlchemy.data.ingredients[refId] ~= nil
end
function CustomAlchemy.determineApparatuses(pid)
local player_apparatuses = {0, 0, 0}
player_apparatuses[0] = 0
local inventory = Players[pid].data.inventory
for _, item in pairs(inventory) do
if CustomAlchemy.isApparatus(item.refId) then
local apparatus = CustomAlchemy.getApparatus(item.refId)
player_apparatuses[apparatus.type] = math.max(player_apparatuses[apparatus.type], apparatus.quality)
end
end
return player_apparatuses
end
CustomAlchemy.skillEffects = {}
CustomAlchemy.skillEffects[21]=true
CustomAlchemy.skillEffects[26]=true
CustomAlchemy.skillEffects[78]=true
CustomAlchemy.skillEffects[83]=true
CustomAlchemy.skillEffects[89]=true
CustomAlchemy.attributeEffects = {}
CustomAlchemy.attributeEffects[17]=true
CustomAlchemy.attributeEffects[22]=true
CustomAlchemy.attributeEffects[74]=true
CustomAlchemy.attributeEffects[79]=true
CustomAlchemy.attributeEffects[85]=true
function CustomAlchemy.needsCombinedId(id)
return CustomAlchemy.attributeEffects[id] ~= nil or CustomAlchemy.skillEffects[id] ~= nil
end
function CustomAlchemy.isSkillEffect(id)
return CustomAlchemy.skillEffects[id]
end
function CustomAlchemy.isAttributeEffect(id)
return CustomAlchemy.attributeEffects[id]
end
CustomAlchemy.maxEffectId = 256
function CustomAlchemy.makeCombinedId(id, parameter)
return (parameter + 1) * CustomAlchemy.maxEffectId + id
end
function CustomAlchemy.isCombinedId(id)
return id >= CustomAlchemy.maxEffectId
end
function CustomAlchemy.parseCombinedId(combinedId)
local id = combinedId % CustomAlchemy.maxEffectId
local parameter = math.floor(combinedId / CustomAlchemy.maxEffectId) - 1
return {
effectId = id,
parameter = parameter
}
end
function CustomAlchemy.failure(pid, label)
GuiFramework.MessageBox({
pid = pid,
name = 'CustomAlchemy_failure',
label = label
})
tes3mp.PlaySpeech(pid, CustomAlchemy.config.fail.sound)
end
function CustomAlchemy.success(pid,count)
local message = nil
if count == 1 then
message = CustomAlchemy.config.success.message1
else
message = string.format(CustomAlchemy.config.success.message, count)
end
tes3mp.PlaySpeech(pid, CustomAlchemy.config.success.sound)
GuiFramework.MessageBox({
pid = pid,
name = 'CustomAlchemy_success',
message = message
})
end
function CustomAlchemy.brewPotions(pid, name)
local player_apparatuses = CustomAlchemy.determineApparatuses(pid)
--check if the player has a mortar
if player_apparatuses[0] == 0 then
CustomAlchemy.failure(pid, CustomAlchemy.config.fail.messageMortarRequired)
CustomAlchemy.cancel(pid)
return
end
local containerInventory = CustomAlchemy.getContainerInventory(pid)
--if there are too many different ingredients (>4 by default), we can't brew a potion
if #containerInventory <= CustomAlchemy.config.maximumIngredientCount then
local potion_effects = {} --keeping track of all effects of our ingredients
local min_ingredient_count = nil --how many potions we can brew
local potion_ingredients = {} --list of all ingredients we will use
for _, item in pairs(containerInventory) do
if min_ingredient_count == nil then
min_ingredient_count = item.count
else
min_ingredient_count = math.min(min_ingredient_count, item.count)
end
local ingredient = CustomAlchemy.data.ingredients[item.refId]
table.insert(potion_ingredients, ingredient)
if ingredient~=nil then
for index, id in pairs(ingredient.effects) do
if id~=-1 then
local effectId = id
if CustomAlchemy.isSkillEffect(id) then
effectId = CustomAlchemy.makeCombinedId(id, ingredient.skills[index])
elseif CustomAlchemy.isAttributeEffect(id) then
effectId = CustomAlchemy.makeCombinedId(id, ingredient.attributes[index])
end
if potion_effects[effectId] == nil then
potion_effects[effectId] = 1
else
potion_effects[effectId] = 1 + potion_effects[effectId]
end
end
end
end
end
--removing ingredients that we ended up using
for _, item in pairs(containerInventory) do
inventoryHelper.removeItem(
containerInventory,
item.refId,
min_ingredient_count,
item.charge,
item.enchantmentCharge,
item.soul
)
end
--return whatever is left to the player
CustomAlchemy.emptyContainer(pid)
local status = Formulas.makeAlchemyStatus(pid, player_apparatuses, potion_ingredients)
local potion_count = Formulas.getPotionCount(status, min_ingredient_count)
local recordTable = {
name = name,
weight = status.weight,
icon = status.icon,
model = status.model,
value = status.value
}
recordTable.effects = {}
for combinedId, count in pairs(potion_effects) do
--if there are fewer than necessary (2 by default) ingredients with the same effect, don't add it
if count >= CustomAlchemy.config.potionEffectTreshold then
local effectId = 0
local skill = -1
local attribute = -1
if CustomAlchemy.isCombinedId(combinedId) then
local parsed = CustomAlchemy.parseCombinedId(combinedId)
effectId = parsed.effectId
if CustomAlchemy.isSkillEffect(effectId) then
skill = parsed.parameter
else
attribute = parsed.parameter
end
else
effectId = combinedId
end
local effectData = CustomAlchemy.data.effects[effectId]
if effectData == nil then
effectData = CustomAlchemy.data.effects[tostring(effectId)]
end
local magnitude = Formulas.getEffectMagnitude(status, effectData)
local duration = Formulas.getEffectDuration(status, effectData)
local effect = {
id = effectId,
attribute = attribute,
skill = skill,
rangeType = 0,
area = 0,
magnitudeMin = magnitude,
magnitudeMax = magnitude,
duration = duration
}
table.insert(recordTable.effects, effect)
end
end
if recordTable.effects[1] ~= nil then
if potion_count < 1 then
CustomAlchemy.failure(pid, CustomAlchemy.config.fail.messageAttempt)
CustomAlchemy.updateContainer(pid)
return
end
local recordStore = RecordStores["potion"]
local potionId = recordStore:GenerateRecordId()
recordStore.data.generatedRecords[potionId] = recordTable
recordStore:AddLinkToPlayer(potionId, Players[pid])
Players[pid]:AddLinkToRecord("potion", potionId)
recordStore:Save()
recordStore:LoadGeneratedRecords(pid, recordStore.data.generatedRecords, {potionId})
inventoryHelper.addItem(Players[pid].data.inventory, potionId, potion_count, -1, -1, "")
tes3mp.ClearInventoryChanges(pid)
tes3mp.SetInventoryChangesAction(pid, enumerations.inventory.ADD)
tes3mp.AddItemChange(pid, potionId, potion_count, -1, -1, "")
tes3mp.SendInventoryChanges(pid)
CustomAlchemy.success(pid, potion_count)
CustomAlchemy.updateContainer(pid)
-- Improve alchemy skill
if CustomAlchemy.config.progressSkill then
LevelingFramework.progressSkill(pid, "Alchemy", 2, potion_count)
Players[pid]:LoadSkills()
Players[pid]:LoadLevel()
end
else
CustomAlchemy.failure(pid, CustomAlchemy.config.fail.messageUseless)
CustomAlchemy.setContainerInventory(pid,{})
CustomAlchemy.updateContainer(pid)
end
else
CustomAlchemy.failure(pid, CustomAlchemy.config.fail.messageTooMany)
CustomAlchemy.cancel(pid)
CustomAlchemy.updateContainer(pid)
end
end
function CustomAlchemy.addIngredient(pid)
CustomAlchemy.activateContainer(pid)
end
function CustomAlchemy.cancel(pid)
CustomAlchemy.emptyContainer(pid)
CustomAlchemy.refreshUI(pid)
end
--UI actions
function CustomAlchemy.showApparatusGUI(pid)
GuiFramework.CustomMessageBox({
pid = pid,
name = "CustomAlchemy_apparatus",
buttons = CustomAlchemy.config.menu.apparatusButtons,
returnValues = { "brew", "add", "cancel" },
callback = CustomAlchemy.processApparatusGUI
})
end
function CustomAlchemy.processApparatusGUI(pid, name, input, data, parameters)
if data == "brew" then
CustomAlchemy.showPotionNameGUI(pid)
elseif data == "add" then
CustomAlchemy.addIngredient(pid)
elseif data == "cancel" then
CustomAlchemy.cancel(pid)
end
end
function CustomAlchemy.showPotionNameGUI(pid)
GuiFramework.InputDialog({
pid = pid,
name = "CustomAlchemy_potionName",
label = CustomAlchemy.config.menu.nameLabel,
callback = CustomAlchemy.processPotionNameGUI
})
end
function CustomAlchemy.processPotionNameGUI(pid, name, data, parameters)
if data~=nil then
CustomAlchemy.brewPotions(pid, data)
else
CustomAlchemy.showPotionNameGUI(pid)
end
end
--Event hooks
function CustomAlchemy.OnServerPostInit()
CustomAlchemy.loadData()
if CustomAlchemy.config.importESPs then
CustomAlchemy.importESPs()
end
CustomAlchemy.createAlchemyContainerRecord()
if CustomAlchemy.config.disableQuickKeys then
for refId, _ in pairs(CustomAlchemy.data.apparatuses) do
QuickKeyCleaner.banItem(refId)
end
end
end
function CustomAlchemy.OnServerExit(eventStatus)
for pid, player in pairs(Players) do
CustomAlchemy.destroyContainer(pid)
CustomAlchemy.destroyContainerBurden(pid)
end
CustomAlchemy.saveData()
end
function CustomAlchemy.OnPlayerAuthentified(eventStatus, pid)
if eventStatus.validCustomHandlers then
CustomAlchemy.createContainer(pid)
CustomAlchemy.createContainerBurden(pid)
end
end
function CustomAlchemy.OnPlayerDisconnectValidator(eventStatus, pid)
CustomAlchemy.destroyContainer(pid)
CustomAlchemy.destroyContainerBurden(pid)
end
function CustomAlchemy.OnPlayerItemUseValidator(eventStatus, pid, refId)
if CustomAlchemy.isApparatus(refId) then
CustomAlchemy.activateContainer(pid)
if not CustomAlchemy.isContainerEmpty(pid) then
CustomAlchemy.showApparatusGUI(pid)
end
return customEventHooks.makeEventStatus(false, nil)
end
end
function CustomAlchemy.OnContainerValidator(eventStatus, pid, instanceId, index)
if not eventStatus.validCustomHandlers then
return
end
if instanceId == CustomAlchemy.getContainerInstanceId(pid) then
if CustomAlchemy.filterContainerIngredients(pid, index) then
return customEventHooks.makeEventStatus(false, false)
end
end
end
function CustomAlchemy.OnContainer(eventStatus, pid, instanceId, index)
if not eventStatus.validCustomHandlers then
return
end
if instanceId == CustomAlchemy.getContainerInstanceId(pid) then
CustomAlchemy.updateContainerWeight(pid)
CustomAlchemy.updateContainerBurden(pid)
end
end
customEventHooks.registerHandler("OnServerPostInit", CustomAlchemy.OnServerPostInit)
customEventHooks.registerHandler("OnServerExit", CustomAlchemy.OnCellUnloadValidator)
customEventHooks.registerHandler("OnPlayerAuthentified", CustomAlchemy.OnPlayerAuthentified)
customEventHooks.registerValidator("OnPlayerDisconnect", CustomAlchemy.OnPlayerDisconnectValidator)
customEventHooks.registerValidator("OnPlayerItemUse", CustomAlchemy.OnPlayerItemUseValidator)
customEventHooks.registerValidator("ContainerFramework_OnContainer", CustomAlchemy.OnContainerValidator)
customEventHooks.registerHandler("ContainerFramework_OnContainer", CustomAlchemy.OnContainer)
customCommandHooks.registerCommand("caimportesps", function(pid, cmd)
CustomAlchemy.importESPs()
tes3mp.SendMessage(pid, "Imported ESPs!")
end)
customCommandHooks.setRankRequirement("caimportesps", CustomAlchemy.config.cmdRank)
return CustomAlchemy