Jump to content

[1.16.5] playSound and addParticle with hitEntity


auriny

Recommended Posts

howdy! i ran into a problem. it lies in the fact that I wrote such a code that should work, but there are no sounds and particles on entity hits.

    public boolean hitEntity(ItemStack stack, LivingEntity target, LivingEntity attacker, World world) {
        if (!attacker.world.isRemote()) {
            target.addPotionEffect(new EffectInstance(Effects.SLOWNESS, 45, 1));
            world.playSound(null, attacker.getPosX(), attacker.getPosY(), attacker.getPosZ(),
                    SoundEvents.BLOCK_ANCIENT_DEBRIS_BREAK, SoundCategory.PLAYERS, 1, 1);
            world.addParticle(new BlockParticleData(ParticleTypes.BLOCK, Blocks.ANCIENT_DEBRIS.getDefaultState()),
                    true, target.getPosX(), target.getPosY() + 0.5D,
                    target.getPosZ(), 0, 1, 0);
        }
        return super.hitEntity(stack, target, attacker);
    }

as I understand it, the problem is that there is no world in the return, but it is impossible to embed it there, because. the hitEntity method initially did not have such a parameter. how to be?

 

snapshot mappings

Link to comment
Share on other sites

Particles are client sided, so by having the if(!attacker.world.isRemote()), they won't appear. Since you have the world parameter, the method won't run unless you have called it from somewhere else, you can simply just get the world from the attacker and remove the world parameter. Below is an example.

public boolean hitEntity(ItemStack stack, LivingEntity target, LivingEntity attacker) 
{
	World world = attacker.world(); //get the world from the attacker

    if (!world.isRemote()) 
	{
        target.addPotionEffect(new EffectInstance(Effects.SLOWNESS, 45, 1));
        world.playSound(null, attacker.getPosX(), attacker.getPosY(), attacker.getPosZ(),
            SoundEvents.BLOCK_ANCIENT_DEBRIS_BREAK, SoundCategory.PLAYERS, 1, 1);
	}
	
	else
	{
		world.addParticle(new BlockParticleData(ParticleTypes.BLOCK, Blocks.ANCIENT_DEBRIS.getDefaultState()),
            true, target.getPosX(), target.getPosY() + 0.5D,
            target.getPosZ(), 0, 1, 0);
	}
    return super.hitEntity(stack, target, attacker);
}

 

Edited by ProfitOrange
Link to comment
Share on other sites

3 hours ago, ProfitOrange said:

Particles are client sided, so by having the if(!attacker.world.isRemote()), they won't appear. Since you have the world parameter, the method won't run unless you have called it from somewhere else, you can simply just get the world from the attacker and remove the world parameter. Below is an example.

public boolean hitEntity(ItemStack stack, LivingEntity target, LivingEntity attacker) 
{
	World world = attacker.world(); //get the world from the attacker

    if (!world.isRemote()) 
	{
        target.addPotionEffect(new EffectInstance(Effects.SLOWNESS, 45, 1));
        world.playSound(null, attacker.getPosX(), attacker.getPosY(), attacker.getPosZ(),
            SoundEvents.BLOCK_ANCIENT_DEBRIS_BREAK, SoundCategory.PLAYERS, 1, 1);
	}
	
	else
	{
		world.addParticle(new BlockParticleData(ParticleTypes.BLOCK, Blocks.ANCIENT_DEBRIS.getDefaultState()),
            true, target.getPosX(), target.getPosY() + 0.5D,
            target.getPosZ(), 0, 1, 0);
	}
    return super.hitEntity(stack, target, attacker);
}

 

thank you, sound worked, but didnt particles 

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.