Solved How to check if entity is NPC and send message?

Discussion in 'Spigot Plugin Development' started by kinukin, Jan 31, 2020.

  1. I'm trying to make an npc send a message if a player is nearby. I'm using CitizensAPI.

    How can I check if an entity is a NPC that is close to a player?

    This is what I got so far:
    Code (Text):
        @EventHandler
        public void onNpcTalk(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            if(p.getLocation().distance(npclocation) < 10) {
                p.sendMessage("Hi welcome");
            }
        }
     
  2. I wouldn't do it quite like this, because every time a player moves a muscle within 10 blocks of the NPC it will send a message to the player, resulting in huge spam. Also, since you're running this task every time a player moves, you don't want to use Location.distance which uses a performance-heavy square root. You want location.distanceSquared, then square your threshold (10 --> 100) and check against that. As for your actual question, I don't really know anything about the Citizens API but the javadocs are here: https://jd.citizensnpcs.co/ On first impression it looks like you might be able to use ChatTrigger to make your NPC do this automatically but it doesn't have much documentation so I'm not sure.
     
  3. All citizens NPC have stored that in their Metadata so Entity#hasMetadata("NPC") should do the trick.
     
    • Winner Winner x 2
    • Useful Useful x 2