diff --git a/data/events/scripts/creature.lua b/data/events/scripts/creature.lua index 063c6ec978..34f105a87d 100644 --- a/data/events/scripts/creature.lua +++ b/data/events/scripts/creature.lua @@ -1,37 +1,32 @@ function Creature:onChangeOutfit(outfit) - local onChangeMount = EventCallback.onChangeMount - if onChangeMount then - if not onChangeMount(self, outfit.lookMount) then + if hasEvent.onChangeMount then + if not Event.onChangeMount(self, outfit.lookMount) then return false end end - local onChangeOutfit = EventCallback.onChangeOutfit - if onChangeOutfit then - return onChangeOutfit(self, outfit) + if hasEvent.onChangeOutfit then + return Event.onChangeOutfit(self, outfit) end return true end function Creature:onAreaCombat(tile, isAggressive) - local onAreaCombat = EventCallback.onAreaCombat - if onAreaCombat then - return onAreaCombat(self, tile, isAggressive) + if hasEvent.onAreaCombat then + return Event.onAreaCombat(self, tile, isAggressive) end return RETURNVALUE_NOERROR end function Creature:onTargetCombat(target) - local onTargetCombat = EventCallback.onTargetCombat - if onTargetCombat then - return onTargetCombat(self, target) + if hasEvent.onTargetCombat then + return Event.onTargetCombat(self, target) end return RETURNVALUE_NOERROR end function Creature:onHear(speaker, words, type) - local onHear = EventCallback.onHeard - if onHear then - onHear(self, speaker, words, type) + if hasEvent.onHear then + Event.onHear(self, speaker, words, type) end end diff --git a/data/events/scripts/monster.lua b/data/events/scripts/monster.lua index c892e79417..dce3b6cc48 100644 --- a/data/events/scripts/monster.lua +++ b/data/events/scripts/monster.lua @@ -1,8 +1,7 @@ function Monster:onDropLoot(corpse) - local onDropLoot = EventCallback.onDropLoot - if onDropLoot then - onDropLoot(self, corpse) - end + if hasEvent.onDropLoot then + Event.onDropLoot(self, corpse) + end local player = Player(corpse:getCorpseOwner()) if player then player:updateKillTracker(self, corpse) @@ -10,9 +9,8 @@ function Monster:onDropLoot(corpse) end function Monster:onSpawn(position, startup, artificial) - local onSpawn = EventCallback.onSpawn - if onSpawn then - return onSpawn(self, position, startup, artificial) + if hasEvent.onSpawn then + return Event.onSpawn(self, position, startup, artificial) end return true end diff --git a/data/events/scripts/party.lua b/data/events/scripts/party.lua index 2d7dedceca..90dff228d1 100644 --- a/data/events/scripts/party.lua +++ b/data/events/scripts/party.lua @@ -1,47 +1,41 @@ function Party:onJoin(player) - local onJoin = EventCallback.onJoin - if onJoin then - return onJoin(self, player) + if hasEvent.onJoin then + return Event.onJoin(self, player) end return true end function Party:onLeave(player) - local onLeave = EventCallback.onLeave - if onLeave then - return onLeave(self, player) + if hasEvent.onLeave then + return Event.onLeave(self, player) end return true end function Party:onDisband() - local onDisband = EventCallback.onDisband - if onDisband then - return onDisband(self) + if hasEvent.onDisband then + return Event.onDisband(self) end return true end function Party:onInvite(player) - local onInvite = EventCallback.onInvite - if onInvite then - return onInvite(self, player) + if hasEvent.onInvite then + return Event.onInvite(self, player) end return true end function Party:onRevokeInvitation(player) - local onRevokeInvitation = EventCallback.onRevokeInvitation - if onRevokeInvitation then - return onRevokeInvitation(self, player) + if hasEvent.onRevokeInvitation then + return Event.onRevokeInvitation(self, player) end return true end function Party:onPassLeadership(player) - local onPassLeadership = EventCallback.onPassLeadership - if onPassLeadership then - return onPassLeadership(self, player) + if hasEvent.onPassLeadership then + return Event.onPassLeadership(self, player) end return true end @@ -69,6 +63,5 @@ function Party:onShareExperience(exp) end exp = math.ceil((exp * sharedExperienceMultiplier) / (#self:getMembers() + 1)) - local onShareExperience = EventCallback.onShareExperience - return onShareExperience and onShareExperience(self, exp, rawExp) or exp + return hasEvent.onShareExperience and Event.onShareExperience(self, exp, rawExp) or exp end diff --git a/data/events/scripts/player.lua b/data/events/scripts/player.lua index 74c3ce61e1..4299142a76 100644 --- a/data/events/scripts/player.lua +++ b/data/events/scripts/player.lua @@ -1,16 +1,14 @@ function Player:onBrowseField(position) - local onBrowseField = EventCallback.onBrowseField - if onBrowseField then - return onBrowseField(self, position) + if hasEvent.onBrowseField then + return Event.onBrowseField(self, position) end return true end function Player:onLook(thing, position, distance) local description = "" - local onLook = EventCallback.onLook - if onLook then - description = onLook(self, thing, position, distance, description) + if hasEvent.onLook then + description = Event.onLook(self, thing, position, distance, description) end if description ~= "" then @@ -20,9 +18,8 @@ end function Player:onLookInBattleList(creature, distance) local description = "" - local onLookInBattleList = EventCallback.onLookInBattleList - if onLookInBattleList then - description = onLookInBattleList(self, creature, distance, description) + if hasEvent.onLookInBattleList then + description = Event.onLookInBattleList(self, creature, distance, description) end if description ~= "" then @@ -32,9 +29,8 @@ end function Player:onLookInTrade(partner, item, distance) local description = "You see " .. item:getDescription(distance) - local onLookInTrade = EventCallback.onLookInTrade - if onLookInTrade then - description = onLookInTrade(self, partner, item, distance, description) + if hasEvent.onLookInTrade then + description = Event.onLookInTrade(self, partner, item, distance, description) end if description ~= "" then @@ -44,9 +40,8 @@ end function Player:onLookInShop(itemType, count) local description = "You see " - local onLookInShop = EventCallback.onLookInShop - if onLookInShop then - description = onLookInShop(self, itemType, count, description) + if hasEvent.onLookInShop then + description = Event.onLookInShop(self, itemType, count, description) end if description ~= "" then @@ -55,46 +50,40 @@ function Player:onLookInShop(itemType, count) end function Player:onLookInMarket(itemType) - local onLookInMarket = EventCallback.onLookInMarket - if onLookInMarket then - onLookInMarket(self, itemType) + if hasEvent.onLookInMarket then + Event.onLookInMarket(self, itemType) end end function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder) - local onMoveItem = EventCallback.onMoveItem - if onMoveItem then - return onMoveItem(self, item, count, fromPosition, toPosition, fromCylinder, toCylinder) + if hasEvent.onMoveItem then + return Event.onMoveItem(self, item, count, fromPosition, toPosition, fromCylinder, toCylinder) end return RETURNVALUE_NOERROR end function Player:onItemMoved(item, count, fromPosition, toPosition, fromCylinder, toCylinder) - local onItemMoved = EventCallback.onItemMoved - if onItemMoved then - onItemMoved(self, item, count, fromPosition, toPosition, fromCylinder, toCylinder) + if hasEvent.onItemMoved then + Event.onItemMoved(self, item, count, fromPosition, toPosition, fromCylinder, toCylinder) end end function Player:onMoveCreature(creature, fromPosition, toPosition) - local onMoveCreature = EventCallback.onMoveCreature - if onMoveCreature then - return onMoveCreature(self, creature, fromPosition, toPosition) + if hasEvent.onMoveCreature then + return Event.onMoveCreature(self, creature, fromPosition, toPosition) end return true end function Player:onReportRuleViolation(targetName, reportType, reportReason, comment, translation) - local onReportRuleViolation = EventCallback.onReportRuleViolation - if onReportRuleViolation then - onReportRuleViolation(self, targetName, reportType, reportReason, comment, translation) + if hasEvent.onReportRuleViolation then + Event.onReportRuleViolation(self, targetName, reportType, reportReason, comment, translation) end end function Player:onReportBug(message, position, category) - local onReportBug = EventCallback.onReportBug - if onReportBug then - return onReportBug(self, message, position, category) + if hasEvent.onReportBug then + return Event.onReportBug(self, message, position, category) end return true end @@ -108,33 +97,29 @@ function Player:onRotateItem(item) end function Player:onTurn(direction) - local onTurn = EventCallback.onTurn - if onTurn then - return onTurn(self, direction) + if hasEvent.onTurn then + return Event.onTurn(self, direction) end return true end function Player:onTradeRequest(target, item) - local onTradeRequest = EventCallback.onTradeRequest - if onTradeRequest then - return onTradeRequest(self, target, item) + if hasEvent.onTradeRequest then + return Event.onTradeRequest(self, target, item) end return true end function Player:onTradeAccept(target, item, targetItem) - local onTradeAccept = EventCallback.onTradeAccept - if onTradeAccept then - return onTradeAccept(self, target, item, targetItem) + if hasEvent.onTradeAccept then + return Event.onTradeAccept(self, target, item, targetItem) end return true end function Player:onTradeCompleted(target, item, targetItem, isSuccess) - local onTradeCompleted = EventCallback.onTradeCompleted - if onTradeCompleted then - onTradeCompleted(self, target, item, targetItem, isSuccess) + if hasEvent.onTradeCompleted then + Event.onTradeCompleted(self, target, item, targetItem, isSuccess) end end @@ -227,27 +212,24 @@ function Player:onPodiumEdit(item, outfit, direction, isVisible) end function Player:onGainExperience(source, exp, rawExp, sendText) - local onGainExperience = EventCallback.onGainExperience - return onGainExperience and onGainExperience(self, source, exp, rawExp, sendText) or exp + return hasEvent.onGainExperience and Event.onGainExperience(self, source, exp, rawExp) or exp end function Player:onLoseExperience(exp) - local onLoseExperience = EventCallback.onLoseExperience - return onLoseExperience and onLoseExperience(self, exp) or exp + return hasEvent.onLoseExperience and Event.onLoseExperience(self, exp) or exp end function Player:onGainSkillTries(skill, tries) - local onGainSkillTries = EventCallback.onGainSkillTries if not APPLY_SKILL_MULTIPLIER then - return onGainSkillTries and onGainSkillTries(self, skill, tries) or tries + return hasEvent.onGainSkillTries and Event.onGainSkillTries(self, skill, tries) or tries end if skill == SKILL_MAGLEVEL then tries = tries * configManager.getNumber(configKeys.RATE_MAGIC) - return onGainSkillTries and onGainSkillTries(self, skill, tries) or tries + return hasEvent.onGainSkillTries and Event.onGainSkillTries(self, skill, tries) or tries end tries = tries * configManager.getNumber(configKeys.RATE_SKILL) - return onGainSkillTries and onGainSkillTries(self, skill, tries) or tries + return hasEvent.onGainSkillTries and Event.onGainSkillTries(self, skill, tries) or tries end function Player:onWrapItem(item) @@ -277,8 +259,7 @@ function Player:onWrapItem(item) return end - local onWrapItem = EventCallback.onWrapItem - if not onWrapItem or onWrapItem(self, item) then + if not hasEvent.onWrapItem or Event.onWrapItem(self, item) then local oldId = item:getId() item:remove(1) local item = tile:addItem(wrapId) @@ -289,9 +270,8 @@ function Player:onWrapItem(item) end function Player:onInventoryUpdate(item, slot, equip) - local onInventoryUpdate = EventCallback.onInventoryUpdate - if onInventoryUpdate then - onInventoryUpdate(self, item, slot, equip) + if hasEvent.onInventoryUpdate then + Event.onInventoryUpdate(self, item, slot, equip) end end diff --git a/data/scripts/eventcallbacks/monster/default_onDropLoot.lua b/data/scripts/events/monster/default_onDropLoot.lua similarity index 91% rename from data/scripts/eventcallbacks/monster/default_onDropLoot.lua rename to data/scripts/events/monster/default_onDropLoot.lua index c6d9e012e4..24c88f31ca 100644 --- a/data/scripts/eventcallbacks/monster/default_onDropLoot.lua +++ b/data/scripts/events/monster/default_onDropLoot.lua @@ -1,6 +1,6 @@ -local ec = EventCallback +local event = Event() -ec.onDropLoot = function(self, corpse) +event.onDropLoot = function(self, corpse) if configManager.getNumber(configKeys.RATE_LOOT) == 0 then return end @@ -39,4 +39,4 @@ ec.onDropLoot = function(self, corpse) end end -ec:register() +event:register() diff --git a/data/scripts/eventcallbacks/player/default_onGainExperience.lua b/data/scripts/events/player/default_onGainExperience.lua similarity index 97% rename from data/scripts/eventcallbacks/player/default_onGainExperience.lua rename to data/scripts/events/player/default_onGainExperience.lua index f327f63aad..8f92568c1c 100644 --- a/data/scripts/eventcallbacks/player/default_onGainExperience.lua +++ b/data/scripts/events/player/default_onGainExperience.lua @@ -33,7 +33,7 @@ local function useStamina(player) player:setStamina(staminaMinutes) end -local default = EventCallback +local default = Event() function default.onGainExperience(player, source, exp, rawExp, sendText) if not source or source:isPlayer() then @@ -74,7 +74,7 @@ default:register() -- For this event, we use the trigger index math.huge so that this event is called last, thus ensuring that the -- experience message is sent to the client with the correct value. -local message = EventCallback +local message = Event() function message.onGainExperience(player, source, exp, rawExp, sendText) if sendText and exp ~= 0 then diff --git a/data/scripts/eventcallbacks/player/default_onLook.lua b/data/scripts/events/player/default_onLook.lua similarity index 94% rename from data/scripts/eventcallbacks/player/default_onLook.lua rename to data/scripts/events/player/default_onLook.lua index 882375f4da..6ec046a837 100644 --- a/data/scripts/eventcallbacks/player/default_onLook.lua +++ b/data/scripts/events/player/default_onLook.lua @@ -1,6 +1,6 @@ -local ec = EventCallback +local event = Event() -ec.onLook = function(self, thing, position, distance, description) +event.onLook = function(self, thing, position, distance, description) local description = "You see " .. thing:getDescription(distance) if self:getGroup():getAccess() then if thing:isItem() then @@ -54,4 +54,4 @@ ec.onLook = function(self, thing, position, distance, description) return description end -ec:register() +event:register() diff --git a/data/scripts/eventcallbacks/player/default_onLookInBattleList.lua b/data/scripts/events/player/default_onLookInBattleList.lua similarity index 88% rename from data/scripts/eventcallbacks/player/default_onLookInBattleList.lua rename to data/scripts/events/player/default_onLookInBattleList.lua index 39bcfdf21c..0e04f38261 100644 --- a/data/scripts/eventcallbacks/player/default_onLookInBattleList.lua +++ b/data/scripts/events/player/default_onLookInBattleList.lua @@ -1,6 +1,6 @@ -local ec = EventCallback +local event = Event() -ec.onLookInBattleList = function(self, creature, distance) +event.onLookInBattleList = function(self, creature, distance) local description = "You see " .. creature:getDescription(distance) if self:getGroup():getAccess() then local str = "%s\nHealth: %d / %d" @@ -23,4 +23,4 @@ ec.onLookInBattleList = function(self, creature, distance) return description end -ec:register() +event:register() diff --git a/data/scripts/eventcallbacks/player/default_onLookInMarket.lua b/data/scripts/events/player/default_onLookInMarket.lua similarity index 98% rename from data/scripts/eventcallbacks/player/default_onLookInMarket.lua rename to data/scripts/events/player/default_onLookInMarket.lua index cc766356c8..d3ddf292a9 100644 --- a/data/scripts/eventcallbacks/player/default_onLookInMarket.lua +++ b/data/scripts/events/player/default_onLookInMarket.lua @@ -1,9 +1,9 @@ local showAtkWeaponTypes = {WEAPON_CLUB, WEAPON_SWORD, WEAPON_AXE, WEAPON_DISTANCE} local showDefWeaponTypes = {WEAPON_CLUB, WEAPON_SWORD, WEAPON_AXE, WEAPON_DISTANCE, WEAPON_SHIELD} -local ec = EventCallback +local event = Event() -ec.onLookInMarket = function(self, itemType) +event.onLookInMarket = function(self, itemType) local response = NetworkMessage() response:addByte(0xF8) response:addU16(itemType:getClientId()) @@ -308,4 +308,4 @@ ec.onLookInMarket = function(self, itemType) response:sendToPlayer(self) end -ec:register() +event:register() diff --git a/data/scripts/eventcallbacks/player/default_onLookInShop.lua b/data/scripts/events/player/default_onLookInShop.lua similarity index 88% rename from data/scripts/eventcallbacks/player/default_onLookInShop.lua rename to data/scripts/events/player/default_onLookInShop.lua index 30ccfd66dd..ec8f97168f 100644 --- a/data/scripts/eventcallbacks/player/default_onLookInShop.lua +++ b/data/scripts/events/player/default_onLookInShop.lua @@ -1,6 +1,6 @@ -local ec = EventCallback +local event = Event() -ec.onLookInShop = function(self, itemType, count, description) +event.onLookInShop = function(self, itemType, count, description) local description = "You see " .. itemType:getItemDescription() if self:getGroup():getAccess() then description = string.format("%s\nItem ID: %d", description, itemType:getId()) @@ -22,4 +22,4 @@ ec.onLookInShop = function(self, itemType, count, description) return description end -ec:register() +event:register() diff --git a/data/scripts/eventcallbacks/player/default_onMoveItem.lua b/data/scripts/events/player/default_onMoveItem.lua similarity index 91% rename from data/scripts/eventcallbacks/player/default_onMoveItem.lua rename to data/scripts/events/player/default_onMoveItem.lua index 987e67b4ba..67a7fb5eeb 100644 --- a/data/scripts/eventcallbacks/player/default_onMoveItem.lua +++ b/data/scripts/events/player/default_onMoveItem.lua @@ -1,6 +1,6 @@ -local ec = EventCallback +local event = Event() -ec.onMoveItem = function(self, item, count, fromPosition, toPosition, fromCylinder, toCylinder) +event.onMoveItem = function(self, item, count, fromPosition, toPosition, fromCylinder, toCylinder) if item:getAttribute("wrapid") ~= 0 then local tile = Tile(toPosition) if (fromPosition.x ~= CONTAINER_POSITION and toPosition.x ~= CONTAINER_POSITION) or tile and not tile:getHouse() then @@ -45,4 +45,4 @@ ec.onMoveItem = function(self, item, count, fromPosition, toPosition, fromCylind return RETURNVALUE_NOERROR end -ec:register() +event:register() diff --git a/data/scripts/eventcallbacks/player/default_onReportBug.lua b/data/scripts/events/player/default_onReportBug.lua similarity index 90% rename from data/scripts/eventcallbacks/player/default_onReportBug.lua rename to data/scripts/events/player/default_onReportBug.lua index 503c91bf37..ac0e2f805d 100644 --- a/data/scripts/eventcallbacks/player/default_onReportBug.lua +++ b/data/scripts/events/player/default_onReportBug.lua @@ -1,6 +1,6 @@ -local ec = EventCallback +local event = Event() -ec.onReportBug = function(self, message, position, category) +event.onReportBug = function(self, message, position, category) if self:getAccountType() == ACCOUNT_TYPE_NORMAL then return false end @@ -28,4 +28,4 @@ ec.onReportBug = function(self, message, position, category) return true end -ec:register() +event:register() diff --git a/data/scripts/eventcallbacks/player/default_onReportRuleViolation.lua b/data/scripts/events/player/default_onReportRuleViolation.lua similarity index 90% rename from data/scripts/eventcallbacks/player/default_onReportRuleViolation.lua rename to data/scripts/events/player/default_onReportRuleViolation.lua index b2b1db35df..35afbc0735 100644 --- a/data/scripts/eventcallbacks/player/default_onReportRuleViolation.lua +++ b/data/scripts/events/player/default_onReportRuleViolation.lua @@ -7,9 +7,9 @@ local function hasPendingReport(name, targetName, reportType) return false end -local ec = EventCallback +local event = Event() -ec.onReportRuleViolation = function(self, targetName, reportType, reportReason, comment, translation) +event.onReportRuleViolation = function(self, targetName, reportType, reportReason, comment, translation) local name = self:getName() if hasPendingReport(name, targetName, reportType) then self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your report is being processed.") @@ -37,4 +37,4 @@ ec.onReportRuleViolation = function(self, targetName, reportType, reportReason, self:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("Thank you for reporting %s. Your report will be processed by %s team as soon as possible.", targetName, configManager.getString(configKeys.SERVER_NAME))) end -ec:register() +event:register() diff --git a/data/scripts/eventcallbacks/player/default_onRotateItem.lua b/data/scripts/events/player/default_onRotateItem.lua similarity index 85% rename from data/scripts/eventcallbacks/player/default_onRotateItem.lua rename to data/scripts/events/player/default_onRotateItem.lua index fe7696bc31..f5a8489292 100644 --- a/data/scripts/eventcallbacks/player/default_onRotateItem.lua +++ b/data/scripts/events/player/default_onRotateItem.lua @@ -1,4 +1,4 @@ -local ec = EventCallback +local ec = Event() ec.onRotateItem = function(self, item) local newId = item:getType():getRotateTo() diff --git a/data/scripts/eventcallbacks/player/default_onUpdateStorage.lua b/data/scripts/events/player/default_onUpdateStorage.lua similarity index 96% rename from data/scripts/eventcallbacks/player/default_onUpdateStorage.lua rename to data/scripts/events/player/default_onUpdateStorage.lua index e5ffb7fbd6..7bf2a6bbad 100644 --- a/data/scripts/eventcallbacks/player/default_onUpdateStorage.lua +++ b/data/scripts/events/player/default_onUpdateStorage.lua @@ -1,6 +1,6 @@ local lastQuestUpdate = {} -local ec = EventCallback +local ec = Event() ec.onUpdateStorage = function(creature, key, value, oldValue, isSpawn) if isSpawn then diff --git a/data/scripts/lib/event_callbacks.lua b/data/scripts/lib/event_callbacks.lua index e9f3bc55d3..956037b55a 100644 --- a/data/scripts/lib/event_callbacks.lua +++ b/data/scripts/lib/event_callbacks.lua @@ -1,8 +1,8 @@ local unpack = unpack local pack = table.pack -local EventCallbackData, callbacks, updateableParameters, autoID = {}, {}, {}, 0 --- This metatable creates an auto-configuration mechanism to create new types of EventCallbacks +local EventData, callbacks, updateableParameters, autoID = {}, {}, {}, 0 +-- This metatable creates an auto-configuration mechanism to create new types of Events local ec = setmetatable({}, { __newindex = function(self, key, value) autoID = autoID + 1 callbacks[key] = autoID @@ -16,11 +16,10 @@ local ec = setmetatable({}, { __newindex = function(self, key, value) end updateableParameters[autoID] = update callbacks[autoID] = info - EventCallbackData[autoID] = {maxn = 0} - EVENT_CALLBACK_LAST = autoID + EventData[autoID] = {maxn = 0} end}) ---@ Definitions of valid EventCallback types to hook according to the given field name +--@ Definitions of valid Event types to hook according to the given field name --@ The fields within the assigned table, allow to save arbitrary information -- Creature ec.onChangeOutfit = {} @@ -65,37 +64,7 @@ ec.onSpellCheck = {} ec.onDropLoot = {} ec.onSpawn = {} -EventCallback = { - register = function(self, triggerIndex) - if isScriptsInterface() then - local eventType = rawget(self, 'eventType') - local callback = rawget(self, 'callback') - if not eventType or not callback then - debugPrint("[Warning - EventCallback::register] need to setup a callback before you can register.") - return - end - - local eventData = EventCallbackData[eventType] - eventData.maxn = #eventData + 1 - eventData[eventData.maxn] = { - callback = callback, - triggerIndex = tonumber(triggerIndex) or 0 - } - table.sort(eventData, function(ecl, ecr) return ecl.triggerIndex < ecr.triggerIndex end) - self.eventType = nil - self.callback = nil - end - end, - - clear = function(self) - EventCallbackData = {} - for i = 1, EVENT_CALLBACK_LAST do - EventCallbackData[i] = {maxn = 0} - end - end -} - -setmetatable(EventCallback, { +local EventMeta = { __newindex = function(self, key, callback) if not isScriptsInterface() then return @@ -103,40 +72,71 @@ setmetatable(EventCallback, { local eventType = callbacks[key] if not eventType then - debugPrint(string.format("[Warning - EventCallback::%s] is not a valid callback.", key)) + debugPrint(string.format("[Warning - Event::%s] is not a valid callback.", key)) return end if type(callback) ~= "function" then - debugPrint(string.format("[Warning - EventCallback::%s] a function is expected.", key)) + debugPrint(string.format("[Warning - Event::%s] a function is expected.", key)) return end rawset(self, 'eventType', eventType) rawset(self, 'callback', callback) + 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) end, __index = function(self, key) local callback = callbacks[key] if not callback then - if not isScriptsInterface() then - return - end - - return rawget(self, key) - end - - local eventData = EventCallbackData[callback] - local maxn = eventData.maxn - if maxn == 0 then return end + local events = EventData[callback] + local eventsCount = events.maxn + local updateableParams = updateableParameters[callback] return function(...) local results, args, info = {}, pack(...), callbacks[callback] - for index = 1, maxn do + for index = 1, eventsCount do repeat - results = {eventData[index].callback(unpack(args))} + results = {events[index].callback(unpack(args))} local output = results[1] -- If the call returns nil then we continue with the next call if output == nil then @@ -154,15 +154,27 @@ setmetatable(EventCallback, { return output end -- We left the loop why have we reached the end - if index == eventData.maxn then + if index == eventsCount then return unpack(results) end until true -- Update the results for the next call - for index, value in pairs(updateableParameters[callback]) do - args[index] = results[value] + for i, value in pairs(updateableParams) do + args[i] = results[value] end end end end }) + +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() diff --git a/data/scripts/monsters/cobra_flask.lua b/data/scripts/monsters/cobra_flask.lua index 11957f7ebe..7ccad59c63 100644 --- a/data/scripts/monsters/cobra_flask.lua +++ b/data/scripts/monsters/cobra_flask.lua @@ -1,8 +1,8 @@ local cobras = {"cobra scout", "cobra vizier", "cobra assassin"} -local ev = EventCallback +local event = Event() -function ev.onSpawn(monster, position, startup, artificial) +function event.onSpawn(monster, position, startup, artificial) if table.contains(cobras, monster:getName():lower()) then local storage = Game.getStorageValue(GlobalStorageKeys.cobraBastionFlask) if storage then @@ -17,7 +17,7 @@ function ev.onSpawn(monster, position, startup, artificial) return true end -ev:register(-1) +event:register(-1) local cobraFlask = Action() diff --git a/data/talkactions/scripts/reload.lua b/data/talkactions/scripts/reload.lua index 2ba4e88cd3..35c8cabafa 100644 --- a/data/talkactions/scripts/reload.lua +++ b/data/talkactions/scripts/reload.lua @@ -70,9 +70,9 @@ function onSay(player, words, param) return false end - -- need to clear EventCallback.data or we end up having duplicated events on /reload scripts + -- need to clear Event.data or we end up having duplicated events on /reload scripts if table.contains({RELOAD_TYPE_SCRIPTS, RELOAD_TYPE_ALL}, reloadType) then - EventCallback:clear() + Event:clear() Game.clearQuests() end diff --git a/src/luascript.cpp b/src/luascript.cpp index 480193fa3f..97eedbad67 100644 --- a/src/luascript.cpp +++ b/src/luascript.cpp @@ -4079,7 +4079,7 @@ int LuaScriptInterface::luaIsScriptsInterface(lua_State* L) if (getScriptEnv()->getScriptInterface() == &g_scripts->getScriptInterface()) { pushBoolean(L, true); } else { - reportErrorFunc(L, "EventCallback: can only be called inside (data/scripts/)"); + reportErrorFunc(L, "Event: can only be called inside (data/scripts/)"); pushBoolean(L, false); } return 1; diff --git a/src/script.cpp b/src/script.cpp index 099c1488a8..457e7c1dfc 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -29,7 +29,7 @@ bool Scripts::loadScripts(std::string folderName, bool isLib, bool reload) std::string disable = ("#"); for (fs::recursive_directory_iterator it(dir); it != endit; ++it) { auto fn = it->path().parent_path().filename(); - if ((fn == "lib" && !isLib) || fn == "events") { + if (fn == "lib" && !isLib) { continue; } if (fs::is_regular_file(*it) && it->path().extension() == ".lua") {