PrismarineJS / mineflayer-navigate

mineflayer plugin which gives bots a high level 3d navigating API using A*
59 stars 22 forks source link

handle onPathFound and other on* function in params of navigateTo #12

Closed rom1504 closed 11 years ago

rom1504 commented 11 years ago

It would be useful to be able to change those callbacks. Events are not very adapted some time. For exemple i'm doing bot.navigate.once("pathFound", and bot.navigate.once("cannotFind", in my function to get the nearest reachable mob and once any of these events has been caught the listener of the other one should be removed. I'm not sure how to do that and it might be possible, but it would be simpler if i could simply give callbacks to the navigateTo function.

vogonistic commented 11 years ago

Not the cutest of code, but you can manually remove the listeners as well.

function stopListeningToNavigator() {
  bot.navigate.removeListener('cannotFind', onCannotFind);
  bot.navigate.removeListener('pathFound', onPathFound);
}

function onPathFound() {
  stopListeningToNavigator();
  // your code here
}

function onCannotFind() {
  stopListeningToNavigator();
  // your code here
}

bot.navigate.on('pathFound', onPathFound);
bot.navigate.on('cannotFind', onCannotFind);
rom1504 commented 11 years ago

Yes I'm doing that right now, but it's a bit more complicated than this because my onPathFound and onCannotFind function are not always the same. So what i did is store them in global variables, but it's ugly and i'm not sure it cannot have unplanned behaviour (global variables :( )

andrewrk commented 11 years ago

fixed by 4771e362316864be220e44bf310d5c645915eea2