/
EntityMovement.java
161 lines (140 loc) · 6.94 KB
/
EntityMovement.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package net.aufdemrand.denizen.utilities.entity;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.minecraft.server.v1_8_R2.EntityInsentient;
import net.minecraft.server.v1_8_R2.GenericAttributes;
import net.minecraft.server.v1_8_R2.NavigationAbstract;
import net.minecraft.server.v1_8_R2.PathEntity;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity;
import org.bukkit.entity.Entity;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class EntityMovement {
private final static Map<UUID, BukkitTask> followTasks = new HashMap<UUID, BukkitTask>();
public static void stopFollowing(Entity follower) {
if (follower == null)
return;
UUID uuid = follower.getUniqueId();
if (followTasks.containsKey(uuid))
followTasks.get(uuid).cancel();
}
public static void toggleAI(Entity entity, boolean hasAI) {
net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle();
if (!(nmsEntity instanceof EntityInsentient))
return;
nmsEntity.getDataWatcher().watch(15, (byte)(hasAI ? 0 : 1));
}
public static boolean isAIDisabled(Entity entity) {
net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle();
if (!(nmsEntity instanceof EntityInsentient))
return true;
return nmsEntity.getDataWatcher().getByte(15) != 0;
}
public static double getSpeed(Entity entity) {
net.minecraft.server.v1_8_R2.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
if (!(nmsEntityEntity instanceof EntityInsentient))
return 0.0;
EntityInsentient nmsEntity = (EntityInsentient) nmsEntityEntity;
return nmsEntity.getAttributeInstance(GenericAttributes.d).b();
}
public static void setSpeed(Entity entity, double speed) {
net.minecraft.server.v1_8_R2.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
if (!(nmsEntityEntity instanceof EntityInsentient))
return;
EntityInsentient nmsEntity = (EntityInsentient) nmsEntityEntity;
nmsEntity.getAttributeInstance(GenericAttributes.d).setValue(speed);
}
public static void follow(final Entity target, final Entity follower, final double speed, final double lead,
final double maxRange, final boolean allowWander) {
if (target == null || follower == null)
return;
final net.minecraft.server.v1_8_R2.Entity nmsEntityFollower = ((CraftEntity) follower).getHandle();
if (!(nmsEntityFollower instanceof EntityInsentient))
return;
final EntityInsentient nmsFollower = (EntityInsentient) nmsEntityFollower;
final NavigationAbstract followerNavigation = nmsFollower.getNavigation();
UUID uuid = follower.getUniqueId();
if (followTasks.containsKey(uuid))
followTasks.get(uuid).cancel();
final int locationNearInt = (int) Math.floor(lead);
final boolean hasMax = maxRange > lead;
followTasks.put(follower.getUniqueId(), new BukkitRunnable() {
private boolean inRadius = false;
public void run(){
if (!target.isValid() || !follower.isValid()) {
this.cancel();
}
followerNavigation.a(2F);
Location targetLocation = target.getLocation();
PathEntity path;
if (hasMax && !Utilities.checkLocation(targetLocation, follower.getLocation(), maxRange)
&& !target.isDead() && target.isOnGround()) {
if (!inRadius) {
follower.teleport(Utilities.getWalkableLocationNear(targetLocation, locationNearInt));
}
else {
inRadius = false;
path = followerNavigation.a(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
if (path != null) {
followerNavigation.a(path, 1D);
followerNavigation.a(2D);
}
}
}
else if (!inRadius && !Utilities.checkLocation(targetLocation, follower.getLocation(), lead)) {
path = followerNavigation.a(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
if (path != null) {
followerNavigation.a(path, 1D);
followerNavigation.a(2D);
}
}
else {
inRadius = true;
}
if (inRadius && !allowWander) {
followerNavigation.n();
}
nmsFollower.getAttributeInstance(GenericAttributes.d).setValue(speed);
}
}.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, 20));
}
public static void walkTo(final Entity entity, Location location, double speed, final Runnable callback) {
if (entity == null || location == null)
return;
net.minecraft.server.v1_8_R2.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
if (!(nmsEntityEntity instanceof EntityInsentient))
return;
final EntityInsentient nmsEntity = (EntityInsentient) nmsEntityEntity;
final NavigationAbstract followerNavigation = nmsEntity.getNavigation();
final PathEntity path;
path = followerNavigation.a(location.getX(), location.getY(), location.getZ());
if (path != null) {
final boolean aiDisabled = isAIDisabled(entity);
toggleAI(entity, true);
followerNavigation.a(path, 1D);
followerNavigation.a(2D);
final double oldSpeed = nmsEntity.getAttributeInstance(GenericAttributes.d).b();
nmsEntity.getAttributeInstance(GenericAttributes.d).setValue(speed);
new BukkitRunnable() {
@Override
public void run() {
if (followerNavigation.m() || path.b()) {
if (callback != null)
callback.run();
nmsEntity.getAttributeInstance(GenericAttributes.d).setValue(oldSpeed);
if (aiDisabled)
toggleAI(entity, false);
cancel();
}
}
}.runTaskTimer(DenizenAPI.getCurrentInstance(), 1, 1);
}
if (!Utilities.checkLocation(location, entity.getLocation(), 20)) {
entity.teleport(location);
}
}
}