Vswe's Summer Courses | You're not logged in. Sign up or log in | Server time: | Donate
One can now see other persons' badge hunts. Simply head over to their profiles to access them.

Go back to course page

The anatomy of a Minecraft model

How to work with and create models for Minecraft

entity motion "bug?"...

  • 2014-01-16 12:19:46
    Hi there!
    I am experiencing a pretty annoying thing with my mountable entity.
    It's a vehicle that I call the Hover Board. I don't know if it's a bug or something wrong with my code but about every third second it teleports back to where I spawned it. This is my entity class:

    package em.entities;

    import net.minecraft.client.Minecraft;
    import net.minecraft.entity.Entity;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.util.AxisAlignedBB;
    import net.minecraft.world.World;

    public class EntityHover extends Entity{
        
        double motionXs = 0;
        double motionYs = 0;
        double motionZs = 0;
        private boolean[] lastPressedState = new boolean[6];

        public EntityHover(World world) {
            super(world);
            setSize(1.5F, 1F);
        }

        @Override
        public AxisAlignedBB getBoundingBox() {
            return boundingBox;        
        }

        @Override
        public AxisAlignedBB getCollisionBox(Entity entity) {
            if (entity != riddenByEntity) {
                return entity.boundingBox;
            }else{
                return null;
            }
        }
        
        @Override
        public boolean canBeCollidedWith() {
            return !isDead;
        }
        
        @Override
        public boolean interactFirst(EntityPlayer player) {
            if (!worldObj.isRemote && riddenByEntity == null) {
                player.mountEntity(this);
            }
            
            return true;
        }
        
        @Override
        public double getMountedYOffset() {
            return -0.15;
        }

        @Override
        public void onUpdate() {

            if(worldObj.isRemote) {
                boolean forward = Minecraft.getMinecraft().gameSettings.keyBindForward.pressed;
                boolean back = Minecraft.getMinecraft().gameSettings.keyBindBack.pressed;
                boolean left = Minecraft.getMinecraft().gameSettings.keyBindLeft.pressed;
                boolean right = Minecraft.getMinecraft().gameSettings.keyBindRight.pressed;
                boolean up = Minecraft.getMinecraft().gameSettings.keyBindJump.pressed;
                boolean down = Minecraft.getMinecraft().gameSettings.keyBindPlayerList.pressed;
                
                if (forward && !lastPressedState[0] && riddenByEntity == Minecraft.getMinecraft().thePlayer) {
                    motionXs -= 0.1;
                }else if (back && !lastPressedState[1] && riddenByEntity == Minecraft.getMinecraft().thePlayer) {
                    motionXs += 0.1;
                }else if (left && !lastPressedState[2] && riddenByEntity == Minecraft.getMinecraft().thePlayer) {
                    motionZs += 0.1;
                }else if (right && !lastPressedState[3] && riddenByEntity == Minecraft.getMinecraft().thePlayer) {
                    motionZs -= 0.1;
                }else if (up && !lastPressedState[4] && riddenByEntity == Minecraft.getMinecraft().thePlayer && worldObj.isAirBlock((int)posX, (int)posY + 1, (int)posZ)) {
                    motionYs += 0.1;
                }else if (down && !lastPressedState[5] && riddenByEntity == Minecraft.getMinecraft().thePlayer && worldObj.isAirBlock((int)posX, (int)posY - 1, (int)posZ)) {
                    motionYs -= 0.1;
                }
                
                motionX = motionXs;
                motionY = motionYs;
                motionZ = motionZs;
                
                lastPressedState[0] = forward;
                lastPressedState[1] = back;
                lastPressedState[2] = left;
                lastPressedState[3] = right;
                lastPressedState[4] = up;
                lastPressedState[5] = down;

            }
            
            setPosition(posX + motionX, posY + motionY, posZ + motionZ);    
        }
        
        @Override
        protected void entityInit() {
            dataWatcher.addObject(15, (byte)10);
        }

        @Override
        protected void readEntityFromNBT(NBTTagCompound compound) {
        }

        @Override
        protected void writeEntityToNBT(NBTTagCompound compound) {
        }

    }
  • 2014-01-16 13:05:32
    You only move it on the client side, that make it go back since you never actually moved it in the first place.
  • 2014-01-16 13:56:24
    Aww! What a stupid miss!
    The problem is that now it doesn't move at all :(

    (PS : the only thing I changed was in onUpdate() from if(worldObj.isRemote) to if(!worldObj.isRemote))
  • 2014-01-16 17:57:33
    Why doesn't it move?
  • 2014-01-16 22:36:52
    Since key presses only exists on the client side.
  • 2014-01-17 15:04:35
    Then how do I solve this?
    Sry for beeing so "nooby" but indeed I am one :/
  • 2014-01-18 14:48:54
    Well, you need to send the data to the server (since the server has no keyboard on its own).

    First of all you need to define which client that would send this information (for instance the player riding the entity). Then you would have to send a packet with the required information. All of this is shown in course 3.
  • 2014-01-22 16:59:03
    Ok, now I've done what I could but it doesn't work. It really annoys me that I can't solve the problem myself but that's the case.

    This is my entity class...
    http://pastebin.com/jxsfqJ3v

    ...and this is my packethandler
    http://pastebin.com/vd57h0T6

    What have I done wrong this time?
  • 2014-01-23 10:03:21
    Maybe that's it

    if(f || b || l || r || u || d) {
    PacketHandler.sendHoverMotionPacket(this, motion1, motion1, motion1); <--- (sending only motion1)
    }
  • 2014-01-23 19:11:18
    I feel like a professional failer, I changed that but it still doesn't work :/
  • 2014-01-25 14:06:25
    What have I done wrong?
  • 2014-01-25 19:08:57
    try filling your code with System.out.println("..."); to make sure that everything is happening how it should, like if the key is pressed, if the packet is sent and received, if you are on the client or server side (you can use this: FMLCommonHandler.instance().getEffectiveSide().isClient() or FMLCommonHandler.instance().getEffectiveSide().isServer() ) and so on
  • 2014-01-25 21:13:47
    Why didn't I think of that!?
    I finally found the problem. In my packethandler, before I sent the motion to the server, I checked this:

    entityPlayer.riddenByEntity == entityPlayer

    ...instead of this:

    entity.riddenByEntity == entityPlayer

    Which made everything pretty wierd, so thanks a lot everyone!!!!
  • 2014-01-25 21:14:52
    I mean before I sent the motion to the client, of course ;)
  • Log in or sign up to reply to this thread