Skip to content

RangedEntity-rangedAttack(LivingEntity target, float charge) dont work #12526

@CraziiDust

Description

@CraziiDust

Expected behavior

When the method is called, the entity fires an arrow with a loaded crossbow.

Observed/Actual behavior

Nothing happens

Steps/models to reproduce

I created a mobgoal, what makes the entity perform ranged attack

@Override
    public void tick() {
        LivingEntity target = entity.getTarget();
        if (target != null) {
            boolean hasSight = entity.hasLineOfSight(target);
            boolean var2 = seeTime > 0;
            if (hasSight != var2) {
                seeTime = 0;
            }

            if (hasSight) {
                ++seeTime;
            } else {
                --seeTime;
            }

            double distanceSqr = entity.getLocation().distanceSquared(target.getLocation());
            boolean canAttack = (distanceSqr > attackRadiusSqr || seeTime > 5) && attackDelay == 0;

            entity.lookAt(target,30f,30f);

            switch (crossbowState) {
                case UNCHARGED -> {
                    if (canAttack) {
                        entity.startUsingItem(EquipmentSlot.HAND);
                        crossbowState = STATE.CHARGING;
                    }
                }
                case CHARGING -> {
                    if (!entity.hasActiveItem()) {
                        crossbowState = STATE.UNCHARGED;
                    }

                    int ticks = entity.getActiveItemUsedTime();
                    ItemStack item = entity.getEquipment().getItem(EquipmentSlot.HAND);

                    if (ticks > 25) {
                        crossbowState = STATE.CHARGED;
                        attackDelay = 20;

                        item.editMeta(CrossbowMeta.class, meta -> meta.addChargedProjectile(ARROW));
                    }
                }
                case CHARGED -> {
                    --attackDelay;
                    if (attackDelay == 0) {
                        crossbowState = STATE.READY_TO_FIRE;
                    }
                }
                case READY_TO_FIRE ->
                {
                    if (hasSight) {
                        entity.completeUsingActiveItem();
                        entity.rangedAttack(target, 1.0f);
                        crossbowState = STATE.UNCHARGED;
                    }
                }
            }
        }
    }

    enum STATE {
        UNCHARGED,
        CHARGING,
        CHARGED,
        READY_TO_FIRE
    }

Plugin and Datapack List

Only my plugin

Paper version

Paper version 1.21.4-227-ver/1.21.4@7675321

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions