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
|
package org.bukkit.craftbukkit.entity;
import com.google.common.collect.MapMaker;
import net.minecraft.server.*;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.util.Vector;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
public abstract class CraftEntity implements org.bukkit.entity.Entity {
private static final Map<String, CraftPlayer> players = new MapMaker().softValues().makeMap();
protected final CraftServer server;
protected Entity entity;
private EntityDamageEvent lastDamageEvent;
public CraftEntity(final CraftServer server, final Entity entity) {
this.server = server;
this.entity = entity;
}
public static CraftEntity getEntity(CraftServer server, Entity entity) {
/**
* Order is *EXTREMELY* important -- keep it right! =D
*/
if (entity instanceof EntityLiving) {
// Players
if (entity instanceof EntityHuman) {
if (entity instanceof EntityPlayer) { return getPlayer((EntityPlayer) entity); }
else { return new CraftHumanEntity(server, (EntityHuman) entity); }
}
else if (entity instanceof EntityCreature) {
// Animals
if (entity instanceof EntityAnimal) {
if (entity instanceof EntityChicken) { return new CraftChicken(server, (EntityChicken) entity); }
else if (entity instanceof EntityCow) {
if (entity instanceof EntityMushroomCow) { return new CraftMushroomCow(server, (EntityMushroomCow) entity); }
else { return new CraftCow(server, (EntityCow) entity); }
}
else if (entity instanceof EntityPig) { return new CraftPig(server, (EntityPig) entity); }
else if (entity instanceof EntityWolf) { return new CraftWolf(server, (EntityWolf) entity); }
else if (entity instanceof EntitySheep) { return new CraftSheep(server, (EntitySheep) entity); }
else { return new CraftAnimals(server, (EntityAnimal) entity); }
}
// Monsters
else if (entity instanceof EntityMonster) {
if (entity instanceof EntityZombie) {
if (entity instanceof EntityPigZombie) { return new CraftPigZombie(server, (EntityPigZombie) entity); }
else { return new CraftZombie(server, (EntityZombie) entity); }
}
else if (entity instanceof EntityCreeper) { return new CraftCreeper(server, (EntityCreeper) entity); }
else if (entity instanceof EntityEnderman) { return new CraftEnderman(server, (EntityEnderman) entity); }
else if (entity instanceof EntitySilverfish) { return new CraftSilverfish(server, (EntitySilverfish) entity); }
else if (entity instanceof EntityGiantZombie) { return new CraftGiant(server, (EntityGiantZombie) entity); }
else if (entity instanceof EntitySkeleton) { return new CraftSkeleton(server, (EntitySkeleton) entity); }
else if (entity instanceof EntityBlaze) { return new CraftBlaze(server, (EntityBlaze) entity); }
else if (entity instanceof EntitySpider) {
if (entity instanceof EntityCaveSpider) { return new CraftCaveSpider(server, (EntityCaveSpider) entity); }
else { return new CraftSpider(server, (EntitySpider) entity); }
}
else { return new CraftMonster(server, (EntityMonster) entity); }
}
// Water Animals
else if (entity instanceof EntityWaterAnimal) {
if (entity instanceof EntitySquid) { return new CraftSquid(server, (EntitySquid) entity); }
else { return new CraftWaterMob(server, (EntityWaterAnimal) entity); }
}
else if (entity instanceof EntitySnowman) { return new CraftSnowman(server, (EntitySnowman) entity); }
else if (entity instanceof EntityVillager) { return new CraftVillager(server, (EntityVillager) entity); }
else { return new CraftCreature(server, (EntityCreature) entity); }
}
// Slimes are a special (and broken) case
else if (entity instanceof EntitySlime) {
if (entity instanceof EntityMagmaCube) { return new CraftMagmaCube(server, (EntityMagmaCube) entity); }
else { return new CraftSlime(server, (EntitySlime) entity); }
}
// Flying
else if (entity instanceof EntityFlying) {
if (entity instanceof EntityGhast) { return new CraftGhast(server, (EntityGhast) entity); }
else { return new CraftFlying(server, (EntityFlying) entity); }
}
else if (entity instanceof EntityComplex) {
if (entity instanceof EntityEnderDragon) { return new CraftEnderDragon(server, (EntityEnderDragon) entity); }
}
else { return new CraftLivingEntity(server, (EntityLiving) entity); }
}
else if (entity instanceof EntityComplexPart) {
EntityComplexPart part = (EntityComplexPart) entity;
if (part.owner instanceof EntityEnderDragon) { return new CraftEnderDragonPart(server, (EntityComplexPart) entity); }
else { return new CraftComplexPart(server, (EntityComplexPart) entity); }
}
else if (entity instanceof EntityExperienceOrb) { return new CraftExperienceOrb(server, (EntityExperienceOrb) entity); }
else if (entity instanceof EntityArrow) { return new CraftArrow(server, (EntityArrow) entity); }
else if (entity instanceof EntityBoat) { return new CraftBoat(server, (EntityBoat) entity); }
else if (entity instanceof EntityProjectile) {
if (entity instanceof EntityEgg) { return new CraftEgg(server, (EntityEgg) entity); }
else if (entity instanceof EntitySnowball) { return new CraftSnowball(server, (EntitySnowball) entity); }
else if (entity instanceof EntityPotion) { return new CraftThrownPotion(server, (EntityPotion) entity); }
else if (entity instanceof EntityEnderPearl) { return new CraftEnderPearl(server, (EntityEnderPearl) entity); }
}
else if (entity instanceof EntityFallingBlock) { return new CraftFallingSand(server, (EntityFallingBlock) entity); }
else if (entity instanceof EntityFireball) {
if (entity instanceof EntitySmallFireball) { return new CraftSmallFireball(server, (EntitySmallFireball) entity); }
else { return new CraftFireball(server, (EntityFireball) entity); }
}
else if (entity instanceof EntityEnderSignal) { return new CraftEnderSignal(server, (EntityEnderSignal) entity); }
else if (entity instanceof EntityEnderCrystal) { return new CraftEnderCrystal(server, (EntityEnderCrystal) entity); }
else if (entity instanceof EntityFishingHook) { return new CraftFish(server, (EntityFishingHook) entity); }
else if (entity instanceof EntityItem) { return new CraftItem(server, (EntityItem) entity); }
else if (entity instanceof EntityWeather) {
if (entity instanceof EntityWeatherLighting) { return new CraftLightningStrike(server, (EntityWeatherLighting) entity); }
else { return new CraftWeather(server, (EntityWeather) entity); }
}
else if (entity instanceof EntityMinecart) {
EntityMinecart mc = (EntityMinecart) entity;
if (mc.type == CraftMinecart.Type.StorageMinecart.getId()) { return new CraftStorageMinecart(server, mc); }
else if (mc.type == CraftMinecart.Type.PoweredMinecart.getId()) { return new CraftPoweredMinecart(server, mc); }
else { return new CraftMinecart(server, mc); }
}
else if (entity instanceof EntityPainting) { return new CraftPainting(server, (EntityPainting) entity); }
else if (entity instanceof EntityTNTPrimed) { return new CraftTNTPrimed(server, (EntityTNTPrimed) entity); }
throw new IllegalArgumentException("Unknown entity");
}
public Location getLocation() {
return new Location(getWorld(), entity.locX, entity.locY, entity.locZ, entity.yaw, entity.pitch);
}
public Vector getVelocity() {
return new Vector(entity.motX, entity.motY, entity.motZ);
}
public void setVelocity(Vector vel) {
entity.motX = vel.getX();
entity.motY = vel.getY();
entity.motZ = vel.getZ();
entity.velocityChanged = true;
}
public World getWorld() {
return ((WorldServer) entity.world).getWorld();
}
public boolean teleport(Location location) {
return teleport(location, TeleportCause.PLUGIN);
}
public boolean teleport(Location location, TeleportCause cause) {
entity.world = ((CraftWorld) location.getWorld()).getHandle();
entity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
// entity.setLocation() throws no event, and so cannot be cancelled
return true;
}
public boolean teleport(org.bukkit.entity.Entity destination) {
return teleport(destination.getLocation());
}
public boolean teleport(org.bukkit.entity.Entity destination, TeleportCause cause) {
return teleport(destination.getLocation(), cause);
}
public List<org.bukkit.entity.Entity> getNearbyEntities(double x, double y, double z) {
@SuppressWarnings("unchecked")
List<Entity> notchEntityList = entity.world.getEntities(entity, entity.boundingBox.grow(x, y, z));
List<org.bukkit.entity.Entity> bukkitEntityList = new java.util.ArrayList<org.bukkit.entity.Entity>(notchEntityList.size());
for (Entity e: notchEntityList) {
bukkitEntityList.add(e.getBukkitEntity());
}
return bukkitEntityList;
}
public int getEntityId() {
return entity.id;
}
public int getFireTicks() {
return entity.fireTicks;
}
public int getMaxFireTicks() {
return entity.maxFireTicks;
}
public void setFireTicks(int ticks) {
entity.fireTicks = ticks;
}
public void remove() {
entity.dead = true;
}
public boolean isDead() {
return !entity.isAlive();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CraftEntity other = (CraftEntity) obj;
if (this.server != other.server && (this.server == null || !this.server.equals(other.server))) {
return false;
}
if (this.entity != other.entity && (this.entity == null || !this.entity.equals(other.entity))) {
return false;
}
return true;
}
public Server getServer() {
return server;
}
public Vector getMomentum() {
return getVelocity();
}
public void setMomentum(Vector value) {
setVelocity(value);
}
public org.bukkit.entity.Entity getPassenger() {
return isEmpty() ? null : (CraftEntity) getHandle().passenger.getBukkitEntity();
}
public boolean setPassenger(org.bukkit.entity.Entity passenger) {
if (passenger instanceof CraftEntity) {
((CraftEntity) passenger).getHandle().setPassengerOf(getHandle());
return true;
} else {
return false;
}
}
public boolean isEmpty() {
return getHandle().passenger == null;
}
public boolean eject() {
if (getHandle().passenger == null) {
return false;
}
getHandle().passenger.setPassengerOf(null);
return true;
}
public float getFallDistance() {
return getHandle().fallDistance;
}
public void setFallDistance(float distance) {
getHandle().fallDistance = distance;
}
public void setLastDamageCause(EntityDamageEvent event) {
lastDamageEvent = event;
}
public EntityDamageEvent getLastDamageCause() {
return lastDamageEvent;
}
public UUID getUniqueId() {
return getHandle().uniqueId;
}
private static CraftPlayer getPlayer(EntityPlayer entity) {
CraftPlayer result = players.get(entity.name);
if (result == null) {
result = new CraftPlayer((CraftServer)Bukkit.getServer(), entity);
players.put(entity.name, result);
} else {
result.setHandle(entity);
}
return result;
}
public int getTicksLived() {
return getHandle().ticksLived;
}
public void setTicksLived(int value) {
if (value <= 0) {
throw new IllegalArgumentException("Age must be at least 1 tick");
}
getHandle().ticksLived = value;
}
public Entity getHandle() {
return entity;
}
public void setHandle(final Entity entity) {
this.entity = entity;
}
@Override
public String toString() {
return "CraftEntity{" + "id=" + getEntityId() + '}';
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + (this.server != null ? this.server.hashCode() : 0);
hash = 89 * hash + (this.entity != null ? this.entity.hashCode() : 0);
return hash;
}
}
|