Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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();

  • Author

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();

 

}}}}}

 

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.

  • Author

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)

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.

  • Author

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

  • Author

This is the if statement I used to make the potion effect end at 0

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

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

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.