summaryrefslogtreecommitdiffstats
path: root/nms-patches/EntityItem.patch
blob: f12c053cdc52b2f91eea837b300eb5fe9f1c4011 (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
--- a/net/minecraft/server/EntityItem.java
+++ b/net/minecraft/server/EntityItem.java
@@ -3,6 +3,10 @@
 import java.util.Iterator;
 import java.util.UUID;
 import javax.annotation.Nullable;
+// CraftBukkit start
+import org.bukkit.event.entity.EntityPickupItemEvent;
+import org.bukkit.event.player.PlayerPickupItemEvent;
+// CraftBukkit end
 
 public class EntityItem extends Entity {
 
@@ -13,6 +17,7 @@
     private UUID f;
     private UUID g;
     public float a;
+    private int lastTick = MinecraftServer.currentTick - 1; // CraftBukkit
 
     public EntityItem(World world) {
         super(EntityTypes.ITEM, world);
@@ -48,9 +53,12 @@
             this.die();
         } else {
             super.tick();
-            if (this.pickupDelay > 0 && this.pickupDelay != 32767) {
-                --this.pickupDelay;
-            }
+            // CraftBukkit start - Use wall time for pickup and despawn timers
+            int elapsedTicks = MinecraftServer.currentTick - this.lastTick;
+            if (this.pickupDelay != 32767) this.pickupDelay -= elapsedTicks;
+            if (this.age != -32768) this.age += elapsedTicks;
+            this.lastTick = MinecraftServer.currentTick;
+            // CraftBukkit end
 
             this.lastX = this.locX;
             this.lastY = this.locY;
@@ -100,9 +108,11 @@
                 this.motY *= -0.5D;
             }
 
+            /* Craftbukkit start - moved up
             if (this.age != -32768) {
                 ++this.age;
             }
+            // Craftbukkit end */
 
             this.impulse |= this.at();
             if (!this.world.isClientSide) {
@@ -117,6 +127,12 @@
             }
 
             if (!this.world.isClientSide && this.age >= 6000) {
+                // CraftBukkit start - fire ItemDespawnEvent
+                if (org.bukkit.craftbukkit.event.CraftEventFactory.callItemDespawnEvent(this).isCancelled()) {
+                    this.age = 0;
+                    return;
+                }
+                // CraftBukkit end
                 this.die();
             }
 
@@ -165,6 +181,7 @@
                     } else if (itemstack1.getCount() + itemstack.getCount() > itemstack1.getMaxStackSize()) {
                         return false;
                     } else {
+                        if (org.bukkit.craftbukkit.event.CraftEventFactory.callItemMergeEvent(this, entityitem).isCancelled()) return false; // CraftBukkit
                         itemstack1.add(itemstack.getCount());
                         entityitem.pickupDelay = Math.max(entityitem.pickupDelay, this.pickupDelay);
                         entityitem.age = Math.min(entityitem.age, this.age);
@@ -197,6 +214,11 @@
         } else if (!this.getItemStack().isEmpty() && this.getItemStack().getItem() == Items.NETHER_STAR && damagesource.isExplosion()) {
             return false;
         } else {
+            // CraftBukkit start
+            if (org.bukkit.craftbukkit.event.CraftEventFactory.handleNonLivingEntityDamageEvent(this, damagesource, f)) {
+                return false;
+            }
+            // CraftBukkit end
             this.aA();
             this.e = (int) ((float) this.e - f);
             if (this.e <= 0) {
@@ -255,6 +277,35 @@
             Item item = itemstack.getItem();
             int i = itemstack.getCount();
 
+            // CraftBukkit start - fire PlayerPickupItemEvent
+            int canHold = entityhuman.inventory.canHold(itemstack);
+            int remaining = i - canHold;
+
+            if (this.pickupDelay <= 0 && canHold > 0) {
+                itemstack.setCount(canHold);
+                // Call legacy event
+                PlayerPickupItemEvent playerEvent = new PlayerPickupItemEvent((org.bukkit.entity.Player) entityhuman.getBukkitEntity(), (org.bukkit.entity.Item) this.getBukkitEntity(), remaining);
+                playerEvent.setCancelled(!entityhuman.canPickUpLoot);
+                this.world.getServer().getPluginManager().callEvent(playerEvent);
+                if (playerEvent.isCancelled()) {
+                    return;
+                }
+
+                // Call newer event afterwards
+                EntityPickupItemEvent entityEvent = new EntityPickupItemEvent((org.bukkit.entity.Player) entityhuman.getBukkitEntity(), (org.bukkit.entity.Item) this.getBukkitEntity(), remaining);
+                entityEvent.setCancelled(!entityhuman.canPickUpLoot);
+                this.world.getServer().getPluginManager().callEvent(entityEvent);
+                if (entityEvent.isCancelled()) {
+                    return;
+                }
+
+                itemstack.setCount(canHold + remaining);
+
+                // Possibly < 0; fix here so we do not have to modify code below
+                this.pickupDelay = 0;
+            }
+            // CraftBukkit end
+
             if (this.pickupDelay == 0 && (this.g == null || 6000 - this.age <= 200 || this.g.equals(entityhuman.getUniqueID())) && entityhuman.inventory.pickup(itemstack)) {
                 entityhuman.receive(this, i);
                 if (itemstack.isEmpty()) {
@@ -294,6 +345,7 @@
     }
 
     public void setItemStack(ItemStack itemstack) {
+        com.google.common.base.Preconditions.checkArgument(!itemstack.isEmpty(), "Cannot drop air"); // CraftBukkit
         this.getDataWatcher().set(EntityItem.b, itemstack);
     }