Skip to content

Commit fd70c49

Browse files
authored
fix: casks and kegs (opentibiabr#3344)
1 parent d95b21a commit fd70c49

1 file changed

Lines changed: 73 additions & 45 deletions

File tree

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,89 @@
1-
local targetIdList = {
2-
[25879] = { itemId = 285, transform = 266 },
3-
[25880] = { itemId = 283, transform = 236 },
4-
[25881] = { itemId = 284, transform = 239 },
5-
[25882] = { itemId = 284, transform = 7643 },
6-
[25883] = { itemId = 284, transform = 23375 },
7-
[25889] = { itemId = 285, transform = 268 },
8-
[25890] = { itemId = 283, transform = 237 },
9-
[25891] = { itemId = 284, transform = 238 },
10-
[25892] = { itemId = 284, transform = 23373 },
11-
[25899] = { itemId = 284, transform = 7642 },
12-
[25900] = { itemId = 284, transform = 23374 },
13-
[25903] = { itemId = 285, transform = 266 },
14-
[25904] = { itemId = 283, transform = 236 },
15-
[25905] = { itemId = 284, transform = 239 },
16-
[25906] = { itemId = 284, transform = 7643 },
17-
[25907] = { itemId = 284, transform = 23375 },
18-
[25908] = { itemId = 285, transform = 268 },
19-
[25909] = { itemId = 283, transform = 237 },
20-
[25910] = { itemId = 284, transform = 238 },
21-
[25911] = { itemId = 284, transform = 23373 },
22-
[25913] = { itemId = 284, transform = 7642 },
23-
[25914] = { itemId = 284, transform = 23374 },
1+
local requiredItems = { 283, 284, 285 }
2+
3+
local transformations = {
4+
{ fromItemId = 25879, toItemId = 266 },
5+
{ fromItemId = 25880, toItemId = 236 },
6+
{ fromItemId = 25881, toItemId = 239 },
7+
{ fromItemId = 25882, toItemId = 7643 },
8+
{ fromItemId = 25883, toItemId = 23375 },
9+
{ fromItemId = 25889, toItemId = 268 },
10+
{ fromItemId = 25890, toItemId = 237 },
11+
{ fromItemId = 25891, toItemId = 238 },
12+
{ fromItemId = 25892, toItemId = 23373 },
13+
{ fromItemId = 25899, toItemId = 7642 },
14+
{ fromItemId = 25900, toItemId = 23374 },
15+
{ fromItemId = 25903, toItemId = 266 },
16+
{ fromItemId = 25904, toItemId = 236 },
17+
{ fromItemId = 25905, toItemId = 239 },
18+
{ fromItemId = 25906, toItemId = 7643 },
19+
{ fromItemId = 25907, toItemId = 23375 },
20+
{ fromItemId = 25908, toItemId = 268 },
21+
{ fromItemId = 25909, toItemId = 237 },
22+
{ fromItemId = 25910, toItemId = 238 },
23+
{ fromItemId = 25911, toItemId = 23373 },
24+
{ fromItemId = 25913, toItemId = 7642 },
25+
{ fromItemId = 25914, toItemId = 23374 },
2426
}
2527

26-
local flasks = Action()
28+
local function processTransformation(player, item, charges, transformation)
29+
local transformedCount = 0
2730

28-
function flasks.onUse(player, item, fromPosition, target, toPosition, isHotkey)
29-
if not target or not target:getItem() then
31+
if charges <= 0 then
32+
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The item has no charges remaining.")
3033
return false
3134
end
3235

33-
local charges = target:getCharges()
34-
local itemCount = item:getCount()
35-
if itemCount > charges then
36-
itemCount = charges
36+
for _, requiredItemId in ipairs(requiredItems) do
37+
local itemCount = player:getItemCount(requiredItemId)
38+
39+
if itemCount > 0 then
40+
local transformable = math.min(itemCount, charges - transformedCount)
41+
42+
if transformable > 0 then
43+
player:removeItem(requiredItemId, transformable)
44+
player:addItem(transformation.toItemId, transformable)
45+
46+
transformedCount = transformedCount + transformable
47+
48+
if transformedCount >= charges then
49+
break
50+
end
51+
end
52+
end
3753
end
3854

39-
local targetId = targetIdList[target:getId()]
40-
if targetId and item:getId() == targetId.itemId and charges > 0 then
41-
local potMath = item:getCount() - itemCount
42-
local parent = item:getParent()
43-
if not (parent:isContainer() and parent:addItem(item:getId(), potMath)) then
44-
player:addItem(item:getId(), potMath, true)
55+
if transformedCount > 0 then
56+
local newCharges = charges - transformedCount
57+
58+
if newCharges > 0 then
59+
item:setAttribute(ITEM_ATTRIBUTE_CHARGES, newCharges)
60+
else
61+
item:remove(1)
4562
end
4663

47-
item:transform(targetId.transform, itemCount)
48-
charges = charges - itemCount
49-
target:transform(target:getId(), charges)
50-
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("Remaining %s charges.", charges))
64+
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Charges remaining: " .. newCharges)
65+
else
66+
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You need flasks to carry all that.")
67+
end
68+
69+
return true
70+
end
5171

52-
if charges == 0 then
53-
target:remove()
72+
local function onUseTransformation(player, item, position, target, toPosition, isHotkey)
73+
local charges = item:getAttribute(ITEM_ATTRIBUTE_CHARGES) or 0
74+
75+
for _, transformation in ipairs(transformations) do
76+
if item:getId() == transformation.fromItemId then
77+
return processTransformation(player, item, charges, transformation)
5478
end
55-
return true
5679
end
80+
5781
return false
5882
end
5983

60-
flasks:id(283, 284, 285)
61-
flasks:register()
84+
for _, transformation in ipairs(transformations) do
85+
local action = Action()
86+
action:id(transformation.fromItemId)
87+
action.onUse = onUseTransformation
88+
action:register()
89+
end

0 commit comments

Comments
 (0)