2021-08-27 11:14:09 +02:00
--- a/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
+++ b/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
2021-11-21 23:00:00 +01:00
@@ -32,6 +32,11 @@
2022-02-28 16:00:00 +01:00
import net.minecraft.world.level.ChunkCoordIntPair;
import org.slf4j.Logger;
2021-09-01 10:55:18 +02:00
+// 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 {
2022-02-28 16:00:00 +01:00
static final Logger LOGGER = LogUtils.getLogger();
2021-11-01 08:54:44 +01:00
@@ -55,6 +60,16 @@
2021-09-01 10:55:18 +02:00
this.entityGetter = new LevelEntityGetterAdapter<>(this.visibleEntityStorage, this.sectionStorage);
}
+ // CraftBukkit start - add method to get all entities in chunk
+ public List<Entity> getEntities(ChunkCoordIntPair chunkCoordIntPair) {
2021-11-21 23:00:00 +01:00
+ return sectionStorage.getExistingSectionsInChunk(chunkCoordIntPair.toLong()).flatMap(EntitySection::getEntities).map(entity -> (Entity) entity).collect(Collectors.toList());
2021-11-01 08:54:44 +01:00
+ }
+
+ public boolean isPending(long pair) {
+ return chunkLoadStatuses.get(pair) == b.PENDING;
2021-09-01 10:55:18 +02:00
+ }
+ // CraftBukkit end
+
2021-11-21 23:00:00 +01:00
void removeSectionIfEmpty(long i, EntitySection<T> entitysection) {
if (entitysection.isEmpty()) {
this.sectionStorage.remove(i);
2021-11-01 08:54:44 +01:00
@@ -196,6 +211,12 @@
2021-09-01 10:55:18 +02:00
}
2021-11-21 23:00:00 +01:00
private boolean storeChunkSections(long i, Consumer<T> consumer) {
2021-09-01 10:55:18 +02:00
+ // CraftBukkit start - add boolean for event call
2021-11-21 23:00:00 +01:00
+ return storeChunkSections(i, consumer, false);
2021-09-01 10:55:18 +02:00
+ }
+
2021-11-21 23:00:00 +01:00
+ private boolean storeChunkSections(long i, Consumer<T> consumer, boolean callEvent) {
2021-09-01 10:55:18 +02:00
+ // CraftBukkit end
PersistentEntitySectionManager.b persistententitysectionmanager_b = (PersistentEntitySectionManager.b) this.chunkLoadStatuses.get(i);
if (persistententitysectionmanager_b == PersistentEntitySectionManager.b.PENDING) {
2021-11-01 08:54:44 +01:00
@@ -207,6 +228,7 @@
2021-09-01 10:55:18 +02:00
if (list.isEmpty()) {
if (persistententitysectionmanager_b == PersistentEntitySectionManager.b.LOADED) {
+ if (callEvent) CraftEventFactory.callEntitiesUnloadEvent(((EntityStorage) permanentStorage).level, new ChunkCoordIntPair(i), ImmutableList.of()); // CraftBukkit
2021-11-21 23:00:00 +01:00
this.permanentStorage.storeEntities(new ChunkEntities<>(new ChunkCoordIntPair(i), ImmutableList.of()));
2021-09-01 10:55:18 +02:00
}
2021-11-01 08:54:44 +01:00
@@ -215,6 +237,7 @@
2021-11-21 23:00:00 +01:00
this.requestChunkLoad(i);
2021-09-01 10:55:18 +02:00
return false;
} else {
+ if (callEvent) CraftEventFactory.callEntitiesUnloadEvent(((EntityStorage) permanentStorage).level, new ChunkCoordIntPair(i), list.stream().map(entity -> (Entity) entity).collect(Collectors.toList())); // CraftBukkit
2021-11-21 23:00:00 +01:00
this.permanentStorage.storeEntities(new ChunkEntities<>(new ChunkCoordIntPair(i), list));
2021-09-01 10:55:18 +02:00
list.forEach(consumer);
return true;
2021-11-01 08:54:44 +01:00
@@ -238,7 +261,7 @@
2021-11-21 23:00:00 +01:00
private boolean processChunkUnload(long i) {
boolean flag = this.storeChunkSections(i, (entityaccess) -> {
entityaccess.getPassengersAndSelf().forEach(this::unloadEntity);
2021-09-01 10:55:18 +02:00
- });
+ }, true); // CraftBukkit - add boolean for event call
if (!flag) {
return false;
2021-11-01 08:54:44 +01:00
@@ -254,19 +277,23 @@
2021-08-27 11:14:09 +02:00
}
2021-11-21 23:00:00 +01:00
private void processUnloads() {
2021-08-27 11:14:09 +02:00
- this.chunksToUnload.removeIf((i) -> {
+ this.chunksToUnload.removeIf((java.util.function.LongPredicate) (i) -> { // CraftBukkit - decompile error
2021-11-21 23:00:00 +01:00
return this.chunkVisibility.get(i) != Visibility.HIDDEN ? true : this.processChunkUnload(i);
2021-08-27 11:14:09 +02:00
});
}
2021-11-21 23:00:00 +01:00
private void processPendingLoads() {
2021-08-27 11:14:09 +02:00
- ChunkEntities chunkentities;
+ ChunkEntities<T> chunkentities; // CraftBukkit - decompile error
while ((chunkentities = (ChunkEntities) this.loadingInbox.poll()) != null) {
2021-11-21 23:00:00 +01:00
chunkentities.getEntities().forEach((entityaccess) -> {
this.addEntity(entityaccess, true);
2021-09-01 10:55:18 +02:00
});
2021-11-21 23:00:00 +01:00
this.chunkLoadStatuses.put(chunkentities.getPos().toLong(), PersistentEntitySectionManager.b.LOADED);
2021-09-01 10:55:18 +02:00
+ // CraftBukkit start - call entity load event
2021-11-21 23:00:00 +01:00
+ List<Entity> entities = getEntities(chunkentities.getPos());
+ CraftEventFactory.callEntitiesLoadEvent(((EntityStorage) permanentStorage).level, chunkentities.getPos(), entities);
2021-09-01 10:55:18 +02:00
+ // CraftBukkit end
}
}
2021-11-01 08:54:44 +01:00
@@ -292,7 +319,7 @@
2021-08-27 11:14:09 +02:00
}
2021-11-21 23:00:00 +01:00
public void autoSave() {
- this.getAllChunksToSave().forEach((i) -> {
+ this.getAllChunksToSave().forEach((java.util.function.LongConsumer) (i) -> { // CraftBukkit - decompile error
2021-08-27 11:14:09 +02:00
boolean flag = this.chunkVisibility.get(i) == Visibility.HIDDEN;
if (flag) {
2021-11-01 08:54:44 +01:00
@@ -311,7 +338,7 @@
2021-08-27 11:14:09 +02:00
while (!longset.isEmpty()) {
2021-11-21 23:00:00 +01:00
this.permanentStorage.flush(false);
this.processPendingLoads();
2021-08-27 11:14:09 +02:00
- longset.removeIf((i) -> {
+ longset.removeIf((java.util.function.LongPredicate) (i) -> { // CraftBukkit - decompile error
boolean flag = this.chunkVisibility.get(i) == Visibility.HIDDEN;
2021-11-21 23:00:00 +01:00
return flag ? this.processChunkUnload(i) : this.storeChunkSections(i, (entityaccess) -> {
2021-11-01 08:54:44 +01:00
@@ -323,7 +350,15 @@
2021-08-27 11:14:09 +02:00
}
public void close() throws IOException {
2021-11-21 23:00:00 +01:00
- this.saveAll();
2021-08-27 11:14:09 +02:00
+ // CraftBukkit start - add save boolean
+ close(true);
+ }
+
+ public void close(boolean save) throws IOException {
+ if (save) {
2021-11-21 23:00:00 +01:00
+ this.saveAll();
2021-08-27 11:14:09 +02:00
+ }
+ // CraftBukkit end
this.permanentStorage.close();
}
2021-11-01 08:54:44 +01:00
@@ -350,7 +385,7 @@
2021-11-21 23:00:00 +01:00
public void dumpSections(Writer writer) throws IOException {
CSVWriter csvwriter = CSVWriter.builder().addColumn("x").addColumn("y").addColumn("z").addColumn("visibility").addColumn("load_status").addColumn("entity_count").build(writer);
2021-08-27 11:14:09 +02:00
2021-11-21 23:00:00 +01:00
- this.sectionStorage.getAllChunksWithExistingSections().forEach((i) -> {
+ this.sectionStorage.getAllChunksWithExistingSections().forEach((java.util.function.LongConsumer) (i) -> { // CraftBukkit - decompile error
2021-08-27 11:14:09 +02:00
PersistentEntitySectionManager.b persistententitysectionmanager_b = (PersistentEntitySectionManager.b) this.chunkLoadStatuses.get(i);
2021-11-21 23:00:00 +01:00
this.sectionStorage.getExistingSectionPositionsInChunk(i).forEach((j) -> {
2021-11-01 08:54:44 +01:00
@@ -389,7 +424,7 @@
2021-08-27 11:14:09 +02:00
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;
}