summaryrefslogtreecommitdiffstats
path: root/nms-patches/EntityHuman.patch
blob: d633c8aa7f99ab4ca66aa72512693d116f01dff0 (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
--- a/net/minecraft/server/EntityHuman.java
+++ b/net/minecraft/server/EntityHuman.java
@@ -15,6 +15,18 @@
 import java.util.function.ToIntFunction;
 import javax.annotation.Nullable;
 
+// CraftBukkit start
+import org.bukkit.craftbukkit.entity.CraftHumanEntity;
+import org.bukkit.entity.Item;
+import org.bukkit.entity.Player;
+import org.bukkit.event.entity.CreatureSpawnEvent;
+import org.bukkit.event.entity.EntityCombustByEntityEvent;
+import org.bukkit.event.player.PlayerBedLeaveEvent;
+import org.bukkit.event.player.PlayerDropItemEvent;
+import org.bukkit.event.player.PlayerVelocityEvent;
+import org.bukkit.util.Vector;
+// CraftBukkit end
+
 public abstract class EntityHuman extends EntityLiving {
 
     private static final DataWatcherObject<Float> a = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.c);
@@ -24,10 +36,10 @@
     protected static final DataWatcherObject<NBTTagCompound> bz = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.p);
     protected static final DataWatcherObject<NBTTagCompound> bA = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.p);
     public PlayerInventory inventory = new PlayerInventory(this);
-    protected InventoryEnderChest enderChest = new InventoryEnderChest();
+    protected InventoryEnderChest enderChest = new InventoryEnderChest(this); // CraftBukkit - add "this" to constructor
     public Container defaultContainer;
     public Container activeContainer;
-    protected FoodMetaData foodData = new FoodMetaData();
+    protected FoodMetaData foodData = new FoodMetaData(this); // CraftBukkit - add "this" to constructor
     protected int bG;
     public float bH;
     public float bI;
@@ -60,6 +72,17 @@
     @Nullable
     public EntityFishingHook hookedFish;
 
+    // CraftBukkit start
+    public boolean fauxSleeping;
+    public String spawnWorld = "";
+    public int oldLevel = -1;
+
+    @Override
+    public CraftHumanEntity getBukkitEntity() {
+        return (CraftHumanEntity) super.getBukkitEntity();
+    }
+    // CraftBukkit end
+
     public EntityHuman(World world, GameProfile gameprofile) {
         super(EntityTypes.PLAYER, world);
         this.cd = ItemStack.a;
@@ -184,7 +207,7 @@
         ItemStack itemstack = this.getEquipment(EnumItemSlot.HEAD);
 
         if (itemstack.getItem() == Items.TURTLE_HELMET && !this.a(TagsFluid.WATER)) {
-            this.addEffect(new MobEffect(MobEffects.WATER_BREATHING, 200, 0, false, false, true));
+            this.addEffect(new MobEffect(MobEffects.WATER_BREATHING, 200, 0, false, false, true), org.bukkit.event.entity.EntityPotionEffectEvent.Cause.TURTLE_HELMET); // CraftBukkit
         }
 
     }
@@ -369,7 +392,8 @@
 
         if (this.world.getDifficulty() == EnumDifficulty.PEACEFUL && this.world.getGameRules().getBoolean("naturalRegeneration")) {
             if (this.getHealth() < this.getMaxHealth() && this.ticksLived % 20 == 0) {
-                this.heal(1.0F);
+                // CraftBukkit - added regain reason of "REGEN" for filtering purposes.
+                this.heal(1.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.REGEN);
             }
 
             if (this.foodData.c() && this.ticksLived % 10 == 0) {
@@ -393,7 +417,7 @@
 
         this.o((float) attributeinstance.getValue());
         float f = MathHelper.sqrt(this.motX * this.motX + this.motZ * this.motZ);
-        float f1 = (float) (Math.atan(-this.motY * 0.20000000298023224D) * 15.0D);
+        float f1 = (float) ( org.bukkit.craftbukkit.TrigMath.atan(-this.motY * 0.20000000298023224D) * 15.0D); // CraftBukkit
 
         if (f > 0.1F) {
             f = 0.1F;
@@ -516,6 +540,7 @@
 
     @Nullable
     public EntityItem a(boolean flag) {
+        // Called only when dropped by Q or CTRL-Q
         return this.a(this.inventory.splitStack(this.inventory.itemInHandIndex, flag && !this.inventory.getItemInHand().isEmpty() ? this.inventory.getItemInHand().getCount() : 1), false, true);
     }
 
@@ -558,6 +583,30 @@
                 entityitem.motZ += Math.sin((double) f1) * (double) f;
             }
 
+            // CraftBukkit start - fire PlayerDropItemEvent
+            Player player = (Player) this.getBukkitEntity();
+            Item drop = (Item) entityitem.getBukkitEntity();
+
+            PlayerDropItemEvent event = new PlayerDropItemEvent(player, drop);
+            this.world.getServer().getPluginManager().callEvent(event);
+
+            if (event.isCancelled()) {
+                org.bukkit.inventory.ItemStack cur = player.getInventory().getItemInHand();
+                if (flag1 && (cur == null || cur.getAmount() == 0)) {
+                    // The complete stack was dropped
+                    player.getInventory().setItemInHand(drop.getItemStack());
+                } else if (flag1 && cur.isSimilar(drop.getItemStack()) && cur.getAmount() < cur.getMaxStackSize() && drop.getItemStack().getAmount() == 1) {
+                    // Only one item is dropped
+                    cur.setAmount(cur.getAmount() + 1);
+                    player.getInventory().setItemInHand(cur);
+                } else {
+                    // Fallback
+                    player.getInventory().addItem(drop.getItemStack());
+                }
+                return null;
+            }
+            // CraftBukkit end
+
             ItemStack itemstack1 = this.a(entityitem);
 
             if (flag1) {
@@ -655,6 +704,13 @@
             this.a(true, true, false);
         }
 
+        // CraftBukkit start
+        this.spawnWorld = nbttagcompound.getString("SpawnWorld");
+        if ("".equals(spawnWorld)) {
+            this.spawnWorld = this.world.getServer().getWorlds().get(0).getName();
+        }
+        // CraftBukkit end
+
         if (nbttagcompound.hasKeyOfType("SpawnX", 99) && nbttagcompound.hasKeyOfType("SpawnY", 99) && nbttagcompound.hasKeyOfType("SpawnZ", 99)) {
             this.e = new BlockPosition(nbttagcompound.getInt("SpawnX"), nbttagcompound.getInt("SpawnY"), nbttagcompound.getInt("SpawnZ"));
             this.f = nbttagcompound.getBoolean("SpawnForced");
@@ -705,6 +761,7 @@
         if (!this.getShoulderEntityRight().isEmpty()) {
             nbttagcompound.set("ShoulderEntityRight", this.getShoulderEntityRight());
         }
+        nbttagcompound.setString("SpawnWorld", spawnWorld); // CraftBukkit - fixes bed spawns for multiworld worlds
 
     }
 
@@ -722,10 +779,10 @@
                     this.a(true, true, false);
                 }
 
-                this.releaseShoulderEntities();
+                // this.releaseShoulderEntities(); // CraftBukkit - moved down
                 if (damagesource.s()) {
                     if (this.world.getDifficulty() == EnumDifficulty.PEACEFUL) {
-                        f = 0.0F;
+                        return false; // CraftBukkit - f = 0.0f -> return false
                     }
 
                     if (this.world.getDifficulty() == EnumDifficulty.EASY) {
@@ -737,7 +794,13 @@
                     }
                 }
 
-                return f == 0.0F ? false : super.damageEntity(damagesource, f);
+                // CraftBukkit start - Don't filter out 0 damage
+                boolean damaged = super.damageEntity(damagesource, f);
+                if (damaged) {
+                    this.releaseShoulderEntities();
+                }
+                return damaged;
+                // CraftBukkit end
             }
         }
     }
@@ -751,10 +814,29 @@
     }
 
     public boolean a(EntityHuman entityhuman) {
-        ScoreboardTeamBase scoreboardteambase = this.be();
-        ScoreboardTeamBase scoreboardteambase1 = entityhuman.be();
+        // CraftBukkit start - Change to check OTHER player's scoreboard team according to API
+        // To summarize this method's logic, it's "Can parameter hurt this"
+        org.bukkit.scoreboard.Team team;
+        if (entityhuman instanceof EntityPlayer) {
+            EntityPlayer thatPlayer = (EntityPlayer) entityhuman;
+            team = thatPlayer.getBukkitEntity().getScoreboard().getPlayerTeam(thatPlayer.getBukkitEntity());
+            if (team == null || team.allowFriendlyFire()) {
+                return true;
+            }
+        } else {
+            // This should never be called, but is implemented anyway
+            org.bukkit.OfflinePlayer thisPlayer = entityhuman.world.getServer().getOfflinePlayer(entityhuman.getName());
+            team = entityhuman.world.getServer().getScoreboardManager().getMainScoreboard().getPlayerTeam(thisPlayer);
+            if (team == null || team.allowFriendlyFire()) {
+                return true;
+            }
+        }
 
-        return scoreboardteambase == null ? true : (!scoreboardteambase.isAlly(scoreboardteambase1) ? true : scoreboardteambase.allowFriendlyFire());
+        if (this instanceof EntityPlayer) {
+            return !team.hasPlayer(((EntityPlayer) this).getBukkitEntity());
+        }
+        return !team.hasPlayer(this.world.getServer().getOfflinePlayer(this.getName()));
+        // CraftBukkit end
     }
 
     protected void damageArmor(float f) {
@@ -797,7 +879,12 @@
         return (float) i / (float) this.inventory.armor.size();
     }
 
-    protected void damageEntity0(DamageSource damagesource, float f) {
+    // CraftBukkit start
+    protected boolean damageEntity0(DamageSource damagesource, float f) { // void -> boolean
+        if (true) {
+            return super.damageEntity0(damagesource, f);
+        }
+        // CraftBukkit end
         if (!this.isInvulnerable(damagesource)) {
             f = this.applyArmorModifier(damagesource, f);
             f = this.applyMagicModifier(damagesource, f);
@@ -823,6 +910,7 @@
 
             }
         }
+        return false; // CraftBukkit
     }
 
     public void openSign(TileEntitySign tileentitysign) {}
@@ -944,8 +1032,15 @@
                     if (entity instanceof EntityLiving) {
                         f3 = ((EntityLiving) entity).getHealth();
                         if (j > 0 && !entity.isBurning()) {
-                            flag4 = true;
-                            entity.setOnFire(1);
+                            // CraftBukkit start - Call a combust event when somebody hits with a fire enchanted item
+                            EntityCombustByEntityEvent combustEvent = new EntityCombustByEntityEvent(this.getBukkitEntity(), entity.getBukkitEntity(), 1);
+                            org.bukkit.Bukkit.getPluginManager().callEvent(combustEvent);
+
+                            if (!combustEvent.isCancelled()) {
+                                flag4 = true;
+                                entity.setOnFire(combustEvent.getDuration());
+                            }
+                            // CraftBukkit end
                         }
                     }
 
@@ -976,8 +1071,11 @@
                                 EntityLiving entityliving = (EntityLiving) iterator.next();
 
                                 if (entityliving != this && entityliving != entity && !this.r(entityliving) && (!(entityliving instanceof EntityArmorStand) || !((EntityArmorStand) entityliving).isMarker()) && this.h(entityliving) < 9.0D) {
+                                    // CraftBukkit start - Only apply knockback if the damage hits
+                                    if (entityliving.damageEntity(DamageSource.playerAttack(this).sweep(), f4)) {
                                     entityliving.a(this, 0.4F, (double) MathHelper.sin(this.yaw * 0.017453292F), (double) (-MathHelper.cos(this.yaw * 0.017453292F)));
-                                    entityliving.damageEntity(DamageSource.playerAttack(this), f4);
+                                    }
+                                    // CraftBukkit end
                                 }
                             }
 
@@ -986,11 +1084,28 @@
                         }
 
                         if (entity instanceof EntityPlayer && entity.velocityChanged) {
+                            // CraftBukkit start - Add Velocity Event
+                            boolean cancelled = false;
+                            Player player = (Player) entity.getBukkitEntity();
+                            org.bukkit.util.Vector velocity = new Vector( d1, d2, d3 );
+
+                            PlayerVelocityEvent event = new PlayerVelocityEvent(player, velocity.clone());
+                            world.getServer().getPluginManager().callEvent(event);
+
+                            if (event.isCancelled()) {
+                                cancelled = true;
+                            } else if (!velocity.equals(event.getVelocity())) {
+                                player.setVelocity(event.getVelocity());
+                            }
+
+                            if (!cancelled) {
                             ((EntityPlayer) entity).playerConnection.sendPacket(new PacketPlayOutEntityVelocity(entity));
                             entity.velocityChanged = false;
                             entity.motX = d1;
                             entity.motY = d2;
                             entity.motZ = d3;
+                            }
+                            // CraftBukkit end
                         }
 
                         if (flag2) {
@@ -1039,7 +1154,14 @@
 
                             this.a(StatisticList.DAMAGE_DEALT, Math.round(f5 * 10.0F));
                             if (j > 0) {
-                                entity.setOnFire(j * 4);
+                                // CraftBukkit start - Call a combust event when somebody hits with a fire enchanted item
+                                EntityCombustByEntityEvent combustEvent = new EntityCombustByEntityEvent(this.getBukkitEntity(), entity.getBukkitEntity(), j * 4);
+                                org.bukkit.Bukkit.getPluginManager().callEvent(combustEvent);
+
+                                if (!combustEvent.isCancelled()) {
+                                    entity.setOnFire(combustEvent.getDuration());
+                                }
+                                // CraftBukkit end
                             }
 
                             if (this.world instanceof WorldServer && f5 > 2.0F) {
@@ -1055,6 +1177,11 @@
                         if (flag4) {
                             entity.extinguish();
                         }
+                        // CraftBukkit start - resync on cancelled event
+                        if (this instanceof EntityPlayer) {
+                            ((EntityPlayer) this).getBukkitEntity().updateInventory();
+                        }
+                        // CraftBukkit end
                     }
                 }
 
@@ -1116,15 +1243,15 @@
         return this.h;
     }
 
-    public EntityHuman.EnumBedResult a(BlockPosition blockposition) {
-        EnumDirection enumdirection = (EnumDirection) this.world.getType(blockposition).get(BlockFacingHorizontal.FACING);
-
+    // CraftBukkit start - moved bed result checks from below into separate method
+    private EntityHuman.EnumBedResult getBedResult(BlockPosition blockposition, EnumDirection enumdirection) {
         if (!this.world.isClientSide) {
             if (this.isSleeping() || !this.isAlive()) {
                 return EntityHuman.EnumBedResult.OTHER_PROBLEM;
             }
 
-            if (!this.world.worldProvider.isOverworld()) {
+            // CraftBukkit - moved world and biome check from BlockBed interact handling here
+            if (!world.worldProvider.canRespawn() || world.getBiome(blockposition) == Biomes.NETHER || !this.world.worldProvider.isOverworld()) {
                 return EntityHuman.EnumBedResult.NOT_POSSIBLE_HERE;
             }
 
@@ -1146,6 +1273,26 @@
                 }
             }
         }
+        return EntityHuman.EnumBedResult.OK;
+    }
+
+    public EntityHuman.EnumBedResult a(BlockPosition blockposition) {
+        EnumDirection enumdirection = (EnumDirection) this.world.getType(blockposition).get(BlockFacingHorizontal.FACING);
+        // CraftBukkit start - moved checks into separate method above
+        EntityHuman.EnumBedResult bedResult = this.getBedResult(blockposition, enumdirection);
+
+        if (bedResult == EntityHuman.EnumBedResult.OTHER_PROBLEM) {
+            return bedResult; // return immediately if the result is not bypassable by plugins
+        }
+
+        if (this.getBukkitEntity() instanceof Player) {
+            bedResult = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerBedEnterEvent(this, blockposition, bedResult);
+
+            if (bedResult != EntityHuman.EnumBedResult.OK) {
+                return bedResult;
+            }
+        }
+        // CraftBukkit end
 
         if (this.isPassenger()) {
             this.stopRiding();
@@ -1212,6 +1359,24 @@
             this.world.everyoneSleeping();
         }
 
+        // CraftBukkit start - fire PlayerBedLeaveEvent
+        if (this.getBukkitEntity() instanceof Player) {
+            Player player = (Player) this.getBukkitEntity();
+
+            org.bukkit.block.Block bed;
+            BlockPosition blockposition = this.bedPosition;
+            if (blockposition != null) {
+                bed = this.world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ());
+            } else {
+                bed = this.world.getWorld().getBlockAt(player.getLocation());
+            }
+
+            PlayerBedLeaveEvent event = new PlayerBedLeaveEvent(player, bed, flag2);
+            this.world.getServer().getPluginManager().callEvent(event);
+            flag2 = event.shouldSetSpawnLocation();
+        }
+        // CraftBukkit end
+
         this.sleepTicks = flag ? 0 : 100;
         if (flag2) {
             this.setRespawnPosition(this.bedPosition, false);
@@ -1263,9 +1428,11 @@
         if (blockposition != null) {
             this.e = blockposition;
             this.f = flag;
+            this.spawnWorld = this.world.worldData.getName(); // CraftBukkit
         } else {
             this.e = null;
             this.f = false;
+            this.spawnWorld = ""; // CraftBukkit
         }
 
     }
@@ -1331,7 +1498,11 @@
             this.motY = d3 * 0.6D;
             this.aU = f3;
             this.fallDistance = 0.0F;
-            this.setFlag(7, false);
+            // CraftBukkit start
+            if (getFlag(7) && !org.bukkit.craftbukkit.event.CraftEventFactory.callToggleGlideEvent(this, false).isCancelled()) {
+                this.setFlag(7, false);
+            }
+            // CraftBukkit end
         } else {
             super.a(f, f1, f2);
         }
@@ -1631,13 +1802,17 @@
     }
 
     protected void releaseShoulderEntities() {
-        this.spawnEntityFromShoulder(this.getShoulderEntityLeft());
-        this.setShoulderEntityLeft(new NBTTagCompound());
-        this.spawnEntityFromShoulder(this.getShoulderEntityRight());
-        this.setShoulderEntityRight(new NBTTagCompound());
+        // CraftBukkit start
+        if (this.spawnEntityFromShoulder(this.getShoulderEntityLeft())) {
+            this.setShoulderEntityLeft(new NBTTagCompound());
+        }
+        if (this.spawnEntityFromShoulder(this.getShoulderEntityRight())) {
+            this.setShoulderEntityRight(new NBTTagCompound());
+        }
+        // CraftBukkit end
     }
 
-    private void spawnEntityFromShoulder(@Nullable NBTTagCompound nbttagcompound) {
+    private boolean spawnEntityFromShoulder(@Nullable NBTTagCompound nbttagcompound) { // CraftBukkit void->boolean
         if (!this.world.isClientSide && !nbttagcompound.isEmpty()) {
             Entity entity = EntityTypes.a(nbttagcompound, this.world);
 
@@ -1646,9 +1821,10 @@
             }
 
             entity.setPosition(this.locX, this.locY + 0.699999988079071D, this.locZ);
-            this.world.addEntity(entity);
+            return this.world.addEntity(entity, CreatureSpawnEvent.SpawnReason.SHOULDER_ENTITY); // CraftBukkit
         }
 
+        return true; // CraftBukkit
     }
 
     public abstract boolean isSpectator();
@@ -1859,7 +2035,7 @@
             return entitymonster.c(this.a);
         }
 
-        public boolean test(@Nullable Object object) {
+        public boolean test(@Nullable EntityMonster object) { // CraftBukkit - decompile error
             return this.a((EntityMonster) object);
         }