Concussion909 Posted July 20, 2015 Posted July 20, 2015 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(); Quote
Concussion909 Posted July 20, 2015 Author Posted July 20, 2015 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(); }}}}} Quote
Choonster Posted July 20, 2015 Posted July 20, 2015 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. Quote 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.
Concussion909 Posted July 21, 2015 Author Posted July 21, 2015 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) Quote
Choonster Posted July 21, 2015 Posted July 21, 2015 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. Quote 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.
Concussion909 Posted July 23, 2015 Author Posted July 23, 2015 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 Quote
Concussion909 Posted July 24, 2015 Author Posted July 24, 2015 Not for this one, but I found a solution to it anyway Quote
Concussion909 Posted July 24, 2015 Author Posted July 24, 2015 This is the if statement I used to make the potion effect end at 0 if (event.entityLiving.isPotionActive(MainClass.MobDeflector)) { Quote
Recommended Posts
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.