Jump to content

Adding potionEffect to Attacking entity


urabaros

Recommended Posts

Hi!

I'm trying to add potion effect and particles to entity that is attaching player that wearing special armor. 

I've already added potion effect and particles to player and it works perfectly fine. What am I doing wrong with entity?

@OnlyIn(Dist.CLIENT)
	@Override
	public void onArmorTick(ItemStack stack, World world, PlayerEntity player) {
		world.addParticle(ParticleTypes.SQUID_INK, (double)player.prevPosX, (double)player.prevPosY - 0.15D, 
				(double)player.prevPosZ, 0.01D, 0.01D, 0.01D);
		if(world.isNightTime()) {
			player.addPotionEffect(new EffectInstance(Effects.NIGHT_VISION, 40, 0));
		} 
		
		LivingEntity entityIn = player.getAttackingEntity();
		
		if(player.hitByEntity(entityIn)) {
			entityIn.addPotionEffect(new EffectInstance(Effects.SLOWNESS, 60, 0));
			entityIn.addPotionEffect(new EffectInstance(Effects.POISON, 60, 0));
			world.addParticle(ParticleTypes.LARGE_SMOKE, (double)entityIn.prevPosX, (double)entityIn.prevPosY + 1D, 
					(double)entityIn.prevPosZ, 0.01D, 0.01D, 0.01D);
		}
     
	}

 

Link to comment
Share on other sites

  • Thanks, I think I accidently put it there, i've deleted it.
  • I didn't find anything else, I've just searched in available functions of player.(functions that eclipse suggests) and it was the most applicable one. What function should I use instead?
  • It doesn't really matter for me, but if you tell me how to do it anther way, I would be gla-a-ad.
Link to comment
Share on other sites

Yes, that's logical... I've done that, but I think that I did something wrong. I placed this into my armor class:

    @SubscribeEvent
    public void onLivingAttackEvent(LivingAttackEvent event)
    {
        event.getEntityLiving().addPotionEffect(new EffectInstance(Effects.WEAKNESS, 700, 1));
        event.getEntityLiving().addPotionEffect(new EffectInstance(Effects.SLOWNESS, 700, 1));
        event.getEntityLiving().addPotionEffect(new EffectInstance(Effects.POISON, 700, 1 ));
    }

And i have noooo idea how to add particles to this mob. 

Link to comment
Share on other sites

Yes, I was considering adding it to EventHandler, but I can't figure out how should I check if player is wearing armor or not.

I'm adding particles with function addParticle:


public class EventHandler {

    @SubscribeEvent
    public void onLivingAttackEvent(LivingAttackEvent event)
    {
        event.getEntityLiving().addPotionEffect(new EffectInstance(Effects.WEAKNESS, 700, 1));
        event.getEntityLiving().addPotionEffect(new EffectInstance(Effects.SLOWNESS, 700, 1));
        event.getEntityLiving().addPotionEffect(new EffectInstance(Effects.POISON, 700, 1 ));
        
        event.getEntity().world.addParticle(ParticleTypes.CLOUD, (double) event.getEntity().prevPosX, (double) event.getEntity().prevPosY - 0.15D,
                    (double) event.getEntity().prevPosZ, 0.01D, 0.01D, 0.01D);
    }

}

P.s. i'm very sorry for my ignorance, but i'm really really want to learn it

Link to comment
Share on other sites

Ok, it seems right for me now, but it still doesn't work

I've deleted particles line, because I think it's too much for my Java level right now 😕


public class EventHandler {

    @SubscribeEvent
    public void onLivingAttackEvent(LivingAttackEvent event)
    {
        if (event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == ItemInit.CUSTOM_HELMET.get()) 
        {
            event.getEntityLiving().addPotionEffect(new EffectInstance(Effects.WEAKNESS, 700, 1));
            event.getEntityLiving().addPotionEffect(new EffectInstance(Effects.SLOWNESS, 700, 1));
            event.getEntityLiving().addPotionEffect(new EffectInstance(Effects.POISON, 700, 1 ));
        }

    }

}

Link to comment
Share on other sites

Yea, I was registering it wrong! Now it's:

		MinecraftForge.EVENT_BUS.register(EventHandler.class);

And, of course, I've change my method to static. So it was working, but it was working wrong, because it was adding potion effect to player, so I've changed it to

event.getEntityLiving().getAttackingEntity().addPotionEffect(new EffectInstance(Effects.WEAKNESS, 700, 1));

But now it just breaks as soon as mob attacks me. What can be the reason?

May be I should fo it through .getLastDamageSource() or something else?

Link to comment
Share on other sites

Yea! 

@SubscribeEvent
	public static void onLivingAttackEvent(LivingAttackEvent event)
	{
		if (event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == ItemInit.CUSTOM_HELMET.get()) 
		{
			event.getSource().getTrueSource().getEntity().onKillCommand();
		}

	}

It works this way with onKillCommand, but I can't understand how to convert Entity to EntityLiving, because I can add potion effects only to EntityLiving.

Link to comment
Share on other sites

Thank you! It works!

In case someone need the same thing, here is the right code:


	@SubscribeEvent
	public static void onLivingAttackEvent(LivingAttackEvent event)
	{
		if (event.getEntityLiving().getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == ItemInit.CUSTOM_HELMET.get()) 
		{		
			
			 if (event.getSource().getTrueSource().getEntity() instanceof MobEntity) 
			 {
				 ((MobEntity)event.getSource().getTrueSource().getEntity()).addPotionEffect(new EffectInstance(Effects.SLOWNESS, 700, 1));
				 ((MobEntity)event.getSource().getTrueSource().getEntity()).addPotionEffect(new EffectInstance(Effects.WEAKNESS, 700, 1));
					
			 }
				 
		}

	}

But for some reason I can add Poison or Instance damage to mobs 😕 it simply doesn't appear in the game

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.