2021-06-11 12:02:28 +00:00
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
2022-10-07 18:43:26 +00:00
index cbf5995e7fdea520080501368d968e7f9ad749c8..7778f8b1e439e47bbe7658239f1727083fc13342 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
2022-10-07 18:43:26 +00:00
@@ -2293,11 +2293,12 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
2021-06-11 12:02:28 +00: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
- 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
2022-10-07 18:43:26 +00:00
@@ -3061,6 +3062,12 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
2021-06-11 12:02:28 +00:00
@Nullable
2022-07-04 14:38:06 +00:00
public Entity teleportTo(ServerLevel worldserver, PositionImpl location) {
2021-06-11 12:02:28 +00:00
// 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
2021-06-14 01:06:38 +00:00
if (this.level instanceof ServerLevel && !this.isRemoved()) {
2021-06-11 12:02:28 +00:00
this.level.getProfiler().push("changeDimension");
// CraftBukkit start
2022-10-07 18:43:26 +00:00
@@ -3087,6 +3094,11 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
2021-06-11 12:02:28 +00:00
// 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
2021-11-24 11:38:00 +00:00
Entity entity = this.getType().create(worldserver);
2021-06-11 12:02:28 +00:00
if (entity != null) {
2022-10-07 18:43:26 +00:00
@@ -3100,10 +3112,6 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
2021-06-11 12:02:28 +00: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
}
2022-10-07 18:43:26 +00:00
@@ -3224,7 +3232,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
2021-06-11 12:02:28 +00:00
}
public boolean canChangeDimensions() {
- return true;
+ return isAlive() && valid; // Paper
}
public float getBlockExplosionResistance(Explosion explosion, BlockGetter world, BlockPos pos, BlockState blockState, FluidState fluidState, float max) {
2021-12-08 18:25:57 +00:00
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
2022-10-02 07:56:36 +00:00
index d6071df933506bf3d55fc827483b33ecc5174834..03194c5acd46f6014f0a1d964d079b114f86c951 100644
2021-12-08 18:25:57 +00:00
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
2022-10-02 07:56:36 +00:00
@@ -1646,9 +1646,9 @@ public abstract class LivingEntity extends Entity {
2021-12-08 18:25:57 +00:00
// Paper start
2022-06-08 04:39:43 +00:00
org.bukkit.event.entity.EntityDeathEvent deathEvent = this.dropAllDeathLoot(damageSource);
2021-12-08 18:25:57 +00:00
if (deathEvent == null || !deathEvent.isCancelled()) {
- if (this.deathScore >= 0 && entityliving != null) {
2022-06-08 04:39:43 +00:00
- entityliving.awardKillScore(this, this.deathScore, damageSource);
2021-12-08 18:25:57 +00:00
- }
+ // if (this.deathScore >= 0 && entityliving != null) { // Paper moved to be run earlier in #dropAllDeathLoot before destroying the drop items in CraftEventFactory#callEntityDeathEvent
2022-06-08 04:39:43 +00:00
+ // entityliving.awardKillScore(this, this.deathScore, damageSource);
2021-12-08 18:25:57 +00:00
+ // }
// Paper start - clear equipment if event is not cancelled
2022-02-18 18:16:41 +00:00
if (this instanceof Mob) {
for (EquipmentSlot slot : this.clearedEquipmentSlots) {
2022-10-02 07:56:36 +00:00
@@ -1746,8 +1746,13 @@ public abstract class LivingEntity extends Entity {
2021-12-08 18:25:57 +00:00
this.dropCustomDeathLoot(source, i, flag);
2022-02-18 18:16:41 +00:00
this.clearEquipmentSlots = prev; // Paper
2021-12-08 18:25:57 +00: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 12:02:28 +00: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
2022-10-02 07:56:36 +00:00
index e463ae13ce6f65675c2b6d553ecf91db5a047dbc..7ff1e7e4d493770bfdbc0ad5e8f10387cefc42d2 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
+++ b/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
2022-10-02 07:56:36 +00:00
@@ -609,7 +609,7 @@ public class ArmorStand extends LivingEntity {
2021-06-11 12:02:28 +00:00
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);
}
}
2022-10-02 07:56:36 +00:00
@@ -617,7 +617,7 @@ public class ArmorStand extends LivingEntity {
2021-06-11 12:02:28 +00:00
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
2022-10-02 07:56:36 +00:00
index 118d7068700bbd4aeaf766ee8d3984b474c6bf9a..751f376e175881d9e9f750c90143a7c39431ce6c 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2022-10-02 07:56:36 +00:00
@@ -809,6 +809,11 @@ public class CraftEventFactory {
2021-12-08 18:25:57 +00: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
2022-10-02 07:56:36 +00:00
@@ -822,11 +827,13 @@ public class CraftEventFactory {
2021-12-08 18:25:57 +00:00
playDeathSound(victim, event);
// Paper end
victim.expToDrop = event.getDroppedExp();
+ lootCheck.run(); // Paper - advancement triggers before destroying items
2021-06-11 12:02:28 +00: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;