Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Adding potionEffect to Attacking entity


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 post
Share on other sites
  • Why did you add @OnlyIn to this method? Do not use @OnlyIn.
  • Why are you calling hitByEntity here? It makes zero sense in this context and I don't think you know what this method actually does (the proper Mojang name for this method is skipAttackInteraction, the MCP name is terrible).
  • Are you sure you want to add the effect again every single tick?
  • Like 1
Link to post
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 post
Share on other sites
28 minutes ago, urabaros said:

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?

How would I know? You have not explained what you were trying to do here.

 

28 minutes ago, urabaros said:

It doesn't really matter for me, but if you tell me how to do it anther way, I would be gla-a-ad.

I mean... if the effect is going to last 2 minutes or whatever you don't have to add it every tick. In fact that will prevent some effects from working, because their timer is always reset.

  • Like 1
Link to post
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 post
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 post
Share on other sites
1 minute ago, urabaros said:

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.

What does that have to do with the class you put it in? 🤔
Call LivingEntity#getItemStackFromSlot to get armor or other equipment. Then check if it is your armor items.

3 minutes ago, urabaros said:

I'm adding particles with function addParticle:

This won't work, as this is a server side event. You have to specifically use the particle methods in ServerWorld that send pacekts if you want to spawn particles from the server.

  • Like 1
Link to post
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 post
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 post
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 post
Share on other sites

Why on earth do you keep calling random methods? Why are you calling Entity#getEntity? Why are you calling onKillCommand?

2 minutes ago, urabaros said:

but I can't understand how to convert Entity to EntityLiving

Learn basic Java, instanceof and casting.

  • Like 1
Link to post
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 post
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.

Guest
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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.