aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0410-Fix-numerous-item-duplication-issues-and-teleport-is.patch
blob: 8d65b504d1ddeba9d149958a2ffd909a5a42dcc8 (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
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 25 Apr 2020 06:46:35 -0400
Subject: [PATCH] Fix numerous item duplication issues and teleport issues

This notably fixes the newest "Donkey Dupe", but also fixes a lot
of dupe bugs in general around nether portals and entity world transfer

We also fix item duplication generically by anytime we clone an item
to drop it on the ground, destroy the source item.

This avoid an itemstack ever existing twice in the world state pre
clean up stage.

So even if something NEW comes up, it would be impossible to drop the
same item twice because the source was destroyed.

diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 31a571ec0f4cd60b9ef192d1deb2c529e7a354ff..0dc85e33e9748a87e2dfbbbfb1861bdf636dab33 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -2167,11 +2167,12 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, i
         } else {
             // CraftBukkit start - Capture drops for death event
             if (this instanceof net.minecraft.world.entity.LivingEntity && !((net.minecraft.world.entity.LivingEntity) this).forceDrops) {
-                ((net.minecraft.world.entity.LivingEntity) this).drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(stack));
+                ((net.minecraft.world.entity.LivingEntity) this).drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(stack)); // Paper - mirror so we can destroy it later
                 return null;
             }
             // CraftBukkit end
-            ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + (double) yOffset, this.getZ(), stack);
+            ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + (double) yOffset, this.getZ(), stack.copy()); // Paper - clone so we can destroy original
+            stack.setCount(0); // Paper - destroy this item - if this ever leaks due to game bugs, ensure it doesn't dupe
 
             entityitem.setDefaultPickUpDelay();
             // CraftBukkit start
@@ -2936,6 +2937,12 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, i
     @Nullable
     public Entity teleportTo(ServerLevel worldserver, BlockPos location) {
         // CraftBukkit end
+        // Paper start - fix bad state entities causing dupes
+        if (!isAlive() || !valid) {
+            LOGGER.warn("Illegal Entity Teleport " + this + " to " + worldserver + ":" + location, new Throwable());
+            return null;
+        }
+        // Paper end
         if (this.level instanceof ServerLevel && !this.isRemoved()) {
             this.level.getProfiler().push("changeDimension");
             // CraftBukkit start
@@ -2962,6 +2969,11 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, i
                 // CraftBukkit end
 
                 this.level.getProfiler().popPush("reloading");
+                // Paper start - Change lead drop timing to prevent dupe
+                if (this instanceof Mob) {
+                    ((Mob) this).dropLeash(true, true); // Paper drop lead
+                }
+                // Paper end
                 Entity entity = this.getType().create(worldserver);
 
                 if (entity != null) {
@@ -2975,10 +2987,6 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, i
                     // CraftBukkit start - Forward the CraftEntity to the new entity
                     this.getBukkitEntity().setHandle(entity);
                     entity.bukkitEntity = this.getBukkitEntity();
-
-                    if (this instanceof Mob) {
-                        ((Mob) this).dropLeash(true, false); // Unleash to prevent duping of leads.
-                    }
                     // CraftBukkit end
                 }
 
@@ -3100,7 +3108,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, i
     }
 
     public boolean canChangeDimensions() {
-        return true;
+        return isAlive() && valid; // Paper
     }
 
     public float getBlockExplosionResistance(Explosion explosion, BlockGetter world, BlockPos pos, BlockState blockState, FluidState fluidState, float max) {
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index a34566fbdb2e2ffb80f239c0d1d5c94ecf7f91ff..479a10b24d5c8d825d3c11d1800c89d769e0f441 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -1622,9 +1622,9 @@ public abstract class LivingEntity extends Entity {
                 // Paper start
                 org.bukkit.event.entity.EntityDeathEvent deathEvent = this.dropAllDeathLoot(source);
                 if (deathEvent == null || !deathEvent.isCancelled()) {
-                    if (this.deathScore >= 0 && entityliving != null) {
-                        entityliving.awardKillScore(this, this.deathScore, source);
-                    }
+                    // if (this.deathScore >= 0 && entityliving != null) { // Paper moved to be run earlier in #dropAllDeathLoot before destroying the drop items in CraftEventFactory#callEntityDeathEvent
+                    //     entityliving.awardKillScore(this, this.deathScore, source);
+                    // }
                     // Paper start - clear equipment if event is not cancelled
                     if (this instanceof Mob) {
                         for (EquipmentSlot slot : this.clearedEquipmentSlots) {
@@ -1721,8 +1721,13 @@ public abstract class LivingEntity extends Entity {
             this.dropCustomDeathLoot(source, i, flag);
             this.clearEquipmentSlots = prev; // Paper
         }
-        // CraftBukkit start - Call death event
-        org.bukkit.event.entity.EntityDeathEvent deathEvent = CraftEventFactory.callEntityDeathEvent(this, this.drops); // Paper
+        // CraftBukkit start - Call death event // Paper start - call advancement triggers with correct entity equipment
+        org.bukkit.event.entity.EntityDeathEvent deathEvent = CraftEventFactory.callEntityDeathEvent(this, this.drops, () -> {
+            final LivingEntity entityliving = this.getKillCredit();
+            if (this.deathScore >= 0 && entityliving != null) {
+                entityliving.awardKillScore(this, this.deathScore, source);
+            }
+        }); // Paper end
         this.postDeathDropItems(deathEvent); // Paper
         this.drops = new ArrayList<>();
         // CraftBukkit end
diff --git a/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java b/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
index a3a900d10440ed5ebe24370a77ccb6cad911cfc9..0d468631b9c260091e732925da43c177ebda892f 100644
--- a/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
+++ b/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
@@ -610,7 +610,7 @@ public class ArmorStand extends LivingEntity {
         for (i = 0; i < this.handItems.size(); ++i) {
             itemstack = (ItemStack) this.handItems.get(i);
             if (!itemstack.isEmpty()) {
-                drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(itemstack)); // CraftBukkit - add to drops
+                drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(itemstack)); // CraftBukkit - add to drops // Paper - mirror so we can destroy it later - though this call site was safe
                 this.handItems.set(i, ItemStack.EMPTY);
             }
         }
@@ -618,7 +618,7 @@ public class ArmorStand extends LivingEntity {
         for (i = 0; i < this.armorItems.size(); ++i) {
             itemstack = (ItemStack) this.armorItems.get(i);
             if (!itemstack.isEmpty()) {
-                drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(itemstack)); // CraftBukkit - add to drops
+                drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(itemstack)); // CraftBukkit - add to drops // Paper - mirror so we can destroy it later - though this call site was safe
                 this.armorItems.set(i, ItemStack.EMPTY);
             }
         }
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 550e30bfbdcf1ae0adf2cc68e3ae3d3dd4faad56..d998c0dd9e03b9c0f0a6075e5a52ba50b44ed0c4 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -820,6 +820,11 @@ public class CraftEventFactory {
     }
 
     public static EntityDeathEvent callEntityDeathEvent(net.minecraft.world.entity.LivingEntity victim, List<org.bukkit.inventory.ItemStack> drops) {
+        // Paper start
+        return CraftEventFactory.callEntityDeathEvent(victim, drops, com.google.common.util.concurrent.Runnables.doNothing());
+    }
+    public static EntityDeathEvent callEntityDeathEvent(net.minecraft.world.entity.LivingEntity victim, List<org.bukkit.inventory.ItemStack> drops, Runnable lootCheck) {
+        // Paper end
         CraftLivingEntity entity = (CraftLivingEntity) victim.getBukkitEntity();
         EntityDeathEvent event = new EntityDeathEvent(entity, drops, victim.getExpReward());
         populateFields(victim, event); // Paper - make cancellable
@@ -833,11 +838,13 @@ public class CraftEventFactory {
         playDeathSound(victim, event);
         // Paper end
         victim.expToDrop = event.getDroppedExp();
+        lootCheck.run(); // Paper - advancement triggers before destroying items
 
         for (org.bukkit.inventory.ItemStack stack : event.getDrops()) {
             if (stack == null || stack.getType() == Material.AIR || stack.getAmount() == 0) continue;
 
-            world.dropItem(entity.getLocation(), stack);
+            world.dropItem(entity.getLocation(), stack); // Paper - note: dropItem already clones due to this being bukkit -> NMS
+            if (stack instanceof CraftItemStack) stack.setAmount(0); // Paper - destroy this item - if this ever leaks due to game bugs, ensure it doesn't dupe, but don't nuke bukkit stacks of manually added items
         }
 
         return event;