From ff48f7ea37ba84a0a68514422b98229f86d61fe7 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 12 Dec 2024 13:07:49 -0300 Subject: [PATCH] refactor: simplify and optimize CultsOnItemMoved event logic --- .../cults_of_tibia/event_on_item_moved.lua | 58 +++++++++---------- .../event_on_item_moved.lua | 1 - data/events/scripts/player.lua | 12 +++- .../eventcallbacks/player/on_report_bug.lua | 1 - 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/data-otservbr-global/scripts/quests/cults_of_tibia/event_on_item_moved.lua b/data-otservbr-global/scripts/quests/cults_of_tibia/event_on_item_moved.lua index 13ecaa37431..15fc805c37d 100644 --- a/data-otservbr-global/scripts/quests/cults_of_tibia/event_on_item_moved.lua +++ b/data-otservbr-global/scripts/quests/cults_of_tibia/event_on_item_moved.lua @@ -3,44 +3,42 @@ local callback = EventCallback("CultsOnItemMoved") function callback.onItemMoved(item, count, fromPosition, toPosition, fromCylinder, toCylinder) local fromPos = Position(33023, 31904, 14) local toPos = Position(33052, 31932, 15) - local removeItem = false if player:getPosition():isInRange(fromPos, toPos) and item:getId() == 23729 then local tile = Tile(toPosition) - if tile then - local tileBoss = tile:getTopCreature() - if tileBoss and tileBoss:isMonster() then - local bossName = tileBoss:getName():lower() - if bossName == "the remorseless corruptor" then - tileBoss:addHealth(-17000) - tileBoss:remove() - - local monster = Game.createMonster("The Corruptor of Souls", toPosition) - if not monster then - return false - end - - removeItem = true - monster:registerEvent("CheckTile") - - local storedHealth = Game.getStorageValue("healthSoul") - if storedHealth > 0 then - monster:addHealth(-(monster:getHealth() - storedHealth)) - end - - Game.setStorageValue("CheckTile", os.time() + 30) - elseif bossName == "the corruptor of souls" then - Game.setStorageValue("CheckTile", os.time() + 30) - removeItem = true - end - end + if not tile then + return + end + + local tileBoss = tile:getTopCreature() + if not (tileBoss and tileBoss:isMonster()) then + return end - if removeItem then + local bossName = tileBoss:getName():lower() + if bossName == "the remorseless corruptor" then + tileBoss:addHealth(-17000) + tileBoss:remove() + + local monster = Game.createMonster("The Corruptor of Souls", toPosition) + if not monster then + return + end + + monster:registerEvent("CheckTile") + + local storedHealth = Game.getStorageValue("healthSoul") + if storedHealth > 0 then + monster:addHealth(-(monster:getHealth() - storedHealth)) + end + + Game.setStorageValue("CheckTile", os.time() + 30) + item:remove(1) + elseif bossName == "the corruptor of souls" then + Game.setStorageValue("CheckTile", os.time() + 30) item:remove(1) end end - return true end callback:register() diff --git a/data-otservbr-global/scripts/quests/the_secret_library_quest/event_on_item_moved.lua b/data-otservbr-global/scripts/quests/the_secret_library_quest/event_on_item_moved.lua index d165d2be73a..01bb4ef715e 100644 --- a/data-otservbr-global/scripts/quests/the_secret_library_quest/event_on_item_moved.lua +++ b/data-otservbr-global/scripts/quests/the_secret_library_quest/event_on_item_moved.lua @@ -7,7 +7,6 @@ function callback.onItemMoved(item, count, fromPosition, toPosition, fromCylinde Game.setStorageValue(Storage.Quest.U11_80.TheSecretLibrary.SmallIslands.Turtle, os.time() + 10 * 60) item:remove(1) end - return true end callback:register() diff --git a/data/events/scripts/player.lua b/data/events/scripts/player.lua index 8e96828ac74..a8fe0bfb679 100644 --- a/data/events/scripts/player.lua +++ b/data/events/scripts/player.lua @@ -370,7 +370,9 @@ function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, return true end -function Player:onItemMoved(item, count, fromPosition, toPosition, fromCylinder, toCylinder) end +function Player:onItemMoved(item, count, fromPosition, toPosition, fromCylinder, toCylinder) + return true +end function Player:onMoveCreature(creature, fromPosition, toPosition) local player = creature:getPlayer() @@ -381,8 +383,12 @@ function Player:onMoveCreature(creature, fromPosition, toPosition) return true end -function Player:onReportRuleViolation(targetName, reportType, reportReason, comment, translation) end -function Player:onReportBug(message, position, category) end +function Player:onReportRuleViolation(targetName, reportType, reportReason, comment, translation) + return +end +function Player:onReportBug(message, position, category) + return true +end function Player:onTurn(direction) if self:getGroup():getAccess() and self:getDirection() == direction then diff --git a/data/scripts/eventcallbacks/player/on_report_bug.lua b/data/scripts/eventcallbacks/player/on_report_bug.lua index 15077b2759b..ede9ad0e4ac 100644 --- a/data/scripts/eventcallbacks/player/on_report_bug.lua +++ b/data/scripts/eventcallbacks/player/on_report_bug.lua @@ -24,7 +24,6 @@ function callback.playerOnReportBug(player, message, position, category) file:close() player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("Your report has been sent to %s.", SERVER_NAME)) - return true end callback:register()