-
-
Notifications
You must be signed in to change notification settings - Fork 945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix several bugs in villager trading #3230
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -391,15 +391,20 @@ function inject (bot, { hideErrors }) { | |
} | ||
|
||
function tradeMatch (limitItem, targetItem) { | ||
return targetItem.type === limitItem.type && targetItem.count >= limitItem.count | ||
return ( | ||
targetItem !== null && | ||
limitItem !== null && | ||
targetItem.type === limitItem.type && | ||
targetItem.count >= limitItem.count | ||
) | ||
} | ||
|
||
function expectTradeUpdate (window) { | ||
const trade = window.selectedTrade | ||
const hasItem = !!window.slots[2] | ||
|
||
if (hasItem !== tradeMatch(trade.inputItem1, window.slots[0])) { | ||
if (trade.hasItems2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a typo, the field is called |
||
if (trade.hasItem2) { | ||
return hasItem !== tradeMatch(trade.inputItem2, window.slots[1]) | ||
} | ||
return true | ||
|
@@ -418,7 +423,7 @@ function inject (bot, { hideErrors }) { | |
} | ||
} else if (window.type === 'minecraft:merchant') { | ||
const toUpdate = [] | ||
if (slot <= 2 && !window.selectedTrade.tradeDisabled && expectTradeUpdate(window)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
if (slot <= 1 && !window.selectedTrade.tradeDisabled && expectTradeUpdate(window)) { | ||
toUpdate.push(once(bot.currentWindow, 'updateSlot:2')) | ||
} | ||
if (slot === 2) { | ||
|
@@ -427,6 +432,12 @@ function inject (bot, { hideErrors }) { | |
} | ||
} | ||
await Promise.all(toUpdate) | ||
|
||
if (slot === 2 && !window.selectedTrade.tradeDisabled && expectTradeUpdate(window)) { | ||
// After the trade goes through, if the inputs are still satisfied, | ||
// expect another update in slot 2 | ||
await once(bot.currentWindow, 'updateSlot:2') | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the trade loop, if the requirements of a trade are still satisfied after |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,8 +141,11 @@ function inject (bot, { version }) { | |
itemCount2 = villager.count(Trade.inputItem2.type, Trade.inputItem2.metadata) | ||
hasEnoughItem2 = itemCount2 >= Trade.inputItem2.count * count | ||
} | ||
if (!hasEnoughItem1 || !hasEnoughItem2) { | ||
throw new Error('Not enough items to trade') | ||
if (!hasEnoughItem1) { | ||
throw new Error('Not enough item 1 to trade') | ||
} | ||
if (!hasEnoughItem2) { | ||
throw new Error('Not enough item 2 to trade') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log a bit more information here. I'm happy to leave this out if it amounts to an unnecessary breaking change. |
||
} | ||
|
||
selectTrade(choice) | ||
|
@@ -152,31 +155,12 @@ function inject (bot, { version }) { | |
if (Trade.hasItem2) proms.push(once(villager, 'updateSlot:1')) | ||
if (bot.supportFeature('setSlotAsTransaction')) { | ||
proms.push(once(villager, 'updateSlot:2')) | ||
await new Promise((resolve, reject) => { | ||
let countOfItemOneLeftToTake = itemCount1 > 64 ? 64 : itemCount1 | ||
let countOfItemTwoLeftToTake = 0 | ||
if (Trade.hasItem2) { | ||
countOfItemTwoLeftToTake = itemCount2 > 64 ? 64 : itemCount2 | ||
} | ||
const listener = (slot, oldItem, newItem) => { | ||
if (!(slot >= villager.inventoryStart && slot <= villager.inventoryEnd)) return | ||
if (newItem === null) { | ||
if (oldItem.type === Trade.inputItem1.type) countOfItemOneLeftToTake -= oldItem.count | ||
else if (Trade.hasItem2 && oldItem.type === Trade.inputItem2.type) countOfItemTwoLeftToTake -= oldItem.count | ||
} | ||
if (countOfItemOneLeftToTake === 0 && countOfItemTwoLeftToTake === 0) { | ||
villager.off('updateSlot', listener) | ||
resolve() | ||
} | ||
} | ||
villager.on('updateSlot', listener) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bot consistently gets hung up here, and I honestly can't figure out what the purpose of this is. It's possible that it was necessary before some of the other changes I made. @nickelpro do you remember by chance? |
||
} | ||
await Promise.all(proms) | ||
} | ||
|
||
for (let i = 0; i < count; i++) { | ||
await putRequirements(villager, Trade, count) | ||
await putRequirements(villager, Trade) | ||
// ToDo: See if this does anything kappa | ||
Trade.nbTradeUses++ | ||
if (Trade.maximumNbTradeUses - Trade.nbTradeUses === 0) { | ||
|
@@ -215,24 +199,25 @@ function inject (bot, { version }) { | |
} | ||
} | ||
|
||
async function putRequirements (window, Trade, count) { | ||
async function putRequirements (window, Trade) { | ||
const [slot1, slot2] = window.slots | ||
const { stackSize: stackSize1, type: type1, metadata: metadata1 } = Trade.inputItem1 | ||
const tradeCount1 = Trade.realPrice | ||
const neededCount1 = Math.min(stackSize1, tradeCount1 * count) | ||
|
||
const input1 = !slot1 | ||
? neededCount1 | ||
: (slot1.count < tradeCount1 ? neededCount1 - slot1.count : 0) | ||
await deposit(window, type1, metadata1, input1, 0) | ||
const { type: type1, metadata: metadata1 } = Trade.inputItem1 | ||
|
||
const input1 = slot1 | ||
? Math.max(0, Trade.realPrice - slot1.count) | ||
: Trade.realPrice | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only put the requirements for a single trade at a time. This seems to greatly increase reliability, especially for trades with multiple inputs. |
||
if (input1) { | ||
await deposit(window, type1, metadata1, input1, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only call |
||
} | ||
if (Trade.hasItem2) { | ||
const { count: tradeCount2, stackSize: stackSize2, type: type2, metadata: metadata2 } = Trade.inputItem2 | ||
const needCount2 = Math.min(stackSize2, tradeCount2 * count) | ||
const { count: tradeCount2, type: type2, metadata: metadata2 } = Trade.inputItem2 | ||
|
||
const input2 = !slot2 | ||
? needCount2 | ||
: (slot2.count < tradeCount2 ? needCount2 - slot2.count : 0) | ||
await deposit(window, type2, metadata2, input2, 1) | ||
const input2 = slot2 | ||
? Math.max(0, tradeCount2 - slot2.count) | ||
: tradeCount2 | ||
if (input2) { | ||
await deposit(window, type2, metadata2, input2, 1) | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes,
tradeMatch
is called with a nulltargetItem
. If there is nothing in thetargetItem
slot, then the trade requirements cannot be satisfied, so return false in that case.