Custom PathFinderGoal not working correctly.

Discussion in 'Plugin Development' started by xMakerx, Sep 7, 2014.

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

    xMakerx

    Hi there, I'm trying to create a custom PathfinderGoal that makes it that my custom entities will teleport near a player's location, however, since I am testing this, I only want my custom zombies to teleport near a predefined location. This works sometimes, but other times when I spawn zombies they fall through blocks into the void.

    CustomZombie class:
    Code:java
    1. package org.npc.slender.bosses;
    2.  
    3. import java.lang.reflect.Field;
    4.  
    5. import org.bukkit.Color;
    6. import org.bukkit.Material;
    7. import org.bukkit.craftbukkit.v1_7_R3.inventory.CraftItemStack;
    8. import org.bukkit.craftbukkit.v1_7_R3.util.UnsafeList;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.inventory.meta.LeatherArmorMeta;
    11.  
    12. import net.minecraft.server.v1_7_R3.EntityHuman;
    13. import net.minecraft.server.v1_7_R3.EntityZombie;
    14. import net.minecraft.server.v1_7_R3.GenericAttributes;
    15. import net.minecraft.server.v1_7_R3.PathfinderGoalFloat;
    16. import net.minecraft.server.v1_7_R3.PathfinderGoalHurtByTarget;
    17. import net.minecraft.server.v1_7_R3.PathfinderGoalLookAtPlayer;
    18. import net.minecraft.server.v1_7_R3.PathfinderGoalMeleeAttack;
    19. import net.minecraft.server.v1_7_R3.PathfinderGoalNearestAttackableTarget;
    20. import net.minecraft.server.v1_7_R3.PathfinderGoalOpenDoor;
    21. import net.minecraft.server.v1_7_R3.PathfinderGoalRandomLookaround;
    22. import net.minecraft.server.v1_7_R3.PathfinderGoalRandomStroll;
    23. import net.minecraft.server.v1_7_R3.PathfinderGoalSelector;
    24. import net.minecraft.server.v1_7_R3.World;
    25.  
    26. public class CustomZombie extends EntityZombie {
    27.  
    28. public CustomZombie(World world) {
    29. super(world);
    30. this.setupGoals();
    31. this.prepare();
    32. }
    33.  
    34. /**
    35.   * Setup Appearance
    36.   */
    37.  
    38. private void prepare() {
    39. ItemStack test = new ItemStack(Material.DIAMOND_AXE);
    40. ItemStack helmet = new ItemStack(Material.LEATHER_HELMET);
    41. ItemStack plate = new ItemStack(Material.LEATHER_CHESTPLATE);
    42. ItemStack leggings = new ItemStack(Material.LEATHER_LEGGINGS);
    43. ItemStack boots = new ItemStack(Material.LEATHER_BOOTS);
    44.  
    45. LeatherArmorMeta white = (LeatherArmorMeta)helmet.getItemMeta();
    46. white.setColor(Color.WHITE);
    47. LeatherArmorMeta black = (LeatherArmorMeta)plate.getItemMeta();
    48. black.setColor(Color.BLACK);
    49.  
    50. helmet.setItemMeta(white);
    51. plate.setItemMeta(black);
    52. leggings.setItemMeta(black);
    53. boots.setItemMeta(black);
    54.  
    55. this.getBukkitEntity().getHandle().setEquipment(0, CraftItemStack.asNMSCopy(test));
    56. this.getBukkitEntity().getHandle().setEquipment(1, CraftItemStack.asNMSCopy(boots));
    57. this.getBukkitEntity().getHandle().setEquipment(2, CraftItemStack.asNMSCopy(leggings));
    58. this.getBukkitEntity().getHandle().setEquipment(3, CraftItemStack.asNMSCopy(plate));
    59. this.getBukkitEntity().getHandle().setEquipment(4, CraftItemStack.asNMSCopy(helmet));
    60. this.getBukkitEntity().getHandle().setInvisible(true);
    61. }
    62.  
    63. private void setupGoals() {
    64. try {
    65. Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
    66. bField.setAccessible(true);
    67. Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
    68. cField.setAccessible(true);
    69. bField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    70. bField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    71. cField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    72. cField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    73. } catch (Exception exc) {
    74. exc.printStackTrace();
    75. }
    76. this.goalSelector.a(0, new PathfinderGoalFloat(this));
    77. this.goalSelector.a(2, new PathfinderGoalTeleportToNearestPlayer(this));
    78. //this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, 1.25D, false));
    79. //this.goalSelector.a(3, new PathfinderGoalOpenDoor(this, true));
    80. //this.goalSelector.a(7, new PathfinderGoalRandomStroll(this, 1.2D));
    81. //this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
    82. this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true));
    83. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 0, true));
    84. }
    85.  
    86. @Override
    87. public boolean aD() {
    88. super.aD();
    89. this.getAttributeInstance(GenericAttributes.d).setValue(0.240000000004172312D);
    90. this.getAttributeInstance(GenericAttributes.e).setValue(7.0D);
    91. return false;
    92. }
    93.  
    94. @Override
    95. protected String aT() {
    96. return "mob.enderdragon.end";
    97. }
    98.  
    99. @Override
    100. protected String t() {
    101. return "mob.enderdragon.growl";
    102. }
    103.  
    104.  
    105.  
    106. }
    107.  


    Custom PathfinderGoal class:
    Code:java
    1. package org.npc.slender.bosses;
    2.  
    3. import java.util.Random;
    4.  
    5. import net.minecraft.server.v1_7_R3.EntityCreature;
    6. import net.minecraft.server.v1_7_R3.PathfinderGoal;
    7. import net.minecraft.server.v1_7_R3.Vec3D;
    8.  
    9. import org.bukkit.Location;
    10.  
    11. public class PathfinderGoalTeleportToNearestPlayer extends PathfinderGoal {
    12.  
    13. private EntityCreature entity;
    14. private boolean teleported;
    15.  
    16. public PathfinderGoalTeleportToNearestPlayer(EntityCreature entity) {
    17. this.entity = entity;
    18. this.teleported = false;
    19. }
    20.  
    21. @Override
    22. public boolean a() {
    23. Random random = new Random();
    24. if(random.nextInt(10) >= 3) {
    25. return true;
    26. }else {
    27. return false;
    28. }
    29. }
    30.  
    31. @Override
    32. public void c() {
    33. if(!teleported) {
    34. teleported = true;
    35. Random random = new Random();
    36. float length = entity.length;
    37. double locX = 944;
    38. double locY = 11;
    39. double locZ = 52;
    40. double headHeight = 0.5;
    41. Vec3D vec3d = Vec3D.a(entity.locX - locX, entity.boundingBox.b + (double) (length / 2.0F) - locY + headHeight, entity.locZ - locZ);
    42. vec3d = vec3d.a();
    43. double d0 = 16.0D;
    44. double d1 = entity.locX + (random.nextDouble() - 0.5D) * 8.0D - vec3d.a * d0;
    45. double d2 = entity.locY + (double) (random.nextInt(16) - 8) - vec3d.b * d0;
    46. if(d2 < 10) {
    47. d2 = 10;
    48. }
    49. double d3 = entity.locZ + (random.nextDouble() - 0.5D) * 8.0D - vec3d.c * d0;
    50. Location loc = new Location(entity.getBukkitEntity().getWorld(), d1, d2, d3);
    51. entity.getBukkitEntity().teleport(loc);
    52. }
    53. }
    54. }
    55.  
     
Thread Status:
Not open for further replies.

Share This Page