How to detect an NPC?

Discussion in 'Spigot Plugin Development' started by Korex, May 10, 2016.

  1. Hey guys,
    usually you know the Plugin Citiziens.
    With the help of this plugin you can spawn fake NPC's.

    You've got an option where u can configurate things like sending messages when you are next to the NPC.

    But I dont know how to code that. I will sending a Message when im next to an NPC/an other player.

    I think you can do that with the PlayerMoveEvent but I dont know how to check if there's an NPC next to you.

    Hope you can help me ;)
  2. You could have a runnable running comparing the distanceSquared between every player and your NPC every now and then, I guess. No need for PlayerMoveEvent in this case.

    BTW - wow, I haven't been around for a while.
  3. NathanWolf


    Not sure which part you want help with, but Citizens puts an "NPC" metadata tag on NPC entities so you can easily identify them.
    • Agree Agree x 1
  4. Yes but how can I detect them for example with the PlayerMoveEvent?
  5. I'm not sure if Citizens' NPC is considered an Entity (or if it's just a fake entity controlled by packets sent to client only), but there is an Entity#getNearbyEntities method.

    Like I said though, you probably should keep your NPCs stored in some form (HashSet for example) and compare their Location to the Locations of your players.
    • Agree Agree x 2
  6. No an Citiziens NPC is not an fake NCP. (tested it).
  7. NathanWolf


    Well you have two choices - scan for nearby entities or scan a list of know NPC locations.

    Neither of these will be super efficient, so keep in mind that the PlayerMoveEvent is called a LOT!