Player controlled entity movement

Discussion in 'Spigot Plugin Development' started by KingFaris10, May 30, 2015.

Thread Status:
Not open for further replies.
  1. Hi, I followed this tutorial partially: http://bukkit.org/threads/tutorial-1-7-5-wasd-entity-riding.163019/
    To control my Iron Golem.

    However, the method "e(float sideMot, float forMot)" is only running (tested with a System.out.println()) when the entity (Iron Golem) dies.

    Code (Text):

        public CustomGolem(World world) {
            super(world);

            this.loadEntity();
        }

        private void loadEntity() {
            this.setInvisible(false);

            try {
                ReflectionUtilities.FieldAccess bField = ReflectionUtilities.getField(PathfinderGoalSelector.class, "b");
                ReflectionUtilities.FieldAccess cField = ReflectionUtilities.getField(PathfinderGoalSelector.class, "c");
                bField.set(this.goalSelector, new UnsafeList<PathfinderGoalSelector>());
                bField.set(this.targetSelector, new UnsafeList<PathfinderGoalSelector>());
                cField.set(this.goalSelector, new UnsafeList<PathfinderGoalSelector>());
                cField.set(this.targetSelector, new UnsafeList<PathfinderGoalSelector>());

                this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8F));
                this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        @Override
        protected void dropDeathLoot(boolean flag, int i) {
        }

        @Override
        public void e(float sideMot, float forMot) {
            if (this.passenger == null || !(this.passenger instanceof EntityHuman)) {
                super.e(sideMot, forMot);
                this.S = 0.5F;    // Make sure the entity can walk over half slabs, instead of jumping
                return;
            }

            this.lastYaw = this.yaw = this.passenger.yaw;
            this.pitch = this.passenger.pitch * 0.5F;

            // Set the entity's pitch, yaw, head rotation etc.
            this.setYawPitch(this.yaw, this.pitch);
            this.aK = this.aI = this.yaw;

            this.S = 1F;    // The custom entity will now automatically climb up 1 high blocks

            sideMot = ((EntityLiving) this.passenger).aZ * 0.5F;
            forMot = ((EntityLiving) this.passenger).ba;

            if (forMot <= 0.0F) {
                forMot *= 0.25F;    // Make backwards slower
            }
            sideMot *= 0.75F;    // Also make sideways slower

            float speed = 0.35F;    // 0.2 is the default entity speed. I made it slightly faster so that riding is better than walking
            this.k(speed);    // Apply the speed
            super.e(sideMot, forMot);    // Apply the motion to the entity
            try {
                ReflectionUtilities.FieldAccess jumpField = ReflectionUtilities.getField(EntityLiving.class, "aY");
                if (jumpField != null && this.onGround) {    // Wouldn't want it jumping while in the air would we?
                    if (jumpField.get(Boolean.class, this.passenger)) {
                        double jumpHeight = 0.5D;
                        this.motY = jumpHeight;    // Used all the time in NMS for entity jumping
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
     
     
  2. It's g(float sideMot, float forMot) in 1.8

    Also not sure if the jumping field is correct, but haven't been using it myself and don't have time to check right now, sorry
     
    • Like Like x 1
  3. Thanks, and yeah don't worry, I've changed all the fields by decompling 1.7.5 CraftBukkit and comparing the usages of the fields between CB 1.7.5 and Spigot 1.8.6.

    I'll test the "g" soon, thanks again.
     
  4. Thank you! The 'g' works.

    Yaw and pitch of player is controlling the iron golem, just need to fix player movement controlling the golem as it isn't working at the moment. I'll figure it I guess =)

    Edit: Fixed, forgot to change super.e() to super.g()
     

  5. Hi, so I'm having the same issue that you had - but for slimes. I have this currently:
    Code (Text):
    public class CustomEntitySlime extends EntitySlime {
        public CustomEntitySlime(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, new UnsafeList<PathfinderGoalSelector>());
                bField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
                cField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
                cField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
            } catch (Exception exc) {
                exc.printStackTrace();
            }
        }

        public static CraftSlime spawn(Location location){
            World mcWorld = ((CraftWorld) location.getWorld()).getHandle();
            CustomEntitySlime customEntity = new CustomEntitySlime(mcWorld);
            customEntity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
            mcWorld.addEntity(customEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
            return (CraftSlime) customEntity.getBukkitEntity();
        }

        @Override
        public void g(float sideMot, float forMot) {
            if (this.passenger == null || !(this.passenger instanceof EntityHuman)) {
                super.g(sideMot, forMot);
                this.S = 0.5F;    // Make sure the entity can walk over half slabs, instead of jumping
                return;
            }

            this.lastYaw = this.yaw = this.passenger.yaw;
            this.pitch = this.passenger.pitch * 0.5F;

            // Set the entity's pitch, yaw, head rotation etc.
            this.setYawPitch(this.yaw, this.pitch);
            this.aK = this.aI = this.yaw;

            this.S = 1F;    // The custom entity will now automatically climb up 1 high blocks

            sideMot = ((EntityLiving) this.passenger).aZ * 0.5F;
            forMot = ((EntityLiving) this.passenger).ba;

            if (forMot <= 0.0F) {
                forMot *= 0.25F;    // Make backwards slower
            }
            sideMot *= 0.75F;    // Also make sideways slower

            float speed = 0.35F;    // 0.2 is the default entity speed. I made it slightly faster so that riding is better than walking
            this.k(speed);    // Apply the speed
            super.g(sideMot, forMot);    // Apply the motion to the entity
        }
    }

    But when I spawn the entity in:
    Code (Text):
    //Spawn slime
    Slime slime = CustomEntitySlime.spawn(loc);
    slime.setPassenger(player);

    When setting the passenger, 9 out of 10 times they won't mount the slime correctly. They'll just be put next to it and glitch out.

    Is this something you've come across?
     
    #5 Simo389, May 31, 2015
    Last edited: May 31, 2015
  6. @Simo389 No, I haven't come across this, but maybe it's just the nature of slimes due to their small sizes? Or due to slimes jumping causing this glitch? Or only certain entities can be ridden like that? Try printing out messages in the "g" method.

    By the way, to set passengers for me, I set the entity's (NMS) passenger first, then I use nmsEntity.getBukkitEntity().setPassenger(player);

    I do this because I noticed that when I did nmsEntity.passenger = ((CraftPlayer) player).getHandle();
    Not: nmsEntity.getBukkitEntity().setPassenger(player);

    It did nothing.

    This may not help you at all though.
     
  7. The way that worked for me was doing:
    ((CraftPlayer) player).getHandle().mount(entity);

    If you want to go down the NMS route that is.
     
    • Like Like x 1
  8. Didn't realise this existed, thanks.
     
  9. 'May 31st 2015'... Are you alright?
     
  10. Uhmp, I am sorry a have not seen the date *facepalm
     
    • Funny Funny x 2
  11. This method work in 1.10 ?
     
  12. OP, you might want to close the thread
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.