Jump to content

Recommended Posts

Posted (edited)

Hi,

Having an issue with an entity that extends EntityThrowable. Sometimes, especially if the thrower is moving forwards, the projectile will register a hit as soon as it spawns, but only on the client side. If the player catches up with the projectile, this can continue to occur, often without triggering a hit on the server side. I thought I had found a way to consistently prevent this, but this solution seems to have stopped working. The vanilla projectiles don't seem to have any logic to prevent this, and yet don't have the issue. I'm not sure what the deciding difference is between my projectile and the vanilla ones.

 

The projectile class:

public class EntityMagicOrb extends EntityThrowable implements IEntityAdditionalSpawnData {

    private SpellProjectile spell;
    public byte data;
    private int ignoreTime;

    public EntityMagicOrb(World world) {
        super(world);
    }

    public EntityMagicOrb(World worldIn, double x, double y, double z) {
        super(worldIn, x, y, z);
    }

    public EntityMagicOrb(World world, EntityPlayer player, SpellProjectile spell) {
        super(world, player);

        this.ignoreEntity = player;

        this.spell = spell;
    }

    @Override
    protected void onImpact(@Nonnull RayTraceResult result) {
        boolean kill = false;
        if (result.typeOfHit == RayTraceResult.Type.ENTITY) {
            if (!result.entityHit.isDead) {
                Entity targetEntity = result.entityHit;
                MultiPartEntityPart part = null;

                if (targetEntity instanceof MultiPartEntityPart) {
                    part = (MultiPartEntityPart) targetEntity;
                    IEntityMultiPart ientitymultipart = part.parent;
                    if (ientitymultipart instanceof EntityLivingBase) {
                        targetEntity = (EntityLivingBase) ientitymultipart;
                    }
                }

                if (spell.projectileEffectOnEntity(this, (EntityPlayer) this.getThrower(),
                        targetEntity, this.getEntityWorld(), part)) {
                    kill = true;
                }
            }
        } else if (result.typeOfHit == RayTraceResult.Type.BLOCK) {
            if (spell.projectileEffectOnBlock(this, (EntityPlayer) this.getThrower(),
                    this.getEntityWorld(), result.getBlockPos(), result.sideHit)) {
                kill = true;
            }
        } else {
            spell.projectileEffect(this, (EntityPlayer) this.getThrower(), this.getEntityWorld());
            kill = true;
        }
        if (kill && !world.isRemote) {
            this.setDead();
        }
    }

    @Override
    public void onUpdate() {
        if (spell == null) {
            setDead();
        }
        else {
            spell.projectileTick(this, this.world);
        }
    }

    public void adjustStartPos() {
        this.posX += motionX;
        this.posY += motionY;
        this.posZ += motionZ;
    }

    public Element getElement() {
        return spell.getElement();
    }

    @Override
    protected float getGravityVelocity() {
        return 0.02F;
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);
        nbt.setByte("Spell", (byte) spell.getIndex());
        nbt.setByte("Data", data);
        return nbt;
    }

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        spell = (SpellProjectile) Spells.getFromList(nbt.getByte("Spell"));
        data = nbt.getByte("Data");
    }

    @Override
    public void writeSpawnData(ByteBuf buffer) {
        if (spell != null) {
            buffer.writeInt(spell.getIndex());
        }
        else {
            buffer.writeInt(-1);
        }
        buffer.writeByte(data);
    }
    @Override
    public void readSpawnData(ByteBuf additionalData) {
        spell = (SpellProjectile) Spells.getFromList(additionalData.readInt());
        data = additionalData.readByte();
    }

    public void processProjectile() {
        if (this.ticksExisted > 100 || this.inGround) {
            this.setDead();
        }
        else {
            this.lastTickPosX = this.posX;
            this.lastTickPosY = this.posY;
            this.lastTickPosZ = this.posZ;

            if (!this.world.isRemote) {
                this.setFlag(6, this.isGlowing());
            }
            this.onEntityUpdate();

            Vec3d vec3d = new Vec3d(this.posX, this.posY, this.posZ);
            Vec3d vec3d1 = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
            RayTraceResult raytraceresult = this.world.rayTraceBlocks(vec3d, vec3d1, false, true, false);
            vec3d = new Vec3d(this.posX, this.posY, this.posZ);
            vec3d1 = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);

            if (raytraceresult != null) {
                vec3d1 = new Vec3d(raytraceresult.hitVec.x, raytraceresult.hitVec.y, raytraceresult.hitVec.z);
            }

            Entity entity = null;
            List<Entity> list = this.world.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().expand(this.motionX, this.motionY, this.motionZ).grow(1.0D));
            double d0 = 0.0D;
            boolean flag = false;

            for (Entity entity1 : list) {
                if (entity1.canBeCollidedWith()) {
                    if (entity1 == this.ignoreEntity) {
                        flag = true;
                    } else if (this.thrower != null && this.ticksExisted < 2 && this.ignoreEntity == null) {
                        this.ignoreEntity = entity1;
                        flag = true;
                    } else {
                        flag = false;
                        AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().grow(0.3D);
                        RayTraceResult raytraceresult1 = axisalignedbb.calculateIntercept(vec3d, vec3d1);

                        if (raytraceresult1 != null) {
                            double d1 = vec3d.squareDistanceTo(raytraceresult1.hitVec);
                            if (d1 < d0 || d0 == 0.0D) {
                                entity = entity1;
                                d0 = d1;
                            }
                        }
                    }
                }
            }

            if (this.ignoreEntity != null) {
                if (flag) {
                    this.ignoreTime = 2;
                } else if (this.ignoreTime-- <= 0) {
                    this.ignoreEntity = null;
                }
            }

            if (entity != null) {
                raytraceresult = new RayTraceResult(entity);
            }

            if (raytraceresult != null) {
                if (raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK && this.world.getBlockState(raytraceresult.getBlockPos()).getBlock() == Blocks.PORTAL) {
                    this.setPortal(raytraceresult.getBlockPos());
                } else if (!net.minecraftforge.event.ForgeEventFactory.onProjectileImpact(this, raytraceresult)) {
                    this.onImpact(raytraceresult);
                }
            }

            this.posX += this.motionX;
            this.posY += this.motionY;
            this.posZ += this.motionZ;
            this.rotationYaw = (float) (MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI));

            while (this.rotationPitch - this.prevRotationPitch >= 180.0F) {
                this.prevRotationPitch += 360.0F;
            }
            while (this.rotationYaw - this.prevRotationYaw < -180.0F) {
                this.prevRotationYaw -= 360.0F;
            }
            while (this.rotationYaw - this.prevRotationYaw >= 180.0F) {
                this.prevRotationYaw += 360.0F;
            }

            this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
            this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;

            if (!this.hasNoGravity()) {
                this.motionY -= this.getGravityVelocity();
            }

            this.setPosition(this.posX, this.posY, this.posZ);
        }
    }
}

 

Note: processProjectile() is mostly copied from the superclass onUpdate(), but modified to remove collision with water. I've tested with both this method and the normal onUpdate(), it's not the source of the issue.

 

And here's the code that spawns the projectile:

public boolean spawnProjectile(EntityPlayer player, World world) {
        if (!world.isRemote) {
            EntityMagicOrb entity = new EntityMagicOrb(world, player, this);
            entity.setNoGravity(true);
            onProjectileShoot(entity, player);
            entity.adjustStartPos();
            world.spawnEntity(entity);
            return true;
        }
        return false;
    }

 

Edited by CactusCoffee

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.