Solved Detect if the player has attacked an npc

Discussion in 'Spigot Plugin Development' started by GamerNock_, Jun 3, 2017.

  1. Code to spawn NPC:
    Code (Text):
    public HashMap<Player, EntityPlayer> npcs = new HashMap<>();
       
        public void spawnnpc(Player p) {
            MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
            WorldServer nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle();
            EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, new GameProfile(UUID.randomUUID(), p.getName()), new PlayerInteractManager(nmsWorld));
            double x = p.getLocation().getX(); double y = p.getLocation().getY() + 3; double z = p.getLocation().getZ(); float yaw = p.getLocation().getYaw(); float pitch = p.getLocation().getPitch();
            npc.setLocation(x, y, z, yaw, pitch);
            PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
            connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, npc));
            connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
            npcs.put(p, npc);
            Bukkit.getScheduler().runTaskLater(this, new Runnable() {
               
                @Override
                public void run() {
                    connection.sendPacket(new PacketPlayOutEntityDestroy(npc.getId()));
                    connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, npc));
                    npcs.remove(p);
                }
            }, 2*20);
        }
    I need to detect if the player has attacked this NPC (EntityPlayer), does anyone know how to do this?
     
  2. U just need a PacketReader and than get the PacketPlayInUseEntity u can use protocollib for it or just watch this(german) example:
     
    • Informative Informative x 1
    • Useful Useful x 1
  3. Thankyou it works :)