WASD Chicken Follows Direction But Doesn't Move

Discussion in 'Spigot Plugin Development' started by Bladian, May 2, 2015.

  1. I have my custom chicken which follows the the direction but doesn't move when I push the keys.

    Any help?

    Here is the CustomChicken Class.

    It's set to have no pathfinders at all, except this one.

    Code (Text):
    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();


        }
    }
     
  2. At first I thought it was a problem with the g method but since the chicken follows my view of sight I don't think so.

    My only other idea is that the chicken doesn't work because it has a glide mode, if so I would have to make it more like a bat.
     
  3. No, the problem is not the chicken, I've already done that. How do you spawn the chicken ?
     
  4. @MinusKube

    Here is my EntityType enum:

    Code (Text):
    package me.mounts.mounts;

    import net.minecraft.server.v1_8_R2.Entity;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_8_R2.CraftWorld;
    import org.bukkit.entity.Player;

    import java.util.Map;

    import static me.mounts.mounts.Utils.getPrivateField;

    /**
    * Created by maxmigliorini on 02/05/15.
    */

    public enum EntityTypes {


        CUSTOM_CHICKEN("Chicken", 93, CustomChicken.class); //You can add as many as you want.

        private EntityTypes(String name, int id, Class<CustomChicken> custom) {
            addToMaps(custom, name, id);
        }

        public static void spawnEntity(Entity entity, Location loc, Player p) {
            entity.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
            ((CraftWorld) loc.getWorld()).getHandle().addEntity(entity);
            org.bukkit.entity.Entity animal = entity.getBukkitEntity();
            animal.setPassenger(p);

        }

        private static void addToMaps(Class clazz, String name, int id) {

            ((Map) getPrivateField("d", net.minecraft.server.v1_8_R2.EntityTypes.class, null)).put(clazz, name);
            ((Map) getPrivateField("f", net.minecraft.server.v1_8_R2.EntityTypes.class, null)).put(clazz, Integer.valueOf(id));
        }
    }
     
    And here is how I spawn it:

    Code (Text):
    if(command.getName().equalsIgnoreCase("chicken")) {

                    EntityTypes.spawnEntity(new CustomChicken(p.getWorld()), p.getLocation(), p);

                    p.sendMessage("It works!");

                }
            }
    The chicken does spawn, but remains in a constant gliding position.
     
    #4 Bladian, May 2, 2015
    Last edited: May 3, 2015
  5. Is it maybe because I removed all the pathfinders so there is nothing to ovveride?
     
  6. Quick Update:

    The chicken follows my pitch and yaw so maybe a bug with my WASD.
     
  7. Another quick update:

    I think I've come to the conclusion that the class is made for 1.8.3 and I'm using an updated version of it. Maybe I need to search through the EntityHorse.class and switch it around?
     
  8. Maybe it isn't working because the functional part is inside this catch block "catch (NoSuchFieldException e1)"
     
  9. Forget about, turns out it was working but I had to edit a couple of things.

    One major problem though. The chicken doesn't start moving until after 2-30 seconds. Is there any way to update the entity or something of the sort?