summaryrefslogtreecommitdiffstats
path: root/nms-patches/EntityLiving.patch
blob: 1a0a62879952f12839346b116d35005b955933c5 (plain)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
--- ../work/decompile-8eb82bde//net/minecraft/server/EntityLiving.java	2015-02-12 14:00:06.621316929 +0000
+++ src/main/java/net/minecraft/server/EntityLiving.java	2015-02-12 14:00:06.621316929 +0000
@@ -8,6 +8,19 @@
 import java.util.Random;
 import java.util.UUID;
 
+// CraftBukkit start
+import java.util.ArrayList;
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.entity.LivingEntity;
+import org.bukkit.entity.Vehicle;
+import org.bukkit.event.entity.EntityDamageEvent;
+import org.bukkit.event.entity.EntityDamageEvent.DamageModifier;
+import org.bukkit.event.entity.EntityRegainHealthEvent;
+import org.bukkit.event.vehicle.VehicleExitEvent;
+// CraftBukkit end
+
 public abstract class EntityLiving extends Entity {
 
     private static final UUID a = UUID.fromString("662A6B8D-DA3E-4C1C-8813-96EA6097278D");
@@ -67,6 +80,11 @@
     private float bk;
     private int bl;
     private float bm;
+    // CraftBukkit start
+    public int expToDrop;
+    public int maxAirTicks = 300;
+    ArrayList<org.bukkit.inventory.ItemStack> drops = null;
+    // CraftBukkit end
 
     public void G() {
         this.damageEntity(DamageSource.OUT_OF_WORLD, Float.MAX_VALUE);
@@ -75,7 +93,8 @@
     public EntityLiving(World world) {
         super(world);
         this.aW();
-        this.setHealth(this.getMaxHealth());
+        // CraftBukkit - setHealth(getMaxHealth()) inlined and simplified to skip the instanceof check for EntityPlayer, as getBukkitEntity() is not initialized in constructor
+        this.datawatcher.watch(6, (float) this.getAttributeInstance(GenericAttributes.maxHealth).getValue());
         this.k = true;
         this.aF = (float) ((Math.random() + 1.0D) * 0.009999999776482582D);
         this.setPosition(this.locX, this.locY, this.locZ);
@@ -116,8 +135,14 @@
                 }
 
                 int i = (int) (150.0D * d1);
-
-                ((WorldServer) this.world).a(EnumParticle.BLOCK_DUST, this.locX, this.locY, this.locZ, i, 0.0D, 0.0D, 0.0D, 0.15000000596046448D, new int[] { Block.getCombinedId(iblockdata)});
+                
+                // CraftBukkit start - visiblity api
+                if (this instanceof EntityPlayer) {
+                    ((WorldServer) this.world).sendParticles((EntityPlayer) this, EnumParticle.BLOCK_DUST, false, this.locX, this.locY, this.locZ, i, 0.0D, 0.0D, 0.0D, 0.15000000596046448D, new int[] { Block.getCombinedId(iblockdata)});
+                } else {
+                    ((WorldServer) this.world).a(EnumParticle.BLOCK_DUST, this.locX, this.locY, this.locZ, i, 0.0D, 0.0D, 0.0D, 0.15000000596046448D, new int[] { Block.getCombinedId(iblockdata)}); 
+                }
+                // CraftBukkit end
             }
         }
 
@@ -174,7 +199,11 @@
                 this.mount((Entity) null);
             }
         } else {
-            this.setAirTicks(300);
+            // CraftBukkit start - Only set if needed to work around a DataWatcher inefficiency
+            if (this.getAirTicks() != 300) {
+                this.setAirTicks(maxAirTicks);
+            }
+            // CraftBukkit end
         }
 
         if (this.isAlive() && this.U()) {
@@ -220,6 +249,18 @@
         this.lastPitch = this.pitch;
         this.world.methodProfiler.b();
     }
+    
+    // CraftBukkit start
+    public int getExpReward() {
+        int exp = this.getExpValue(this.killer);
+
+        if (!this.world.isStatic && (this.lastDamageByPlayerTime > 0 || this.alwaysGivesExp()) && this.aZ() && this.world.getGameRules().getBoolean("doMobLoot")) {
+            return exp;
+        } else {
+            return 0;
+        }
+    }
+    // CraftBukkit end    
 
     public boolean isBaby() {
         return false;
@@ -227,19 +268,18 @@
 
     protected void aY() {
         ++this.deathTicks;
-        if (this.deathTicks == 20) {
+        if (this.deathTicks >= 20 && !this.dead) { // CraftBukkit - (this.deathTicks == 20) -> (this.deathTicks >= 20 && !this.dead)
             int i;
 
-            if (!this.world.isStatic && (this.lastDamageByPlayerTime > 0 || this.alwaysGivesExp()) && this.aZ() && this.world.getGameRules().getBoolean("doMobLoot")) {
-                i = this.getExpValue(this.killer);
-
-                while (i > 0) {
-                    int j = EntityExperienceOrb.getOrbValue(i);
-
-                    i -= j;
-                    this.world.addEntity(new EntityExperienceOrb(this.world, this.locX, this.locY, this.locZ, j));
-                }
+            // CraftBukkit start - Update getExpReward() above if the removed if() changes!
+            i = this.expToDrop;
+            while (i > 0) {
+                int j = EntityExperienceOrb.getOrbValue(i);
+                i -= j;
+                this.world.addEntity(new EntityExperienceOrb(this.world, this.locX, this.locY, this.locZ, j));
             }
+            this.expToDrop = 0;
+            // CraftBukkit end
 
             this.die();
 
@@ -375,6 +415,17 @@
                 }
             }
         }
+        
+        // CraftBukkit start
+        if (nbttagcompound.hasKey("Bukkit.MaxHealth")) {
+            NBTBase nbtbase = nbttagcompound.get("Bukkit.MaxHealth");
+            if (nbtbase.getTypeId() == 5) {
+                this.getAttributeInstance(GenericAttributes.maxHealth).setValue((double) ((NBTTagFloat) nbtbase).c());
+            } else if (nbtbase.getTypeId() == 3) {
+                this.getAttributeInstance(GenericAttributes.maxHealth).setValue((double) ((NBTTagInt) nbtbase).d());
+            }
+        }
+        // CraftBukkit end
 
         if (nbttagcompound.hasKeyOfType("HealF", 99)) {
             this.setHealth(nbttagcompound.getFloat("HealF"));
@@ -395,9 +446,14 @@
         this.hurtTimestamp = nbttagcompound.getInt("HurtByTimestamp");
     }
 
+    // CraftBukkit start
+    private boolean isTickingEffects = false;
+    private List<Object> effectsToProcess = Lists.newArrayList();
+    // CraftBukkit end
+
     protected void bh() {
         Iterator iterator = this.effects.keySet().iterator();
-
+        isTickingEffects = true; // CraftBukkit
         while (iterator.hasNext()) {
             Integer integer = (Integer) iterator.next();
             MobEffect mobeffect = (MobEffect) this.effects.get(integer);
@@ -411,6 +467,16 @@
                 this.a(mobeffect, false);
             }
         }
+        // CraftBukkit start
+        isTickingEffects = false;
+        for (Object e : effectsToProcess) {
+            if (e instanceof MobEffect) {
+                addEffect((MobEffect) e);
+            } else {
+                removeEffect((Integer) e);
+            }
+        }
+        // CraftBukkit end
 
         if (this.updateEffects) {
             if (!this.world.isStatic) {
@@ -486,7 +552,8 @@
     }
 
     public boolean hasEffect(int i) {
-        return this.effects.containsKey(Integer.valueOf(i));
+        // CraftBukkit - Add size check for efficiency
+        return this.effects.size() != 0 && this.effects.containsKey(Integer.valueOf(i));
     }
 
     public boolean hasEffect(MobEffectList mobeffectlist) {
@@ -498,6 +565,12 @@
     }
 
     public void addEffect(MobEffect mobeffect) {
+        // CraftBukkit start
+        if (isTickingEffects) {
+            effectsToProcess.add(mobeffect);
+            return;
+        }
+        // CraftBukkit end
         if (this.d(mobeffect)) {
             if (this.effects.containsKey(Integer.valueOf(mobeffect.getEffectId()))) {
                 ((MobEffect) this.effects.get(Integer.valueOf(mobeffect.getEffectId()))).a(mobeffect);
@@ -527,6 +600,12 @@
     }
 
     public void removeEffect(int i) {
+        // CraftBukkit start
+        if (isTickingEffects) {
+            effectsToProcess.add(i);
+            return;
+        }
+        // CraftBukkit end
         MobEffect mobeffect = (MobEffect) this.effects.remove(Integer.valueOf(i));
 
         if (mobeffect != null) {
@@ -560,20 +639,52 @@
 
     }
 
+    // CraftBukkit start - Delegate so we can handle providing a reason for health being regained
     public void heal(float f) {
+        heal(f, EntityRegainHealthEvent.RegainReason.CUSTOM);
+    }
+        
+    public void heal(float f, EntityRegainHealthEvent.RegainReason regainReason) {
         float f1 = this.getHealth();
 
         if (f1 > 0.0F) {
-            this.setHealth(f1 + f);
+            EntityRegainHealthEvent event = new EntityRegainHealthEvent(this.getBukkitEntity(), f, regainReason);
+            this.world.getServer().getPluginManager().callEvent(event);
+
+            if (!event.isCancelled()) {
+                this.setHealth((float) (this.getHealth() + event.getAmount()));
+            }
+            // CraftBukkit end
         }
 
     }
 
     public final float getHealth() {
+        // CraftBukkit start - Use unscaled health
+        if (this instanceof EntityPlayer) {
+            return (float) ((EntityPlayer) this).getBukkitEntity().getHealth();
+        }
+        // CraftBukkit end
         return this.datawatcher.getFloat(6);
     }
 
     public void setHealth(float f) {
+        // CraftBukkit start - Handle scaled health
+        if (this instanceof EntityPlayer) {
+            org.bukkit.craftbukkit.entity.CraftPlayer player = ((EntityPlayer) this).getBukkitEntity();
+            // Squeeze
+            if (f < 0.0F) {
+                player.setRealHealth(0.0D);
+            } else if (f > player.getMaxHealth()) {
+                player.setRealHealth(player.getMaxHealth());
+            } else {
+                player.setRealHealth(f);
+            }
+
+            this.datawatcher.watch(6, Float.valueOf(player.getScaledHealth()));
+            return;
+        }
+        // CraftBukkit end
         this.datawatcher.watch(6, Float.valueOf(MathHelper.a(f, 0.0F, this.getMaxHealth())));
     }
 
@@ -589,7 +700,8 @@
             } else if (damagesource.o() && this.hasEffect(MobEffectList.FIRE_RESISTANCE)) {
                 return false;
             } else {
-                if ((damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && this.getEquipment(4) != null) {
+                // CraftBukkit - Moved into d(DamageSource, float)
+                if (false && (damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && this.getEquipment(4) != null) {
                     this.getEquipment(4).damage((int) (f * 4.0F + this.random.nextFloat() * f * 2.0F), this);
                     f *= 0.75F;
                 }
@@ -602,16 +714,34 @@
                         return false;
                     }
 
-                    this.d(damagesource, f - this.lastDamage);
+                    // CraftBukkit start
+                    if (!this.d(damagesource, f - this.lastDamage)) {
+                        return false;
+                    }
+                    // CraftBukkit end
                     this.lastDamage = f;
                     flag = false;
                 } else {
+                    // CraftBukkit start
+                    float previousHealth = this.getHealth();
+                    if (!this.d(damagesource, f)) {
+                        return false;
+                    }
                     this.lastDamage = f;
                     this.noDamageTicks = this.maxNoDamageTicks;
-                    this.d(damagesource, f);
+                    // CraftBukkit end
                     this.hurtTicks = this.at = 10;
                 }
 
+                // CraftBukkit start
+                if(this instanceof EntityAnimal){
+                    ((EntityAnimal)this).cq();
+                    if(this instanceof EntityTameableAnimal){
+                        ((EntityTameableAnimal)this).getGoalSit().setSitting(false);
+                    }
+                }
+                // CraftBukkit end
+
                 this.au = 0.0F;
                 Entity entity = damagesource.getEntity();
 
@@ -717,11 +847,19 @@
             }
 
             if (this.aZ() && this.world.getGameRules().getBoolean("doMobLoot")) {
+                this.drops = new ArrayList<org.bukkit.inventory.ItemStack>(); // CraftBukkit - Setup drop capture
+                 
                 this.dropDeathLoot(this.lastDamageByPlayerTime > 0, i);
                 this.dropEquipment(this.lastDamageByPlayerTime > 0, i);
                 if (this.lastDamageByPlayerTime > 0 && this.random.nextFloat() < 0.025F + (float) i * 0.01F) {
                     this.getRareDrop();
-                }
+                } 
+                // CraftBukkit start - Call death event
+                CraftEventFactory.callEntityDeathEvent(this, this.drops);
+                this.drops = null;
+            } else {
+                CraftEventFactory.callEntityDeathEvent(this);
+                // CraftBukkit end
             }
         }
 
@@ -781,8 +919,13 @@
         int i = MathHelper.f((f - 3.0F - f2) * f1);
 
         if (i > 0) {
+            // CraftBukkit start
+            if (!this.damageEntity(DamageSource.FALL, (float) i)) {
+                return;
+            }
+            // CraftBukkit end
             this.makeSound(this.n(i), 1.0F, 1.0F);
-            this.damageEntity(DamageSource.FALL, (float) i);
+            // this.damageEntity(DamageSource.FALL, (float) i); // CraftBukkit - moved up
             int j = MathHelper.floor(this.locX);
             int k = MathHelper.floor(this.locY - 0.20000000298023224D);
             int l = MathHelper.floor(this.locZ);
@@ -826,7 +969,7 @@
             int i = 25 - this.bq();
             float f1 = f * (float) i;
 
-            this.damageArmor(f);
+            // this.damageArmor(f); // CraftBukkit - Moved into d(DamageSource, float)
             f = f1 / 25.0F;
         }
 
@@ -840,8 +983,9 @@
             int i;
             int j;
             float f1;
-
-            if (this.hasEffect(MobEffectList.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) {
+               
+            // CraftBukkit - Moved to d(DamageSource, float)
+            if (false && this.hasEffect(MobEffectList.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) {
                 i = (this.getEffect(MobEffectList.RESISTANCE).getAmplifier() + 1) * 5;
                 j = 25 - i;
                 f1 = f * (float) j;
@@ -867,22 +1011,117 @@
         }
     }
 
-    protected void d(DamageSource damagesource, float f) {
+    // CraftBukkit start
+    protected boolean d(final DamageSource damagesource, float f) { // void -> boolean, add final
         if (!this.isInvulnerable(damagesource)) {
-            f = this.applyArmorModifier(damagesource, f);
-            f = this.applyMagicModifier(damagesource, f);
-            float f1 = f;
+            final boolean human = this instanceof EntityHuman;
+            float originalDamage = f;
+            Function<Double, Double> hardHat = new Function<Double, Double>() {
+                @Override
+                public Double apply(Double f) {
+                    if ((damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && EntityLiving.this.getEquipment(4) != null) {
+                        return -(f - (f * 0.75F));
+                    }
+                    return -0.0;
+                }
+            };
+            float hardHatModifier = hardHat.apply((double) f).floatValue();
+            f += hardHatModifier;
+
+            Function<Double, Double> blocking = new Function<Double, Double>() {
+                @Override
+                public Double apply(Double f) {
+                    if (human) {
+                        if (!damagesource.ignoresArmor() && ((EntityHuman) EntityLiving.this).isBlocking() && f > 0.0F) {
+                            return -(f - ((1.0F + f) * 0.5F));
+                        }
+                    }
+                    return -0.0;
+                }
+            };
+            float blockingModifier = blocking.apply((double) f).floatValue();
+            f += blockingModifier;
+
+            Function<Double, Double> armor = new Function<Double, Double>() {
+                @Override
+                public Double apply(Double f) {
+                    return -(f - EntityLiving.this.applyArmorModifier(damagesource, f.floatValue()));
+                }
+            };
+            float armorModifier = armor.apply((double) f).floatValue();
+            f += armorModifier;
+
+            Function<Double, Double> resistance = new Function<Double, Double>() {
+                @Override
+                public Double apply(Double f) {
+                    if (!damagesource.isStarvation() && EntityLiving.this.hasEffect(MobEffectList.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) {
+                        int i = (EntityLiving.this.getEffect(MobEffectList.RESISTANCE).getAmplifier() + 1) * 5;
+                        int j = 25 - i;
+                        float f1 = f.floatValue() * (float) j;
+                        return -(f - (f1 / 25.0F));
+                    }
+                    return -0.0;
+                }
+            };
+            float resistanceModifier = resistance.apply((double) f).floatValue();
+            f += resistanceModifier;
+
+            Function<Double, Double> magic = new Function<Double, Double>() {
+                @Override
+                public Double apply(Double f) {
+                    return -(f - EntityLiving.this.applyMagicModifier(damagesource, f.floatValue()));
+                }
+            };
+            float magicModifier = magic.apply((double) f).floatValue();
+            f += magicModifier;
+
+            Function<Double, Double> absorption = new Function<Double, Double>() {
+                @Override
+                public Double apply(Double f) {
+                    return -(Math.max(f - Math.max(f - EntityLiving.this.getAbsorptionHearts(), 0.0F), 0.0F));
+                }
+            };
+            float absorptionModifier = absorption.apply((double) f).floatValue();
+
+            EntityDamageEvent event = CraftEventFactory.handleLivingEntityDamageEvent(this, damagesource, originalDamage, hardHatModifier, blockingModifier, armorModifier, resistanceModifier, magicModifier, absorptionModifier, hardHat, blocking, armor, resistance, magic, absorption);
+            if (event.isCancelled()) {
+                return false;
+            }
+
+            f = (float) event.getFinalDamage();
+
+            // Apply damage to helmet
+            if ((damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && this.getEquipment(4) != null) {
+                this.getEquipment(4).damage((int) (event.getDamage() * 4.0F + this.random.nextFloat() * event.getDamage() * 2.0F), this);
+            }
+            
+            // Apply damage to armor
+            if (!damagesource.ignoresArmor()) {
+                float armorDamage = (float) (event.getDamage() + event.getDamage(DamageModifier.BLOCKING) + event.getDamage(DamageModifier.HARD_HAT));
+                this.damageArmor(armorDamage);
+            }
 
-            f = Math.max(f - this.getAbsorptionHearts(), 0.0F);
-            this.setAbsorptionHearts(this.getAbsorptionHearts() - (f1 - f));
+            absorptionModifier = (float) -event.getDamage(DamageModifier.ABSORPTION);
+            this.setAbsorptionHearts(Math.max(this.getAbsorptionHearts() - absorptionModifier, 0.0F));
             if (f != 0.0F) {
+                if (human) {
+                    ((EntityHuman) this).applyExhaustion(damagesource.getExhaustionCost());
+                }
+                // CraftBukkit end
                 float f2 = this.getHealth();
 
                 this.setHealth(f2 - f);
                 this.br().a(damagesource, f2, f);
+                // CraftBukkit start
+                if (human) {
+                    return true;
+                }
+                // CraftBukkit end
                 this.setAbsorptionHearts(this.getAbsorptionHearts() - f);
             }
+            return true; // CraftBukkit
         }
+        return false; // CraftBukkit
     }
 
     public CombatTracker br() {
@@ -1236,7 +1475,8 @@
         if (f > 0.0025000002F) {
             f3 = 1.0F;
             f2 = (float) Math.sqrt((double) f) * 3.0F;
-            f1 = (float) Math.atan2(d1, d0) * 180.0F / 3.1415927F - 90.0F;
+            // CraftBukkit - Math -> TrigMath
+            f1 = (float) org.bukkit.craftbukkit.TrigMath.atan2(d1, d0) * 180.0F / 3.1415927F - 90.0F;
         }
 
         if (this.ax > 0.0F) {
@@ -1400,6 +1640,13 @@
         if (list != null && !list.isEmpty()) {
             for (int i = 0; i < list.size(); ++i) {
                 Entity entity = (Entity) list.get(i);
+                
+                // TODO better check now?
+                // CraftBukkit start - Only handle mob (non-player) collisions every other tick
+                if (entity instanceof EntityLiving && !(this instanceof EntityPlayer) && this.ticksLived % 2 == 0) {
+                    continue;
+                }
+                // CraftBukkit end
 
                 if (entity.ae()) {
                     this.s(entity);
@@ -1415,6 +1662,18 @@
 
     public void mount(Entity entity) {
         if (this.vehicle != null && entity == null) {
+            // CraftBukkit start
+            Entity originalVehicle = this.vehicle;
+            if ((this.bukkitEntity instanceof LivingEntity) && (this.vehicle.getBukkitEntity() instanceof Vehicle)) {
+                VehicleExitEvent event = new VehicleExitEvent((Vehicle) this.vehicle.getBukkitEntity(), (LivingEntity) this.bukkitEntity);
+                getBukkitEntity().getServer().getPluginManager().callEvent(event);
+
+                if (event.isCancelled() || vehicle != originalVehicle) {
+                    return;
+                }
+            }
+            // CraftBukkit end
+
             if (!this.world.isStatic) {
                 this.q(this.vehicle);
             }