Help with PathFinding // Making NPC damage-able

Discussion in 'Plugin Development' started by Mindlessmink, Sep 17, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Mindlessmink

    Hey there, So. I'm trying to create an NPC and I have a few questions.

    [1] I'd like to know how to use PathFinderGoals since I've never really tried working with NPCs.
    [2] How would I make an NPC damage-able?

    New Test JavaProject to test. This is what I've been trying. [READ COMMENTS]

    Code:
     private EntityPlayer npc;
     public void onEnable() {
     WorldServer nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle();
     MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
     npc = new EntityPlayer(nmsServer, nmsWorld, new GameProfile(UUID.randomUUID(), "Aur4ch3ker4dfd5"),
     new PlayerInteractManager(nmsWorld));
     Bukkit.getPluginCommand("MAC").setExecutor(new MACCommand(this));
     Bukkit.getPluginCommand("teleport").setExecutor(new TPCommand());
     Bukkit.getPluginCommand("staff").setExecutor(new ADMINCommand());
     Bukkit.getPluginManager().registerEvents(this, this);
     Bukkit.getPluginManager().registerEvents(new FlyHack(this), this);
     Bukkit.getPluginManager().registerEvents(new AntiAntiKnockback(this), this);
     saveDefaultConfig();
     }
     int count = 0;
     @SuppressWarnings({ "unchecked", "rawtypes" })
     ArrayList<String> checking = new ArrayList();
     
     @EventHandler
     public void on(EntityDamageByEntityEvent e){
     if (e.getEntity() instanceof Player){
     Player p = (Player)e.getEntity();
     if (e.getDamager() instanceof Player){
     Player k = (Player)e.getDamager();
     if (p.getName().equalsIgnoreCase("Aur4ch3ker4dfd5")){
     count++;
     if (count > 4){
     BC("KillAura / ForceField", k);
     //I obviously can't do this because this returns when an Entity takes damage by another Entity, however. I cannot damage this Entity.
     }
     }
     }
     }
     }
     
     @EventHandler
     public void on(PlayerMoveEvent e){
     Player p = e.getPlayer();
     //This is where I'd make the NPC follow the Player using PathFinderGoals.
     npc.setInvisible(false);
     this.npc.setSneaking(true);
     final PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
     connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER,
     new EntityPlayer[] { this.npc }));
     connection.sendPacket(new PacketPlayOutNamedEntitySpawn(this.npc));
     this.checking.add(p.getName());
     }
     
     public static void StuckInGround(Player player) {
     player.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix
     + "&7Hey there! It looks like you might be stuck in the ground. Please press SHIFT + Right Click"));
     }
     public int VL = 1;
     public void BC(String hack, Player p) {
     if (VL < 6) {
     Bukkit.broadcast(
     ChatColor.translateAlternateColorCodes('&',
     prefix + " &e" + p.getName() + " &6is &eSUSPECTED &6for &4" + hack.toString()
     + " &6(ping=" + ((CraftPlayer) p).getHandle().ping + ") &eVL" + VL++),
     "mah.notify");
     return;
     }
     if (VL > 5 && VL < 10) {
     Bukkit.broadcast(
     ChatColor
     .translateAlternateColorCodes('&',
     prefix + " &e" + p.getName() + " &6is &cPROBABLY &6using &4" + hack.toString()
     + " &6(ping=" + ((CraftPlayer) p).getHandle().ping + ") &eVL" + VL++),
     "mah.notify");
     }
     if (VL > 9) {
     Bukkit.broadcast(
     ChatColor.translateAlternateColorCodes('&',
     prefix + " &e" + p.getName() + " &6is &4DEFINETLY &6using &4" + hack.toString()
     + " &6(ping=" + ((CraftPlayer) p).getHandle().ping + ") &eVL" + VL++),
     "mah.notify");
     }
     }
    
     
  2. @Mindlessmink Identation! please!
    You need to create a new class for your entity, extending the original nms entity, then remove all the old pathfinders and add the ones you want. You should check this tutorial, the integer given when defining pathfinder goals is the priority, the lower the int, the higher the priority.
     
  3. Offline

    Mindlessmink

    Last edited: Sep 18, 2016
  4. Offline

    JanTuck

    EntityPlayer is Player.getHandle() so pretty much a player

    Sent from Tapatalk
     
  5. Offline

    Mindlessmink

    @Zombie_Striker, Tagging you because I think you're good with this :p

    AntiKillAura class/Custom Player:

    Code:
    public class AntiKillAura extends EntityPlayer {
     public AntiKillAura(MinecraftServer minecraftserver, WorldServer worldserver, GameProfile gameprofile, PlayerInteractManager playerinteractmanager) {
     super(minecraftserver, worldserver, gameprofile, playerinteractmanager);
     
     this.setCustomName("Aur4Ch3k3rM8");
     this.setSneaking(true);
     }
     
     public static Object getPrivateField(String fieldName, Class clazz, Object object){
     Field field;
     Object o = null;
     
     try
     {
     field = clazz.getDeclaredField(fieldName);
     
     field.setAccessible(true);
     
     o = field.get(object);
     }
     catch(NoSuchFieldException e){
     e.printStackTrace();
     }
     catch(IllegalAccessException e){
     e.printStackTrace();
     }
     
     return o;
     }
    }
    
    Place where I try to spawn it:
    Code:
     int count = 0;
     @EventHandler
     public void on(EntityDamageByEntityEvent e){
     if (e.getEntity() instanceof Player){
     Player p = (Player) e.getEntity();
     if (e.getDamager() instanceof Player){
     Player k = (Player) e.getDamager();
     if (p.getName().equalsIgnoreCase("Aur4Ch3k3rM8")){
     checking.add(k.getName());
     count++;
     if (count > 4){
     BC("KillAura / Forcefield", k);
     }
     }
     }
     }
     }
     @EventHandler
     public void onPlayerHit(EntityDamageByEntityEvent e){
     if (e.getDamager() instanceof Player){
     MinecraftServer server = ((CraftServer)Bukkit.getServer()).getServer();
     WorldServer world = ((CraftWorld)Bukkit.getWorlds().get(0)).getHandle();
     Player k = (Player)e.getDamager();
     EntityTypes.spawnEntity(new AntiKillAura(server, world, new GameProfile(UUID.randomUUID(), "Aur4Ch3k3rM8"), new PlayerInteractManager(world)),new Location(k.getWorld(), k.getLocation().getX() - 3, k.getLocation().getY() + 2, k.getLocation().getZ()));
     }
     }
     @EventHandler
     public void on(PlayerMoveEvent e){
     Player p = e.getPlayer();
     Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
     public void run(){
     if (checking.contains(p.getName())){
     checking.remove(p.getName());
     Bukkit.getWorlds().get(0).getEntities().clear();
     }
     }
     }, 0, 60);
     }
    
    EntityTypes enum:
    Code:
    public enum EntityTypes {
     
     CUSTOM_PLAYER("Player", AntiKillAura.class);
     
     private EntityTypes(String name, Class<? extends Entity> custom){
     addToMaps(custom, name);
     }
     
     public static void spawnEntity(Entity entity, Location loc){
     entity.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
     ((CraftWorld)loc.getWorld()).getHandle().addEntity(entity);
     }
     
     public static Object getPrivateField(String fieldName, Class clazz, Object object){
     Field field;
     Object o = null;
     
     try 
     {
     field = clazz.getDeclaredField(fieldName);
     
     field.setAccessible(true);
     
     o = field.get(object);
     }
     catch(NoSuchFieldException e){
     e.printStackTrace();
     }
     catch(IllegalAccessException e){
     e.printStackTrace();
     }
     
     return o;
     }
     
     private static void addToMaps(Class clazz, String name){
     ((Map)getPrivateField("c", net.minecraft.server.v1_8_R3.EntityTypes.class, null)).put(clazz, name);
     ((Map)getPrivateField("d", net.minecraft.server.v1_8_R3.EntityTypes.class, null)).put(clazz, name);
     }
    }
    
     
    Last edited: Sep 18, 2016
  6. Offline

    Mindlessmink

    BUMPTY BUMP BUMP

    - Okay, that totally wasn't weird :/
     
  7. Offline

    Lordloss

    There are plenty of tutorials for this, please try some google.
     
  8. Offline

    Mindlessmink

    BUMP
    Okay... Sorry, But I kind of just wanna make the NPC damageable. And @Lordloss I've tried every tutorial on google for this, All are for Entity's such as Zombies, etc... It's different when using NPCs.

    Basically what I need to know is how I can make the Player able to damage and or detect when they interact with the NPC. Is there a Packet for this? Or will I need ProtocolLib? (I've never used ProtocolLib before)
     
Thread Status:
Not open for further replies.

Share This Page