Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MinecraftForge
GitHub Repository: MinecraftForge/MinecraftForge
Path: blob/1.21.x/src/main/java/net/minecraftforge/event/entity/living/LivingChangeTargetEvent.java
7456 views
1
/*
2
* Copyright (c) Forge Development LLC and contributors
3
* SPDX-License-Identifier: LGPL-2.1-only
4
*/
5
6
package net.minecraftforge.event.entity.living;
7
8
import net.minecraft.world.entity.LivingEntity;
9
import net.minecraft.world.entity.Mob;
10
import net.minecraft.world.entity.ai.behavior.StartAttacking;
11
import net.minecraftforge.common.ForgeHooks;
12
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
13
import net.minecraftforge.eventbus.api.event.MutableEvent;
14
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
15
16
/**
17
* This event allows you to change the target an entity has. <br>
18
* This event is fired before {@link LivingSetAttackTargetEvent}. <br>
19
* <br>
20
* This event is fired via the {@link ForgeHooks#onLivingChangeTarget(LivingEntity, LivingEntity, ILivingTargetType)}<br>
21
* <br>
22
* {@link #getOriginalTarget()} returns the target that should originally be set.
23
* The return value cannot be affected by calling {@link #setNewTarget(LivingEntity)}.<br>
24
* {@link #getNewTarget()} returns the new target that this entity will have.
25
* The return value can be affected by calling {@link #setNewTarget(LivingEntity)}.<br>
26
* {@link #getTargetType()} returns the target type that caused the change of targets.<br>
27
* <br>
28
* This event is {@linkplain Cancellable cancellable}.<br>
29
* <br>
30
* If you cancel this event, the target will not be changed and it will stay the same.
31
* Cancelling this event will prevent {@link LivingSetAttackTargetEvent} from being posted.<br>
32
*/
33
public final class LivingChangeTargetEvent extends MutableEvent implements Cancellable, LivingEvent {
34
public static final CancellableEventBus<LivingChangeTargetEvent> BUS = CancellableEventBus.create(LivingChangeTargetEvent.class);
35
36
private final LivingEntity entity;
37
private final ILivingTargetType targetType;
38
private final LivingEntity originalTarget;
39
private LivingEntity newTarget;
40
41
public LivingChangeTargetEvent(LivingEntity entity, LivingEntity originalTarget, ILivingTargetType targetType) {
42
this.entity = entity;
43
this.originalTarget = originalTarget;
44
this.newTarget = originalTarget;
45
this.targetType = targetType;
46
}
47
48
@Override
49
public LivingEntity getEntity() {
50
return entity;
51
}
52
53
/**
54
* {@return the new target of this entity.}
55
*/
56
public LivingEntity getNewTarget() {
57
return newTarget;
58
}
59
60
/**
61
* Sets the new target this entity shall have.
62
* @param newTarget The new target of this entity.
63
*/
64
public void setNewTarget(LivingEntity newTarget) {
65
this.newTarget = newTarget;
66
}
67
68
/**
69
* {@return the living target type.}
70
*/
71
public ILivingTargetType getTargetType() {
72
return targetType;
73
}
74
75
/**
76
* {@return the original entity MC intended to use as a target before firing this event.}
77
*/
78
public LivingEntity getOriginalTarget() {
79
return originalTarget;
80
}
81
82
83
/**
84
* A living target type indicates what kind of system caused a change of
85
* targets. For a list of default target types, take a look at
86
* {@link LivingTargetType}.
87
*/
88
public interface ILivingTargetType {}
89
90
/**
91
* This enum contains two default living target types.
92
*/
93
public enum LivingTargetType implements ILivingTargetType {
94
/**
95
* This target type indicates that the target has been set by calling {@link Mob#setTarget(LivingEntity)}.
96
*/
97
MOB_TARGET,
98
/**
99
* This target type indicates that the target has been set by the {@link StartAttacking} behavior.
100
*/
101
BEHAVIOR_TARGET
102
}
103
}
104
105