--- a/net/minecraft/world/level/entity/PersistentEntitySectionManager.java +++ b/net/minecraft/world/level/entity/PersistentEntitySectionManager.java @@ -32,6 +32,12 @@ import net.minecraft.world.level.ChunkCoordIntPair; import org.slf4j.Logger; +// CraftBukkit start +import net.minecraft.world.level.chunk.storage.EntityStorage; +import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.event.entity.EntityRemoveEvent; +// CraftBukkit end + public class PersistentEntitySectionManager implements AutoCloseable { static final Logger LOGGER = LogUtils.getLogger(); @@ -55,6 +61,16 @@ this.entityGetter = new LevelEntityGetterAdapter<>(this.visibleEntityStorage, this.sectionStorage); } + // CraftBukkit start - add method to get all entities in chunk + public List getEntities(ChunkCoordIntPair chunkCoordIntPair) { + return sectionStorage.getExistingSectionsInChunk(chunkCoordIntPair.toLong()).flatMap(EntitySection::getEntities).map(entity -> (Entity) entity).collect(Collectors.toList()); + } + + public boolean isPending(long pair) { + return chunkLoadStatuses.get(pair) == b.PENDING; + } + // CraftBukkit end + void removeSectionIfEmpty(long i, EntitySection entitysection) { if (entitysection.isEmpty()) { this.sectionStorage.remove(i); @@ -196,6 +212,12 @@ } private boolean storeChunkSections(long i, Consumer consumer) { + // CraftBukkit start - add boolean for event call + return storeChunkSections(i, consumer, false); + } + + private boolean storeChunkSections(long i, Consumer consumer, boolean callEvent) { + // CraftBukkit end PersistentEntitySectionManager.b persistententitysectionmanager_b = (PersistentEntitySectionManager.b) this.chunkLoadStatuses.get(i); if (persistententitysectionmanager_b == PersistentEntitySectionManager.b.PENDING) { @@ -207,6 +229,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.storeEntities(new ChunkEntities<>(new ChunkCoordIntPair(i), ImmutableList.of())); } @@ -215,6 +238,7 @@ this.requestChunkLoad(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.storeEntities(new ChunkEntities<>(new ChunkCoordIntPair(i), list)); list.forEach(consumer); return true; @@ -238,7 +262,7 @@ private boolean processChunkUnload(long i) { boolean flag = this.storeChunkSections(i, (entityaccess) -> { entityaccess.getPassengersAndSelf().forEach(this::unloadEntity); - }); + }, true); // CraftBukkit - add boolean for event call if (!flag) { return false; @@ -249,24 +273,28 @@ } private void unloadEntity(EntityAccess entityaccess) { - entityaccess.setRemoved(Entity.RemovalReason.UNLOADED_TO_CHUNK); + entityaccess.setRemoved(Entity.RemovalReason.UNLOADED_TO_CHUNK, EntityRemoveEvent.Cause.UNLOAD); // CraftBukkit - add Bukkit remove cause entityaccess.setLevelCallback(EntityInLevelCallback.NULL); } private void processUnloads() { - this.chunksToUnload.removeIf((i) -> { + this.chunksToUnload.removeIf((java.util.function.LongPredicate) (i) -> { // CraftBukkit - decompile error return this.chunkVisibility.get(i) != Visibility.HIDDEN ? true : this.processChunkUnload(i); }); } private void processPendingLoads() { - ChunkEntities chunkentities; + ChunkEntities chunkentities; // CraftBukkit - decompile error while ((chunkentities = (ChunkEntities) this.loadingInbox.poll()) != null) { chunkentities.getEntities().forEach((entityaccess) -> { this.addEntity(entityaccess, true); }); this.chunkLoadStatuses.put(chunkentities.getPos().toLong(), PersistentEntitySectionManager.b.LOADED); + // CraftBukkit start - call entity load event + List entities = getEntities(chunkentities.getPos()); + CraftEventFactory.callEntitiesLoadEvent(((EntityStorage) permanentStorage).level, chunkentities.getPos(), entities); + // CraftBukkit end } } @@ -292,7 +320,7 @@ } public void autoSave() { - this.getAllChunksToSave().forEach((i) -> { + this.getAllChunksToSave().forEach((java.util.function.LongConsumer) (i) -> { // CraftBukkit - decompile error boolean flag = this.chunkVisibility.get(i) == Visibility.HIDDEN; if (flag) { @@ -311,7 +339,7 @@ while (!longset.isEmpty()) { this.permanentStorage.flush(false); this.processPendingLoads(); - longset.removeIf((i) -> { + longset.removeIf((java.util.function.LongPredicate) (i) -> { // CraftBukkit - decompile error boolean flag = this.chunkVisibility.get(i) == Visibility.HIDDEN; return flag ? this.processChunkUnload(i) : this.storeChunkSections(i, (entityaccess) -> { @@ -323,7 +351,15 @@ } public void close() throws IOException { - this.saveAll(); + // CraftBukkit start - add save boolean + close(true); + } + + public void close(boolean save) throws IOException { + if (save) { + this.saveAll(); + } + // CraftBukkit end this.permanentStorage.close(); } @@ -350,7 +386,7 @@ 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); - this.sectionStorage.getAllChunksWithExistingSections().forEach((i) -> { + this.sectionStorage.getAllChunksWithExistingSections().forEach((java.util.function.LongConsumer) (i) -> { // CraftBukkit - decompile error PersistentEntitySectionManager.b persistententitysectionmanager_b = (PersistentEntitySectionManager.b) this.chunkLoadStatuses.get(i); this.sectionStorage.getExistingSectionPositionsInChunk(i).forEach((j) -> { @@ -394,7 +430,7 @@ private EntitySection currentSection; a(final EntityAccess entityaccess, final long i, final EntitySection entitysection) { - this.entity = entityaccess; + this.entity = (T) entityaccess; // CraftBukkit - decompile error this.currentSectionKey = i; this.currentSection = entitysection; }