Jump to content

Damage not work


Fennx1000

Recommended Posts

I am trying to make my inventoryTick damage all entities in a 5 block radius constantly and apply particles but the damage is not working. Does anyone know how to make this work? 

public class ElectricPowerCharm extends Item {

    public ElectricPowerCharm(Properties properties) {
        super(properties);
    }

    private int delay = 200;
    private int range = 5; // Range in blocks
    private float damageAmount = 2.0f; // Amount of damage to apply per tick

    @Override
    public void inventoryTick(ItemStack stack, Level level, Entity entity, int itemSlot, boolean isSelected) {
        if (level.isClientSide && entity instanceof Player) {
            Player player = (Player) entity;

            double posX = player.getX();
            double posY = player.getY() + 1.0; // Offset particles slightly above the player's head
            double posZ = player.getZ();

            AABB aabb = new AABB(posX - range, posY - range, posZ - range, posX + range, posY + range, posZ + range);
            for (Entity targetEntity : level.getEntities(player, aabb)) {
                if (targetEntity instanceof LivingEntity) {
                    LivingEntity livingEntity = (LivingEntity) targetEntity;
                    double entityX = livingEntity.getX();
                    double entityY = livingEntity.getY() + 0.7d;
                    double entityZ = livingEntity.getZ();

                    for (int i = 1; i <= 50; i++) {
                        double motionX = level.random.nextGaussian() + 1d;
                        double motionY = level.random.nextGaussian() + 1d;
                        double motionZ = level.random.nextGaussian() + 1d;
                        level.addParticle(ParticleTypes.ELECTRIC_SPARK, entityX, entityY, entityZ, motionX, motionY, motionZ);
                    }

                        livingEntity.hurt(DamageSource.playerAttack(player), damageAmount); // Apply damage
                }
            }
        }
        super.inventoryTick(stack, level, entity, itemSlot, isSelected);
    }
}

 

Link to comment
Share on other sites

2 hours ago, Fennx1000 said:
if (level.isClientSide && entity instanceof Player) {

Damage is applied on the server. Apply damage on the server and then use #sendParticles to render it on the client.

2 hours ago, Fennx1000 said:
private int delay = 200;
    private int range = 5; // Range in blocks
    private float damageAmount = 2.0f; // Amount of damage to apply per tick

You shouldn't do this btw since there will only be one instance of your item, so these values will be shared across all unless you register a new item.

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.