Cached native biome handles

- Biome wrappers read canonical keys only on cache insertion
- Native handle identity prevents stale biome reuse after registry replacement
- The biome wrapper cache retains at most 4096 entries
- Regression tests cover key reuse, namespaces, replacement handles, and eviction
This commit is contained in:
Brian Neumann-Fopiano committed 2026-09-06 12:08:31 -04:00
1 parent 5570923d30
commit cb85a09c0a
2 files changed
+166 -22

No files matched your search

@@ -19,43 +19,33 @@
package art.arcane.iris.platform.bukkit;
import art.arcane.iris.spi.PlatformBiome;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Biome;
import java.util.concurrent.ConcurrentHashMap;
/**
* Interned Bukkit adapter for a neutral biome handle.
*/
public final class BukkitBiome implements PlatformBiome {
private static final ConcurrentHashMap<String, BukkitBiome> CACHE = new ConcurrentHashMap<>();
private static final Cache<Biome, BukkitBiome> CACHE = Caffeine.newBuilder()
.weakKeys()
.maximumSize(4_096)
.build();
private final Biome biome;
private final String key;
private final String namespace;
private BukkitBiome(Biome biome, String key) {
private BukkitBiome(Biome biome) {
this.biome = biome;
this.key = key;
int colon = key.indexOf(':');
this.namespace = colon >= 0 ? key.substring(0, colon) : "minecraft";
NamespacedKey biomeKey = biome.getKey();
this.key = biomeKey.toString();
this.namespace = biomeKey.getNamespace();
}
public static BukkitBiome of(Biome biome) {
String key = biomeKey(biome);
return CACHE.computeIfAbsent(key, (String k) -> new BukkitBiome(biome, k));
}
private static String biomeKey(Biome biome) {
for (String method : new String[]{"getKeyOrNull", "getKeyOrThrow", "getKey"}) {
try {
Object key = Biome.class.getMethod(method).invoke(biome);
if (key != null) {
return key.toString();
}
} catch (Throwable ignored) {
}
}
return biome.toString();
return CACHE.get(biome, BukkitBiome::new);
}
@Override