juliarn / npc-lib

Asynchronous, high-performance Minecraft NPC library for 1.8-1.21 servers.
MIT License
296 stars 50 forks source link

Do not keep player in trackedPlayers if the Pre track event was cance… #119

Closed Cryptite closed 5 months ago

Cryptite commented 6 months ago

This is perhaps not the the best way go to about it, but in our case this was causing an issue.

If you hadn't otherwise provided a tracking rule, but still wanted to cancel the pre track event, you could never subsequently track an NPC because the player was already in the trackedPlayers collection, despite having been cancelled.

derklaro commented 6 months ago

Nice catch! How about not adding the player to the list first, calling the event, check-adding him if not cancelled?

Pseudo code:

if !players.contains(p)
  if call(event).cancelled()
    return

  if players.add(p)
    return

  ...
Cryptite commented 6 months ago

Yeah I suppose contains could be slightly more performant than a guaranteed add and remove, which would have the set being reordered twice.