[NMS] How to change zombie pathfind range (target radius) and speed?

Discussion in 'Plugin Development' started by bars96, Feb 15, 2014.

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

    bars96

    I've tried that code but it's infinitely spam my console with error.

    Error:
    Main class:
    Code:java
    1. public void onEnable() {
    2. try {
    3. @SuppressWarnings("rawtypes")
    4. Class[] args = new Class[3];
    5. args[0] = Class.class;
    6. args[1] = String.class;
    7. args[2] = int.class;
    8. Method a = EntityTypes.class.getDeclaredMethod("a", args);
    9. a.setAccessible(true);
    10. a.invoke(a, CustomZombie.class, "Zombie", 54);
    11. } catch (Exception e) {
    12. e.printStackTrace();
    13. }
    14. }


    EntityListener:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. private void onCreatureSpawn(CreatureSpawnEvent e) {
    3. if ((e.getEntityType() != EntityType.ZOMBIE) && (e.getEntityType() != EntityType.PIG_ZOMBIE) && (e.getEntityType() != EntityType.GIANT)) {
    4. e.setCancelled(true);
    5. return;
    6. }
    7.  
    8. Location l = e.getLocation();
    9. net.minecraft.server.v1_5_R3.Entity mcEntity = ((CraftEntity) e.getEntity()).getHandle();
    10. net.minecraft.server.v1_5_R3.World mcWorld = ((CraftWorld) e.getEntity().getWorld()).getHandle();
    11. if (e.getEntityType() == EntityType.ZOMBIE) {
    12. CustomZombie cz = new CustomZombie(mcWorld);
    13. cz.setPosition(l.getX(), l.getY(), l.getZ());
    14. mcWorld.removeEntity(mcEntity);
    15. mcWorld.addEntity(cz, CreatureSpawnEvent.SpawnReason.CUSTOM); // ERROR LINE #101
    16. }
    17. }


    CustomZombie:
    Code:java
    1. public class CustomZombie extends EntityZombie {
    2. public CustomZombie(World world) {
    3. super(world);
    4. try {
    5. Field fGoalSelector = EntityLiving.class.getDeclaredField("goalSelector");
    6. fGoalSelector.setAccessible(true);
    7. fGoalSelector.set(this.goalSelector, new UnsafeList<Object>());
    8. fGoalSelector.set(this.targetSelector, new UnsafeList<Object>());
    9.  
    10. this.goalSelector.a(0, new PathfinderGoalFloat(this));
    11. this.goalSelector.a(1, new PathfinderGoalBreakDoor(this));
    12. this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, 0.39F, false));
    13. this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityVillager.class, 0.39F, true));
    14. this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, 0.23F));
    15. this.goalSelector.a(5, new PathfinderGoalMoveThroughVillage(this, 0.23F, false));
    16. this.goalSelector.a(6, new PathfinderGoalRandomStroll(this, 0.23F));
    17. this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
    18. this.goalSelector.a(7, new PathfinderGoalRandomLookaround(this));
    19. this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, false));
    20. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 72.0F, 0, true));
    21. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class, 16.0F, 0, false));
    22. } catch (Exception e) {
    23. e.printStackTrace();
    24. }
    25. }
    26. }
     
  2. Offline

    xTrollxDudex

    bars96
    That's not a full stacktrace.
     
  3. Offline

    bars96

    Full. That error spamming console every milli second.
     
  4. Offline

    xTrollxDudex

    bars96
    If that was the full stack trace, then it would say the error type would it not?
     
  5. Offline

    bars96

    I don't understand you. And don't tag me.
     
  6. Offline

    LucasEmanuel

    That is not the full stack trace, you have left out the most important part of it, the "caused by" part.
     
  7. Offline

    bars96

    Attached Files:

  8. Offline

    LucasEmanuel


    This is the error you left out:
    You cause this on line 29 according to:
     
  9. Offline

    bars96

    Okay, I deleted 29 and 30 lines (7 and 8 in posted code), which contains UnsafeList, but get this error (every milli second too):
    Error line #101:
    Code:java
    1. mcWorld.addEntity(cz, CreatureSpawnEvent.SpawnReason.CUSTOM);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  10. Offline

    purefocus2

    When 1.7.2 came out, they added a check that wouldn't allow you to call the method a in EntityTypes if there's already an entry with the same ID, so now you need to do a direct edit to the maps. But, however, it won't cause an error until you try and spawn the mob. Try this:

    Code:java
    1.  
    2. package com.chosencraft.purefocus.party.utils;
    3.  
    4. import java.lang.reflect.Field;
    5. import java.util.Map;
    6.  
    7. import net.minecraft.server.v1_7_R1.Entity;
    8. import net.minecraft.server.v1_7_R1.EntityTypes;
    9.  
    10. public class MobUtils
    11. {
    12. public static Object getPrivateStatic(Class<?> clazz, String f) throws Exception
    13. {
    14. Field field = clazz.getDeclaredField(f);
    15. field.setAccessible(true);
    16. return field.get(null);
    17. }
    18.  
    19. /*
    20.   * Since 1.7.2 added a check in their entity registration, simply bypass it
    21.   * and write to the maps ourself.
    22.   */
    23. @SuppressWarnings("unchecked")
    24. public static void a(Class<? extends Entity> paramClass, String paramString, int paramInt)
    25. {
    26. try
    27. {
    28. ((Map<String, Class<? extends Entity>>) getPrivateStatic(EntityTypes.class, "c")).put(paramString, paramClass);
    29. ((Map<Class<? extends Entity>, String>) getPrivateStatic(EntityTypes.class, "d")).put(paramClass, paramString);
    30. ((Map<Integer, Class<? extends Entity>>) getPrivateStatic(EntityTypes.class, "e")).put(Integer.valueOf(paramInt), paramClass);
    31. ((Map<Class<? extends Entity>, Integer>) getPrivateStatic(EntityTypes.class, "f")).put(paramClass, Integer.valueOf(paramInt));
    32. ((Map<String, Integer>) getPrivateStatic(EntityTypes.class, "g")).put(paramString, Integer.valueOf(paramInt));
    33. }
    34. catch (Exception exc)
    35. {
    36. exc.printStackTrace();
    37. }
    38. }
    39. }
    40.  


    Usage:
    Code:java
    1.  
    2. public void onEnable(){
    3. MobUtils.a(CustomZombie.class, "Zombie", 54);
    4. }
    5.  
     
  11. Offline

    tylersyme

    this.getAttributeInstance(GenericAttributes.a).setValue(100); //Max Health

    Use that in your constructor, but since I don't remember which variable represents speed or awareness you'll have to experiment a bit by changing "GenericAttributes.other_variables". There are only 5 GenericAttributes so it won't be too difficult
     
  12. Offline

    bars96

    All previous code was very trouble and incompatible with other plugins because it cancelled zombie spawn event and spawn new, custom zombie. I've used this code now:
    Code:java
    1. @EventHandler
    2. private void onEntityTarget(EntityTargetEvent e) {
    3. if (!(e.getTarget() instanceof Player) || !(e.getEntity() instanceof Zombie)) return;
    4. Player p = (Player) e.getTarget();
    5. Zombie z = (Zombie) e.getEntity();
    6. double d = p.getLocation().distance(z.getLocation());
    7. if (d > ((int) (p.getExp() * 72))) { // Maximum range is 72, maximum player experience is 1
    8. e.setCancelled(true);
    9. return;
    10. }
    11. }
    12.  
    13. @EventHandler(priority = EventPriority.HIGHEST)
    14. private void onCreatureSpawn(CreatureSpawnEvent e) {
    15. if (e.isCancelled()) return;
    16. if ((e.getEntityType() != EntityType.ZOMBIE) && (e.getEntityType() != EntityType.PIG_ZOMBIE) && (e.getEntityType() != EntityType.GIANT) && (e.getSpawnReason() != SpawnReason.SPAWNER_EGG)) {
    17. e.setCancelled(true);
    18. return;
    19. }
    20.  
    21. Location l = e.getLocation();
    22. if (e.getEntityType() == EntityType.ZOMBIE) {
    23. Zombie z = (Zombie) e.getEntity();
    24. if (z.isVillager()) z.setVillager(false);
    25. makeFaster(z);
    26. }
    27. }
    28.  
    29. public void makeFaster(Zombie z) {
    30. EntityZombie zombie = ((CraftZombie) z).getHandle();
    31. Field fGoalSelector;
    32. Field tGoalSelector;
    33. try {
    34. fGoalSelector = EntityLiving.class.getDeclaredField("goalSelector");
    35. fGoalSelector.setAccessible(true);
    36. PathfinderGoalSelector gs = new PathfinderGoalSelector(((CraftWorld) z.getWorld()).getHandle() != null && ((CraftWorld) z.getWorld()).getHandle().methodProfiler != null ? ((CraftWorld) z.getWorld()).getHandle().methodProfiler : null);
    37. gs.a(0, new PathfinderGoalFloat(zombie));
    38. gs.a(1, new PathfinderGoalBreakDoor(zombie));
    39. gs.a(2, new PathfinderGoalMeleeAttack(zombie, EntityHuman.class, 0.39F, false));
    40. gs.a(3, new PathfinderGoalMeleeAttack(zombie, EntityVillager.class, 0.39F, true));
    41. gs.a(4, new PathfinderGoalMoveTowardsRestriction(zombie, 0.39F));
    42. gs.a(5, new PathfinderGoalMoveThroughVillage(zombie, 0.39F, false));
    43. gs.a(6, new PathfinderGoalRandomStroll(zombie, 0.39F));
    44. gs.a(7, new PathfinderGoalLookAtPlayer(zombie, EntityHuman.class, 8.0F));
    45. gs.a(7, new PathfinderGoalRandomLookaround(zombie));
    46. fGoalSelector.set(zombie, gs);
    47.  
    48. tGoalSelector = EntityLiving.class.getDeclaredField("targetSelector");
    49. tGoalSelector.setAccessible(true);
    50. PathfinderGoalSelector ts = new PathfinderGoalSelector(((CraftWorld) z.getWorld()).getHandle() != null && ((CraftWorld) z.getWorld()).getHandle().methodProfiler != null ? ((CraftWorld) z.getWorld()).getHandle().methodProfiler : null);
    51. ts.a(1, new PathfinderGoalHurtByTarget(zombie, false));
    52. ts.a(2, new PathfinderGoalNearestAttackableTarget(zombie, EntityHuman.class, 72.0F, 0, true));
    53. ts.a(2, new PathfinderGoalNearestAttackableTarget(zombie, EntityVillager.class, 72.0F, 0, false));
    54. tGoalSelector.set(zombie, ts);
    55. } catch (NoSuchFieldException ex) {
    56. ex.printStackTrace();
    57. } catch (IllegalAccessException ex) {
    58. ex.printStackTrace();
    59. }
    60. }

    All works fine (without errors) but zombie view distance don't changed. How to change zombie agro radius?

    I need change this because z.setTarget(p) don't work in far distances like 72.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  13. Offline

    bars96

  14. Offline

    bars96

  15. Offline

    Ivan

    Have you tried following my tutorial? You can stop other plugins from cancelling the spawns by listening to the HIGHEST event level and setting cancelled to false.
     
    Garris0n likes this.
  16. Offline

    Garris0n

    Also note that you only have to hack your way into the "d" and "f" lists if you don't want your mob to override the type it's using (as in replace it in spawns, etc).
     
    Ivan likes this.
  17. Offline

    bars96

    Mobs spawning normally but they target me at distance <= 16 blocks. I want to increase it.
     
  18. Offline

    Ivan

    Yes... did you read my post?
     
  19. Offline

    bars96

    Yes... But I don't understand what you want to say.
     
Thread Status:
Not open for further replies.

Share This Page