-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
EntityHumanNPC.java
362 lines (316 loc) · 12.4 KB
/
EntityHumanNPC.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package net.citizensnpcs.npc.entity;
import java.io.IOException;
import java.net.Socket;
import java.util.List;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.event.NPCPushEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Inventory;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.npc.network.EmptyNetHandler;
import net.citizensnpcs.npc.network.EmptyNetworkManager;
import net.citizensnpcs.npc.network.EmptySocket;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
import net.citizensnpcs.util.nms.PlayerControllerJump;
import net.citizensnpcs.util.nms.PlayerControllerLook;
import net.citizensnpcs.util.nms.PlayerControllerMove;
import net.citizensnpcs.util.nms.PlayerNavigation;
import net.minecraft.server.v1_8_R1.AttributeInstance;
import net.minecraft.server.v1_8_R1.Block;
import net.minecraft.server.v1_8_R1.BlockPosition;
import net.minecraft.server.v1_8_R1.Entity;
import net.minecraft.server.v1_8_R1.EntityPlayer;
import net.minecraft.server.v1_8_R1.EnumGamemode;
import net.minecraft.server.v1_8_R1.EnumPlayerInfoAction;
import net.minecraft.server.v1_8_R1.EnumProtocolDirection;
import net.minecraft.server.v1_8_R1.GenericAttributes;
import net.minecraft.server.v1_8_R1.MathHelper;
import net.minecraft.server.v1_8_R1.MinecraftServer;
import net.minecraft.server.v1_8_R1.NavigationAbstract;
import net.minecraft.server.v1_8_R1.NetworkManager;
import net.minecraft.server.v1_8_R1.Packet;
import net.minecraft.server.v1_8_R1.PacketPlayOutEntityEquipment;
import net.minecraft.server.v1_8_R1.PacketPlayOutEntityHeadRotation;
import net.minecraft.server.v1_8_R1.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_8_R1.PlayerInteractManager;
import net.minecraft.server.v1_8_R1.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R1.CraftServer;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;
import com.mojang.authlib.GameProfile;
public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
private PlayerControllerJump controllerJump;
private PlayerControllerLook controllerLook;
private PlayerControllerMove controllerMove;
private boolean gravity = true;
private int jumpTicks = 0;
private PlayerNavigation navigation;
private final CitizensNPC npc;
private final Location packetLocationCache = new Location(null, 0, 0, 0);
public EntityHumanNPC(MinecraftServer minecraftServer, WorldServer world, GameProfile gameProfile,
PlayerInteractManager playerInteractManager, NPC npc) {
super(minecraftServer, world, gameProfile, playerInteractManager);
this.npc = (CitizensNPC) npc;
if (npc != null) {
playerInteractManager.setGameMode(EnumGamemode.SURVIVAL);
initialise(minecraftServer);
}
}
@Override
protected void a(double d0, boolean flag, Block block, BlockPosition blockposition) {
if (npc == null || !npc.isFlyable()) {
super.a(d0, flag, block, blockposition);
}
}
@Override
public void collide(net.minecraft.server.v1_8_R1.Entity entity) {
// this method is called by both the entities involved - cancelling
// it will not stop the NPC from moving.
super.collide(entity);
if (npc != null) {
Util.callCollisionEvent(npc, entity.getBukkitEntity());
}
}
@Override
public void e(float f, float f1) {
if (npc == null || !npc.isFlyable()) {
super.e(f, f1);
}
}
@Override
public void g(double x, double y, double z) {
if (npc == null) {
super.g(x, y, z);
return;
}
if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) {
if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) {
super.g(x, y, z);
}
return;
}
Vector vector = new Vector(x, y, z);
NPCPushEvent event = Util.callPushEvent(npc, vector);
if (!event.isCancelled()) {
vector = event.getCollisionVector();
super.g(vector.getX(), vector.getY(), vector.getZ());
}
// when another entity collides, this method is called to push the
// NPC so we prevent it from doing anything if the event is
// cancelled.
}
@Override
public void g(float f, float f1) {
if (npc == null || !npc.isFlyable()) {
super.g(f, f1);
} else {
NMS.flyingMoveLogic(this, f, f1);
}
}
@Override
public CraftPlayer getBukkitEntity() {
if (npc != null && bukkitEntity == null) {
bukkitEntity = new PlayerNPC(this);
}
return super.getBukkitEntity();
}
public PlayerControllerJump getControllerJump() {
return controllerJump;
}
public PlayerControllerMove getControllerMove() {
return controllerMove;
}
private Packet getListPacket(Player player, boolean removeFromPlayerList) {
return new PacketPlayOutPlayerInfo(removeFromPlayerList ? EnumPlayerInfoAction.REMOVE_PLAYER
: EnumPlayerInfoAction.ADD_PLAYER, ((CraftPlayer) player).getHandle());
}
public NavigationAbstract getNavigation() {
return navigation;
}
@Override
public NPC getNPC() {
return npc;
}
private void initialise(MinecraftServer minecraftServer) {
Socket socket = new EmptySocket();
NetworkManager conn = null;
try {
conn = new EmptyNetworkManager(EnumProtocolDirection.CLIENTBOUND);
playerConnection = new EmptyNetHandler(minecraftServer, conn, this);
conn.a(playerConnection);
socket.close();
} catch (IOException e) {
// swallow
}
AttributeInstance range = this.getAttributeInstance(GenericAttributes.b);
if (range == null) {
range = this.getAttributeMap().b(GenericAttributes.b);
}
range.setValue(Setting.DEFAULT_PATHFINDING_RANGE.asDouble());
controllerJump = new PlayerControllerJump(this);
controllerLook = new PlayerControllerLook(this);
controllerMove = new PlayerControllerMove(this);
navigation = new PlayerNavigation(this, world);
NMS.setStepHeight(this, 1); // the default (0) breaks step climbing
}
public boolean isNavigating() {
return npc.getNavigator().isNavigating();
}
@Override
public boolean j_() {
if (npc == null || !npc.isFlyable()) {
return super.j_();
} else {
return false;
}
}
private void moveOnCurrentHeading() {
NMS.updateAI(this);
if (aW) {
if (onGround && jumpTicks == 0) {
bE();
jumpTicks = 10;
}
} else {
jumpTicks = 0;
}
aX *= 0.98F;
aY *= 0.98F;
aZ *= 0.9F;
g(aX, aY); // movement method
NMS.setHeadYaw(this, yaw);
if (jumpTicks > 0) {
jumpTicks--;
}
}
@Override
public void s_() {
super.s_();
if (npc == null)
return;
boolean navigating = npc.getNavigator().isNavigating();
updatePackets(navigating);
if (gravity && !navigating && getBukkitEntity() != null
&& Util.isLoaded(getBukkitEntity().getLocation(LOADED_LOCATION)) && !NMS.inWater(getBukkitEntity())) {
move(0, -0.2, 0);
// gravity. also works around an entity.onGround not updating issue
// (onGround is normally updated by the client)
}
if (Math.abs(motX) < EPSILON && Math.abs(motY) < EPSILON && Math.abs(motZ) < EPSILON) {
motX = motY = motZ = 0;
}
if (navigating) {
if (!NMS.isNavigationFinished(navigation)) {
NMS.updateNavigation(navigation);
}
moveOnCurrentHeading();
} else if (motX != 0 || motZ != 0 || motY != 0) {
g(0, 0); // is this necessary? it does controllable but
// sometimes
// players sink into the ground
}
if (noDamageTicks > 0) {
--noDamageTicks;
}
npc.update();
}
public void setMoveDestination(double x, double y, double z, double speed) {
controllerMove.a(x, y, z, speed);
}
public void setShouldJump() {
controllerJump.a();
}
public void setTargetLook(Entity target, float yawOffset, float renderOffset) {
controllerLook.a(target, yawOffset, renderOffset);
}
public void updateAI() {
controllerMove.c();
controllerLook.a();
controllerJump.b();
}
private void updatePackets(boolean navigating) {
if (world.getWorld().getFullTime() % Setting.PACKET_UPDATE_DELAY.asInt() == 0) {
// set skin flag byte to all visible (DataWatcher API is lacking so
// catch the NPE as a sign that this is a MC 1.7 server without the
// skin flag)
try {
datawatcher.watch(10, Byte.valueOf((byte) 127));
} catch (NullPointerException e) {
datawatcher.a(10, Byte.valueOf((byte) 127));
}
Location current = getBukkitEntity().getLocation(packetLocationCache);
Packet[] packets = new Packet[navigating ? 5 : 6];
if (!navigating) {
packets[5] = new PacketPlayOutEntityHeadRotation(this,
(byte) MathHelper.d(NMS.getHeadYaw(this) * 256.0F / 360.0F));
}
for (int i = 0; i < 5; i++) {
packets[i] = new PacketPlayOutEntityEquipment(getId(), i, getEquipment(i));
}
boolean removeFromPlayerList = npc.data().get("removefromplayerlist",
Setting.REMOVE_PLAYERS_FROM_PLAYER_LIST.asBoolean());
NMS.addOrRemoveFromPlayerList(getBukkitEntity(), removeFromPlayerList);
Player otherOnline = null;
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.getName().equalsIgnoreCase(getName()) && !(player instanceof NPCHolder)) {
otherOnline = player;
break;
}
}
// NMS.sendToOnline(getListPacket(getBukkitEntity(), true));
if (otherOnline != null) {
// NMS.sendToOnline(getListPacket(otherOnline, false));
}
NMS.sendPacketsNearby(getBukkitEntity(), current, packets);
}
}
public void updatePathfindingRange(float pathfindingRange) {
this.navigation.setRange(pathfindingRange);
}
public static class PlayerNPC extends CraftPlayer implements NPCHolder {
private final CraftServer cserver;
private final CitizensNPC npc;
private PlayerNPC(EntityHumanNPC entity) {
super((CraftServer) Bukkit.getServer(), entity);
this.npc = entity.npc;
this.cserver = (CraftServer) Bukkit.getServer();
npc.getTrait(Inventory.class);
}
@Override
public EntityHumanNPC getHandle() {
return (EntityHumanNPC) this.entity;
}
@Override
public List<MetadataValue> getMetadata(String metadataKey) {
return cserver.getEntityMetadata().getMetadata(this, metadataKey);
}
@Override
public NPC getNPC() {
return npc;
}
@Override
public boolean hasMetadata(String metadataKey) {
return cserver.getEntityMetadata().hasMetadata(this, metadataKey);
}
@Override
public void removeMetadata(String metadataKey, Plugin owningPlugin) {
cserver.getEntityMetadata().removeMetadata(this, metadataKey, owningPlugin);
}
public void setGravityEnabled(boolean enabled) {
getHandle().gravity = enabled;
}
@Override
public void setMetadata(String metadataKey, MetadataValue newMetadataValue) {
cserver.getEntityMetadata().setMetadata(this, metadataKey, newMetadataValue);
}
}
private static final float EPSILON = 0.005F;
private static final Location LOADED_LOCATION = new Location(null, 0, 0, 0);
}