Skip to content

Commit 5916625

Browse files
committed
fix: farplayers tracking
1 parent 0f178b8 commit 5916625

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

src/main/java/me/mapacheee/extendedhorizons/fakechunks/farplayers/FarPlayerTrackingService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public final class FarPlayerTrackingService {
2727
private static final int FAR_ENTITY_ID_ALLOCATION_ATTEMPTS = 10_000;
2828
private static final double FAR_RADIUS_PADDING = 0.35d;
2929
private static final int CHUNK_SHIFT = 4;
30-
private static final double CHUNK_SIZE = 1 << CHUNK_SHIFT;
3130

3231
private final Container<EhConfig> configContainer;
3332
private final FarPlayerCacheService cacheService;
@@ -90,8 +89,8 @@ public void track(
9089
continue;
9190
}
9291

93-
int stateChunkX = (int) (state.x() / CHUNK_SIZE);
94-
int stateChunkZ = (int) (state.z() / CHUNK_SIZE);
92+
int stateChunkX = (int) Math.floor(state.x()) >> CHUNK_SHIFT;
93+
int stateChunkZ = (int) Math.floor(state.z()) >> CHUNK_SHIFT;
9594
int relChunkX = stateChunkX - viewerChunkX;
9695
int relChunkZ = stateChunkZ - viewerChunkZ;
9796
double distSq = (double) relChunkX * relChunkX + (double) relChunkZ * relChunkZ;

src/main/java/me/mapacheee/extendedhorizons/fakechunks/farplayers/cache/FarPlayerCacheService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.minecraft.world.entity.EquipmentSlot;
1313
import net.minecraft.world.item.ItemStack;
1414

15+
import java.time.Duration;
1516
import java.util.ArrayList;
1617
import java.util.Collection;
1718
import java.util.Collections;
@@ -25,8 +26,9 @@
2526
public final class FarPlayerCacheService {
2627

2728
private static final int REGION_SIZE_BITS = 5;
28-
private static final int REGION_SIZE = 1 << REGION_SIZE_BITS;
29+
private static final int BLOCK_TO_CHUNK_SHIFT = 4;
2930
private static final int DEFAULT_MAX_ENTRIES = 500;
31+
private static final int DEFAULT_TTL_SECONDS = 30;
3032

3133
private final Container<EhConfig> configContainer;
3234
private volatile Cache<UUID, FarPlayerState> statesCache;
@@ -43,11 +45,14 @@ public FarPlayerCacheService(Container<EhConfig> configContainer) {
4345

4446
public void rebuild() {
4547
int maxEntries = this.configContainer.get().farPlayerCacheEntries();
48+
Duration ttl = Duration.ofSeconds(DEFAULT_TTL_SECONDS);
4649
this.statesCache = Caffeine.newBuilder()
4750
.maximumSize(maxEntries)
51+
.expireAfterWrite(ttl)
4852
.build();
4953
this.equipmentCache = Caffeine.newBuilder()
5054
.maximumSize(maxEntries)
55+
.expireAfterWrite(ttl)
5156
.build();
5257
}
5358

@@ -59,8 +64,8 @@ public void updateState(UUID playerId, FarPlayerState state) {
5964
this.statesCache.put(playerId, state);
6065

6166
UUID worldId = state.worldId();
62-
int chunkX = (int) (state.x() / REGION_SIZE);
63-
int chunkZ = (int) (state.z() / REGION_SIZE);
67+
int chunkX = (int) Math.floor(state.x()) >> BLOCK_TO_CHUNK_SHIFT;
68+
int chunkZ = (int) Math.floor(state.z()) >> BLOCK_TO_CHUNK_SHIFT;
6469
long regionKey = getRegionKey(chunkX, chunkZ);
6570

6671
UUID prevWorld = this.playerLastWorld.get(playerId);

0 commit comments

Comments
 (0)