craftbukkit/nms-patches/net/minecraft/world/level/entity/PersistentEntitySectionManager.patch

162 lines
7.1 KiB
Diff
Raw Normal View History

--- a/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
+++ b/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
@@ -32,6 +32,12 @@
2022-03-01 02:00:00 +11:00
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<T extends EntityAccess> implements AutoCloseable {
2022-03-01 02:00:00 +11:00
static final Logger LOGGER = LogUtils.getLogger();
@@ -55,6 +61,16 @@
2025-03-26 03:05:00 +11:00
this.entityGetter = new LevelEntityGetterAdapter<T>(this.visibleEntityStorage, this.sectionStorage);
}
+ // CraftBukkit start - add method to get all entities in chunk
+ public List<Entity> getEntities(ChunkCoordIntPair chunkCoordIntPair) {
2021-11-22 09:00:00 +11:00
+ 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
+
2021-11-22 09:00:00 +11:00
void removeSectionIfEmpty(long i, EntitySection<T> entitysection) {
if (entitysection.isEmpty()) {
this.sectionStorage.remove(i);
@@ -87,6 +103,11 @@
if (!flag) {
this.callbacks.onCreated(t0);
}
+ // CraftBukkit - Mark entity as in world
+ if (t0 instanceof Entity entity) {
+ entity.inWorld = true;
+ }
+ // CraftBukkit end
Visibility visibility = getEffectiveStatus(t0, entitysection.getStatus());
@@ -200,6 +221,12 @@
}
2021-11-22 09:00:00 +11:00
private boolean storeChunkSections(long i, Consumer<T> consumer) {
+ // CraftBukkit start - add boolean for event call
2021-11-22 09:00:00 +11:00
+ return storeChunkSections(i, consumer, false);
+ }
+
2021-11-22 09:00:00 +11:00
+ private boolean storeChunkSections(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) {
@@ -211,6 +238,7 @@
if (list.isEmpty()) {
if (persistententitysectionmanager_b == PersistentEntitySectionManager.b.LOADED) {
+ if (callEvent) CraftEventFactory.callEntitiesUnloadEvent(((EntityStorage) permanentStorage).level, new ChunkCoordIntPair(i), ImmutableList.of()); // CraftBukkit
2025-03-26 03:05:00 +11:00
this.permanentStorage.storeEntities(new ChunkEntities(new ChunkCoordIntPair(i), ImmutableList.of()));
}
@@ -219,6 +247,7 @@
2021-11-22 09:00:00 +11:00
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
2025-03-26 03:05:00 +11:00
this.permanentStorage.storeEntities(new ChunkEntities(new ChunkCoordIntPair(i), list));
list.forEach(consumer);
return true;
@@ -242,7 +271,7 @@
2021-11-22 09:00:00 +11:00
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;
@@ -253,12 +282,12 @@
}
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);
}
2021-11-22 09:00:00 +11:00
private void processUnloads() {
- this.chunksToUnload.removeIf((i) -> {
+ this.chunksToUnload.removeIf((java.util.function.LongPredicate) (i) -> { // CraftBukkit - decompile error
2021-11-22 09:00:00 +11:00
return this.chunkVisibility.get(i) != Visibility.HIDDEN ? true : this.processChunkUnload(i);
});
}
@@ -271,6 +300,10 @@
2021-11-22 09:00:00 +11:00
this.addEntity(entityaccess, true);
});
2021-11-22 09:00:00 +11:00
this.chunkLoadStatuses.put(chunkentities.getPos().toLong(), PersistentEntitySectionManager.b.LOADED);
+ // CraftBukkit start - call entity load event
2021-11-22 09:00:00 +11:00
+ List<Entity> entities = getEntities(chunkentities.getPos());
+ CraftEventFactory.callEntitiesLoadEvent(((EntityStorage) permanentStorage).level, chunkentities.getPos(), entities);
+ // CraftBukkit end
}
}
@@ -296,7 +329,7 @@
}
2021-11-22 09:00:00 +11:00
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) {
@@ -315,7 +348,7 @@
while (!longset.isEmpty()) {
2021-11-22 09:00:00 +11:00
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;
2021-11-22 09:00:00 +11:00
return flag ? this.processChunkUnload(i) : this.storeChunkSections(i, (entityaccess) -> {
@@ -327,7 +360,15 @@
}
public void close() throws IOException {
2021-11-22 09:00:00 +11:00
- this.saveAll();
+ // CraftBukkit start - add save boolean
+ close(true);
+ }
+
+ public void close(boolean save) throws IOException {
+ if (save) {
2021-11-22 09:00:00 +11:00
+ this.saveAll();
+ }
+ // CraftBukkit end
this.permanentStorage.close();
}
@@ -354,7 +395,7 @@
2021-11-22 09:00:00 +11: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-11-22 09:00:00 +11:00
- 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);
2021-11-22 09:00:00 +11:00
this.sectionStorage.getExistingSectionPositionsInChunk(i).forEach((j) -> {
@@ -398,7 +439,7 @@
private EntitySection<T> currentSection;
2024-04-24 01:15:00 +10:00
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;
}