-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdreamMount_registrations.lua
210 lines (178 loc) · 7.2 KB
/
dreamMount_registrations.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
local Format = string.format
local Traceback = debug.traceback
local Uppercase = string.upper
local AddSpell = tes3mp.AddSpell
local ClearSpellbookChanges = tes3mp.ClearSpellbookChanges
local ContainsItem = inventoryHelper.containsItem
local CustomMessageBox = tes3mp.CustomMessageBox
local EquipItem = tes3mp.EquipItem
local ProcessCommand = commandHandler.ProcessCommand
local RegisterCommand = customCommandHooks.registerCommand
local RegisterHandler = customEventHooks.registerHandler
local RegisterValidator = customEventHooks.registerValidator
local SendEquipment = tes3mp.SendEquipment
local SendMessage = tes3mp.SendMessage
local SendSpellbookChanges = tes3mp.SendSpellbookChanges
local SetSpellbookChangesAction = tes3mp.SetSpellbookChangesAction
local SpellbookAdd = enumerations.spellbook.ADD
local SpellbookRemove = enumerations.spellbook.REMOVE
local MediumBlue = color.MediumBlue
local Red = color.Red
local DreamMountFunctionsPath = 'custom.dreamMount.dreamMount_functions'
local DreamMountFunctions = require(DreamMountFunctionsPath)
local DreamMountUnsupportedCommandStr = '%sUnsupported DreamMount subcommand %s%s!\n'
local DreamMountInvalidEquipSlotErr = "Invalid equipment slot provided %s!"
local DreamMountNoCallbackErr = 'No DreamMount callback associated with this function %s!\n%s'
local DreamCoreNoPlayerSpellbookWithoutSelfErr = 'Cannot call player spellbook update without self!\n'
local DreamCoreInvalidSpellDataErr = 'Invalid spellData table provided!\n'
local ItemTemplate = dataTableBuilder.BuildObjectData()
local EquipEnums = enumerations.equipment
---@alias SpellSendData table<string, boolean>
---@alias EquipSendData table<string, string|false>
---@param self table Player table indexed from Players[pid]
---@param spellData SpellSendData
local function updatePlayerSpellbook(self, spellData)
assert(self, DreamCoreNoPlayerSpellbookWithoutSelfErr .. Traceback(3))
assert(type(spellData) == 'table', DreamCoreInvalidSpellDataErr .. Traceback(3))
local playerId = self.pid
ClearSpellbookChanges(playerId)
for spellId, addOrRemove in pairs(spellData) do
SetSpellbookChangesAction(playerId , (addOrRemove and SpellbookAdd) or SpellbookRemove)
AddSpell(playerId, spellId)
end
SendSpellbookChanges(playerId)
end
---@param self table Player table index from Players[pid]
---@param equipmentUpdateTable EquipSendData
local function updateEquipment(self, equipmentUpdateTable)
local myPid = self.pid
local myData = self.data
local prevEquipment = self.previousEquipment
local myEquipment = myData.equipment
local myInventory = myData.inventory
for equipmentSlot, itemId in pairs(equipmentUpdateTable) do
local slotId = EquipEnums[Uppercase(equipmentSlot)]
assert(slotId, Format(DreamMountInvalidEquipSlotErr, equipmentSlot))
if itemId ~= false and ContainsItem(myInventory, itemId) then
local targetItem = ItemTemplate
targetItem.refId = itemId
EquipItem(myPid, slotId
, targetItem.refId, targetItem.count
, targetItem.charge, targetItem.enchantmentCharge)
prevEquipment[slotId] = myEquipment[slotId]
myEquipment[slotId] = targetItem
else
myEquipment[slotId] = nil
end
end
SendEquipment(myPid)
end
---@param self JSONPlayer
---@param id integer numeric id of the messagebox
---@param label string Header text of the messagebox
---@param buttons string Semicolon-separated string of button texts
local function customMessageBox(self, id, label, buttons)
CustomMessageBox(self.pid, id, label, buttons)
end
--- Extend built-in functionality of certain object types on server initialization
local function extendBuiltins()
Player['MessageBox'] = customMessageBox
Player['updateSpellbook'] = updatePlayerSpellbook
Player['updateEquipment'] = updateEquipment
end
---@class HandlerRegistration
---@field registrar function
---@field callbackModule table
---@field event string
---@field callbackName string
---@field canUseSelf boolean
---Replacement for internal registration functions which uses table lookups
---in order to avoid "stuck" references to functions when reloading
---@param registerData HandlerRegistration
local function localRegisterThing(registerData)
local lookupModule = registerData.callbackModule
local callbackName = registerData.callbackName
local DWGenericCallback = lookupModule[callbackName]
assert(DWGenericCallback
, Format(DreamMountNoCallbackErr
, callbackName
, Traceback(3)))
registerData.registrar(registerData.event, registerData.canUseSelf
and function(...) return DWGenericCallback(lookupModule, ...) end
or function(...) return DWGenericCallback(...) end
)
end
---@param pid PlayerId
local function reloadMountFuncs(pid)
ProcessCommand(pid, { 'load', DreamMountFunctionsPath })
DreamMountFunctions:initMountData()
end
---@param failedCommand string
---@return string Unsupported mount message
local function unsupportedCommandString(failedCommand)
return Format(DreamMountUnsupportedCommandStr
, MediumBlue, failedCommand, Red)
end
---@alias Subcommand
---| "'menu'"
---| "'reloadConfig'"
---| "'defaultConfig'"
---| "'clearPlayerVars'"
---| "'summon'"
---| "'container'"
---@alias CommandId
---| "'showPreferredMountMenu'"
---| "'reloadMountConfig'"
---| "'defaultMountConfig'"
---| "'clearCustomVariablesCommand'"
---| "'summonCreatureMount'"
---| "'openContainerForNonSummon'"
---@type table <Subcommand, CommandId>
local mountFuncs = {
menu = 'showPreferredMountMenu',
reloadconfig = 'reloadMountConfig',
defaultconfig = 'defaultMountConfig',
clearplayervars = 'clearCustomVariablesCommand',
summon = "summonCreatureMount",
container = "openContainerForNonSummon",
}
---@param pid PlayerId
---@param cmd string[]
local function handleMountCommand(pid, cmd)
local subCommand = cmd[2]
if subCommand then subCommand = subCommand:lower() end
if not subCommand then DreamMountFunctions:toggleMountCommand(pid)
elseif subCommand == 'reload' and DreamMountFunctions.validateUser(pid) then reloadMountFuncs(pid)
elseif mountFuncs[subCommand] then DreamMountFunctions[mountFuncs[subCommand]](DreamMountFunctions, pid, cmd)
else SendMessage(pid, unsupportedCommandString(subCommand), false) end
end
for eventType, eventTable in pairs {
EventHandlers = {
OnServerPostInit = 'initMountData',
OnGUIAction = 'setPreferredMount',
OnPlayerDisconnect = 'slowSaveOnEmptyWorld',
OnObjectDialogueChoice = 'reloadMountMerchants',
OnActorCellChange = 'trackPlayerMountCell',
OnActorDeath = 'onMountDied',
OnPlayerFinishLogin = 'cleanUpMountOnLogin',
OnObjectHit = 'dismountOnHit',
},
EventValidators = {
OnObjectActivate = 'handleMountActivation',
OnPlayerInventory = 'denyMountClothingRemoval',
OnPlayerEquipment = 'replaceMountClothingOnRemoval',
}
} do
local registrarType = (eventType == 'EventHandlers') and RegisterHandler or RegisterValidator
for eventName, callbackName in pairs(eventTable) do
localRegisterThing {
registrar = registrarType,
event = eventName,
callbackModule = DreamMountFunctions,
callbackName = callbackName,
canUseSelf = true
}
end
end
RegisterCommand('ride', handleMountCommand)
RegisterHandler('OnServerPostInit', extendBuiltins)