2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 25 Apr 2020 06:46:35 -0400
2024-01-21 12:11:43 +01:00
Subject: [PATCH] Fix item duplication and teleport issues
2021-06-11 14:02:28 +02:00
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
2024-11-04 18:42:38 +01:00
index 416e76f2124aba0c9ad6e6ecb052e73a8b743f37..8b4ded85500379bd7dafba51a91732dc56b815e6 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
2024-11-04 18:42:38 +01:00
@@ -2631,11 +2631,12 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2021-06-11 14:02:28 +02:00
} 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
2024-10-26 00:04:29 +02:00
- ItemEntity entityitem = new ItemEntity(world, this.getX(), this.getY() + (double) yOffset, this.getZ(), stack);
+ ItemEntity entityitem = new ItemEntity(world, this.getX(), this.getY() + (double) yOffset, this.getZ(), stack.copy()); // Paper - copy so we can destroy original
2021-06-11 14:02:28 +02:00
+ stack.setCount(0); // Paper - destroy this item - if this ever leaks due to game bugs, ensure it doesn't dupe
entityitem.setDefaultPickUpDelay();
// CraftBukkit start
2024-11-04 18:42:38 +01:00
@@ -3463,6 +3464,12 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2024-10-23 16:04:01 +02:00
public Entity teleport(TeleportTransition teleportTarget) {
2024-06-13 22:14:13 +02:00
Level world = this.level();
2024-01-21 12:11:43 +01:00
+ // Paper start - Fix item duplication and teleport issues
2024-11-01 22:25:31 +01:00
+ if ((!this.isAlive() || !this.valid) && (teleportTarget.newLevel() != world)) {
2024-10-25 12:30:19 +02:00
+ LOGGER.warn("Illegal Entity Teleport " + this + " to " + teleportTarget.newLevel() + ":" + teleportTarget.position(), new Throwable());
2021-06-11 14:02:28 +02:00
+ return null;
+ }
2024-01-21 12:11:43 +01:00
+ // Paper end - Fix item duplication and teleport issues
2024-06-13 22:14:13 +02:00
if (world instanceof ServerLevel worldserver) {
if (!this.isRemoved()) {
// CraftBukkit start
2024-11-04 18:42:38 +01:00
@@ -3551,6 +3558,11 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2024-10-23 16:04:01 +02:00
gameprofilerfiller.pop();
return null;
} else {
+ // Paper start - Fix item duplication and teleport issues
+ if (this instanceof Leashable leashable) {
+ leashable.dropLeash(true, true); // Paper drop lead
+ }
+ // Paper end - Fix item duplication and teleport issues
entity.restoreFrom(this);
this.removeAfterChangingDimensions();
// CraftBukkit start - Forward the CraftEntity to the new entity
2024-11-04 18:42:38 +01:00
@@ -3663,6 +3675,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2021-06-11 14:02:28 +02:00
}
2024-10-23 16:04:01 +02:00
public boolean canTeleport(Level from, Level to) {
+ if (!this.isAlive() || !this.valid) return false; // Paper - Fix item duplication and teleport issues
if (from.dimension() == Level.END && to.dimension() == Level.OVERWORLD) {
Iterator iterator = this.getPassengers().iterator();
2021-06-11 14:02:28 +02:00
2021-12-08 19:25:57 +01:00
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
2024-11-04 18:42:38 +01:00
index 33d969e09372930e740a8a4a84e5503284df9c28..4d39aa2fdc311acb34d7b674365141b995c324b7 100644
2021-12-08 19:25:57 +01:00
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
2024-10-27 18:11:15 +01:00
@@ -1765,9 +1765,9 @@ public abstract class LivingEntity extends Entity implements Attackable {
2021-12-08 19:25:57 +01:00
// Paper start
2024-06-13 22:14:13 +02:00
org.bukkit.event.entity.EntityDeathEvent deathEvent = this.dropAllDeathLoot(worldserver, damageSource);
2021-12-08 19:25:57 +01:00
if (deathEvent == null || !deathEvent.isCancelled()) {
- if (this.deathScore >= 0 && entityliving != null) {
2022-06-08 06:39:43 +02:00
- entityliving.awardKillScore(this, this.deathScore, damageSource);
2021-12-08 19:25:57 +01:00
- }
2024-01-21 12:11:43 +01:00
+ // if (this.deathScore >= 0 && entityliving != null) { // Paper - Fix item duplication and teleport issues; moved to be run earlier in #dropAllDeathLoot before destroying the drop items in CraftEventFactory#callEntityDeathEvent
2022-06-08 06:39:43 +02:00
+ // entityliving.awardKillScore(this, this.deathScore, damageSource);
2021-12-08 19:25:57 +01:00
+ // }
// Paper start - clear equipment if event is not cancelled
2022-02-18 19:16:41 +01:00
if (this instanceof Mob) {
for (EquipmentSlot slot : this.clearedEquipmentSlots) {
2024-10-27 18:11:15 +01:00
@@ -1861,8 +1861,13 @@ public abstract class LivingEntity extends Entity implements Attackable {
2024-06-13 22:14:13 +02:00
this.dropCustomDeathLoot(world, damageSource, flag);
2022-02-18 19:16:41 +01:00
this.clearEquipmentSlots = prev; // Paper
2021-12-08 19:25:57 +01:00
}
- // CraftBukkit start - Call death event
2024-06-13 22:14:13 +02:00
- org.bukkit.event.entity.EntityDeathEvent deathEvent = CraftEventFactory.callEntityDeathEvent(this, damageSource, this.drops); // Paper
2021-12-08 19:25:57 +01:00
+ // CraftBukkit start - Call death event // Paper start - call advancement triggers with correct entity equipment
2024-06-13 22:14:13 +02:00
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = CraftEventFactory.callEntityDeathEvent(this, damageSource, this.drops, () -> {
2021-12-08 19:25:57 +01:00
+ final LivingEntity entityliving = this.getKillCredit();
+ if (this.deathScore >= 0 && entityliving != null) {
2024-06-13 22:14:13 +02:00
+ entityliving.awardKillScore(this, this.deathScore, damageSource);
2021-12-08 19:25:57 +01:00
+ }
+ }); // Paper end
this.postDeathDropItems(deathEvent); // Paper
this.drops = new ArrayList<>();
// CraftBukkit end
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java b/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
2024-10-23 16:04:01 +02:00
index e20565cf256aacd012a1722c5ebbf9016bc82e42..59fbfe8de2dc5ec020dd61a5e446b0b6f67d76e4 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
+++ b/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
2024-10-23 16:04:01 +02:00
@@ -633,7 +633,7 @@ public class ArmorStand extends LivingEntity {
2021-06-11 14:02:28 +02:00
for (i = 0; i < this.handItems.size(); ++i) {
itemstack = (ItemStack) this.handItems.get(i);
if (!itemstack.isEmpty()) {
2023-10-27 01:34:58 +02:00
- this.drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(itemstack)); // CraftBukkit - add to drops
+ this.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
2021-06-11 14:02:28 +02:00
this.handItems.set(i, ItemStack.EMPTY);
}
}
2024-10-23 16:04:01 +02:00
@@ -641,7 +641,7 @@ public class ArmorStand extends LivingEntity {
2021-06-11 14:02:28 +02:00
for (i = 0; i < this.armorItems.size(); ++i) {
itemstack = (ItemStack) this.armorItems.get(i);
if (!itemstack.isEmpty()) {
2023-10-27 01:34:58 +02:00
- this.drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(itemstack)); // CraftBukkit - add to drops
+ this.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
2021-06-11 14:02:28 +02:00
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
2024-11-01 22:25:31 +01:00
index 74f33aeb6193080446ebbcc5242d95c3b1c97823..f482fc14e171129f9fa60113b1223b2db79ec6ec 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2024-10-23 16:04:01 +02:00
@@ -903,6 +903,11 @@ public class CraftEventFactory {
2021-12-08 19:25:57 +01:00
}
2024-05-11 23:48:37 +02:00
public static EntityDeathEvent callEntityDeathEvent(net.minecraft.world.entity.LivingEntity victim, DamageSource damageSource, List<org.bukkit.inventory.ItemStack> drops) {
2021-12-08 19:25:57 +01:00
+ // Paper start
2024-05-11 23:48:37 +02:00
+ return CraftEventFactory.callEntityDeathEvent(victim, damageSource, drops, com.google.common.util.concurrent.Runnables.doNothing());
2021-12-08 19:25:57 +01:00
+ }
2024-05-11 23:48:37 +02:00
+ public static EntityDeathEvent callEntityDeathEvent(net.minecraft.world.entity.LivingEntity victim, DamageSource damageSource, List<org.bukkit.inventory.ItemStack> drops, Runnable lootCheck) {
2021-12-08 19:25:57 +01:00
+ // Paper end
CraftLivingEntity entity = (CraftLivingEntity) victim.getBukkitEntity();
2024-05-11 23:48:37 +02:00
CraftDamageSource bukkitDamageSource = new CraftDamageSource(damageSource);
2024-10-23 16:04:01 +02:00
CraftWorld world = (CraftWorld) entity.getWorld();
@@ -917,11 +922,13 @@ public class CraftEventFactory {
2021-12-08 19:25:57 +01:00
playDeathSound(victim, event);
// Paper end
victim.expToDrop = event.getDroppedExp();
+ lootCheck.run(); // Paper - advancement triggers before destroying items
2021-06-11 14:02:28 +02:00
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;