[1.19.x] Make LivingSetAttackTargetEvent compatible with the Brain/Behavior system. (Port of PR #8918) (#8954)

This commit is contained in:
Budschie authored and GitHub committed 2022-09-03 20:11:10 +01:00
1 parent 4e2d656e9d
commit ed198f062c
7 files changed
+228 -10

No files matched your search

@@ -118,12 +118,14 @@ import net.minecraftforge.event.entity.EntityTravelToDimensionEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.living.EnderManAngerEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingChangeTargetEvent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingTickEvent;
import net.minecraftforge.event.entity.living.LivingChangeTargetEvent.ILivingTargetType;
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.living.LivingKnockBackEvent;
@@ -263,11 +265,28 @@ public class ForgeHooks
//Optifine Helper Functions u.u, these are here specifically for Optifine
//Note: When using Optifine, these methods are invoked using reflection, which
//incurs a major performance penalty.
// TODO: Remove in 1.20
@Deprecated(since = "1.19.2", forRemoval = true)
public static void onLivingSetAttackTarget(LivingEntity entity, LivingEntity target)
{
MinecraftForge.EVENT_BUS.post(new LivingSetAttackTargetEvent(entity, target));
}
// TODO: Remove in 1.20
@Deprecated(since = "1.19.2", forRemoval = true)
public static void onLivingSetAttackTarget(LivingEntity entity, LivingEntity target, ILivingTargetType targetType)
{
MinecraftForge.EVENT_BUS.post(new LivingSetAttackTargetEvent(entity, target, targetType));
}
public static LivingChangeTargetEvent onLivingChangeTarget(LivingEntity entity, LivingEntity originalTarget, ILivingTargetType targetType)
{
LivingChangeTargetEvent event = new LivingChangeTargetEvent(entity, originalTarget, targetType);
MinecraftForge.EVENT_BUS.post(event);
return event;
}
public static boolean onLivingTick(LivingEntity entity)
{
return MinecraftForge.EVENT_BUS.post(new LivingTickEvent(entity));
@@ -0,0 +1,104 @@
package net.minecraftforge.event.entity.living;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.behavior.StartAttacking;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
/**
* This event allows you to change the target an entity has. <br>
* This event is fired before {@link LivingSetAttackTargetEvent}. <br>
* <br>
* This event is fired via the {@link ForgeHooks#onLivingChangeTarget(LivingEntity, LivingEntity, ILivingTargetType)}<br>
* <br>
* {@link #getOriginalTarget()} returns the target that should originally be set.
* The return value cannot be affected by calling {@link #setNewTarget(LivingEntity)}.<br>
* {@link #getNewTarget()} returns the new target that this entity will have.
* The return value can be affected by calling {@link #setNewTarget(LivingEntity)}.<br>
* {@link #getTargetType()} returns the target type that caused the change of targets.<br>
* <br>
* This event is {@link net.minecraftforge.eventbus.api.Cancelable}.<br>
* <br>
* If you cancel this event, the target will not be changed and it will stay the same.
* Cancelling this event will prevent {@link LivingSetAttackTargetEvent} from being posted.<br>
* <br>
* This event does not have a result. {@link HasResult}<br>
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
*/
@Cancelable
public class LivingChangeTargetEvent extends LivingEvent
{
private final ILivingTargetType targetType;
private final LivingEntity originalTarget;
private LivingEntity newTarget;
public LivingChangeTargetEvent(LivingEntity entity, LivingEntity originalTarget, ILivingTargetType targetType)
{
super(entity);
this.originalTarget = originalTarget;
this.newTarget = originalTarget;
this.targetType = targetType;
}
/**
* {@return the new target of this entity.}
*/
public LivingEntity getNewTarget()
{
return newTarget;
}
/**
* Sets the new target this entity shall have.
* @param newTarget The new target of this entity.
*/
public void setNewTarget(LivingEntity newTarget)
{
this.newTarget = newTarget;
}
/**
* {@return the living target type.}
*/
public ILivingTargetType getTargetType()
{
return targetType;
}
/**
* {@return the original entity MC intended to use as a target before firing this event.}
*/
public LivingEntity getOriginalTarget()
{
return originalTarget;
}
/**
* A living target type indicates what kind of system caused a change of
* targets. For a list of default target types, take a look at
* {@link LivingTargetType}.
*/
public static interface ILivingTargetType
{
}
/**
* This enum contains two default living target types.
*/
public static enum LivingTargetType implements ILivingTargetType
{
/**
* This target type indicates that the target has been set by calling {@link Mob#setTarget(LivingEntity)}.
*/
MOB_TARGET,
/**
* This target type indicates that the target has been set by the {@link StartAttacking} behavior.
*/
BEHAVIOR_TARGET;
}
}
@@ -7,17 +7,23 @@ package net.minecraftforge.event.entity.living;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.behavior.StartAttacking;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingChangeTargetEvent.ILivingTargetType;
import net.minecraftforge.event.entity.living.LivingChangeTargetEvent.LivingTargetType;
/**
* This event is deprecated. Use {@link LivingChangeTargetEvent} instead.<br>
* <br>
* LivingSetAttackTargetEvent is fired when an Entity sets a target to attack.<br>
* This event is fired whenever an Entity sets a target to attack in
* {@link Mob#setTarget(LivingEntity)}.<br>
* {@link Mob#setTarget(LivingEntity)} or {@link StartAttacking#setAttackTarget(Mob, LivingEntity)}<br>
* <br>
* This event is fired via the {@link ForgeHooks#onLivingSetAttackTarget(LivingEntity, LivingEntity)}.<br>
* <br>
* {@link #target} contains the newly targeted Entity.<br>
* {@link #getTarget} returns the newly targeted Entity.<br>
* {@link #getTargetType()} returns the target type that caused the change of targets.<br>
* <br>
* This event is not {@link net.minecraftforge.eventbus.api.Cancelable}.<br>
* <br>
@@ -25,18 +31,41 @@ import net.minecraftforge.common.MinecraftForge;
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
**/
// TODO: Remove in 1.20
@Deprecated(since = "1.19.2", forRemoval = true)
public class LivingSetAttackTargetEvent extends LivingEvent
{
private final LivingEntity target;
private final ILivingTargetType targetType;
private final LivingEntity originalTarget;
public LivingSetAttackTargetEvent(LivingEntity entity, LivingEntity target)
{
super(entity);
this.target = target;
this.targetType = LivingTargetType.MOB_TARGET;
this.originalTarget = target;
}
public LivingSetAttackTargetEvent(LivingEntity entity, LivingEntity target, ILivingTargetType targetType)
{
super(entity);
this.targetType = targetType;
this.originalTarget = target;
}
/**
* {@return the target this living entity had after changing its target to a new
* target, but before posting this event.}
*/
public LivingEntity getTarget()
{
return target;
return originalTarget;
}
/**
* {@return the target type of this event.}
*/
public ILivingTargetType getTargetType()
{
return targetType;
}
}