-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
FollowTrait.java
72 lines (63 loc) · 2.11 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
package net.citizensnpcs.trait;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
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;
@TraitName("followtrait")
public class FollowTrait extends Trait {
@Persist
private boolean active = false;
@Persist
private UUID followingUUID;
private Player player;
@Persist
private boolean protect;
public FollowTrait() {
super("followtrait");
}
private boolean isActive() {
return active && npc.isSpawned() && player != null && npc.getEntity().getWorld().equals(player.getWorld());
}
@EventHandler
public void onEntityDamage(EntityDamageByEntityEvent event) {
if (isActive() && event.getEntity().equals(player)) {
npc.getNavigator().setTarget(event.getDamager(), 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);
}
}
public boolean toggle(OfflinePlayer player, boolean protect) {
this.protect = protect;
if (player.getUniqueId().equals(this.followingUUID) || this.followingUUID == null) {
this.active = !active;
}
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.active;
}
}