9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-30 04:19:30 +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

@@ -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<ChunkSectionEntityMovementListener> 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);
}
}
}

View File

@@ -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<Long, ChunkSectionInventoryEntityTracker> containerEntityMovementTrackerMap = new java.util.HashMap<>();
public static final Map<ChunkSectionIdentifier, ChunkSectionInventoryEntityTracker> 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<ChunkSectionInventoryEntityTracker> 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<ChunkSectionInventoryEntityTracker> 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;

View File

@@ -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<Long, ChunkSectionItemEntityMovementTracker> itemEntityMovementTrackerMap = new java.util.HashMap<>();
public static final Map<ChunkSectionIdentifier, ChunkSectionItemEntityMovementTracker> 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<ChunkSectionItemEntityMovementTracker> 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<ChunkSectionItemEntityMovementTracker> 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;