How i can manipulate speed and the target of an entity?

Discussion in 'Spigot Plugin Development' started by crazyhoorse961, Nov 1, 2016.

  1. Hello,
    I'm trying to change the speed and the entity target of a custom mob i created using nms.
    But i don't know from where start, there are no PathfinderGoals that allows you to manipulate the entities that the mob should target and his walking speed?
    I'm using the spigot 1.10.2 API.
    Thank you
     
    • Funny Funny x 1
    • Choco

      Moderator

      Sure there are
      PathfinderGoalNearestAttackableTarget<T extends EntityLiving>

      As for speed, set an attribute using the Attribute API
       
      • Like Like x 1
      • Agree Agree x 1
      • ...? In other words..?

        Btw, should this target wolfs (cannot test atm sorry)
        Code (Text):
        this.goalSelector.a(14, new PathfinderGoalNearestAttackableTarget<>(this, EntityWolf.class, true));
         
      • Choco

        Moderator

        Yes this should work fine. Though you'll want to add type parameters, "<EntityWolf>" should be fine.

        Attribute API information can be found on the Javadocs. All classes that implement Attributable (LivingEntity is included) should inherit a #getAttribute() method. You can browse Javadocs from here, I'm sure
        https://hub.spigotmc.org/javadocs/s...#getAttribute(org.bukkit.attribute.Attribute)
         
      • Thank you.

        Ill let you know if ill have problems
         
      • Hello, i'm stuck with something stupid;
        Code (Text):
            @EventHandler
            public void onSpawn(CreatureSpawnEvent e){
                //Checks if the entity is a Zombie
                if(e.getEntityType() == EntityType.ZOMBIE){
                    //e.getEntity returns a living entity
                    Entity en = e.getEntity();
                }
            }
        I have this code, now how i apply an attribute ?

        EDIT: I want to add the attribute when the entity spawns
         
        #8 crazyhoorse961, Nov 2, 2016
        Last edited: Nov 2, 2016
      • Bump, sorry but i would need it today.
         
      • Hello? Sorry but i need it today;
        if isn't clear i would need a method to ADD/CHANGE an attribute to a livingentity.
         
        #10 crazyhoorse961, Nov 2, 2016
        Last edited: Nov 2, 2016
      • Just look at this demo plugin by md_5

        To your question from above: You can't add an attribute since a living entity already has all possible attributes. You can only get them (getAttribute (Attribute attributeType)) and set their value...
         
      • Should that work? (Cannot test atm)
        Code (Text):
        e.getEntity().getAttribute(Attribute.GENERIC_ATTACK_SPEED).setBaseValue(8.0D);
         
      • I currently have this;
        Code (Text):
                this.goalSelector.a(0, new PathfinderGoalFloat(this));
                this.goalSelector.a(1, new PathfinderGoalZombieAttack(this, 3.0, true));
                this.goalSelector.a(5, new PathfinderGoalMeleeAttack(this, 2.0, true));
                this.goalSelector.a(9, new PathfinderGoalRandomStroll(this, 0.8D));
                this.goalSelector.a(10, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 15.0F));
                this.goalSelector.a(20, new PathfinderGoalMoveTowardsTarget(this, 30.0D, 25.0F));
                this.goalSelector.a(12, new PathfinderGoalOpenDoor(this, true));
                this.goalSelector.a(13, new PathfinderGoalTargetNearestPlayer(this));
                this.goalSelector.a(3, new PathfinderGoalRandomLookaround(this));
                this.goalSelector.a(14, new PathfinderGoalNearestAttackableTarget<EntityWolf>(this, EntityWolf.class, true));
                this.setHealth(35.0F);
                this.setAI(true);
                this.removeAllEffects();
                this.setAbsorptionHearts(2.0F);
                this.setVillagerType(EnumZombieType.NORMAL);
        But it doesn't move and if i spawn a wolf it doesn't target him.
         
        • Informative Informative x 1
        • Try without the setAI
           
        • Ohi ohi, sh*tty NMS;
          http://pastebin.com/R8eM6dr9
          Server Crashed,
          there's another crash report:
          http://pastebin.com/d10yD6B1
          Actual codes:
          Code (Text):
          //CustomEntities (For registering entities)
          package com.crazyhorse961.mobs;

          import java.lang.reflect.Field;
          import java.util.Map;
          import org.bukkit.entity.EntityType;

          import net.minecraft.server.v1_10_R1.EntityInsentient;
          import net.minecraft.server.v1_10_R1.EntityTypes;

          @SuppressWarnings("deprecation")
          public enum CustomEntities {
             
              ZOMBIE("WOWZombie", EntityType.ZOMBIE.getTypeId(), EntityType.ZOMBIE, WOWZombie.class, WOWZombie.class);
             
              private String name;
              private int id;
              private EntityType entityType;
              private Class<? extends EntityInsentient> nmsClass;
              private Class<? extends EntityInsentient> customClass;

              private CustomEntities(String name, int id, EntityType entityType,
                             Class<? extends EntityInsentient> nmsClass,
                             Class<? extends EntityInsentient> customClass) {
                  this.name = name;
                  this.id = id;
                  this.entityType = entityType;
                  this.nmsClass = nmsClass;
                  this.customClass = customClass;
              }

              public String getName() {
                  return name;
              }

              public int getID() {
                  return id;
              }

              public EntityType getEntityType() {
                  return entityType;
              }

              public Class<? extends EntityInsentient> getNMSClass() {
                  return nmsClass;
              }

              public Class<? extends EntityInsentient> getCustomClass() {
                  return customClass;
              }

              public static void registerEntities() {
                  for (CustomEntities entity : values())
                      a(entity.getCustomClass(), entity.getName(), entity.getID());
              }

              @SuppressWarnings("rawtypes")
              public static void unregisterEntities() {
                  for (CustomEntities entity : values()) {
                      try {
                          ((Map) getPrivateStatic(EntityTypes.class, "d")).remove(entity.getCustomClass());
                      } catch (Exception e) {
                          e.printStackTrace();
                      }

                      try {
                          ((Map) getPrivateStatic(EntityTypes.class, "f")).remove(entity.getCustomClass());
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }

                  for (CustomEntities entity : values())
                      try {
                          a(entity.getNMSClass(), entity.getName(), entity.getID());
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
              }

              @SuppressWarnings("rawtypes")
              private static Object getPrivateStatic(Class clazz, String f) throws Exception {
                  Field field = clazz.getDeclaredField(f);
                  field.setAccessible(true);
                  return field.get(null);
              }

              @SuppressWarnings({"unchecked", "rawtypes"})
              private static void a(Class paramClass, String paramString, int paramInt) {
                  try {
                      ((Map) getPrivateStatic(EntityTypes.class, "c")).put(paramString, paramClass);
                      ((Map) getPrivateStatic(EntityTypes.class, "d")).put(paramClass, paramString);
                      ((Map) getPrivateStatic(EntityTypes.class, "e")).put(paramInt,
                              paramClass);
                      ((Map) getPrivateStatic(EntityTypes.class, "f")).put(paramClass,
                              paramInt);
                      ((Map) getPrivateStatic(EntityTypes.class, "g")).put(paramString,
                              paramInt);
                  } catch (Exception ignored) {
                      ignored.printStackTrace();
                  }
              }
          }
          //WOWZombie, the zombie
          package com.crazyhorse961.mobs;

          import java.lang.reflect.Field;

          import org.bukkit.Location;
          import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
          import org.bukkit.craftbukkit.v1_10_R1.entity.CraftLivingEntity;
          import org.bukkit.entity.Zombie;
          import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
          import com.google.common.collect.Sets;
          import net.minecraft.server.v1_10_R1.EntityHuman;
          import net.minecraft.server.v1_10_R1.EntityWolf;
          import net.minecraft.server.v1_10_R1.EntityZombie;
          import net.minecraft.server.v1_10_R1.EnumZombieType;
          import net.minecraft.server.v1_10_R1.PathfinderGoalFloat;
          import net.minecraft.server.v1_10_R1.PathfinderGoalLookAtPlayer;
          import net.minecraft.server.v1_10_R1.PathfinderGoalMeleeAttack;
          import net.minecraft.server.v1_10_R1.PathfinderGoalMoveTowardsTarget;
          import net.minecraft.server.v1_10_R1.PathfinderGoalNearestAttackableTarget;
          import net.minecraft.server.v1_10_R1.PathfinderGoalOpenDoor;
          import net.minecraft.server.v1_10_R1.PathfinderGoalRandomLookaround;
          import net.minecraft.server.v1_10_R1.PathfinderGoalRandomStroll;
          import net.minecraft.server.v1_10_R1.PathfinderGoalSelector;
          import net.minecraft.server.v1_10_R1.PathfinderGoalTargetNearestPlayer;
          import net.minecraft.server.v1_10_R1.PathfinderGoalZombieAttack;
          import net.minecraft.server.v1_10_R1.World;

          public class WOWZombie extends EntityZombie{
              public WOWZombie(World world) {
                  super(world);
                  try{
                  Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
                  bField.setAccessible(true);
                  Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
                  cField.setAccessible(true);
                  bField.set(goalSelector,  Sets.newLinkedHashSet());
                  bField.set(targetSelector,  Sets.newLinkedHashSet());
                  cField.set(goalSelector,  Sets.newLinkedHashSet());
                  cField.set(targetSelector,  Sets.newLinkedHashSet());
                  }catch (Exception e){
                      e.printStackTrace();
                  }
                 
                  this.goalSelector.a(0, new PathfinderGoalFloat(this));
                  this.goalSelector.a(1, new PathfinderGoalZombieAttack(this, 3.0, true));
                  this.goalSelector.a(5, new PathfinderGoalMeleeAttack(this, 2.0, true));
                  this.goalSelector.a(9, new PathfinderGoalRandomStroll(this, 0.8D));
                  this.goalSelector.a(10, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 15.0F));
                  this.goalSelector.a(20, new PathfinderGoalMoveTowardsTarget(this, 30.0D, 25.0F));
                  this.goalSelector.a(12, new PathfinderGoalOpenDoor(this, true));
                  this.goalSelector.a(13, new PathfinderGoalTargetNearestPlayer(this));
                  this.goalSelector.a(3, new PathfinderGoalRandomLookaround(this));
                  this.goalSelector.a(14, new PathfinderGoalNearestAttackableTarget<EntityWolf>(this, EntityWolf.class, true));
                  this.setHealth(35.0F);
                  this.removeAllEffects();
                  this.setAbsorptionHearts(2.0F);
                  this.setVillagerType(EnumZombieType.NORMAL);
                  this.fireProof = true;
                     
                  }
              public static Zombie spawn(Location loc){
                          World mcWorld = ((CraftWorld) loc.getWorld()).getHandle();
                          final WOWZombie customEnt = new WOWZombie(mcWorld);
                          customEnt.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
                          ((CraftLivingEntity) customEnt.getBukkitEntity()).setRemoveWhenFarAway(true);
                          mcWorld.addEntity(customEnt, SpawnReason.CUSTOM);
                          return (Zombie) customEnt.getBukkitEntity();
              }
          }
          //And the main class
          package com.crazyhorse961.mobs;

          import org.bukkit.Bukkit;
          import org.bukkit.attribute.Attribute;
          import org.bukkit.command.Command;
          import org.bukkit.command.CommandSender;
          import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
          import org.bukkit.entity.EntityType;
          import org.bukkit.entity.Player;
          import org.bukkit.event.EventHandler;
          import org.bukkit.event.Listener;
          import org.bukkit.event.entity.CreatureSpawnEvent;
          import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
          import org.bukkit.plugin.java.JavaPlugin;


          public class CustomMobs extends JavaPlugin implements Listener
          {

              public void onEnable(){
              CustomEntities.registerEntities();
              Bukkit.getPluginManager().registerEvents(this, this);
              this.getCommand("zombie").setExecutor(this);
              }
              public void onDisable(){
                  CustomEntities.unregisterEntities();
              }
             
             
              @Override
              public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                  if(command.getName().equalsIgnoreCase("zombie")){
                      if(sender instanceof Player){
                          Player p = (Player) sender;
                          if(p.hasPermission("wownetwork.spawn.zombie")){
                          CraftWorld world = (CraftWorld)p.getWorld();
                          WOWZombie zombie = new WOWZombie(world.getHandle());
                          double x = p.getLocation().getX();
                          double y = p.getLocation().getY();
                          double z = p.getLocation().getZ();
                          float pitch = p.getLocation().getPitch();
                          float yaw = p.getLocation().getYaw();
                          zombie.setLocation(x, y, z, pitch, yaw);
                          world.getHandle().addEntity(zombie, SpawnReason.CUSTOM);
                         
                          return true;
                          }
                      }
                  }
                  return true;
             
              }
              @EventHandler
              public void onSpawn(CreatureSpawnEvent e){
                  if(e.getEntityType() == EntityType.ZOMBIE){
                      Bukkit.broadcastMessage(e.getEntity().getClass().toString());
                      e.getEntity().getAttribute(Attribute.GENERIC_ATTACK_SPEED).setBaseValue(8.0D);
                     
                     
                  }
              }
          }
          Is that my entity or just a problem with the server/my pc?
           
        • Line 57?