2022-02-18 17:26:00 -04:00
|
|
|
local unpack = unpack
|
|
|
|
|
local pack = table.pack
|
|
|
|
|
|
2024-04-16 16:55:26 -04:00
|
|
|
local EventData, callbacks, updateableParameters, autoID = {}, {}, {}, 0
|
|
|
|
|
-- This metatable creates an auto-configuration mechanism to create new types of Events
|
2022-03-22 20:03:36 -03:00
|
|
|
local ec = setmetatable({}, { __newindex = function(self, key, value)
|
2022-02-18 19:47:48 -03:00
|
|
|
autoID = autoID + 1
|
2022-02-18 17:26:00 -04:00
|
|
|
callbacks[key] = autoID
|
|
|
|
|
local info, update = {}, {}
|
2022-02-18 19:47:48 -03:00
|
|
|
for k, v in pairs(value) do
|
|
|
|
|
if type(k) == "string" then
|
|
|
|
|
info[k] = v
|
|
|
|
|
else
|
|
|
|
|
update[k] = v
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-02-18 17:26:00 -04:00
|
|
|
updateableParameters[autoID] = update
|
|
|
|
|
callbacks[autoID] = info
|
2024-04-16 16:55:26 -04:00
|
|
|
EventData[autoID] = {maxn = 0}
|
2022-02-18 17:26:00 -04:00
|
|
|
end})
|
|
|
|
|
|
2024-04-16 16:55:26 -04:00
|
|
|
--@ Definitions of valid Event types to hook according to the given field name
|
2022-02-18 17:26:00 -04:00
|
|
|
--@ The fields within the assigned table, allow to save arbitrary information
|
2020-10-18 23:50:36 +02:00
|
|
|
-- Creature
|
2022-02-18 17:26:00 -04:00
|
|
|
ec.onChangeOutfit = {}
|
|
|
|
|
ec.onChangeMount = {}
|
|
|
|
|
ec.onAreaCombat = {returnValue=true}
|
|
|
|
|
ec.onTargetCombat = {returnValue=true}
|
|
|
|
|
ec.onHear = {}
|
2023-12-07 19:24:26 -06:00
|
|
|
ec.onChangeZone = {}
|
2024-05-28 19:25:59 -06:00
|
|
|
ec.onUpdateStorage = {}
|
2020-10-18 23:50:36 +02:00
|
|
|
-- Party
|
2022-02-18 17:26:00 -04:00
|
|
|
ec.onJoin = {}
|
|
|
|
|
ec.onLeave = {}
|
|
|
|
|
ec.onDisband = {}
|
|
|
|
|
ec.onShareExperience = {}
|
2023-04-06 19:50:52 -06:00
|
|
|
ec.onInvite = {}
|
|
|
|
|
ec.onRevokeInvitation = {}
|
|
|
|
|
ec.onPassLeadership = {}
|
2020-10-18 23:50:36 +02:00
|
|
|
-- Player
|
2022-02-18 17:26:00 -04:00
|
|
|
ec.onBrowseField = {}
|
2022-02-18 19:47:48 -03:00
|
|
|
ec.onLook = {[5] = 1}
|
|
|
|
|
ec.onLookInBattleList = {[4] = 1}
|
|
|
|
|
ec.onLookInTrade = {[5] = 1}
|
|
|
|
|
ec.onLookInShop = {[4] = 1}
|
2022-02-18 17:26:00 -04:00
|
|
|
ec.onLookInMarket = {}
|
|
|
|
|
ec.onTradeRequest = {}
|
|
|
|
|
ec.onTradeAccept = {}
|
|
|
|
|
ec.onTradeCompleted = {}
|
2023-04-06 20:31:25 -04:00
|
|
|
ec.onMoveItem = {returnValue=true}
|
2022-02-18 17:26:00 -04:00
|
|
|
ec.onItemMoved = {}
|
|
|
|
|
ec.onMoveCreature = {}
|
|
|
|
|
ec.onReportRuleViolation = {}
|
|
|
|
|
ec.onReportBug = {}
|
2023-09-29 12:39:37 -03:00
|
|
|
ec.onRotateItem = {}
|
2024-05-28 19:25:59 -06:00
|
|
|
ec.onTurn = {}
|
2022-02-18 19:47:48 -03:00
|
|
|
ec.onGainExperience = {[3] = 1}
|
|
|
|
|
ec.onLoseExperience = {[2] = 1}
|
|
|
|
|
ec.onGainSkillTries = {[3] = 1}
|
2022-02-18 17:26:00 -04:00
|
|
|
ec.onWrapItem = {}
|
|
|
|
|
ec.onInventoryUpdate = {}
|
2024-01-12 10:36:37 -06:00
|
|
|
ec.onSpellCheck = {}
|
2020-10-18 23:50:36 +02:00
|
|
|
-- Monster
|
2022-02-18 17:26:00 -04:00
|
|
|
ec.onDropLoot = {}
|
|
|
|
|
ec.onSpawn = {}
|
2021-02-26 07:10:01 -04:00
|
|
|
|
2024-04-16 16:55:26 -04:00
|
|
|
local EventMeta = {
|
2022-03-22 20:03:36 -03:00
|
|
|
__newindex = function(self, key, callback)
|
2021-11-27 02:22:15 -04:00
|
|
|
if not isScriptsInterface() then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local eventType = callbacks[key]
|
|
|
|
|
if not eventType then
|
2024-04-16 16:55:26 -04:00
|
|
|
debugPrint(string.format("[Warning - Event::%s] is not a valid callback.", key))
|
2021-11-27 02:22:15 -04:00
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if type(callback) ~= "function" then
|
2024-04-16 16:55:26 -04:00
|
|
|
debugPrint(string.format("[Warning - Event::%s] a function is expected.", key))
|
2021-11-27 02:22:15 -04:00
|
|
|
return
|
2021-02-26 07:10:01 -04:00
|
|
|
end
|
2021-11-27 02:22:15 -04:00
|
|
|
|
|
|
|
|
rawset(self, 'eventType', eventType)
|
|
|
|
|
rawset(self, 'callback', callback)
|
2024-04-16 16:55:26 -04:00
|
|
|
end
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local function register(self, triggerIndex)
|
|
|
|
|
if not isScriptsInterface() then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local eventType = rawget(self, 'eventType')
|
|
|
|
|
local callback = rawget(self, 'callback')
|
|
|
|
|
if not eventType or not callback then
|
|
|
|
|
debugPrint("[Warning - Event::register] need to setup a callback before you can register.")
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local events = EventData[eventType]
|
|
|
|
|
events.maxn = #events + 1
|
|
|
|
|
events[events.maxn] = {
|
|
|
|
|
callback = callback,
|
|
|
|
|
triggerIndex = tonumber(triggerIndex) or 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table.sort(events, function(ecl, ecr) return ecl.triggerIndex < ecr.triggerIndex end)
|
|
|
|
|
self.eventType = nil
|
|
|
|
|
self.callback = nil
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Event = setmetatable({
|
|
|
|
|
clear = function(self)
|
|
|
|
|
EventData = {}
|
|
|
|
|
for i = 1, autoID do
|
|
|
|
|
EventData[i] = {maxn = 0}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
}, {
|
|
|
|
|
__call = function(self)
|
|
|
|
|
return setmetatable({register = register}, EventMeta)
|
2022-02-18 17:26:00 -04:00
|
|
|
end,
|
|
|
|
|
|
2022-03-22 20:03:36 -03:00
|
|
|
__index = function(self, key)
|
2022-02-18 17:26:00 -04:00
|
|
|
local callback = callbacks[key]
|
|
|
|
|
if not callback then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2024-04-16 16:55:26 -04:00
|
|
|
local events = EventData[callback]
|
|
|
|
|
local eventsCount = events.maxn
|
|
|
|
|
local updateableParams = updateableParameters[callback]
|
2022-03-22 20:03:36 -03:00
|
|
|
return function(...)
|
2022-02-18 17:26:00 -04:00
|
|
|
local results, args, info = {}, pack(...), callbacks[callback]
|
2024-04-16 16:55:26 -04:00
|
|
|
for index = 1, eventsCount do
|
2022-02-18 17:26:00 -04:00
|
|
|
repeat
|
2024-04-16 16:55:26 -04:00
|
|
|
results = {events[index].callback(unpack(args))}
|
2022-02-18 17:26:00 -04:00
|
|
|
local output = results[1]
|
|
|
|
|
-- If the call returns nil then we continue with the next call
|
2022-04-04 14:59:30 -03:00
|
|
|
if output == nil then
|
2022-02-18 19:47:48 -03:00
|
|
|
break
|
|
|
|
|
end
|
2022-02-18 17:26:00 -04:00
|
|
|
-- If the call returns false then we exit the loop
|
2022-02-24 17:44:05 -03:00
|
|
|
if not output then
|
2022-02-18 19:47:48 -03:00
|
|
|
return false
|
|
|
|
|
end
|
2022-02-18 17:26:00 -04:00
|
|
|
-- If the call of type returnvalue returns noerror then we continue the loop
|
2022-02-18 19:47:48 -03:00
|
|
|
if info.returnValue then
|
|
|
|
|
if output == RETURNVALUE_NOERROR then
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
return output
|
|
|
|
|
end
|
2022-02-18 17:26:00 -04:00
|
|
|
-- We left the loop why have we reached the end
|
2024-04-16 16:55:26 -04:00
|
|
|
if index == eventsCount then
|
2022-02-18 19:47:48 -03:00
|
|
|
return unpack(results)
|
|
|
|
|
end
|
2022-02-18 17:26:00 -04:00
|
|
|
until true
|
|
|
|
|
-- Update the results for the next call
|
2024-04-16 16:55:26 -04:00
|
|
|
for i, value in pairs(updateableParams) do
|
|
|
|
|
args[i] = results[value]
|
2022-02-18 17:26:00 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-10-18 23:50:36 +02:00
|
|
|
end
|
2021-11-27 02:22:15 -04:00
|
|
|
})
|
2024-04-16 16:55:26 -04:00
|
|
|
|
|
|
|
|
hasEvent = setmetatable({}, {
|
|
|
|
|
__index = function(self, key)
|
|
|
|
|
local callback = callbacks[key]
|
|
|
|
|
if callback then
|
|
|
|
|
return EventData[callback].maxn > 0
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
-- For compatibility with the previous version.
|
|
|
|
|
EventCallback = Event()
|