| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.google.common.cache.CacheBuilder;
|
| 4 | import com.google.common.cache.CacheLoader;
|
| 5 | import com.google.common.cache.LoadingCache;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 8 | import it.unimi.dsi.fastutil.objects.Object2IntMaps;
|
| 9 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 10 | import java.util.Locale;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.concurrent.TimeUnit;
|
| 13 | import net.minecraft.core.Registry;
|
| 14 | import net.minecraft.core.registries.Registries;
|
| 15 | import net.minecraft.resources.ResourceKey;
|
| 16 | import net.minecraft.server.level.ServerLevel;
|
| 17 | import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
| 18 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 19 | import org.slf4j.Logger;
|
| 20 |
|
| 21 | public class FeatureCountTracker {
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | private static final LoadingCache<ServerLevel, FeatureCountTracker.LevelData> data = CacheBuilder.newBuilder()
|
| 24 | .weakKeys()
|
| 25 | .expireAfterAccess(5L, TimeUnit.MINUTES)
|
| 26 | .build(new CacheLoader<ServerLevel, FeatureCountTracker.LevelData>() {
|
| 27 | public FeatureCountTracker.LevelData load(final ServerLevel level) {
|
| 28 | return new FeatureCountTracker.LevelData(Object2IntMaps.synchronize(new Object2IntOpenHashMap<>()), new MutableInt(0));
|
| 29 | }
|
| 30 | });
|
| 31 |
|
| 32 | public static void chunkDecorated(final ServerLevel level) {
|
| 33 | try {
|
| 34 | data.get(level).chunksWithFeatures().increment();
|
| 35 | } catch (Exception e) {
|
| 36 | LOGGER.error("Failed to increment chunk count", e);
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | public static void featurePlaced(final ServerLevel level, final Feature feature, final Optional<PlacedFeature> topFeature) {
|
| 41 | try {
|
| 42 | data.get(level).featureData().computeInt(new FeatureCountTracker.FeatureData(feature, topFeature), (f, old) -> old == null ? 1 : old + 1);
|
| 43 | } catch (Exception e) {
|
| 44 | LOGGER.error("Failed to increment feature count", e);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public static void clearCounts() {
|
| 49 | data.invalidateAll();
|
| 50 | LOGGER.debug("Cleared feature counts");
|
| 51 | }
|
| 52 |
|
| 53 | public static void logCounts() {
|
| 54 | LOGGER.debug("Logging feature counts:");
|
| 55 | data.asMap()
|
| 56 | .forEach(
|
| 57 | (level, featureCounts) -> {
|
| 58 | String name = level.dimension().identifier().toString();
|
| 59 | boolean running = level.getServer().isRunning();
|
| 60 | Registry<PlacedFeature> featureRegistry = level.registryAccess().lookupOrThrow(Registries.PLACED_FEATURE);
|
| 61 | String prefix = (running ? "running" : "dead") + " " + name;
|
| 62 | int chunks = featureCounts.chunksWithFeatures().intValue();
|
| 63 | LOGGER.debug("{} total_chunks: {}", prefix, chunks);
|
| 64 | featureCounts.featureData()
|
| 65 | .forEach(
|
| 66 | (data, count) -> LOGGER.debug(
|
| 67 | "{} {} {} {} {}",
|
| 68 | prefix,
|
| 69 | String.format(Locale.ROOT, "%10d", count),
|
| 70 | String.format(Locale.ROOT, "%10f", (double)count / chunks),
|
| 71 | data.topFeature().flatMap(featureRegistry::getResourceKey).map(ResourceKey::identifier),
|
| 72 | data.feature()
|
| 73 | )
|
| 74 | );
|
| 75 | }
|
| 76 | );
|
| 77 | }
|
| 78 |
|
| 79 | private record FeatureData(Feature feature, Optional<PlacedFeature> topFeature) {
|
| 80 | }
|
| 81 |
|
| 82 | private record LevelData(Object2IntMap<FeatureCountTracker.FeatureData> featureData, MutableInt chunksWithFeatures) {
|
| 83 | }
|
| 84 | }
|
| 85 |
|