LoParkour/src/main/java/dev/loki/loparkour/adaptive/bootstrap/AdaptiveServices.java
loki5512344 c120309663
refactor(config): encapsulate option sections, drop the Option mirror
Option was a facade of 45 public static fields kept in sync with the four
sections by syncFields(); the sections themselves also exposed public static
mutable fields. 88 VisibilityModifier violations in one place and two owners
for every value.

- Option keeps only init(boolean) (the reload entry ConfigLoader calls);
  the mirrors, syncFields() and the unused @Deprecated initStyles are gone
- sections hold private static state behind fluent accessors (OptionSQL.enabled(),
  OptionGeneration.cleanupInterval(), OptionGeneral.goBackLocation()); the two
  Option.SQL = false writes become OptionSQL.disable()
- collection/location accessors hand out unmodifiable views and a Location copy,
  so no caller can mutate loaded config state
- ParticleShape promoted from nested enum to config/options/ParticleShape
- delete 17 never-read generation probability fields and initGenerationProbabilities():
  live weights come from advanced.* via GeneratorProfileManager/GenerationProfileLoader;
  min-y/max-y guard moved 1:1 into initBounds()
- rewrite 63 Option.<FIELD> reads in 19 files to the section accessors
- validateConfigs() now checks the paths the code actually consumes
  (advanced.distance/height/type weights) instead of the dead generation.normal.*
- ParkourRestrictionListener.onChat: getOrDefault(CHAT, true) instead of unboxing
  a Boolean that may be absent

checkstyle 0 errors / 329 -> 240 warnings (VisibilityModifier 163 -> 75);
config/options itself is clean. Dead config keys found on the way are filed as
TODO items 164-166. ./gradlew check green: PMD 0, 94 tests, NullAway unchanged.
2026-10-06 15:54:31 +02:00

172 lines
5.7 KiB
Java

package dev.loki.loparkour.adaptive.bootstrap;
import dev.loki.loparkour.adaptive.core.AdaptiveDifficulty;
import dev.loki.loparkour.adaptive.core.MetricsCollector;
import dev.loki.loparkour.adaptive.model.AdaptiveConfig;
import dev.loki.loparkour.adaptive.model.SkillRating;
import dev.loki.loparkour.adaptive.storage.FileStatsStorage;
import dev.loki.loparkour.adaptive.storage.SQLStatsStorage;
import dev.loki.loparkour.adaptive.storage.StatsRepository;
import dev.loki.loparkour.config.options.section.OptionSQL;
import dev.loki.loparkour.generator.core.coordinator.ParkourGenerator;
import dev.loki.loparkour.generator.core.model.Profile;
import dev.loki.loparkour.player.core.ParkourPlayer;
import dev.loki.loparkour.storage.Storage;
import dev.loki.loparkour.storage.sql.StorageSQL;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
/**
* Bootstrap for the adaptive difficulty subsystem.
*/
public final class AdaptiveServices {
@Nullable
private static Plugin plugin;
@Nullable
private static StatsRepository repository;
@Nullable
private static MetricsCollector metricsCollector;
private AdaptiveServices() {
}
public static void init(@NotNull Plugin pluginInstance) {
plugin = pluginInstance;
AdaptiveConfig.init();
if (!AdaptiveConfig.isEnabled()) {
return;
}
final Runnable setup = () -> startCollector(createRepository());
if (OptionSQL.enabled()) {
Storage.runWhenReady(setup);
} else {
setup.run();
}
}
public static void reload() {
shutdownCollector();
AdaptiveConfig.reload();
if (plugin != null && AdaptiveConfig.isEnabled()) {
final Runnable setup = () -> startCollector(createRepository());
if (OptionSQL.enabled()) {
Storage.runWhenReady(setup);
} else {
setup.run();
}
}
}
public static void attachToGenerator(@NotNull ParkourGenerator generator) {
if (metricsCollector != null) {
generator.registerEventListener(metricsCollector);
}
}
/**
* Applies adaptive difficulty weights to a profile being built for a generator.
*/
public static void applyAdaptiveProfile(@NotNull ParkourGenerator generator, @NotNull Profile profile) {
if (!AdaptiveConfig.isEnabled() || metricsCollector == null) {
return;
}
final double target = AdaptiveDifficulty.resolveTargetDifficulty(generator, AdaptiveServices::resolveSkillRating);
AdaptiveDifficulty.applyToProfile(profile, target);
}
/**
* Updates stored skill rating from current session metrics after a fall.
*/
public static void persistRatingAfterFall(@NotNull ParkourPlayer player) {
if (!AdaptiveConfig.isEnabled() || repository == null || metricsCollector == null) {
return;
}
final UUID uuid = player.getUUID();
final SkillRating session = AdaptiveDifficulty.analyzeSession(player, metricsCollector);
final SkillRating stored = repository.loadSkillRating(uuid);
if (stored != null) {
stored.updateRating(session.getRating(), AdaptiveConfig.getRatingWeight());
repository.saveSkillRating(stored);
} else {
repository.saveSkillRating(session);
}
}
/**
* Rebuilds generator profile/chances from latest ratings (call once per fall event).
*/
public static void refreshGeneratorDifficulty(@NotNull ParkourGenerator generator) {
if (!AdaptiveConfig.isEnabled()) {
return;
}
generator.profileManager.recalculateProfile();
}
public static void unloadPlayer(@NotNull UUID playerUuid) {
if (metricsCollector != null) {
metricsCollector.unloadPlayer(playerUuid);
}
}
public static void shutdown() {
shutdownCollector();
repository = null;
}
@NotNull
private static SkillRating resolveSkillRating(@NotNull ParkourPlayer player) {
final UUID uuid = player.getUUID();
final SkillRating stored = repository != null ? repository.loadSkillRating(uuid) : null;
if (metricsCollector == null) {
return stored != null ? stored : new SkillRating(uuid);
}
final var metrics = metricsCollector.getMetrics(uuid);
if (metrics.getTotalJumps() < AdaptiveConfig.getMinSessions()) {
if (stored != null && stored.isReliable()) {
return stored;
}
return stored != null ? stored : new SkillRating(uuid);
}
final SkillRating session = AdaptiveDifficulty.analyzeSession(player, metricsCollector);
if (stored == null) {
return session;
}
return AdaptiveDifficulty.mergeSessionRating(stored, session);
}
@NotNull
private static StatsRepository createRepository() {
if (OptionSQL.enabled() && StorageSQL.isConnected()) {
return new SQLStatsStorage(StorageSQL.getConnectionManager());
}
return new FileStatsStorage();
}
private static void startCollector(@NotNull StatsRepository repo) {
if (plugin == null) {
return;
}
repository = repo;
metricsCollector = new MetricsCollector(plugin, repo);
metricsCollector.startAutoSave();
}
private static void shutdownCollector() {
if (metricsCollector != null) {
metricsCollector.stopAutoSave();
metricsCollector.saveAllMetrics();
metricsCollector = null;
}
}
}