-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
FollowTrait.java
102 lines (92 loc) · 3.21 KB
/
FollowTrait.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package net.citizensnpcs.trait;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
/**
* Persists a {@link Player} to follow while spawned. Optionally allows protecting of the player as well.
*/
@TraitName("followtrait")
public class FollowTrait extends Trait {
@Persist("active")
private boolean enabled = false;
@Persist
private UUID followingUUID;
private Player player;
@Persist
private boolean protect;
public FollowTrait() {
super("followtrait");
}
/**
* Returns whether the trait is actively following a {@link Player}.
*/
public boolean isActive() {
return enabled && npc.isSpawned() && player != null && npc.getEntity().getWorld().equals(player.getWorld());
}
public boolean isEnabled() {
return enabled;
}
@EventHandler
private void onEntityDamage(EntityDamageByEntityEvent event) {
if (isActive() && event.getEntity().equals(player)) {
Entity damager = event.getDamager();
if (event.getEntity() instanceof Projectile) {
Projectile projectile = (Projectile) event.getEntity();
if (projectile.getShooter() instanceof Entity) {
damager = (Entity) projectile.getShooter();
}
}
npc.getNavigator().setTarget(damager, true);
}
}
@Override
public void run() {
if (player == null || !player.isValid()) {
if (followingUUID == null)
return;
player = Bukkit.getPlayer(followingUUID);
if (player == null) {
return;
}
}
if (!isActive()) {
return;
}
if (!npc.getNavigator().isNavigating()) {
npc.getNavigator().setTarget(player, false);
}
}
/**
* Toggles and/or sets the {@link OfflinePlayer} to follow and whether to protect them (similar to wolves in
* Minecraft, attack whoever attacks the player).
*
* Will toggle if the {@link OfflinePlayer} is the player currently being followed.
*
* @param player
* the player to follow
* @param protect
* whether to protect the player
* @return whether the trait is enabled
*/
public boolean toggle(OfflinePlayer player, boolean protect) {
this.protect = protect;
if (player.getUniqueId().equals(this.followingUUID) || this.followingUUID == null) {
this.enabled = !enabled;
}
this.followingUUID = player.getUniqueId();
if (npc.getNavigator().isNavigating() && this.player != null && npc.getNavigator().getEntityTarget() != null
&& this.player == npc.getNavigator().getEntityTarget().getTarget()) {
npc.getNavigator().cancelNavigation();
}
this.player = null;
return this.enabled;
}
}