Skip to content
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

bot.trade() hangs forever #2681

Open
wmantly opened this issue Jul 1, 2022 · 6 comments
Open

bot.trade() hangs forever #2681

wmantly opened this issue Jul 1, 2022 · 6 comments
Labels
possible bug Stage1 just created by someone new to the project, we don't know yet if it deserves an implementation / a f

Comments

@wmantly
Copy link

wmantly commented Jul 1, 2022

  • [X ] The FAQ doesn't contain a resolution to my issue

Versions

Detailed description of a problem

When calling bot.trade sometimes it will just hang. I have tracked the issue to this line: https://github.com/PrismarineJS/mineflayer/blob/master/lib/plugins/villager.js#L165
The last time updateSlot is called, countOfItemOneLeftToTake is not zero. upateSlots never gets called again and the whole app hangs.

What did you try yet?

I have edited the function in question to find, and midagte the issue. It still hangs.

let count_calls = 0;
let last_countOfItemOneLeftToTake = 9999;
const listener = (slot, oldItem, newItem) => {
  console.log('in listener count', ++count_calls)    
        // console.log('bot inventory', bot.inventory.slots.filter(e=>e).map(e=>`${e.slot}: ${e.count} ${e.displayName}`))

  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
  }
    console.log('in window listener', countOfItemOneLeftToTake,countOfItemTwoLeftToTake)
  if (countOfItemOneLeftToTake === 0 && countOfItemTwoLeftToTake === 0) {
    villager.off('updateSlot', listener)
    resolve()
  }

  console.log('villager', villager.slots.filter(e=>e).map(e=>`${e.slot}: ${e.count} ${e.displayName}`))
  if(last_countOfItemOneLeftToTake === countOfItemOneLeftToTake){
    console.log('work around worked!');
    villager.off('updateSlot', listener)
    resolve()
  }else{
    last_countOfItemOneLeftToTake = countOfItemOneLeftToTake;
  }


}

returns the following out, and just hangs forever

in listener count 1
in listener count 2
in listener count 3
in window listener 21 0
villager [
  '0: 64 Paper',
  '2: 1 Emerald',
  '9: 32 Emerald',
  '15: 29 Emerald',
  '41: 30 Steak',
  '43: 64 Paper',
  '44: 1 Sugar Cane'
]
in listener count 4
in window listener 21 0
villager [
  '0: 64 Paper',
  '2: 1 Emerald',
  '9: 32 Emerald',
  '15: 29 Emerald',
  '41: 30 Steak',
  '37: 43 Paper',
  '44: 1 Sugar Cane'
]
work around worked!

Did you try any method from the API?
Did you try any example? Any error from those?

Your current code

<for villagerID in getNearVillagers()>
  await bot.trade(villagerID, 0, 2)

Expected behavior

For it not to hang...

Additional context

Add any other context about the problem here.

@wmantly wmantly added possible bug Stage1 just created by someone new to the project, we don't know yet if it deserves an implementation / a f labels Jul 1, 2022
@wmantly wmantly changed the title bot.trade hangs bot.trade() hangs forever Jul 1, 2022
@tyjtyj
Copy link

tyjtyj commented Apr 19, 2023

Do you managed to resolved it ? I am stuck here too..

@wmantly
Copy link
Author

wmantly commented May 10, 2023

@tyjtyj nope, i haven't looked at it in a while. There seem to be a few places in the code base that have unresolved promises or errors. I think my best workaround was to have a 60-second time out on each "action" and start the beginning if the timeout wasn't cleared. I also have to make all my actions as idempotent as possible.

@tyjtyj
Copy link

tyjtyj commented May 11, 2023

@tyjtyj nope, i haven't looked at it in a while. There seem to be a few places in the code base that have unresolved promises or errors. I think my best workaround was to have a 60-second time out on each "action" and start the beginning if the timeout wasn't cleared. I also have to make all my actions as idempotent as possible.

This is temporary works for me. The traded item drop on the floor but it go pick up itself. Rather than random hung this works for now

				let villagerWindows = await bot.openVillager(villager)
				await bot.waitForTicks(1);
				Trade = villagerWindows.trades[tradeIndex];
				villagerWindows.selectedTrade = Trade
				if (Trade.tradeDisabled ){ villagerWindows.close();	break } 
				await putTradeRequirements (villagerWindows, Trade, 1)
				await bot.waitForTicks(1);
				bot.clickWindow(2, 0, 0)
				await bot.waitForTicks(1);
				villagerWindows.close();	

Seems everything hung after this .. only closing the windows works.
bot.clickWindow(2, 0, 0)

Sorry for mess up code.

@TheMcSebi
Copy link

Having the same issue. Sometimes it works after restarting the bot, but most of the time the window stays open indefinitely.

@TheMcSebi
Copy link

@wmantly The workaround you posted fixed the issue. Thanks!

@extremeheat
Copy link
Member

If you know the problem, feel free to open a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
possible bug Stage1 just created by someone new to the project, we don't know yet if it deserves an implementation / a f
Projects
None yet
Development

No branches or pull requests

4 participants