Jump to content

Recommended Posts

Posted

I'm trying to create a potion effect so it will give any mob which is attacking the player, damage, however the player remains unharmed from the attack.

 

I've tried a few different ways of doing so, and none have worked so far.. I've tried to use getsourceof damage, but it refuses to recognise the e.source...

 

Newest method I've tried

else if(e.entityLiving.worldObj.rand.nextInt(30) == 0){

if (e.entity instanceof EntityPlayer){

EntityPlayer player = (EntityPlayer) e.entity;

if (e.source.getSourceOfDamage() != null && e.source.getSourceOfDamage() instanceof EntityMob);

 

{

EntityMob mob = (EntityMob) e.source.getSourceOfDamage();

Posted

package net.ValkyrieMod.Events;

 

import net.minecraft.util.DamageSource;

import net.minecraft.util.EntityDamageSource;

import net.minecraftforge.event.entity.living.LivingAttackEvent;

import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import net.ValkyrieMod.main.MainClass;

import net.minecraft.entity.*;

import net.minecraft.entity.monster.EntityMob;

import net.minecraft.entity.monster.EntityZombie;

import net.minecraft.entity.player.EntityPlayer;

 

public class PotionEvent {

@SubscribeEvent

public void onEntityUpdate(LivingUpdateEvent e){

if(e.entityLiving.isPotionActive(MainClass.MobDeflector)){

if(e.entityLiving.getActivePotionEffect(MainClass.MobDeflector).getDuration() == 0){

e.entityLiving.removePotionEffect(MainClass.MobDeflector.id);

return;

}

}

else if(e.entityLiving.worldObj.rand.nextInt(30) == 0){

if (e.entity instanceof EntityPlayer){

 

EntityPlayer player = (EntityPlayer) e.entity;

 

if (e.source.getSourceOfDamage() != null && e.source.getSourceOfDamage() instanceof EntityMob);

{

EntityMob mob = (EntityMob) e.source.getSourceOfDamage();

 

}}}}}

 

Posted

LivingUpdateEvent doesn't have a DamageSource source field (because it's not at all related to an Entity taking damage).

 

You shouldn't need to manually remove the PotionEffect when it runs out, EntityLivingBase will do that automatically.

 

If you want to react to and cancel damage received by an EntityLivingBase, subscribe to LivingHurtEvent instead.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Thanks for pointing out that mistake,

 

And the EntityLivingBase was stopping at 0:00 and not ending the effect, so used the manual way...

 

I'm still in need of a vague way of completing the task since LivingAttackEvent I think would work better for a monster to take damage (I'm new at coding in general)

Posted

LivingAttackEvent would indeed be a better choice for this. For some reason I thought it was fired when a living entity attacked something rather than when a living entity is attacked by something.

 

You should be able to use e.source.getSourceOfDamage() to get the attacking Entity (like your original code), Entity#attackEntityFrom to attack the mob and Event#cancel to cancel the event (preventing the damage to the entity with the potion effect).

 

instanceof will simply return false if used with a null value, so you don't need to explicitly check for null before using it.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Its the only patch to stop the potion effect, but it is no longer working...

 

The potion is reaching 0:00 and then disappears from the inventory menu but the potion effect stays active... Is there any way to make the potion effect properly end after hitting 0:00

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.