SPIGOT-6547: Chunk#getEntities() doesn't return all entities immediately after chunk load

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2021-09-01 18:55:18 +10:00
parent 768b5d52aa
commit 5c075d59dc
4 changed files with 165 additions and 14 deletions

View File

@@ -1,6 +1,31 @@
--- a/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
+++ b/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
@@ -82,7 +82,7 @@
@@ -32,6 +32,11 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+// CraftBukkit start
+import net.minecraft.world.level.chunk.storage.EntityStorage;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+// CraftBukkit end
+
public class PersistentEntitySectionManager<T extends EntityAccess> implements AutoCloseable {
static final Logger LOGGER = LogManager.getLogger();
@@ -55,6 +60,12 @@
this.entityGetter = new LevelEntityGetterAdapter<>(this.visibleEntityStorage, this.sectionStorage);
}
+ // CraftBukkit start - add method to get all entities in chunk
+ public List<Entity> getEntities(ChunkCoordIntPair chunkCoordIntPair) {
+ return sectionStorage.b(chunkCoordIntPair.pair()).flatMap(EntitySection::b).map(entiy -> (Entity) entiy).collect(Collectors.toList());
+ }
+ // CraftBukkit end
+
void a(long i, EntitySection<T> entitysection) {
if (entitysection.a()) {
this.sectionStorage.e(i);
@@ -82,7 +93,7 @@
long i = SectionPosition.c(t0.getChunkCoordinates());
EntitySection<T> entitysection = this.sectionStorage.c(i);
@@ -9,7 +34,54 @@
t0.a(new PersistentEntitySectionManager.a(t0, i, entitysection));
if (!flag) {
this.callbacks.f(t0);
@@ -254,13 +254,13 @@
@@ -186,7 +197,7 @@
});
}
- private void b(long i) {
+ public void b(long i) { // PAIL private -> public, rename scheduleEntityLoading
PersistentEntitySectionManager.b persistententitysectionmanager_b = (PersistentEntitySectionManager.b) this.chunkLoadStatuses.get(i);
if (persistententitysectionmanager_b == PersistentEntitySectionManager.b.FRESH) {
@@ -196,6 +207,12 @@
}
private boolean a(long i, Consumer<T> consumer) {
+ // CraftBukkit start - add boolean for event call
+ return a(i, consumer, false);
+ }
+
+ private boolean a(long i, Consumer<T> consumer, boolean callEvent) {
+ // CraftBukkit end
PersistentEntitySectionManager.b persistententitysectionmanager_b = (PersistentEntitySectionManager.b) this.chunkLoadStatuses.get(i);
if (persistententitysectionmanager_b == PersistentEntitySectionManager.b.PENDING) {
@@ -207,6 +224,7 @@
if (list.isEmpty()) {
if (persistententitysectionmanager_b == PersistentEntitySectionManager.b.LOADED) {
+ if (callEvent) CraftEventFactory.callEntitiesUnloadEvent(((EntityStorage) permanentStorage).level, new ChunkCoordIntPair(i), ImmutableList.of()); // CraftBukkit
this.permanentStorage.a(new ChunkEntities<>(new ChunkCoordIntPair(i), ImmutableList.of()));
}
@@ -215,6 +233,7 @@
this.c(i);
return false;
} else {
+ if (callEvent) CraftEventFactory.callEntitiesUnloadEvent(((EntityStorage) permanentStorage).level, new ChunkCoordIntPair(i), list.stream().map(entity -> (Entity) entity).collect(Collectors.toList())); // CraftBukkit
this.permanentStorage.a(new ChunkEntities<>(new ChunkCoordIntPair(i), list));
list.forEach(consumer);
return true;
@@ -238,7 +257,7 @@
private boolean d(long i) {
boolean flag = this.a(i, (entityaccess) -> {
entityaccess.cD().forEach(this::g);
- });
+ }, true); // CraftBukkit - add boolean for event call
if (!flag) {
return false;
@@ -254,19 +273,23 @@
}
private void f() {
@@ -25,7 +97,17 @@
while ((chunkentities = (ChunkEntities) this.loadingInbox.poll()) != null) {
chunkentities.b().forEach((entityaccess) -> {
@@ -292,7 +292,7 @@
this.a(entityaccess, true);
});
this.chunkLoadStatuses.put(chunkentities.a().pair(), PersistentEntitySectionManager.b.LOADED);
+ // CraftBukkit start - call entity load event
+ List<Entity> entities = getEntities(chunkentities.a()); // PAIL rename getChunkPos
+ CraftEventFactory.callEntitiesLoadEvent(((EntityStorage) permanentStorage).level, chunkentities.a(), entities);
+ // CraftBukkit end
}
}
@@ -292,7 +315,7 @@
}
public void b() {
@@ -34,7 +116,7 @@
boolean flag = this.chunkVisibility.get(i) == Visibility.HIDDEN;
if (flag) {
@@ -311,7 +311,7 @@
@@ -311,7 +334,7 @@
while (!longset.isEmpty()) {
this.permanentStorage.a(false);
this.g();
@@ -43,7 +125,7 @@
boolean flag = this.chunkVisibility.get(i) == Visibility.HIDDEN;
return flag ? this.d(i) : this.a(i, (entityaccess) -> {
@@ -323,7 +323,15 @@
@@ -323,7 +346,15 @@
}
public void close() throws IOException {
@@ -60,7 +142,7 @@
this.permanentStorage.close();
}
@@ -350,7 +358,7 @@
@@ -350,7 +381,7 @@
public void a(Writer writer) throws IOException {
CSVWriter csvwriter = CSVWriter.a().a("x").a("y").a("z").a("visibility").a("load_status").a("entity_count").a(writer);
@@ -69,7 +151,7 @@
PersistentEntitySectionManager.b persistententitysectionmanager_b = (PersistentEntitySectionManager.b) this.chunkLoadStatuses.get(i);
this.sectionStorage.a(i).forEach((j) -> {
@@ -389,7 +397,7 @@
@@ -389,7 +420,7 @@
private EntitySection<T> currentSection;
a(EntityAccess entityaccess, long i, EntitySection entitysection) {
@@ -78,7 +160,7 @@
this.currentSectionKey = i;
this.currentSection = entitysection;
}
@@ -409,7 +417,7 @@
@@ -409,7 +440,7 @@
PersistentEntitySectionManager.this.a(this.currentSectionKey, this.currentSection);
EntitySection<T> entitysection = PersistentEntitySectionManager.this.sectionStorage.c(i);