2019-02-23 17:24:23 +00:00
From e3b5379cd5d3c1a87616e9bec80bdb6b9dfbac86 Mon Sep 17 00:00:00 2001
2018-08-04 03:02:44 +00:00
From: Aikar <aikar@aikar.co>
Date: Fri, 3 Aug 2018 22:47:46 -0400
Subject: [PATCH] Entity add to world fixes
1) Chunk Registration might kill an entity, don't add it to the world if it did!
2) By default, entities are added to the world per slice iteration.
This opens risk of the slices being manipulated during chunk add if an
EntityAddToWorldEvent spawns an entity into this chunk.
Fix this by differing entity add to world for all entities at the same time
3) If a duplicate entity is attempted to add to the world of an entity, and
the original entity is dead, overwrite it as the logic does for unloaod queued entities.
diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
2019-02-23 17:24:23 +00:00
index 1c9e2cb1ce..d22e83f3c6 100644
2018-08-04 03:02:44 +00:00
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
2018-12-18 01:56:54 +00:00
@@ -2,6 +2,8 @@ package net.minecraft.server;
// Paper start
import com.destroystokyo.paper.PaperWorldConfig.DuplicateUUIDMode;
+
+import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;
// Paper end
2019-02-23 17:24:23 +00:00
@@ -954,15 +956,16 @@ public class Chunk implements IChunkAccess {
2018-12-17 05:18:06 +00:00
// Paper end
2019-01-01 03:15:55 +00:00
// CraftBukkit start
2018-12-17 05:18:06 +00:00
- List<Entity> toRemove = new LinkedList<>();
2018-08-26 18:11:49 +00:00
- this.world.a(entityslice.stream().filter((entity) -> {
2018-12-17 23:51:44 +00:00
- if (this.needsDecoration && !CraftEventFactory.doEntityAddEventCalling(this.world, entity, CreatureSpawnEvent.SpawnReason.CHUNK_GEN)) { // Only call for new chunks
2018-12-17 05:18:06 +00:00
- toRemove.add(entity);
- return false;
- }
2018-08-26 18:11:49 +00:00
- return !(entity instanceof EntityHuman);
- }));
2018-12-17 05:18:06 +00:00
- entityslice.removeAll(toRemove);
2018-12-18 01:56:54 +00:00
+ this.world.addChunkEntities(entityslice.stream() // Paper - add all at same time to avoid entities adding to world modifying slice state, skip already added entities (not normal, but can happen)
+ // Paper start - Inline event into stream
+ .filter((entity) -> {
+ if (!this.needsDecoration) {
+ return true;
+ }
+ return CraftEventFactory.doEntityAddEventCalling(this.world, entity, CreatureSpawnEvent.SpawnReason.CHUNK_GEN);
+ })
+ // Paper end - Inline event into stream
+ .filter((entity) -> !(entity instanceof EntityHuman || entity.valid))); // Paper - add all at same time to avoid entities adding to world modifying slice state, skip already added entities (not normal, but can happen)
2018-12-18 02:07:05 +00:00
// CraftBukkit end
2018-08-04 03:02:44 +00:00
}
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
2019-02-23 17:24:23 +00:00
index 74e1f48105..db130852f8 100644
2018-08-04 03:02:44 +00:00
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
2019-02-03 15:34:04 +00:00
@@ -1037,6 +1037,7 @@ public abstract class World implements IEntityAccess, GeneratorAccess, IIBlockAc
2018-08-04 03:02:44 +00:00
}
this.getChunkAt(i, j).a(entity);
+ if (entity.dead) return false; // Paper - don't add dead entities, chunk registration may of killed it
this.entityList.add(entity);
this.b(entity);
return true;
2019-02-03 15:34:04 +00:00
@@ -2442,9 +2443,13 @@ public abstract class World implements IEntityAccess, GeneratorAccess, IIBlockAc
2018-08-26 18:11:49 +00:00
return j;
2018-08-04 03:02:44 +00:00
}
2018-08-26 18:11:49 +00:00
+ public void addChunkEntities(Stream<Entity> collection) { a(collection); } // Paper - OBFHELPER
public void a(Stream<Entity> stream) {
2018-08-04 03:02:44 +00:00
org.spigotmc.AsyncCatcher.catchOp( "entity world add"); // Spigot
2018-08-26 18:11:49 +00:00
stream.forEach((entity) -> {
2018-08-04 03:02:44 +00:00
+ if (entity == null || entity.dead || entity.valid) { // Paper - prevent adding already added or dead entities
2018-08-26 18:11:49 +00:00
+ return;
+ }
2018-08-04 03:02:44 +00:00
this.entityList.add(entity);
2018-08-26 18:11:49 +00:00
this.b(entity);
});
2018-08-04 03:02:44 +00:00
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
2019-02-23 17:24:23 +00:00
index a896f8fe46..609be457ab 100644
2018-08-04 03:02:44 +00:00
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
2019-01-06 17:15:21 +00:00
@@ -968,7 +968,7 @@ public class WorldServer extends World implements IAsyncTaskHandler {
2018-08-04 03:02:44 +00:00
if (this.entitiesByUUID.containsKey(uuid)) {
Entity entity1 = (Entity) this.entitiesByUUID.get(uuid);
2018-08-04 04:33:21 +00:00
- if (this.g.contains(entity1)) {
+ if (this.g.contains(entity1) || entity1.dead) { // Paper - if dupe is dead, overwrite
this.g.remove(entity1);
2018-08-04 03:02:44 +00:00
} else {
if (!(entity instanceof EntityHuman)) {
--
2019-01-01 03:15:55 +00:00
2.20.1
2018-08-04 03:02:44 +00:00