当前位置: 首页 > 知识库问答 >
问题:

为什么箭会击中球员?Minecraft Modding 1.8

侯沈义
2023-03-14

我正在尝试使用Minecraft Forge 1.8库为Minecraft开发一个mod。对于我的mod,我决定创建自己的自定义箭头,从我的自定义弩中发射。在为箭头放置我的Item、Entity和Render类后,我发现箭头继续击中玩家,但它被抛出。我尝试实现IThrowableEntity并对箭头的射手进行了调试检查,但我找不到问题。

这是来自EntityArrow类的代码:onUpdate():

@Override
public void onUpdate() {
    if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
    {
        float f = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
        this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
        this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(this.motionY, (double)f) * 180.0D / Math.PI);
    }

    BlockPos blockpos = new BlockPos(this.xTile, this.yTile, this.zTile);
    IBlockState iblockstate = this.worldObj.getBlockState(blockpos);
    Block block = iblockstate.getBlock();

    if (block.getMaterial() != Material.air)
    {
        block.setBlockBoundsBasedOnState(this.worldObj, blockpos);
        AxisAlignedBB axisalignedbb = block.getCollisionBoundingBox(this.worldObj, blockpos, iblockstate);

        if (axisalignedbb != null && axisalignedbb.isVecInside(new Vec3(this.posX, this.posY, this.posZ)))
        {
            this.inGround = true;
        }
    }

    if (this.arrowShake > 0)
    {
        --this.arrowShake;
    }

    if (this.inGround)
    {
        int j = block.getMetaFromState(iblockstate);

        if (block == this.inTile && j == this.inData)
        {
            ++this.ticksInGround;

            if (this.ticksInGround >= 1200)
            {
                this.setDead();
            }
        }
        else
        {
            this.inGround = false;
            this.motionX *= (double)(this.rand.nextFloat() * 0.2F);
            this.motionY *= (double)(this.rand.nextFloat() * 0.2F);
            this.motionZ *= (double)(this.rand.nextFloat() * 0.2F);
            this.ticksInGround = 0;
            this.ticksInAir = 0;
        }
    }
    else
    {
        ++this.ticksInAir;
        Vec3 vec31 = new Vec3(this.posX, this.posY, this.posZ);
        Vec3 vec3 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
        MovingObjectPosition movingobjectposition = this.worldObj.rayTraceBlocks(vec31, vec3, false, true, false);
        vec31 = new Vec3(this.posX, this.posY, this.posZ);
        vec3 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);

        if (movingobjectposition != null)
        {
            vec3 = new Vec3(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
        }

        Entity entity = null;
        List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
        double d0 = 0.0D;
        int i;
        float f1;

        for (i = 0; i < list.size(); ++i)
        {
            Entity entity1 = (Entity)list.get(i);

            if (entity1.canBeCollidedWith() && (entity1 != this.shootingEntity || this.ticksInAir >= 100)){
            {
                f1 = 0.3F;
                AxisAlignedBB axisalignedbb1 = entity1.getEntityBoundingBox().expand((double)f1, (double)f1, (double)f1);
                MovingObjectPosition movingobjectposition1 = axisalignedbb1.calculateIntercept(vec31, vec3);

                if (movingobjectposition1 != null)
                {
                    double d1 = vec31.distanceTo(movingobjectposition1.hitVec);

                    if (d1 < d0 || d0 == 0.0D)
                    {
                        entity = entity1;
                        d0 = d1;
                    }
                }
            }
        }

        if (entity != null)
        {
            movingobjectposition = new MovingObjectPosition(entity);
        }

        if (movingobjectposition != null && movingobjectposition.entityHit != null && movingobjectposition.entityHit instanceof EntityPlayer)
        {
            EntityPlayer entityplayer = (EntityPlayer)movingobjectposition.entityHit;

            if (entityplayer.capabilities.disableDamage || this.shootingEntity instanceof EntityPlayer && !((EntityPlayer)this.shootingEntity).canAttackPlayer(entityplayer))
            {
                movingobjectposition = null;
            }
        }

        float f2;
        float f3;
        float f4;

        if (movingobjectposition != null)
        {
            if (movingobjectposition.entityHit != null)
            {
                f2 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
                int k = MathHelper.ceiling_double_int((double)f2 * this.damage);

                if (this.getIsCritical())
                {
                    k += this.rand.nextInt(k / 2 + 2);
                }

                DamageSource damagesource;

                if (this.shootingEntity == null)
                {
                    damagesource = causeFlingDamage(this, this);
                }
                else
                {
                    damagesource = causeFlingDamage(this, this.shootingEntity);
                }

                if (this.isBurning() && !(movingobjectposition.entityHit instanceof EntityEnderman))
                {
                    movingobjectposition.entityHit.setFire(5);
                }

                if (movingobjectposition.entityHit.attackEntityFrom(damagesource, (float)k))
                {
                    if (movingobjectposition.entityHit instanceof EntityLivingBase)
                    {
                        EntityLivingBase base = (EntityLivingBase)movingobjectposition.entityHit;

                        if (!this.worldObj.isRemote)
                        {
                            base.setArrowCountInEntity(base.getArrowCountInEntity() + 1);


                        if (this.knockbackStrength > 0)
                        {
                            f4 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);

                            if (f4 > 0.0F)
                            {
                                movingobjectposition.entityHit.addVelocity(this.motionX * (double)this.knockbackStrength * 0.6000000238418579D / (double)f4, 0.1D, this.motionZ * (double)this.knockbackStrength * 0.6000000238418579D / (double)f4);
                            }
                        }

                        if (this.shootingEntity instanceof EntityLivingBase)
                        {
                            EnchantmentHelper.func_151384_a(base, this.shootingEntity);
                            EnchantmentHelper.func_151385_b((EntityLivingBase)this.shootingEntity, base);
                        }

                        if (this.shootingEntity != null && movingobjectposition.entityHit != this.shootingEntity && movingobjectposition.entityHit instanceof EntityPlayer && this.shootingEntity instanceof EntityPlayerMP)
                        {
                            ((EntityPlayerMP)this.shootingEntity).playerNetServerHandler.sendPacket(new S2BPacketChangeGameState(6, 0.0F));
                        }
                    }

                    this.playSound("random.bowhit", 1.0F, 1.2F / (this.rand.nextFloat() * 0.2F + 0.9F));


                    if (!(movingobjectposition.entityHit instanceof EntityEnderman)){
                        this.worldObj.setBlockState(getPosition(), Blocks.hardened_clay.getDefaultState());
                        this.setDead();
                    }
                }
                else
                {
                    this.motionX *= -0.10000000149011612D;
                    this.motionY *= -0.10000000149011612D;
                    this.motionZ *= -0.10000000149011612D;
                    this.rotationYaw += 180.0F;
                    this.prevRotationYaw += 180.0F;
                    this.ticksInAir = 0;
                }
            }
            else
            {
                BlockPos blockpos1 = movingobjectposition.getBlockPos();
                this.xTile = blockpos1.getX();
                this.yTile = blockpos1.getY();
                this.zTile = blockpos1.getZ();
                iblockstate = this.worldObj.getBlockState(blockpos1);
                this.inTile = iblockstate.getBlock();
                this.inData = this.inTile.getMetaFromState(iblockstate);
                this.motionX = (double)((float)(movingobjectposition.hitVec.xCoord - this.posX));
                this.motionY = (double)((float)(movingobjectposition.hitVec.yCoord - this.posY));
                this.motionZ = (double)((float)(movingobjectposition.hitVec.zCoord - this.posZ));
                f3 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
                this.posX -= this.motionX / (double)f3 * 0.05000000074505806D;
                this.posY -= this.motionY / (double)f3 * 0.05000000074505806D;
                this.posZ -= this.motionZ / (double)f3 * 0.05000000074505806D;
                this.playSound("random.bowhit", 1.0F, 1.2F / (this.rand.nextFloat() * 0.2F + 0.9F));
                this.inGround = true;
                this.arrowShake = 7;
                this.setIsCritical(false);

                if (this.inTile.getMaterial() != Material.air)
                {
                    this.inTile.onEntityCollidedWithBlock(this.worldObj, blockpos1, iblockstate, this);
                }
            }
        }

        if (this.getIsCritical())
        {
            for (i = 0; i < 4; ++i)
            {
                this.worldObj.spawnParticle(EnumParticleTypes.CRIT, this.posX + this.motionX * (double)i / 4.0D, this.posY + this.motionY * (double)i / 4.0D, this.posZ + this.motionZ * (double)i / 4.0D, -this.motionX, -this.motionY + 0.2D, -this.motionZ, new int[0]);
            }
        }

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

        for (this.rotationPitch = (float)(Math.atan2(this.motionY, (double)f2) * 180.0D / Math.PI); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
        {
            ;
        }

        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;
        f3 = 0.99F;
        f1 = 0.05F;
    }

在弩中的鼠标右键单击()中声明:

   float f = (float)j / 20.0F;
            f = (f * f + f * 2.0F) / 3.0F;

            if ((double)f < 0.1D)
            {
                return stack;
            }

            if (f > 1.0F)
            {
                f = 1.0F;
            }

            EntityFlingArrow entityarrow = new EntityFlingArrow(world, player, f * 2.0F);

共有1个答案

万坚壁
2023-03-14

获取玩家的边界框,并确保您在该框之外生成箭头。当您的实体和玩家的边界框碰撞时,它就被击中了。

这甚至可以在你实体的产卵上,因为它们会相交。

看看普通弓箭或雪球的代码,它们是如何决定产卵的。然后根据实体的边界框大小进行调整。

 类似资料:
  • 我一直在监控我的应用程序错误,我看到下面的错误太多次了 错误:14077410:SSL例程:SSL23\u GET\u SERVER\u HELLO:sslv3警报握手失败(外部/openssl/SSL/s23\u clnt.c:741 0xaa48cd5c:0x00000000)-javax。网ssl。SSLHandshakeException:javax。网ssl。SSLProtocolExc

  • 问题内容: 经过寻找一些参考的数字出来,-unfortunately-我找不到任何关于理解之间的差异有用-和简单- 描述和。当试图了解我们应该如何使用它们时,这有点令人困惑。 我要说的是,我对-default-最为熟悉,它具有传播错误的最简单形式,如下所示: 到目前为止还不错,但是在以下情况下会出现问题: 到目前为止我所知道的是当调用一个函数时必须由a处理,与。所以呢?!在决定使用或时应遵循的逻辑

  • 当我转到<代码>/银行/1我看到了预期的账户信息。很好,很好。 当我转到<代码>/银行/1/description我看到了描述(好),但我也看到了帐户信息(不好)。 我习惯了Spring的,如果多个路径匹配,事情就会中断——但即便如此,AFAIK,在我的代码中,无论如何只有一个应该匹配? 银行。Java语言 帐户ction.java 日志输出:

  • 我得到一个当我运行我的Java应用程序。典型的原因是什么?

  • 这是我的密码-------- 当我尝试对.post(/register…)路由进行邮递员测试时,它会暂停加载几分钟,然后给出错误-- “无法得到任何响应 连接到 http://localhost:5000/api/users/register 时出错。为什么会发生这种情况: 服务器无法发送响应:确保后端正常工作 自签名 SSL 证书被阻止:通过在“设置”中关闭“SSL 证书验证”来解决此问题 请求

  • 问题内容: 为什么在代码的指定位置出现ConcurrentModificationException?我无法弄清楚自己在做什么错… 正在使用该方法在列表中找到分钟,将其删除并返回其值 问题答案: 一旦修改了从其获得的Collection,则不应认为Iterator可用。(对于java.util.concurrent。*集合类,放宽了此限制。) 您首先要获得一个Iterator ,然后进行修改。修改