PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
4.95k stars 904 forks source link

Generic event for entity movements #63

Closed yocontra closed 11 years ago

yocontra commented 11 years ago

Is there a way to listen for all entity changes? I'm trying to make a radar in the browser (synced over websockets) that updates any time a player moves, a player joins, a mob spawns, a mob moves, a player leaves, etc.

I can listen to all of these events on their own but it seems like a generic event that gets triggered after all relevant entity changes would be useful

andrewrk commented 11 years ago

Could you propose an event name and what arguments it would receive?

yocontra commented 11 years ago

Maybe this could be better implemented as a radar plugin... it seems specific to that use case

Here is my code right now for a basic CLI radar

client.on 'entitySpawn', (entity) ->
  return unless entity.type is 'player'

  for uname, p of client.game.players when p.entity
    x1 = client.entity.position.x
    y1 = client.entity.position.y
    z1 = client.entity.position.z

    x2 = p.entity.position.x
    y2 = p.entity.position.y
    z2 = p.entity.position.z

    x3 = Math.abs (x1-x2)^2
    y3 = Math.abs (y1-y2)^2
    z3 = Math.abs (z1-z2)^2

    prem = x3+z3#+y3
    dist = Math.floor Math.sqrt prem
    console.log "#{uname}: #{p.entity.position} - #{dist}"
andrewrk commented 11 years ago

I think you just want to listen to entitySpawn, entityMoved, and entityGone.

Note that you can tell when players join and leave but you do not know their coordinates unless they are within range of your bot.

andrewrk commented 11 years ago

When a player comes in range of your bot, you'll get entitySpawn. From that point on you'll get entityMoved updates until you receive an entityGone event. I'll keep this issue open until I add this info to the docs.

yocontra commented 11 years ago

Any chance of getting a distance property on the entity object? I have the calculations in my code above - uncomment the y3 if you want to take the y axis into account

yocontra commented 11 years ago

Looks like https://github.com/superjoe30/node-vec3/blob/master/index.js#L81 does a better job - I'll use the built in functionality

andrewrk commented 11 years ago

Points are all instances of vec3 (See https://github.com/superjoe30/mineflayer/blob/master/doc/api.md#mineflayervec3)

So yes, you can do client.entity.position.distanceTo(p.entity.position).

andrewrk commented 11 years ago

Feel free to make pull requests to vec3 if you want more vector functions :)

yocontra commented 11 years ago

Solves my issue - thanks!