diff --git a/leaves-server/minecraft-patches/features/0139-Lithium-Sleeping-Block-Entity.patch b/leaves-server/minecraft-patches/features/0139-Lithium-Sleeping-Block-Entity.patch index 60e63c06..3098f397 100644 --- a/leaves-server/minecraft-patches/features/0139-Lithium-Sleeping-Block-Entity.patch +++ b/leaves-server/minecraft-patches/features/0139-Lithium-Sleeping-Block-Entity.patch @@ -111,7 +111,7 @@ index 86cac164a2bf0e76528396e6aabbfd64cfc29559..da99b4bc7fe8460945070915073be141 int getContainerSize(); diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java -index ca017f5e483a4ff5bc497ad453f4cf63a0bb97f5..0260f6c1518114cca57e945423f1f671740eb802 100644 +index ca017f5e483a4ff5bc497ad453f4cf63a0bb97f5..880b2388d35ad5eedbd8d657f1726a8384b42548 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -309,7 +309,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess @@ -123,7 +123,7 @@ index ca017f5e483a4ff5bc497ad453f4cf63a0bb97f5..0260f6c1518114cca57e945423f1f671 private final VecDeltaCodec packetPositionCodec = new VecDeltaCodec(); public boolean hasImpulse; @Nullable -@@ -5147,6 +5147,25 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess +@@ -5147,6 +5147,19 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess this.setBoundingBox(this.makeBoundingBox()); } // Paper end - Block invalid positions and bounding box @@ -131,19 +131,13 @@ index ca017f5e483a4ff5bc497ad453f4cf63a0bb97f5..0260f6c1518114cca57e945423f1f671 + if (!org.leavesmc.leaves.LeavesConfig.performance.sleepingBlockEntity) return; + if (this instanceof ItemEntity) { + long sectionKey = ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkSectionKey(this); -+ var tracker = org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionItemEntityMovementTracker.itemEntityMovementTrackerMap.computeIfAbsent( -+ sectionKey, -+ k -> new org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionItemEntityMovementTracker(sectionKey) -+ ); -+ tracker.notifyAllListeners(level.getGameTime()); ++ org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionItemEntityMovementTracker tracker = org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionItemEntityMovementTracker.itemEntityMovementTrackerMap.get(new org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionEntityMovementTracker.ChunkSectionIdentifier(sectionKey, level.getMinecraftWorld().uuid)); ++ if (tracker != null) tracker.notifyAllListeners(level.getGameTime()); + } + else if (this instanceof net.minecraft.world.entity.vehicle.ContainerEntity) { + long sectionKey = ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkSectionKey(this); -+ var tracker = org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionInventoryEntityTracker.containerEntityMovementTrackerMap.computeIfAbsent( -+ sectionKey, -+ k -> new org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionInventoryEntityTracker(sectionKey) -+ ); -+ tracker.notifyAllListeners(level.getGameTime()); ++ org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionInventoryEntityTracker tracker = org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionInventoryEntityTracker.containerEntityMovementTrackerMap.get(new org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionEntityMovementTracker.ChunkSectionIdentifier(sectionKey, level.getMinecraftWorld().uuid)); ++ if (tracker != null) tracker.notifyAllListeners(level.getGameTime()); + } + // Leaves end - Lithium Sleeping Block Entity } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionEntityMovementTracker.java b/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionEntityMovementTracker.java index 23941cd0..43324dd7 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionEntityMovementTracker.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionEntityMovementTracker.java @@ -1,18 +1,20 @@ package org.leavesmc.leaves.lithium.common.tracking.entity; import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import java.util.List; +import java.util.UUID; public abstract class ChunkSectionEntityMovementTracker { protected long lastChangeTime = 0; protected final ReferenceOpenHashSet listeners = new ReferenceOpenHashSet<>(); - protected final long sectionKey; + protected final ChunkSectionIdentifier identifier; protected int userCount = 0; - public ChunkSectionEntityMovementTracker(long sectionKey) { - this.sectionKey = sectionKey; + public ChunkSectionEntityMovementTracker(long sectionKey, UUID levelId) { + identifier = ChunkSectionIdentifier.of(sectionKey, levelId); } public void register() { @@ -65,4 +67,11 @@ public abstract class ChunkSectionEntityMovementTracker { } setChanged(time); } + + public record ChunkSectionIdentifier(long sectionKey, UUID levelId) { + @Contract("_, _ -> new") + public static @NotNull ChunkSectionIdentifier of(long sectionKey, UUID levelId) { + return new ChunkSectionIdentifier(sectionKey, levelId); + } + } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionInventoryEntityTracker.java b/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionInventoryEntityTracker.java index 4b6ad915..60e9e039 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionInventoryEntityTracker.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionInventoryEntityTracker.java @@ -13,19 +13,20 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.UUID; public class ChunkSectionInventoryEntityTracker extends ChunkSectionEntityMovementTracker { - public static final Map containerEntityMovementTrackerMap = new java.util.HashMap<>(); + public static final Map containerEntityMovementTrackerMap = new java.util.HashMap<>(); - public ChunkSectionInventoryEntityTracker(long sectionKey) { - super(sectionKey); + public ChunkSectionInventoryEntityTracker(long sectionKey, UUID levelId) { + super(sectionKey, levelId); } @Override public void unregister() { this.userCount--; if (this.userCount <= 0) { - containerEntityMovementTrackerMap.remove(sectionKey); + containerEntityMovementTrackerMap.remove(identifier); } } @@ -36,11 +37,15 @@ public class ChunkSectionInventoryEntityTracker extends ChunkSectionEntityMoveme public static @NotNull List registerAt(ServerLevel world, AABB interactionArea) { WorldSectionBox worldSectionBox = WorldSectionBox.entityAccessBox(world, interactionArea); + UUID levelId = world.uuid; if (worldSectionBox.chunkX1() == worldSectionBox.chunkX2() && worldSectionBox.chunkY1() == worldSectionBox.chunkY2() && worldSectionBox.chunkZ1() == worldSectionBox.chunkZ2()) { - return Collections.singletonList(registerAt(CoordinateUtils.getChunkSectionKey(worldSectionBox.chunkX1(), worldSectionBox.chunkY1(), worldSectionBox.chunkZ1()))); + return Collections.singletonList(registerAt( + CoordinateUtils.getChunkSectionKey(worldSectionBox.chunkX1(), worldSectionBox.chunkY1(), worldSectionBox.chunkZ1()), + levelId + )); } List trackers = new ArrayList<>(); @@ -48,7 +53,7 @@ public class ChunkSectionInventoryEntityTracker extends ChunkSectionEntityMoveme for (int x = worldSectionBox.chunkX1(); x <= worldSectionBox.chunkX2(); x++) { for (int y = worldSectionBox.chunkY1(); y <= worldSectionBox.chunkY2(); y++) { for (int z = worldSectionBox.chunkZ1(); z <= worldSectionBox.chunkZ2(); z++) { - trackers.add(registerAt(CoordinateUtils.getChunkSectionKey(x, y, z))); + trackers.add(registerAt(CoordinateUtils.getChunkSectionKey(x, y, z), levelId)); } } } @@ -56,10 +61,10 @@ public class ChunkSectionInventoryEntityTracker extends ChunkSectionEntityMoveme return trackers; } - private static @NotNull ChunkSectionInventoryEntityTracker registerAt(long key) { + private static @NotNull ChunkSectionInventoryEntityTracker registerAt(long key, UUID levelId) { ChunkSectionInventoryEntityTracker tracker = containerEntityMovementTrackerMap.computeIfAbsent( - key, - k -> new ChunkSectionInventoryEntityTracker(key) + new ChunkSectionIdentifier(key, levelId), + k -> new ChunkSectionInventoryEntityTracker(key, levelId) ); tracker.register(); return tracker; diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionItemEntityMovementTracker.java b/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionItemEntityMovementTracker.java index f4f9e6a3..8b6bdc77 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionItemEntityMovementTracker.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/lithium/common/tracking/entity/ChunkSectionItemEntityMovementTracker.java @@ -13,19 +13,20 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.UUID; public class ChunkSectionItemEntityMovementTracker extends ChunkSectionEntityMovementTracker { - public static final Map itemEntityMovementTrackerMap = new java.util.HashMap<>(); + public static final Map itemEntityMovementTrackerMap = new java.util.HashMap<>(); - public ChunkSectionItemEntityMovementTracker(long sectionKey) { - super(sectionKey); + public ChunkSectionItemEntityMovementTracker(long sectionKey, UUID levelId) { + super(sectionKey, levelId); } @Override public void unregister() { this.userCount--; if (this.userCount <= 0) { - itemEntityMovementTrackerMap.remove(sectionKey); + itemEntityMovementTrackerMap.remove(identifier); } } @@ -36,11 +37,15 @@ public class ChunkSectionItemEntityMovementTracker extends ChunkSectionEntityMov public static @NotNull List registerAt(ServerLevel world, AABB interactionArea) { WorldSectionBox worldSectionBox = WorldSectionBox.entityAccessBox(world, interactionArea); + UUID levelId = world.uuid; if (worldSectionBox.chunkX1() == worldSectionBox.chunkX2() && worldSectionBox.chunkY1() == worldSectionBox.chunkY2() && worldSectionBox.chunkZ1() == worldSectionBox.chunkZ2()) { - return Collections.singletonList(registerAt(CoordinateUtils.getChunkSectionKey(worldSectionBox.chunkX1(), worldSectionBox.chunkY1(), worldSectionBox.chunkZ1()))); + return Collections.singletonList(registerAt( + CoordinateUtils.getChunkSectionKey(worldSectionBox.chunkX1(), worldSectionBox.chunkY1(), worldSectionBox.chunkZ1()), + levelId + )); } List trackers = new ArrayList<>(); @@ -48,7 +53,7 @@ public class ChunkSectionItemEntityMovementTracker extends ChunkSectionEntityMov for (int x = worldSectionBox.chunkX1(); x <= worldSectionBox.chunkX2(); x++) { for (int y = worldSectionBox.chunkY1(); y <= worldSectionBox.chunkY2(); y++) { for (int z = worldSectionBox.chunkZ1(); z <= worldSectionBox.chunkZ2(); z++) { - trackers.add(registerAt(CoordinateUtils.getChunkSectionKey(x, y, z))); + trackers.add(registerAt(CoordinateUtils.getChunkSectionKey(x, y, z), levelId)); } } } @@ -56,10 +61,10 @@ public class ChunkSectionItemEntityMovementTracker extends ChunkSectionEntityMov return trackers; } - private static @NotNull ChunkSectionItemEntityMovementTracker registerAt(long key) { + private static @NotNull ChunkSectionItemEntityMovementTracker registerAt(long key, UUID levelId) { ChunkSectionItemEntityMovementTracker tracker = itemEntityMovementTrackerMap.computeIfAbsent( - key, - k -> new ChunkSectionItemEntityMovementTracker(key) + new ChunkSectionIdentifier(key, levelId), + k -> new ChunkSectionItemEntityMovementTracker(key, levelId) ); tracker.register(); return tracker;