Tutorial [1.8 ONLY]WASD Entity control

Discussion in 'Resources' started by Funergy, Feb 17, 2015.

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

    ChipDev

    Whats the code you used?
     
  2. Code:
        @Override
        public void g(float sideMot, float forMot) {
            if (this.passenger != null && this.passenger instanceof EntityHuman) {
                this.lastYaw = this.yaw = this.passenger.yaw;
                this.pitch = this.passenger.pitch * 0.5F;
                this.setYawPitch(this.yaw, this.pitch);//Update the pitch and yaw
                this.aI = this.aG = this.yaw;
                sideMot = ((EntityLiving) this.passenger).aX * 0.5F;
                forMot = ((EntityLiving) this.passenger).aY;
                if (forMot <= 0.0F) {
                    forMot *= 0.25F;// Make backwards slower
                }
    
                Field jump = null;
                try {
                    jump = EntityLiving.class.getDeclaredField("aW");
                } catch (NoSuchFieldException e1) {
                    e1.printStackTrace();
                } catch (SecurityException e1) {
                    e1.printStackTrace();
                }
                jump.setAccessible(true);
                if (jump != null && this.onGround) {
                    try {
                        if (jump.getBoolean(this.passenger)) {
                            double jumpHeight = 0.5D;
                            this.motY = jumpHeight;
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
               
                this.S = 1.0F;// The custom entity will now automatically climb up 1 high blocks
                this.aK = this.bH() * 0.1F;
                if (!this.world.isStatic) {
                    this.j(0.35F);//Here is the speed the entity will walk.
                    super.g(sideMot, forMot);
                }
    
                this.ay = this.az;//Some extra things
                double d0 = this.locX - this.lastX;
                double d1 = this.locZ - this.lastZ;
                float f4 = MathHelper.sqrt(d0 * d0 + d1 * d1) * 4.0F;
                if (f4 > 1.0F) {
                    f4 = 1.0F;
                }
    
                this.az += (f4 - this.az) * 0.4F;
                this.aA += this.az;
            } else {
                this.S = 0.5F;
                this.aK = 0.02F;
                super.g(sideMot, forMot);
            }
        }
    @TGRHavoc Was told this.S controls the blocks at which an animal will climb automatically. This is true for all animals but the horse apparently.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  3. Offline

    DSH105

    I'm completely fine with you continuing the original tutorial. My only problem was that you didn't noticeably attempt to write/update it yourself (other than a few obfuscation changes) in creating a new thread.

    I haven't been frequenting these forums as often as I used to; by the time I was able to respond to that thread, it was already solved. Sorry I couldn't be of assistance in time ):

    Feel free to poke me on IRC if you require any more help :).
     
    Funergy and ChipDev like this.
  4. Offline

    ChipDev

    Ok, I guess you are right, maybe @Funergy could link to your 1.7.10- tut.
     
  5. Offline

    Funergy

    @DSH105 Why did you think that I just needed to change some obfuscated things?

    Its way more easier than that copy the g() method in entity horse class remove the parts that give an error that that field isn't there. and done. Thats why I said you just need to look at the EntityHorse class but any way I'll link your tutorial for 1.7 users

    @ChipDev done https://funergy.me/screenshot/r01s0c6k0P2QrzCiDdsAb2BIu.png
     
  6. Offline

    CactusComboPvP

    how to work it with enderdragons?
     
  7. Offline

    Funergy

    ChipDev likes this.
  8. Offline

    ChipDev

    Wondering too :p
     
  9. Offline

    DSH105

    Apply the same calculations to the dragon's general move method (or something of the like). I answered a similar question in my old thread ;D
     
  10. Offline

    CactusComboPvP

    @DSH105
    Any examples? I'm not sure of the certain methods of its body and movement
     
  11. Offline

    Funergy

    @CactusComboPvP The method is m(). I think we need to edit the complex parts
    Code:
            this.children = new EntityComplexPart[]{this.bl = new EntityComplexPart(this, "head", 6.0F, 6.0F), this.bm = new EntityComplexPart(this, "body", 8.0F, 8.0F), this.bn = new EntityComplexPart(this, "tail", 4.0F, 4.0F), this.bo = new EntityComplexPart(this, "tail", 4.0F, 4.0F), this.bp = new EntityComplexPart(this, "tail", 4.0F, 4.0F), this.bq = new EntityComplexPart(this, "wing", 4.0F, 4.0F), this.br = new EntityComplexPart(this, "wing", 4.0F, 4.0F)};
    
     
  12. Offline

    DSH105

    Not unless you want the player to control each of the dragon components separately. Here's a sample of what I wrote a while ago for this in EchoPet. It's not ideal, but it should give you an idea of how it can be done.
     
  13. Offline

    Max604

    Hey I'm wondering on how to set up my EditedPig Class.
    Any examples on how to use the NMSUtils method?
     
  14. Offline

    Funergy

    @Max604 Well, there are plenty of tutorials how you can setup a custom entity. Just search on google.
     
  15. Offline

    Max604

    @Fungergy they aren't set up for 1.8.3
     
  16. Offline

    Funergy

    @Max604 Setting up the class is the same on 1.7 and 1.8
    create a class let it extend a living entity.

    public class CustomEntity extends EntityZombie

    and then you will need to add 1 method

    public CustomEntity(World w){super(w);}

    I dont know right now if the super is right. I'm not on my pc right now.
     
  17. Offline

    Zombie_Striker

    Hey, I have used this method and DSH's method to try and ride an Endermite, but neither of the seem to work. Does this method only work for certain mobs?
     
  18. Offline

    Funergy

    @Zombie_Striker This only works for 1.8 version. 1.8.3 doesnt work because of the re-obfuscation.
    I have the newer version. I will give it you asap :p.

    EDIT: This should be working.
    Code:
    @Override
    public void g(float f, float f1) {
    if(this.passenger != null && this.passenger instanceof EntityHuman) {
    this.lastYaw = this.yaw = this.passenger.yaw; this.pitch = this.passenger.pitch * 0.5F; this.setYawPitch(this.yaw, this.pitch); this.aK = this.aI = this.yaw;f = ((EntityLiving)this.passenger).aZ * 0.5F;f1 = ((EntityLiving)this.passenger).ba; if(f1 <= 0.0F) {
    f1 *= 0.25F;}
    Field jump = null; try {
    jump = EntityLiving.class.getDeclaredField("aY");} catch (NoSuchFieldException e1) {
    // TODO Auto-generated catch blocke1.printStackTrace();} catch (SecurityException e1) {
    // TODO Auto-generated catch blocke1.printStackTrace();}
    jump.setAccessible(true); if (jump != null) { // Wouldn't want it jumping while on the ground would we?try {
    if (jump.getBoolean(this.passenger)) {
    
    double jumpHeight = 0.5D; this.motY = jumpHeight; // Used all the time in NMS for entity jumping}
    } catch (IllegalAccessException e) {
    e.printStackTrace();}
    }
    this.S = 1.0F; this.aM = this.bI() * 0.1F; if(!this.world.isClientSide) {
    this.k((float)this.getAttributeInstance(GenericAttributes.d).getValue()); super.g(f, f1);}
    
    this.aA = this.aB; double d0 = this.locX - this.lastX; double d1 = this.locZ - this.lastZ; float f4 = MathHelper.sqrt(d0 * d0 + d1 * d1) * 4.0F; if(f4 > 1.0F) {
    f4 = 1.0F;}
    
    this.aB += (f4 - this.aB) * 0.4F; this.aC += this.aB;} else {
    this.S = 0.5F; this.aM = 0.02F; super.g(f, f1);}
    
    }
    
     
    Last edited: May 12, 2015
  19. Offline

    Zombie_Striker

    @Funergy Fields and methods such as "aM" "isClientSided" "bI" "aB" "aZ" and "ba" are coming up as undefined.
    The version I am using is git-Spigot-8a983f9-e81edfc (MC: 1.8)
     
  20. Offline

    Funergy

  21. Offline

    Zombie_Striker

  22. Offline

    BurnerDiamond

    Where do you get the horse class from?

    Is it from the mincraft.jar or am I missing something?
     
  23. Offline

    Funergy

    @BurnerDiamond Net.Minecraft.server from Spigot. Spigot imports the minecraftserver.jar from minecraft.net
     
  24. Offline

    BurnerDiamond

    One more question, how is my custom entity supposed to be. This is how I have it so far:

    Code:
    package me.mounts.mounts;
    
    /**
    * Created by maxmigliorini on 02/05/15.
    */
    
    import net.minecraft.server.v1_8_R2.*;
    import org.bukkit.craftbukkit.v1_8_R2.CraftWorld;
    
    import java.lang.reflect.Field;
    import java.util.List;
    
    public class CustomChicken extends EntityChicken {
    
    
        @Override
        public void g(float f, float f1) {
            if (this.passenger != null && this.passenger instanceof EntityHuman) {
                this.lastYaw = this.yaw = this.passenger.yaw;
                this.pitch = this.passenger.pitch * 0.5F;
                this.setYawPitch(this.yaw, this.pitch);
                this.aK = this.aI = this.yaw;
                f = ((EntityLiving) this.passenger).aZ * 0.5F;
                f1 = ((EntityLiving) this.passenger).ba;
                if (f1 <= 0.0F) {
                    f1 *= 0.25F;
                }
                Field jump = null;
                try {
                    jump = EntityLiving.class.getDeclaredField("aY");
                } catch (NoSuchFieldException e1) {
                    jump.setAccessible(true);
                    if (jump != null) {
                        try {
                            if (!jump.getBoolean(this.passenger)) {
                            } else {
                                double jumpHeight = 0.5D;
                                this.motY = jumpHeight;
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        this.S = 1.0F;
                        this.aM = this.bI() * 0.1F;
                        if (!this.world.isClientSide) {
                            this.k((float) this.getAttributeInstance(GenericAttributes.d).getValue());
                            super.g(f, f1);
                        }
    
                        this.aA = this.aB;
                        double d0 = this.locX - this.lastX;
                        double d1 = this.locZ - this.lastZ;
                        float f4 = MathHelper.sqrt(d0 * d0 + d1 * d1) * 4.0F;
                        if (f4 > 1.0F) {
                            f4 = 1.0F;
                        }
    
                        this.aB += (f4 - this.aB) * 0.4F;
                        this.aC += this.aB;
                    } else {
                        this.S = 0.5F;
                        this.aM = 0.02F;
                        super.g(f, f1);
                    }
    
                }
            }
        }
    
    
        public CustomChicken(org.bukkit.World world){
    
            super(((CraftWorld)world).getHandle());
    
            List goalB = (List) Utils.getPrivateField("b", PathfinderGoalSelector.class, goalSelector);
            goalB.clear();
            List goalC = (List) Utils.getPrivateField("c", PathfinderGoalSelector.class, goalSelector);
            goalC.clear();
            List targetB = (List) Utils.getPrivateField("b", PathfinderGoalSelector.class, targetSelector);
            targetB.clear();
            List targetC = (List) Utils.getPrivateField("c", PathfinderGoalSelector.class, targetSelector);
            targetC.clear();
    
    
        }
    }
    The problem is that it comes, but doesn't move at all. Any help?

    Hi, I've just tried to use this, and the chicken rotates like it's supposed to, but it doesn't move, any help?

    Here is my full class:

    Code:
    package me.mounts.mounts;
    
    /**
    * Created by maxmigliorini on 02/05/15.
    */
    
    import net.minecraft.server.v1_8_R2.*;
    import org.bukkit.craftbukkit.v1_8_R2.CraftWorld;
    
    import java.lang.reflect.Field;
    import java.util.List;
    
    import static me.mounts.mounts.Utils.getPrivateField;
    
    public class CustomChicken extends EntityChicken {
    
    
        @Override
        public void g(float f, float f1) {
            if (this.passenger != null && this.passenger instanceof EntityHuman) {
                this.lastYaw = this.yaw = this.passenger.yaw;
                this.pitch = this.passenger.pitch * 0.5F;
                this.setYawPitch(this.yaw, this.pitch);
                this.aK = this.aI = this.yaw;
                f = ((EntityLiving) this.passenger).aZ * 0.5F;
                f1 = ((EntityLiving) this.passenger).ba;
                if (f1 <= 0.0F) {
                    f1 *= 0.25F;
                }
                Field jump = null;
                try {
                    jump = EntityLiving.class.getDeclaredField("aY");
                } catch (NoSuchFieldException e1) {
                    jump.setAccessible(true);
                    if (jump != null) {
                        try {
                            if (!jump.getBoolean(this.passenger)) {
                            } else {
                                double jumpHeight = 0.5D;
                                this.motY = jumpHeight;
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        this.S = 1.0F;
                        this.aM = this.bI() * 0.1F;
                        if (!this.world.isClientSide) {
                            this.k((float) this.getAttributeInstance(GenericAttributes.d).getValue());
                            super.g(f, f1);
                        }
    
                        this.aA = this.aB;
                        double d0 = this.locX - this.lastX;
                        double d1 = this.locZ - this.lastZ;
                        float f4 = MathHelper.sqrt(d0 * d0 + d1 * d1) * 4.0F;
                        if (f4 > 1.0F) {
                            f4 = 1.0F;
                        }
    
                        this.aB += (f4 - this.aB) * 0.4F;
                        this.aC += this.aB;
                    } else {
                        this.S = 0.5F;
                        this.aM = 0.02F;
                        super.g(f, f1);
                    }
    
                }
            }
        }
    
    
        public CustomChicken(org.bukkit.World world){
    
            super(((CraftWorld) world).getHandle());
    
            List goalB = (List)getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear();
            List goalC = (List)getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear();
            List targetB = (List)getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear();
            List targetC = (List)getPrivateField("c", PathfinderGoalSelector.class, targetSelector); targetC.clear();
    
            this.goalSelector.a(0, new PathfinderGoalFloat(this));
    
        }
    
    
    }
    Edit:

    Spigot Version:
    [21:42:03 INFO]: This server is running CraftBukkit version git-Spigot-dbe012b-61ef214 (MC: 1.8.3) (Implementing API version 1.8.3-R0.1-SNAPSHOT)

    New question, I basically had to wait for the mob to update, any idea why and how to fix it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  25. Hello,

    I have a problem with my custom mob, the bat doesn't spawn.

    RideAbleBat class:
    Code:
    public class RideAbleBat extends EntityBat
    {
      public RideAbleBat(World world, Player player)
      {
        super(world);
        this.rider = player;
        this.setSize(10, 10);
      }
     
      Player rider = null;
     
      @Override
      public void g(float sideMot, float forMot)
      {
        if ((this.passenger == null) || (!(this.passenger instanceof EntityHuman)))
        {
          super.g(sideMot, forMot);
          this.S = 0.5F;
          return;
        }
        EntityHuman human = (EntityHuman)this.passenger;
        this.lastYaw = (this.yaw = this.passenger.yaw);
        this.pitch = (this.passenger.pitch * 0.5F);
        this.setYawPitch(this.yaw, this.pitch);
        this.aK = (this.aM = this.yaw);
        this.S = 1.0F;
       
    
        sideMot = ((EntityLiving)this.passenger).aZ * 0.5F;
        forMot = ((EntityLiving)this.passenger).ba;
        if (forMot <= 0.0F) {
          forMot *= 0.25F;
        }
        sideMot *= 0.75F;
       
        float speed = 0.35F;
       
    
        f(speed);
        super.g(sideMot, forMot);
        try
        {
          Field jump = null;
          jump = EntityLiving.class.getDeclaredField("aY");
          jump.setAccessible(true);
          if ((jump != null) && (this.onGround)) {
            if (jump.getBoolean(this.passenger))
            {
              double jumpHeight = 0.5D;
              this.motY = jumpHeight;
            }
          }
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    }
    
    Register class:
    Code:
    public enum Register {
        BAT("CustomBat", 4574852, EntityType.BAT, EntityBat.class, RideAbleBat.class);
        private String name;
        private int id;
        private EntityType entityType;
        private Class<? extends EntityInsentient> nmsClass;
        private Class<? extends EntityInsentient> customClass;
        private Register(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 this.name;
        }
        public int getID() {
            return this.id;
        }
        public EntityType getEntityType() {
            return this.entityType;
        }
        public Class<? extends EntityInsentient> getNMSClass() {
            return this.nmsClass;
        }
        public Class<? extends EntityInsentient> getCustomClass() {
            return this.customClass;
        }
        public static void regissterEntities() {
            for (Register entity : values()) {
                try {
                    Method a = EntityTypes.class.getDeclaredMethod("a", new Class<?>[] { Class.class, String.class, int.class });
                    a.setAccessible(true);
                    a.invoke(null, entity.getCustomClass(), entity.getName(), entity.getID()); //error here, ID already registered
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        /*
        public static void registerEntities()
          {
            for (Register entity : values()) {
              a(entity.getCustomClass(), entity.getName(), entity.getID());
            }
            try
            {
             BiomeBase[]  biomes = (BiomeBase[])getPrivateStatic(BiomeBase.class, "biomes");
            }
            catch (Exception exc)
            {
              BiomeBase[] biomes;
              return;
            }
            BiomeBase[] biomes;
            BiomeBase[] arrayOfBiomeBase1 = biomes;int k = biomes.length;
            for (k = 0; biomes.length < k; k++)
            {
              BiomeBase biomeBase = arrayOfBiomeBase1[k];
              if (biomeBase == null) {
                break;
              }
              for (String field : new String[] { "as", "at", "au", "av" }) {
                try
                {
                  Field list = BiomeBase.class.getDeclaredField(field);
                  list.setAccessible(true);
                
                  List<BiomeMeta> mobList = (List)list
                    .get(biomeBase);
                  int i2;
                  int i1;
                  for (Iterator localIterator = mobList.iterator(); localIterator.hasNext(); i1 > i2)
                  {
                    BiomeMeta meta = (BiomeMeta)localIterator.next();
                    Register[] arrayOfRideAbleEntityType2;
                    i2 = (arrayOfRideAbleEntityType2 = values()).length;i1 = 0; continue;Register entity = arrayOfRideAbleEntityType2[i1];
                    if (entity.getNMSClass().equals(meta.b)) {
                      meta.b = entity.getCustomClass();
                    }
                    i1++;
                  }
                }
                catch (Exception e)
                {
                  e.printStackTrace();
                }
              }
            }
          }
    */
        private static Object getPrivateStatic(
                @SuppressWarnings("rawtypes") Class clazz, String f)
                throws Exception {
            Field field = clazz.getDeclaredField(f);
            field.setAccessible(true);
            return field.get(null);
        }
        @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
        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(Integer.valueOf(paramInt), paramClass);
                ((Map) getPrivateStatic(EntityTypes.class, "f")).put(paramClass, Integer.valueOf(paramInt));
                ((Map) getPrivateStatic(EntityTypes.class, "g")).put(paramString, Integer.valueOf(paramInt));
            } catch (Exception localException) {
            }
        }
       
        // MAKING
        public static double mountSpeed = 0.2D;
        private static double maxHealth = 2.0D;
        private static void make(EntityLiving nmsEntity, Player player) {
            if (!canSummonMount(player.getLocation())) {
                player.sendMessage("§cVous ne pouvez pas utiliser cette monture ici.");
                return;
            }
            LivingEntity mount = (LivingEntity) nmsEntity.getBukkitEntity();
            // Adulte
            if (mount instanceof EntityAgeable) ((EntityAgeable)mount).setAge(0);
            Location location = player.getLocation();
            World nmsWorld = ((CraftWorld) location.getWorld()).getHandle();
            nmsEntity.setPosition(location.getX(), location.getY() + 0.3, location.getZ());
            nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM);
            // LA VIE
            mount.setMaxHealth(maxHealth);;
            mount.setPassenger(player);
            player.closeInventory();
        }
       
        public static boolean canSummonMount(Location location) {
            return true; // TODO: improve
        }
       
        public static void rideBat(Player player) {
            Location loc = player.getLocation();
            World nmsWorld = ((CraftWorld) loc.getWorld()).getHandle();
            make(new RideAbleBat(nmsWorld, player), player);
        }
       
    }
    
    Code for spawning :
    Code:
        Register.rideBat(player);
    Can you help me ?
    Good day.
     
  26. Offline

    Funergy

    @Altarry Have you registered the entity
     
  27. Offline

    eyamaz

    Moved to the Resource category to avoid another automatic lock.

    edit: and no, I'm not moving it back to Alternatives.
     
    Last edited: Jun 29, 2015
  28. Offline

    Quaro

    Can you explain me what this checking for?
    if(!this.world.isStatic)
     
  29. Offline

    Funergy

    @Quaro I'm not sure what it does right now. Because its been a long time I've been working on Bukkit plugins.
    But Its needed.
     
  30. Offline

    DSH105

    It's a final boolean which indicates whether it's running on the client side or server side, so it doesn't have any use in a server side Bukkit plugin.
     
    Funergy likes this.
Thread Status:
Not open for further replies.

Share This Page