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-04-24 08:44:48 +02:00
index 04bd6d6bc6af0e423f56329dc3554b04170932f9..af49ff55865b327c167efedcb7413c270824efe3 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-04-24 08:44:48 +02:00
@@ -2447,11 +2447,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
2023-06-08 00:41:25 +02:00
- 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 - 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-04-24 08:44:48 +02:00
@@ -3238,6 +3239,12 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2021-06-11 14:02:28 +02:00
@Nullable
2023-09-22 05:29:51 +02:00
public Entity teleportTo(ServerLevel worldserver, Vec3 location) {
2021-06-11 14:02:28 +02:00
// CraftBukkit end
2024-01-21 12:11:43 +01:00
+ // Paper start - Fix item duplication and teleport issues
2023-06-08 00:41:25 +02:00
+ if (!this.isAlive() || !this.valid) {
2021-06-11 14:02:28 +02:00
+ LOGGER.warn("Illegal Entity Teleport " + this + " to " + worldserver + ":" + location, new Throwable());
+ return null;
+ }
2024-01-21 12:11:43 +01:00
+ // Paper end - Fix item duplication and teleport issues
2023-06-08 00:41:25 +02:00
if (this.level() instanceof ServerLevel && !this.isRemoved()) {
this.level().getProfiler().push("changeDimension");
2021-06-11 14:02:28 +02:00
// CraftBukkit start
2024-04-24 08:44:48 +02:00
@@ -3264,6 +3271,11 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2021-06-11 14:02:28 +02:00
// CraftBukkit end
2023-06-08 00:41:25 +02:00
this.level().getProfiler().popPush("reloading");
2024-01-21 12:11:43 +01:00
+ // Paper start - Fix item duplication and teleport issues
2021-06-11 14:02:28 +02:00
+ if (this instanceof Mob) {
+ ((Mob) this).dropLeash(true, true); // Paper drop lead
+ }
2024-01-21 12:11:43 +01:00
+ // Paper end - Fix item duplication and teleport issues
2021-11-24 12:38:00 +01:00
Entity entity = this.getType().create(worldserver);
2021-06-11 14:02:28 +02:00
if (entity != null) {
2024-04-24 08:44:48 +02:00
@@ -3281,10 +3293,6 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2021-06-11 14:02:28 +02:00
// 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
}
2024-04-24 08:44:48 +02:00
@@ -3405,7 +3413,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2021-06-11 14:02:28 +02:00
}
public boolean canChangeDimensions() {
2023-03-14 20:54:57 +01:00
- return !this.isPassenger() && !this.isVehicle();
2024-01-21 12:11:43 +01:00
+ return !this.isPassenger() && !this.isVehicle() && isAlive() && valid; // Paper - Fix item duplication and teleport issues
2021-06-11 14:02:28 +02:00
}
public float getBlockExplosionResistance(Explosion explosion, BlockGetter world, BlockPos pos, BlockState blockState, FluidState fluidState, float max) {
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-04-24 08:44:48 +02:00
index 10e337fc1fb23d9eaab54f92021a7d95a14468f8..8daaadbc524ee8d7c4a4ddd1d2a822f3b697abd0 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-04-24 08:44:48 +02:00
@@ -1734,9 +1734,9 @@ public abstract class LivingEntity extends Entity implements Attackable {
2021-12-08 19:25:57 +01:00
// Paper start
2022-06-08 06:39:43 +02:00
org.bukkit.event.entity.EntityDeathEvent deathEvent = this.dropAllDeathLoot(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-04-24 08:44:48 +02:00
@@ -1837,8 +1837,13 @@ public abstract class LivingEntity extends Entity implements Attackable {
2021-12-08 19:25:57 +01:00
this.dropCustomDeathLoot(source, i, 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
- 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
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-04-24 08:44:48 +02:00
index cb3729509e50fed64b17f16797825c1d21f7bf5b..bf717b5bcd14291a99414eae5366c0b91ce68a68 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-04-24 08:44:48 +02:00
@@ -628,7 +628,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-04-24 08:44:48 +02:00
@@ -636,7 +636,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-04-24 08:44:48 +02:00
index fae07957147ee22a91d5da925e834a0495021958..f39bfda4487114f5a0a720e328321485839dd790 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-04-24 08:44:48 +02:00
@@ -894,6 +894,11 @@ public class CraftEventFactory {
2021-12-08 19:25:57 +01:00
}
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
2024-04-24 08:44:48 +02:00
@@ -907,11 +912,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;