Fix item duplication 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.
This commit is contained in:
Aikar
2020-04-25 06:46:35 -04:00
parent 8a71e1c7da
commit 4b1f23c2e8
4 changed files with 133 additions and 102 deletions

View File

@@ -141,9 +141,9 @@
+ }
+ // Paper end - Share random for entities to make them more random
+ public org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason spawnReason; // Paper - Entity#getEntitySpawnReason
+ private CraftEntity bukkitEntity;
+
+ private CraftEntity bukkitEntity;
+ public CraftEntity getBukkitEntity() {
+ if (this.bukkitEntity == null) {
+ this.bukkitEntity = CraftEntity.getEntity(this.level.getCraftServer(), this);
@@ -538,17 +538,15 @@
if (!this.level().isClientSide() || this.isControlledByLocalInstance()) {
Entity.MovementEmission entity_movementemission = this.getMovementEmission();
@@ -1131,8 +1455,22 @@
protected SoundEvent getSwimHighSpeedSplashSound() {
@@ -1133,6 +1457,20 @@
return SoundEvents.GENERIC_SPLASH;
+ }
+
}
+ // CraftBukkit start - Add delegate methods
+ public SoundEvent getSwimSound0() {
+ return this.getSwimSound();
}
+ }
+
+ public SoundEvent getSwimSplashSound0() {
+ return this.getSwimSplashSound();
+ }
@@ -891,17 +889,19 @@
protected abstract void readAdditionalSaveData(CompoundTag nbt);
protected abstract void addAdditionalSaveData(CompoundTag nbt);
@@ -2153,9 +2654,22 @@
@@ -2153,9 +2654,23 @@
if (stack.isEmpty()) {
return null;
} else {
- ItemEntity entityitem = new ItemEntity(world, this.getX(), this.getY() + (double) yOffset, this.getZ(), stack);
+ // 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(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
+ stack.setCount(0); // Paper - destroy this item - if this ever leaks due to game bugs, ensure it doesn't dupe
entityitem.setDefaultPickUpDelay();
+ // CraftBukkit start
@@ -914,7 +914,7 @@
world.addFreshEntity(entityitem);
return entityitem;
}
@@ -2184,6 +2698,12 @@
@@ -2184,6 +2699,12 @@
if (this.isAlive() && this instanceof Leashable leashable) {
if (leashable.getLeashHolder() == player) {
if (!this.level().isClientSide()) {
@@ -927,7 +927,7 @@
if (player.hasInfiniteMaterials()) {
leashable.removeLeash();
} else {
@@ -2200,6 +2720,13 @@
@@ -2200,6 +2721,13 @@
if (itemstack.is(Items.LEAD) && leashable.canHaveALeashAttachedToIt()) {
if (!this.level().isClientSide()) {
@@ -941,7 +941,7 @@
leashable.setLeashedTo(player, true);
}
@@ -2265,7 +2792,7 @@
@@ -2265,7 +2793,7 @@
}
public boolean showVehicleHealth() {
@@ -950,7 +950,7 @@
}
public boolean startRiding(Entity entity, boolean force) {
@@ -2273,7 +2800,7 @@
@@ -2273,7 +2801,7 @@
return false;
} else if (!entity.couldAcceptPassenger()) {
return false;
@@ -959,7 +959,7 @@
return false;
} else {
for (Entity entity1 = entity; entity1.vehicle != null; entity1 = entity1.vehicle) {
@@ -2285,11 +2812,32 @@
@@ -2285,11 +2813,32 @@
if (!force && (!this.canRide(entity) || !entity.canAddPassenger(this))) {
return false;
} else {
@@ -993,7 +993,7 @@
this.vehicle = entity;
this.vehicle.addPassenger(this);
entity.getIndirectPassengersStream().filter((entity2) -> {
@@ -2314,19 +2862,30 @@
@@ -2314,19 +2863,30 @@
}
public void removeVehicle() {
@@ -1026,7 +1026,7 @@
protected void addPassenger(Entity passenger) {
if (passenger.getVehicle() != this) {
throw new IllegalStateException("Use x.startRiding(y), not y.addPassenger(x)");
@@ -2349,21 +2908,53 @@
@@ -2349,21 +2909,53 @@
}
}
@@ -1086,7 +1086,7 @@
}
protected boolean canAddPassenger(Entity passenger) {
@@ -2464,7 +3055,7 @@
@@ -2464,7 +3056,7 @@
if (teleporttransition != null) {
ServerLevel worldserver1 = teleporttransition.newLevel();
@@ -1095,7 +1095,7 @@
this.teleport(teleporttransition);
}
}
@@ -2547,7 +3138,7 @@
@@ -2547,7 +3139,7 @@
}
public boolean isCrouching() {
@@ -1104,7 +1104,7 @@
}
public boolean isSprinting() {
@@ -2563,7 +3154,7 @@
@@ -2563,7 +3155,7 @@
}
public boolean isVisuallySwimming() {
@@ -1113,7 +1113,7 @@
}
public boolean isVisuallyCrawling() {
@@ -2571,6 +3162,13 @@
@@ -2571,6 +3163,13 @@
}
public void setSwimming(boolean swimming) {
@@ -1127,7 +1127,7 @@
this.setSharedFlag(4, swimming);
}
@@ -2609,6 +3207,7 @@
@@ -2609,6 +3208,7 @@
@Nullable
public PlayerTeam getTeam() {
@@ -1135,7 +1135,7 @@
return this.level().getScoreboard().getPlayersTeam(this.getScoreboardName());
}
@@ -2624,8 +3223,12 @@
@@ -2624,8 +3224,12 @@
return this.getTeam() != null ? this.getTeam().isAlliedTo(team) : false;
}
@@ -1149,7 +1149,7 @@
}
public boolean getSharedFlag(int index) {
@@ -2644,7 +3247,7 @@
@@ -2644,7 +3248,7 @@
}
public int getMaxAirSupply() {
@@ -1158,7 +1158,7 @@
}
public int getAirSupply() {
@@ -2652,7 +3255,18 @@
@@ -2652,7 +3256,18 @@
}
public void setAirSupply(int air) {
@@ -1178,7 +1178,7 @@
}
public int getTicksFrozen() {
@@ -2679,11 +3293,40 @@
@@ -2679,11 +3294,40 @@
public void thunderHit(ServerLevel world, LightningBolt lightning) {
this.setRemainingFireTicks(this.remainingFireTicks + 1);
@@ -1221,7 +1221,7 @@
}
public void onAboveBubbleCol(boolean drag) {
@@ -2713,7 +3356,7 @@
@@ -2713,7 +3357,7 @@
this.resetFallDistance();
}
@@ -1230,7 +1230,7 @@
return true;
}
@@ -2818,7 +3461,7 @@
@@ -2818,7 +3462,7 @@
public String toString() {
String s = this.level() == null ? "~NULL~" : this.level().toString();
@@ -1239,8 +1239,16 @@
}
public final boolean isInvulnerableToBase(DamageSource damageSource) {
@@ -2852,6 +3495,26 @@
@@ -2850,8 +3494,34 @@
public Entity teleport(TeleportTransition teleportTarget) {
Level world = this.level();
+ // Paper start - Fix item duplication and teleport issues
+ if ((!this.isAlive() || !this.valid) && (teleportTarget.newLevel() != world)) {
+ LOGGER.warn("Illegal Entity Teleport " + this + " to " + teleportTarget.newLevel() + ":" + teleportTarget.position(), new Throwable());
+ return null;
+ }
+ // Paper end - Fix item duplication and teleport issues
if (world instanceof ServerLevel worldserver) {
if (!this.isRemoved()) {
+ // CraftBukkit start
@@ -1266,8 +1274,15 @@
ServerLevel worldserver1 = teleportTarget.newLevel();
boolean flag = worldserver1.dimension() != worldserver.dimension();
@@ -2920,8 +3583,12 @@
@@ -2918,10 +3588,19 @@
gameprofilerfiller.pop();
return null;
} else {
+ // Paper start - Fix item duplication and teleport issues
+ if (this instanceof Leashable leashable) {
+ leashable.dropLeash(); // 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
@@ -1280,7 +1295,7 @@
Iterator iterator1 = list1.iterator();
while (iterator1.hasNext()) {
@@ -2947,7 +3614,7 @@
@@ -2947,7 +3626,7 @@
}
private void sendTeleportTransitionToRidingPlayers(TeleportTransition teleportTarget) {
@@ -1289,7 +1304,7 @@
Iterator iterator = this.getIndirectPassengers().iterator();
while (iterator.hasNext()) {
@@ -2995,8 +3662,9 @@
@@ -2995,8 +3674,9 @@
}
protected void removeAfterChangingDimensions() {
@@ -1300,7 +1315,7 @@
leashable.removeLeash();
}
@@ -3006,6 +3674,20 @@
@@ -3006,11 +3686,26 @@
return PortalShape.getRelativePosition(portalRect, portalAxis, this.position(), this.getDimensions(this.getPose()));
}
@@ -1321,7 +1336,13 @@
public boolean canUsePortal(boolean allowVehicles) {
return (allowVehicles || !this.isPassenger()) && this.isAlive();
}
@@ -3134,10 +3816,16 @@
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();
@@ -3134,10 +3829,16 @@
return (Boolean) this.entityData.get(Entity.DATA_CUSTOM_NAME_VISIBLE);
}
@@ -1341,7 +1362,7 @@
return entity != null;
}
@@ -3187,7 +3875,7 @@
@@ -3187,7 +3888,7 @@
/** @deprecated */
@Deprecated
protected void fixupDimensions() {
@@ -1350,7 +1371,7 @@
EntityDimensions entitysize = this.getDimensions(entitypose);
this.dimensions = entitysize;
@@ -3196,7 +3884,7 @@
@@ -3196,7 +3897,7 @@
public void refreshDimensions() {
EntityDimensions entitysize = this.dimensions;
@@ -1359,7 +1380,7 @@
EntityDimensions entitysize1 = this.getDimensions(entitypose);
this.dimensions = entitysize1;
@@ -3258,10 +3946,29 @@
@@ -3258,10 +3959,29 @@
}
public final void setBoundingBox(AABB boundingBox) {
@@ -1391,7 +1412,7 @@
return this.getDimensions(pose).eyeHeight();
}
@@ -3335,7 +4042,7 @@
@@ -3335,7 +4055,7 @@
}
@Nullable
@@ -1400,7 +1421,7 @@
return null;
}
@@ -3435,7 +4142,7 @@
@@ -3435,7 +4155,7 @@
}
public boolean isControlledByLocalInstance() {
@@ -1409,7 +1430,7 @@
if (entityliving instanceof Player entityhuman) {
return entityhuman.isLocalPlayer();
@@ -3445,7 +4152,7 @@
@@ -3445,7 +4165,7 @@
}
public boolean isControlledByClient() {
@@ -1418,7 +1439,7 @@
return entityliving != null && entityliving.isControlledByClient();
}
@@ -3463,7 +4170,7 @@
@@ -3463,7 +4183,7 @@
return new Vec3((double) f1 * d2 / (double) f3, 0.0D, (double) f2 * d2 / (double) f3);
}
@@ -1427,7 +1448,7 @@
return new Vec3(this.getX(), this.getBoundingBox().maxY, this.getZ());
}
@@ -3488,9 +4195,38 @@
@@ -3488,9 +4208,38 @@
public int getFireImmuneTicks() {
return 1;
}
@@ -1467,7 +1488,7 @@
}
public void lookAt(EntityAnchorArgument.Anchor anchorPoint, Vec3 target) {
@@ -3551,6 +4287,11 @@
@@ -3551,6 +4300,11 @@
vec3d = vec3d.add(vec3d1);
++k1;
}
@@ -1479,7 +1500,7 @@
}
}
}
@@ -3613,7 +4354,7 @@
@@ -3613,7 +4367,7 @@
return new ClientboundAddEntityPacket(this, entityTrackerEntry);
}
@@ -1488,7 +1509,7 @@
return this.type.getDimensions();
}
@@ -3818,8 +4559,16 @@
@@ -3818,8 +4572,16 @@
@Override
public final void setRemoved(Entity.RemovalReason reason) {
@@ -1506,7 +1527,7 @@
}
if (this.removalReason.shouldDestroy()) {
@@ -3827,8 +4576,8 @@
@@ -3827,8 +4589,8 @@
}
this.getPassengers().forEach(Entity::stopRiding);
@@ -1517,7 +1538,7 @@
}
public void unsetRemoved() {
@@ -3887,7 +4636,7 @@
@@ -3887,7 +4649,7 @@
}
public Vec3 getKnownMovement() {