PaperMC/paper-server/nms-patches/net/minecraft/world/level/entity/PersistentEntitySectionManager.patch
CraftBukkit/Spigot 9e3a0e859f SPIGOT-6772: Use entity mailbox and re-schedule entities if they get unloaded
By: DerFrZocker <derrieple@gmail.com>
2021-11-01 18:54:44 +11:00

182 lines
7.8 KiB
Diff

--- a/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
+++ b/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
@@ -32,12 +32,17 @@
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();
final Set<UUID> knownUuids = Sets.newHashSet();
final LevelCallback<T> callbacks;
- private final EntityPersistentStorage<T> permanentStorage;
+ public final EntityPersistentStorage<T> permanentStorage; // PAIL private -> public
private final EntityLookup<T> visibleEntityStorage = new EntityLookup<>();
final EntitySectionStorage<T> sectionStorage;
private final LevelEntityGetter<T> entityGetter;
@@ -55,6 +60,16 @@
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(entity -> (Entity) entity).collect(Collectors.toList());
+ }
+
+ public boolean isPending(long pair) {
+ return chunkLoadStatuses.get(pair) == b.PENDING;
+ }
+ // CraftBukkit end
+
void a(long i, EntitySection<T> entitysection) {
if (entitysection.a()) {
this.sectionStorage.e(i);
@@ -82,7 +97,7 @@
long i = SectionPosition.c(t0.getChunkCoordinates());
EntitySection<T> entitysection = this.sectionStorage.c(i);
- entitysection.a((Object) t0);
+ entitysection.a(t0); // CraftBukkit - decompile error
t0.a(new PersistentEntitySectionManager.a(t0, i, entitysection));
if (!flag) {
this.callbacks.f(t0);
@@ -186,7 +201,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 +211,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 +228,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 +237,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 +261,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 +277,23 @@
}
private void f() {
- this.chunksToUnload.removeIf((i) -> {
+ this.chunksToUnload.removeIf((java.util.function.LongPredicate) (i) -> { // CraftBukkit - decompile error
return this.chunkVisibility.get(i) != Visibility.HIDDEN ? true : this.d(i);
});
}
private void g() {
- ChunkEntities chunkentities;
+ ChunkEntities<T> chunkentities; // CraftBukkit - decompile error
while ((chunkentities = (ChunkEntities) this.loadingInbox.poll()) != null) {
chunkentities.b().forEach((entityaccess) -> {
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 +319,7 @@
}
public void b() {
- this.h().forEach((i) -> {
+ this.h().forEach((java.util.function.LongConsumer) (i) -> { // CraftBukkit - decompile error
boolean flag = this.chunkVisibility.get(i) == Visibility.HIDDEN;
if (flag) {
@@ -311,7 +338,7 @@
while (!longset.isEmpty()) {
this.permanentStorage.a(false);
this.g();
- longset.removeIf((i) -> {
+ longset.removeIf((java.util.function.LongPredicate) (i) -> { // CraftBukkit - decompile error
boolean flag = this.chunkVisibility.get(i) == Visibility.HIDDEN;
return flag ? this.d(i) : this.a(i, (entityaccess) -> {
@@ -323,7 +350,15 @@
}
public void close() throws IOException {
- this.c();
+ // CraftBukkit start - add save boolean
+ close(true);
+ }
+
+ public void close(boolean save) throws IOException {
+ if (save) {
+ this.c(); // PAIL rename saveAll
+ }
+ // CraftBukkit end
this.permanentStorage.close();
}
@@ -350,7 +385,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);
- this.sectionStorage.a().forEach((i) -> {
+ this.sectionStorage.a().forEach((java.util.function.LongConsumer) (i) -> { // CraftBukkit - decompile error
PersistentEntitySectionManager.b persistententitysectionmanager_b = (PersistentEntitySectionManager.b) this.chunkLoadStatuses.get(i);
this.sectionStorage.a(i).forEach((j) -> {
@@ -389,7 +424,7 @@
private EntitySection<T> currentSection;
a(EntityAccess entityaccess, long i, EntitySection entitysection) {
- this.entity = entityaccess;
+ this.entity = (T) entityaccess; // CraftBukkit - decompile error
this.currentSectionKey = i;
this.currentSection = entitysection;
}
@@ -409,7 +444,7 @@
PersistentEntitySectionManager.this.a(this.currentSectionKey, this.currentSection);
EntitySection<T> entitysection = PersistentEntitySectionManager.this.sectionStorage.c(i);
- entitysection.a((Object) this.entity);
+ entitysection.a(this.entity); // CraftBukkit - decompile error
this.currentSection = entitysection;
this.currentSectionKey = i;
this.a(visibility, entitysection.c());