Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package net.minecraft.server;
2
3import org.bukkit.entity.Projectile;
4import org.bukkit.event.entity.EntityDamageByEntityEvent;
5import org.bukkit.event.entity.EntityDamageEvent;
6import org.bukkit.event.player.PlayerFishEvent;
7
8import java.util.List;
9
10// CraftBukkit start
11// CraftBukkit end
12
13public class EntityFish extends Entity {
14
15 private int d = -1;
16 private int e = -1;
17 private int f = -1;
18 private int g = 0;
19 private boolean h = false;
20 public int a = 0;
21 public EntityHuman owner;
22 private int i;
23 private int j = 0;
24 private int k = 0;
25 public Entity c = null;
26 private int l;
27 private double m;
28 private double n;
29 private double o;
30 private double p;
31 private double q;
32
33 public EntityFish(World world) {
34 super(world);
35 this.b(0.25F, 0.25F);
36 this.bK = true;
37 }
38
39 public EntityFish(World world, EntityHuman entityhuman) {
40 super(world);
41 this.bK = true;
42 this.owner = entityhuman;
43 this.owner.hookedFish = this;
44 this.b(0.25F, 0.25F);
45 this.setPositionRotation(entityhuman.locX, entityhuman.locY + 1.62D - (double) entityhuman.height, entityhuman.locZ, entityhuman.yaw, entityhuman.pitch);
46 this.locX -= (double) (MathHelper.cos(this.yaw / 180.0F * 3.1415927F) * 0.16F);
47 this.locY -= 0.10000000149011612D;
48 this.locZ -= (double) (MathHelper.sin(this.yaw / 180.0F * 3.1415927F) * 0.16F);
49 this.setPosition(this.locX, this.locY, this.locZ);
50 this.height = 0.0F;
51 float f = 0.4F;
52
53 this.motX = (double) (-MathHelper.sin(this.yaw / 180.0F * 3.1415927F) * MathHelper.cos(this.pitch / 180.0F * 3.1415927F) * f);
54 this.motZ = (double) (MathHelper.cos(this.yaw / 180.0F * 3.1415927F) * MathHelper.cos(this.pitch / 180.0F * 3.1415927F) * f);
55 this.motY = (double) (-MathHelper.sin(this.pitch / 180.0F * 3.1415927F) * f);
56 this.a(this.motX, this.motY, this.motZ, 1.5F, 1.0F);
57 }
58
59 protected void b() {
60 }
61
62 public void a(double d0, double d1, double d2, float f, float f1) {
63 float f2 = MathHelper.a(d0 * d0 + d1 * d1 + d2 * d2);
64
65 d0 /= (double) f2;
66 d1 /= (double) f2;
67 d2 /= (double) f2;
68 d0 += this.random.nextGaussian() * 0.007499999832361937D * (double) f1;
69 d1 += this.random.nextGaussian() * 0.007499999832361937D * (double) f1;
70 d2 += this.random.nextGaussian() * 0.007499999832361937D * (double) f1;
71 d0 *= (double) f;
72 d1 *= (double) f;
73 d2 *= (double) f;
74 this.motX = d0;
75 this.motY = d1;
76 this.motZ = d2;
77 float f3 = MathHelper.a(d0 * d0 + d2 * d2);
78
79 this.lastYaw = this.yaw = (float) (Math.atan2(d0, d2) * 180.0D / 3.1415927410125732D);
80 this.lastPitch = this.pitch = (float) (Math.atan2(d1, (double) f3) * 180.0D / 3.1415927410125732D);
81 this.i = 0;
82 }
83
84 public void m_() {
85 super.m_();
86 if (this.l > 0) {
87 double d0 = this.locX + (this.m - this.locX) / (double) this.l;
88 double d1 = this.locY + (this.n - this.locY) / (double) this.l;
89 double d2 = this.locZ + (this.o - this.locZ) / (double) this.l;
90
91 double d3;
92
93 for (d3 = this.p - (double) this.yaw; d3 < -180.0D; d3 += 360.0D) {
94 ;
95 }
96
97 while (d3 >= 180.0D) {
98 d3 -= 360.0D;
99 }
100
101 this.yaw = (float) ((double) this.yaw + d3 / (double) this.l);
102 this.pitch = (float) ((double) this.pitch + (this.q - (double) this.pitch) / (double) this.l);
103 --this.l;
104 this.setPosition(d0, d1, d2);
105 this.c(this.yaw, this.pitch);
106 } else {
107 if (!this.world.isStatic) {
108 ItemStack itemstack = this.owner.G();
109
110 if (this.owner.dead || !this.owner.T() || itemstack == null || itemstack.getItem() != Item.FISHING_ROD || this.g(this.owner) > 1024.0D) {
111 this.die();
112 this.owner.hookedFish = null;
113 return;
114 }
115
116 if (this.c != null) {
117 if (!this.c.dead) {
118 this.locX = this.c.locX;
119 this.locY = this.c.boundingBox.b + (double) this.c.width * 0.8D;
120 this.locZ = this.c.locZ;
121 return;
122 }
123
124 this.c = null;
125 }
126 }
127
128 if (this.a > 0) {
129 --this.a;
130 }
131
132 if (this.h) {
133 int i = this.world.getTypeId(this.d, this.e, this.f);
134
135 if (i == this.g) {
136 ++this.i;
137 if (this.i == 1200) {
138 this.die();
139 }
140
141 return;
142 }
143
144 this.h = false;
145 this.motX *= (double) (this.random.nextFloat() * 0.2F);
146 this.motY *= (double) (this.random.nextFloat() * 0.2F);
147 this.motZ *= (double) (this.random.nextFloat() * 0.2F);
148 this.i = 0;
149 this.j = 0;
150 } else {
151 ++this.j;
152 }
153
154 Vec3D vec3d = Vec3D.create(this.locX, this.locY, this.locZ);
155 Vec3D vec3d1 = Vec3D.create(this.locX + this.motX, this.locY + this.motY, this.locZ + this.motZ);
156 MovingObjectPosition movingobjectposition = this.world.a(vec3d, vec3d1);
157
158 vec3d = Vec3D.create(this.locX, this.locY, this.locZ);
159 vec3d1 = Vec3D.create(this.locX + this.motX, this.locY + this.motY, this.locZ + this.motZ);
160 if (movingobjectposition != null) {
161 vec3d1 = Vec3D.create(movingobjectposition.f.a, movingobjectposition.f.b, movingobjectposition.f.c);
162 }
163
164 Entity entity = null;
165 List list = this.world.b((Entity) this, this.boundingBox.a(this.motX, this.motY, this.motZ).b(1.0D, 1.0D, 1.0D));
166 double d4 = 0.0D;
167
168 double d5;
169
170 for (int j = 0; j < list.size(); ++j) {
171 Entity entity1 = (Entity) list.get(j);
172
173 if (entity1.l_() && (entity1 != this.owner || this.j >= 5)) {
174 float f = 0.3F;
175 AxisAlignedBB axisalignedbb = entity1.boundingBox.b((double) f, (double) f, (double) f);
176 MovingObjectPosition movingobjectposition1 = axisalignedbb.a(vec3d, vec3d1);
177
178 if (movingobjectposition1 != null) {
179 d5 = vec3d.a(movingobjectposition1.f);
180 if (d5 < d4 || d4 == 0.0D) {
181 entity = entity1;
182 d4 = d5;
183 }
184 }
185 }
186 }
187
188 if (entity != null) {
189 movingobjectposition = new MovingObjectPosition(entity);
190 }
191
192 if (movingobjectposition != null) {
193 if (movingobjectposition.entity != null) {
194 // CraftBukkit start
195 // TODO add EntityDamagedByProjectileEvent : fishing hook?
196 boolean stick;
197 if (movingobjectposition.entity instanceof EntityLiving) {
198 org.bukkit.entity.Entity damagee = movingobjectposition.entity.getBukkitEntity();
199 Projectile projectile = (Projectile) this.getBukkitEntity();
200
201 // TODO @see EntityArrow#162
202 EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(projectile, damagee, EntityDamageEvent.DamageCause.PROJECTILE, 0);
203 this.world.getServer().getPluginManager().callEvent(event);
204
205 if (event.isCancelled()) {
206 stick = !projectile.doesBounce();
207 } else {
208 // this function returns if the fish should stick in or not, i.e. !bounce
209 stick = movingobjectposition.entity.damageEntity(this, event.getDamage());
210 }
211 } else {
212 stick = movingobjectposition.entity.damageEntity(this.owner, 0);
213 }
214 if (!stick) {
215 // CraftBukkit end
216 this.c = movingobjectposition.entity;
217 }
218 } else {
219 this.h = true;
220 }
221 }
222
223 if (!this.h) {
224 this.move(this.motX, this.motY, this.motZ);
225 float f1 = MathHelper.a(this.motX * this.motX + this.motZ * this.motZ);
226
227 this.yaw = (float) (Math.atan2(this.motX, this.motZ) * 180.0D / 3.1415927410125732D);
228
229 for (this.pitch = (float) (Math.atan2(this.motY, (double) f1) * 180.0D / 3.1415927410125732D); this.pitch - this.lastPitch < -180.0F; this.lastPitch -= 360.0F) {
230 ;
231 }
232
233 while (this.pitch - this.lastPitch >= 180.0F) {
234 this.lastPitch += 360.0F;
235 }
236
237 while (this.yaw - this.lastYaw < -180.0F) {
238 this.lastYaw -= 360.0F;
239 }
240
241 while (this.yaw - this.lastYaw >= 180.0F) {
242 this.lastYaw += 360.0F;
243 }
244
245 this.pitch = this.lastPitch + (this.pitch - this.lastPitch) * 0.2F;
246 this.yaw = this.lastYaw + (this.yaw - this.lastYaw) * 0.2F;
247 float f2 = 0.92F;
248
249 if (this.onGround || this.positionChanged) {
250 f2 = 0.5F;
251 }
252
253 byte b0 = 5;
254 double d6 = 0.0D;
255
256 for (int k = 0; k < b0; ++k) {
257 double d7 = this.boundingBox.b + (this.boundingBox.e - this.boundingBox.b) * (double) (k + 0) / (double) b0 - 0.125D + 0.125D;
258 double d8 = this.boundingBox.b + (this.boundingBox.e - this.boundingBox.b) * (double) (k + 1) / (double) b0 - 0.125D + 0.125D;
259 AxisAlignedBB axisalignedbb1 = AxisAlignedBB.b(this.boundingBox.a, d7, this.boundingBox.c, this.boundingBox.d, d8, this.boundingBox.f);
260
261 if (this.world.b(axisalignedbb1, Material.WATER)) {
262 d6 += 1.0D / (double) b0;
263 }
264 }
265
266 if (d6 > 0.0D) {
267 if (this.k > 0) {
268 --this.k;
269 } else {
270 short short1 = 500;
271
272 if (this.world.s(MathHelper.floor(this.locX), MathHelper.floor(this.locY) + 1, MathHelper.floor(this.locZ))) {
273 short1 = 300;
274 }
275
276 if (this.random.nextInt(short1) == 0) {
277 this.k = this.random.nextInt(30) + 10;
278 this.motY -= 0.20000000298023224D;
279 this.world.makeSound(this, "random.splash", 0.25F, 1.0F + (this.random.nextFloat() - this.random.nextFloat()) * 0.4F);
280 float f3 = (float) MathHelper.floor(this.boundingBox.b);
281
282 float f4;
283 int l;
284 float f5;
285
286 for (l = 0; (float) l < 1.0F + this.length * 20.0F; ++l) {
287 f5 = (this.random.nextFloat() * 2.0F - 1.0F) * this.length;
288 f4 = (this.random.nextFloat() * 2.0F - 1.0F) * this.length;
289 this.world.a("bubble", this.locX + (double) f5, (double) (f3 + 1.0F), this.locZ + (double) f4, this.motX, this.motY - (double) (this.random.nextFloat() * 0.2F), this.motZ);
290 }
291
292 for (l = 0; (float) l < 1.0F + this.length * 20.0F; ++l) {
293 f5 = (this.random.nextFloat() * 2.0F - 1.0F) * this.length;
294 f4 = (this.random.nextFloat() * 2.0F - 1.0F) * this.length;
295 this.world.a("splash", this.locX + (double) f5, (double) (f3 + 1.0F), this.locZ + (double) f4, this.motX, this.motY, this.motZ);
296 }
297 }
298 }
299 }
300
301 if (this.k > 0) {
302 this.motY -= (double) (this.random.nextFloat() * this.random.nextFloat() * this.random.nextFloat()) * 0.2D;
303 }
304
305 d5 = d6 * 2.0D - 1.0D;
306 this.motY += 0.03999999910593033D * d5;
307 if (d6 > 0.0D) {
308 f2 = (float) ((double) f2 * 0.9D);
309 this.motY *= 0.8D;
310 }
311
312 this.motX *= (double) f2;
313 this.motY *= (double) f2;
314 this.motZ *= (double) f2;
315 this.setPosition(this.locX, this.locY, this.locZ);
316 }
317 }
318 }
319
320 public void b(NBTTagCompound nbttagcompound) {
321 nbttagcompound.a("xTile", (short) this.d);
322 nbttagcompound.a("yTile", (short) this.e);
323 nbttagcompound.a("zTile", (short) this.f);
324 nbttagcompound.a("inTile", (byte) this.g);
325 nbttagcompound.a("shake", (byte) this.a);
326 nbttagcompound.a("inGround", (byte) (this.h ? 1 : 0));
327 }
328
329 public void a(NBTTagCompound nbttagcompound) {
330 this.d = nbttagcompound.d("xTile");
331 this.e = nbttagcompound.d("yTile");
332 this.f = nbttagcompound.d("zTile");
333 this.g = nbttagcompound.c("inTile") & 255;
334 this.a = nbttagcompound.c("shake") & 255;
335 this.h = nbttagcompound.c("inGround") == 1;
336 }
337
338 public int h() {
339 byte b0 = 0;
340
341 if (this.c != null) {
342 // CraftBukkit start
343 PlayerFishEvent playerFishEvent = new PlayerFishEvent((org.bukkit.entity.Player) this.owner.getBukkitEntity(), this.c.getBukkitEntity(), PlayerFishEvent.State.CAUGHT_ENTITY);
344 this.world.getServer().getPluginManager().callEvent(playerFishEvent);
345
346 if (playerFishEvent.isCancelled()) {
347 this.die();
348 this.owner.hookedFish = null;
349 return 0;
350 }
351 // CraftBukkit end
352 double d0 = this.owner.locX - this.locX;
353 double d1 = this.owner.locY - this.locY;
354 double d2 = this.owner.locZ - this.locZ;
355 double d3 = (double) MathHelper.a(d0 * d0 + d1 * d1 + d2 * d2);
356 double d4 = 0.1D;
357
358 this.c.motX += d0 * d4;
359 this.c.motY += d1 * d4 + (double) MathHelper.a(d3) * 0.08D;
360 this.c.motZ += d2 * d4;
361 b0 = 3;
362 } else if (this.k > 0) {
363 EntityItem entityitem = new EntityItem(this.world, this.locX, this.locY, this.locZ, new ItemStack(Item.RAW_FISH));
364 // CraftBukkit start
365 PlayerFishEvent playerFishEvent = new PlayerFishEvent((org.bukkit.entity.Player) this.owner.getBukkitEntity(), entityitem.getBukkitEntity(), PlayerFishEvent.State.CAUGHT_FISH);
366 this.world.getServer().getPluginManager().callEvent(playerFishEvent);
367
368 if (playerFishEvent.isCancelled()) {
369 this.die();
370 this.owner.hookedFish = null;
371 return 0;
372 }
373 // CraftBukkit end
374 double d5 = this.owner.locX - this.locX;
375 double d6 = this.owner.locY - this.locY;
376 double d7 = this.owner.locZ - this.locZ;
377 double d8 = (double) MathHelper.a(d5 * d5 + d6 * d6 + d7 * d7);
378 double d9 = 0.1D;
379
380 entityitem.motX = d5 * d9;
381 entityitem.motY = d6 * d9 + (double) MathHelper.a(d8) * 0.08D;
382 entityitem.motZ = d7 * d9;
383 this.world.addEntity(entityitem);
384 this.owner.a(StatisticList.B, 1);
385 b0 = 1;
386 }
387
388 if (this.h) {
389 // CraftBukkit start
390 PlayerFishEvent playerFishEvent = new PlayerFishEvent((org.bukkit.entity.Player) this.owner.getBukkitEntity(), null, PlayerFishEvent.State.IN_GROUND);
391 this.world.getServer().getPluginManager().callEvent(playerFishEvent);
392
393 if (playerFishEvent.isCancelled()) {
394 this.die();
395 this.owner.hookedFish = null;
396 return 0;
397 }
398 // CraftBukkit end
399 b0 = 2;
400 }
401
402 // CraftBukkit start
403 if (b0 == 0) {
404 PlayerFishEvent playerFishEvent = new PlayerFishEvent((org.bukkit.entity.Player) this.owner.getBukkitEntity(), null, PlayerFishEvent.State.FAILED_ATTEMPT);
405 this.world.getServer().getPluginManager().callEvent(playerFishEvent);
406 }
407 // CraftBukkit end
408 this.die();
409 this.owner.hookedFish = null;
410 return b0;
411 }
412}