Jump to content

Killing the entity you are riding desync [SOLVED]


Eastonium

Recommended Posts

I'm currently trying to make an entity that you can hit while riding. However, when you give this entity enough damage to kill it, it is dying server-side but not client side. 

Another peculiarity is that this issue does not occur if you're in creative mode, and if you switch to creative mode while riding the entity when should be dead, it will die and you'll dismount.

I've done some debugging and have found that the damage update from the final blow to the entity is not getting sent to the client, but any damage updates before that are.

This is confirmed to happen with both attacking player damage and damage from lava.

I'll keep looking into this issue, but if someone knows what might be causing this, or how other rideable entities accomplish this, please let me know!

 

EDIT: Current Entity Code

public class EntityFreezeIce extends Entity {
    private static final DataParameter<Float> HEALTH = EntityDataManager.<Float>createKey(EntityFreezeIce.class, DataSerializers.FLOAT);

	public EntityFreezeIce(World worldIn) {
		super(worldIn);
		this.preventEntitySpawning = true;
		this.setSize(0.7F, 1.5F);
	}
	
	public EntityFreezeIce(World worldIn, Entity entityIn) {
		this(worldIn);
		this.setPosition(entityIn.posX, entityIn.posY, entityIn.posZ);
		this.setRotation(entityIn.rotationYaw, 0.0F);
		this.motionX = entityIn.motionX;
		this.motionY = entityIn.motionY;
		this.motionZ = entityIn.motionZ;
		this.setDamage(7.0F);
		entityIn.startRiding(this, true);
	}

	@Override
	protected void entityInit() {
        this.dataManager.register(HEALTH, Float.valueOf(0.0F));		
	}
	
    public void setDamage(float damage) {
        this.dataManager.set(HEALTH, Float.valueOf(damage));
    }
    
    public float getDamage() {
        return ((Float)this.dataManager.get(HEALTH)).floatValue();
    }
	
	@Nullable
	@Override
    public AxisAlignedBB getCollisionBox(Entity entityIn) {
        return entityIn.canBePushed() ? entityIn.getEntityBoundingBox() : null;
    }

    @Nullable
    @Override
    public AxisAlignedBB getCollisionBoundingBox() {
        return this.getEntityBoundingBox();
    }

    @Override
    public boolean canBePushed() {
        return false;
    }
    
    @Override
    public boolean canBeCollidedWith() {
        return !this.isDead;
    }
    
    @Override
	public boolean canRiderInteract() {
        return !this.isDead;
    }

    @Override
    public double getMountedYOffset() {
        return 0.0D;
    }
    
    @Override
    public boolean attackEntityFrom(DamageSource source, float amount) {
        NuiCraft.logger.log(Level.INFO, world.isRemote + " " + this.isDead + " " + this.getDamage());
        if (!this.world.isRemote && !this.isDead) {
            if (this.isEntityInvulnerable(source)) {
                return false;
            } else {
                this.setBeenAttacked();
                this.setDamage(this.getDamage() - amount);
                boolean fromCreativePlayer = source.getTrueSource() instanceof EntityPlayer && 
                		((EntityPlayer)source.getTrueSource()).capabilities.isCreativeMode;
                if (/*fromCreativePlayer || */this.getDamage() <= 0.0F) {
                	this.setDead();
                }
                NuiCraft.logger.log(Level.INFO, world.isRemote + " " + this.isDead + " " + this.getDamage());
                return true;
            }
        } else {
            return true;
        }
    }
    
    @Override
	protected void readEntityFromNBT(NBTTagCompound compound) {
		this.setDamage(compound.getFloat("health"));
	}

	@Override
	protected void writeEntityToNBT(NBTTagCompound compound) {
		compound.setFloat("health", this.getDamage());		
	}
}

 

SOLUTION:

I was cancelling the dismount event for this entity, but did not include a check to see if this entity was dead. Solved by only cancelling the dismount event if my entity was still alive.

 

Edited by Eastonium
Solution
Link to comment
Share on other sites

Not sure exactly but see a couple things that might be related.

 

I don't think you should be calling setDamage() in your performHurtAnimation() code. What is the purpose of that?

 

In other classes that extend Entity, the attackEntityFrom() method returns false if on the client.

 

I think your return value isn't quite right. It is meant to represent whether the entity took any damage. So instead of return this.isDead you should return true.

 

Lastly, in your writeEntityToNBT() and readEntityToNBT() you need to store the values of your data parameters, otherwise the direction and damage will not save and load.

 

Not sure if any of those things will fix your problem, but I think they are all things that should be fixed.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I've tried everything you suggested, but still no luck. Nothing has changed.

My updated code will be on my initial post.

 

EDIT: I have also found that if you make another mob ride this entity, kill the entity (not the mob) and relog, the mob disappears. This happens in creative mode as well.

Is it possible it is trying to kill everything that is riding my entity?

 

EDIT AGAIN:

I found the source of the problem. It wasn't my entity (although it did have some other issues as jabelar pointed out).

The issue is that I was cancelling the event when you tried to dismount this entity. However, I was not checking if me entity was dead when you tried to dismount. This, when trying to kill my entity, it would try to dismount everything, get stopped, and mess up everything.

Edited by Eastonium
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.