Path: blob/1.21.x/src/main/java/net/minecraftforge/event/entity/living/LivingChangeTargetEvent.java
7456 views
/*1* Copyright (c) Forge Development LLC and contributors2* SPDX-License-Identifier: LGPL-2.1-only3*/45package net.minecraftforge.event.entity.living;67import net.minecraft.world.entity.LivingEntity;8import net.minecraft.world.entity.Mob;9import net.minecraft.world.entity.ai.behavior.StartAttacking;10import net.minecraftforge.common.ForgeHooks;11import net.minecraftforge.eventbus.api.bus.CancellableEventBus;12import net.minecraftforge.eventbus.api.event.MutableEvent;13import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;1415/**16* This event allows you to change the target an entity has. <br>17* This event is fired before {@link LivingSetAttackTargetEvent}. <br>18* <br>19* This event is fired via the {@link ForgeHooks#onLivingChangeTarget(LivingEntity, LivingEntity, ILivingTargetType)}<br>20* <br>21* {@link #getOriginalTarget()} returns the target that should originally be set.22* The return value cannot be affected by calling {@link #setNewTarget(LivingEntity)}.<br>23* {@link #getNewTarget()} returns the new target that this entity will have.24* The return value can be affected by calling {@link #setNewTarget(LivingEntity)}.<br>25* {@link #getTargetType()} returns the target type that caused the change of targets.<br>26* <br>27* This event is {@linkplain Cancellable cancellable}.<br>28* <br>29* If you cancel this event, the target will not be changed and it will stay the same.30* Cancelling this event will prevent {@link LivingSetAttackTargetEvent} from being posted.<br>31*/32public final class LivingChangeTargetEvent extends MutableEvent implements Cancellable, LivingEvent {33public static final CancellableEventBus<LivingChangeTargetEvent> BUS = CancellableEventBus.create(LivingChangeTargetEvent.class);3435private final LivingEntity entity;36private final ILivingTargetType targetType;37private final LivingEntity originalTarget;38private LivingEntity newTarget;3940public LivingChangeTargetEvent(LivingEntity entity, LivingEntity originalTarget, ILivingTargetType targetType) {41this.entity = entity;42this.originalTarget = originalTarget;43this.newTarget = originalTarget;44this.targetType = targetType;45}4647@Override48public LivingEntity getEntity() {49return entity;50}5152/**53* {@return the new target of this entity.}54*/55public LivingEntity getNewTarget() {56return newTarget;57}5859/**60* Sets the new target this entity shall have.61* @param newTarget The new target of this entity.62*/63public void setNewTarget(LivingEntity newTarget) {64this.newTarget = newTarget;65}6667/**68* {@return the living target type.}69*/70public ILivingTargetType getTargetType() {71return targetType;72}7374/**75* {@return the original entity MC intended to use as a target before firing this event.}76*/77public LivingEntity getOriginalTarget() {78return originalTarget;79}808182/**83* A living target type indicates what kind of system caused a change of84* targets. For a list of default target types, take a look at85* {@link LivingTargetType}.86*/87public interface ILivingTargetType {}8889/**90* This enum contains two default living target types.91*/92public enum LivingTargetType implements ILivingTargetType {93/**94* This target type indicates that the target has been set by calling {@link Mob#setTarget(LivingEntity)}.95*/96MOB_TARGET,97/**98* This target type indicates that the target has been set by the {@link StartAttacking} behavior.99*/100BEHAVIOR_TARGET101}102}103104105