1212import net .minecraft .world .entity .EquipmentSlot ;
1313import net .minecraft .world .item .ItemStack ;
1414
15+ import java .time .Duration ;
1516import java .util .ArrayList ;
1617import java .util .Collection ;
1718import java .util .Collections ;
2526public 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