9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-19 14:59:32 +00:00

fix: fix sleeping block entity bug (#682)

* fix: fix sleeping block entity bug

* fix: remove unused vars

* chore: replace var
This commit is contained in:
MC_XiaoHei
2025-08-14 20:10:58 +08:00
committed by GitHub
parent d93e9766d3
commit bf55b2a4f5
4 changed files with 46 additions and 33 deletions

View File

@@ -111,7 +111,7 @@ index 86cac164a2bf0e76528396e6aabbfd64cfc29559..da99b4bc7fe8460945070915073be141
int getContainerSize(); int getContainerSize();
diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java 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 --- a/net/minecraft/world/entity/Entity.java
+++ b/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 @@ -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(); private final VecDeltaCodec packetPositionCodec = new VecDeltaCodec();
public boolean hasImpulse; public boolean hasImpulse;
@Nullable @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()); this.setBoundingBox(this.makeBoundingBox());
} }
// Paper end - Block invalid positions and bounding box // Paper end - Block invalid positions and bounding box
@@ -131,19 +131,13 @@ index ca017f5e483a4ff5bc497ad453f4cf63a0bb97f5..0260f6c1518114cca57e945423f1f671
+ if (!org.leavesmc.leaves.LeavesConfig.performance.sleepingBlockEntity) return; + if (!org.leavesmc.leaves.LeavesConfig.performance.sleepingBlockEntity) return;
+ if (this instanceof ItemEntity) { + if (this instanceof ItemEntity) {
+ long sectionKey = ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkSectionKey(this); + long sectionKey = ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkSectionKey(this);
+ var tracker = org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionItemEntityMovementTracker.itemEntityMovementTrackerMap.computeIfAbsent( + 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));
+ sectionKey, + if (tracker != null) tracker.notifyAllListeners(level.getGameTime());
+ k -> new org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionItemEntityMovementTracker(sectionKey)
+ );
+ tracker.notifyAllListeners(level.getGameTime());
+ } + }
+ else if (this instanceof net.minecraft.world.entity.vehicle.ContainerEntity) { + else if (this instanceof net.minecraft.world.entity.vehicle.ContainerEntity) {
+ long sectionKey = ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkSectionKey(this); + long sectionKey = ca.spottedleaf.moonrise.common.util.CoordinateUtils.getChunkSectionKey(this);
+ var tracker = org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionInventoryEntityTracker.containerEntityMovementTrackerMap.computeIfAbsent( + 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));
+ sectionKey, + if (tracker != null) tracker.notifyAllListeners(level.getGameTime());
+ k -> new org.leavesmc.leaves.lithium.common.tracking.entity.ChunkSectionInventoryEntityTracker(sectionKey)
+ );
+ tracker.notifyAllListeners(level.getGameTime());
+ } + }
+ // Leaves end - Lithium Sleeping Block Entity + // Leaves end - Lithium Sleeping Block Entity
} }

View File

@@ -1,18 +1,20 @@
package org.leavesmc.leaves.lithium.common.tracking.entity; package org.leavesmc.leaves.lithium.common.tracking.entity;
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import java.util.UUID;
public abstract class ChunkSectionEntityMovementTracker { public abstract class ChunkSectionEntityMovementTracker {
protected long lastChangeTime = 0; protected long lastChangeTime = 0;
protected final ReferenceOpenHashSet<ChunkSectionEntityMovementListener> listeners = new ReferenceOpenHashSet<>(); protected final ReferenceOpenHashSet<ChunkSectionEntityMovementListener> listeners = new ReferenceOpenHashSet<>();
protected final long sectionKey; protected final ChunkSectionIdentifier identifier;
protected int userCount = 0; protected int userCount = 0;
public ChunkSectionEntityMovementTracker(long sectionKey) { public ChunkSectionEntityMovementTracker(long sectionKey, UUID levelId) {
this.sectionKey = sectionKey; identifier = ChunkSectionIdentifier.of(sectionKey, levelId);
} }
public void register() { public void register() {
@@ -65,4 +67,11 @@ public abstract class ChunkSectionEntityMovementTracker {
} }
setChanged(time); 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);
}
}
} }

View File

@@ -13,19 +13,20 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
public class ChunkSectionInventoryEntityTracker extends ChunkSectionEntityMovementTracker { public class ChunkSectionInventoryEntityTracker extends ChunkSectionEntityMovementTracker {
public static final Map<Long, ChunkSectionInventoryEntityTracker> containerEntityMovementTrackerMap = new java.util.HashMap<>(); public static final Map<ChunkSectionIdentifier, ChunkSectionInventoryEntityTracker> containerEntityMovementTrackerMap = new java.util.HashMap<>();
public ChunkSectionInventoryEntityTracker(long sectionKey) { public ChunkSectionInventoryEntityTracker(long sectionKey, UUID levelId) {
super(sectionKey); super(sectionKey, levelId);
} }
@Override @Override
public void unregister() { public void unregister() {
this.userCount--; this.userCount--;
if (this.userCount <= 0) { if (this.userCount <= 0) {
containerEntityMovementTrackerMap.remove(sectionKey); containerEntityMovementTrackerMap.remove(identifier);
} }
} }
@@ -36,11 +37,15 @@ public class ChunkSectionInventoryEntityTracker extends ChunkSectionEntityMoveme
public static @NotNull List<ChunkSectionInventoryEntityTracker> registerAt(ServerLevel world, AABB interactionArea) { public static @NotNull List<ChunkSectionInventoryEntityTracker> registerAt(ServerLevel world, AABB interactionArea) {
WorldSectionBox worldSectionBox = WorldSectionBox.entityAccessBox(world, interactionArea); WorldSectionBox worldSectionBox = WorldSectionBox.entityAccessBox(world, interactionArea);
UUID levelId = world.uuid;
if (worldSectionBox.chunkX1() == worldSectionBox.chunkX2() && if (worldSectionBox.chunkX1() == worldSectionBox.chunkX2() &&
worldSectionBox.chunkY1() == worldSectionBox.chunkY2() && worldSectionBox.chunkY1() == worldSectionBox.chunkY2() &&
worldSectionBox.chunkZ1() == worldSectionBox.chunkZ2()) { 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<ChunkSectionInventoryEntityTracker> trackers = new ArrayList<>(); List<ChunkSectionInventoryEntityTracker> trackers = new ArrayList<>();
@@ -48,7 +53,7 @@ public class ChunkSectionInventoryEntityTracker extends ChunkSectionEntityMoveme
for (int x = worldSectionBox.chunkX1(); x <= worldSectionBox.chunkX2(); x++) { for (int x = worldSectionBox.chunkX1(); x <= worldSectionBox.chunkX2(); x++) {
for (int y = worldSectionBox.chunkY1(); y <= worldSectionBox.chunkY2(); y++) { for (int y = worldSectionBox.chunkY1(); y <= worldSectionBox.chunkY2(); y++) {
for (int z = worldSectionBox.chunkZ1(); z <= worldSectionBox.chunkZ2(); z++) { 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; return trackers;
} }
private static @NotNull ChunkSectionInventoryEntityTracker registerAt(long key) { private static @NotNull ChunkSectionInventoryEntityTracker registerAt(long key, UUID levelId) {
ChunkSectionInventoryEntityTracker tracker = containerEntityMovementTrackerMap.computeIfAbsent( ChunkSectionInventoryEntityTracker tracker = containerEntityMovementTrackerMap.computeIfAbsent(
key, new ChunkSectionIdentifier(key, levelId),
k -> new ChunkSectionInventoryEntityTracker(key) k -> new ChunkSectionInventoryEntityTracker(key, levelId)
); );
tracker.register(); tracker.register();
return tracker; return tracker;

View File

@@ -13,19 +13,20 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
public class ChunkSectionItemEntityMovementTracker extends ChunkSectionEntityMovementTracker { public class ChunkSectionItemEntityMovementTracker extends ChunkSectionEntityMovementTracker {
public static final Map<Long, ChunkSectionItemEntityMovementTracker> itemEntityMovementTrackerMap = new java.util.HashMap<>(); public static final Map<ChunkSectionIdentifier, ChunkSectionItemEntityMovementTracker> itemEntityMovementTrackerMap = new java.util.HashMap<>();
public ChunkSectionItemEntityMovementTracker(long sectionKey) { public ChunkSectionItemEntityMovementTracker(long sectionKey, UUID levelId) {
super(sectionKey); super(sectionKey, levelId);
} }
@Override @Override
public void unregister() { public void unregister() {
this.userCount--; this.userCount--;
if (this.userCount <= 0) { if (this.userCount <= 0) {
itemEntityMovementTrackerMap.remove(sectionKey); itemEntityMovementTrackerMap.remove(identifier);
} }
} }
@@ -36,11 +37,15 @@ public class ChunkSectionItemEntityMovementTracker extends ChunkSectionEntityMov
public static @NotNull List<ChunkSectionItemEntityMovementTracker> registerAt(ServerLevel world, AABB interactionArea) { public static @NotNull List<ChunkSectionItemEntityMovementTracker> registerAt(ServerLevel world, AABB interactionArea) {
WorldSectionBox worldSectionBox = WorldSectionBox.entityAccessBox(world, interactionArea); WorldSectionBox worldSectionBox = WorldSectionBox.entityAccessBox(world, interactionArea);
UUID levelId = world.uuid;
if (worldSectionBox.chunkX1() == worldSectionBox.chunkX2() && if (worldSectionBox.chunkX1() == worldSectionBox.chunkX2() &&
worldSectionBox.chunkY1() == worldSectionBox.chunkY2() && worldSectionBox.chunkY1() == worldSectionBox.chunkY2() &&
worldSectionBox.chunkZ1() == worldSectionBox.chunkZ2()) { 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<ChunkSectionItemEntityMovementTracker> trackers = new ArrayList<>(); List<ChunkSectionItemEntityMovementTracker> trackers = new ArrayList<>();
@@ -48,7 +53,7 @@ public class ChunkSectionItemEntityMovementTracker extends ChunkSectionEntityMov
for (int x = worldSectionBox.chunkX1(); x <= worldSectionBox.chunkX2(); x++) { for (int x = worldSectionBox.chunkX1(); x <= worldSectionBox.chunkX2(); x++) {
for (int y = worldSectionBox.chunkY1(); y <= worldSectionBox.chunkY2(); y++) { for (int y = worldSectionBox.chunkY1(); y <= worldSectionBox.chunkY2(); y++) {
for (int z = worldSectionBox.chunkZ1(); z <= worldSectionBox.chunkZ2(); z++) { 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; return trackers;
} }
private static @NotNull ChunkSectionItemEntityMovementTracker registerAt(long key) { private static @NotNull ChunkSectionItemEntityMovementTracker registerAt(long key, UUID levelId) {
ChunkSectionItemEntityMovementTracker tracker = itemEntityMovementTrackerMap.computeIfAbsent( ChunkSectionItemEntityMovementTracker tracker = itemEntityMovementTrackerMap.computeIfAbsent(
key, new ChunkSectionIdentifier(key, levelId),
k -> new ChunkSectionItemEntityMovementTracker(key) k -> new ChunkSectionItemEntityMovementTracker(key, levelId)
); );
tracker.register(); tracker.register();
return tracker; return tracker;