HungerGames/src/main/java/com/alttd/hunger_games/config/Config.java

136 lines
5.6 KiB
Java
Raw Normal View History

2026-05-23 21:06:30 +00:00
package com.alttd.hunger_games.config;
import com.alttd.hunger_games.data_objects.GameStage;
2026-05-23 21:06:30 +00:00
import lombok.extern.slf4j.Slf4j;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
2026-05-23 21:06:30 +00:00
import java.io.File;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
2026-05-23 21:06:30 +00:00
@Slf4j
public class Config extends AbstractConfig {
static Config config;
Config() {
super(
new File(File.separator
+ "mnt" + File.separator
+ "configs" + File.separator
+ "HungerGames"),
"config.yml");
}
public static void reload() {
log.info("Reloading config");
config = new Config();
config.readConfig(Config.class, null);
}
public static class SETTINGS {
private static final String prefix = "settings.";
@SuppressWarnings("unused")
private static void load() {
}
}
public static class ROUND {
private static final String prefix = "round.";
public static Duration COUNTDOWN = Duration.ofSeconds(10);
public static int INITIAL_BORDER_SIZE = 5000;
public static List<GameStage> STAGES = List.of(
GameStage.builder().duration(Duration.ofMinutes(5)).worldBorderSize(1000).build(),
GameStage.builder().duration(Duration.ofMinutes(5)).worldBorderSize(750).build(),
GameStage.builder().duration(Duration.ofMinutes(5)).worldBorderSize(500).build()
);
@SuppressWarnings("unused")
private static void load() {
int countdownSeconds = config.getInt(prefix, "countdown-seconds", Math.toIntExact(COUNTDOWN.toSeconds()));
COUNTDOWN = Duration.ofSeconds(countdownSeconds);
INITIAL_BORDER_SIZE = config.getInt(prefix, "initial-border-size", INITIAL_BORDER_SIZE);
ConfigurationSection configurationSection = config.getConfigurationSection(prefix + "stages");
if (configurationSection == null) {
configurationSection = config.createSection(prefix + "stages");
}
Set<String> keys = configurationSection.getKeys(false);
STAGES = getGameStages(keys, configurationSection);
}
private static List<GameStage> getGameStages(Set<String> keys, ConfigurationSection configurationSection) {
if (keys.isEmpty()) {
int key = 0;
for (GameStage stage : STAGES) {
ConfigurationSection section = configurationSection.createSection(String.valueOf(key++));
section.set("duration-seconds", stage.getDuration().toSeconds());
section.set("world-border-size", stage.getWorldBorderSize());
}
return STAGES;
}
List<GameStage> gameStageList = new ArrayList<>();
for (String key : keys) {
ConfigurationSection section = configurationSection.getConfigurationSection(key);
if (section == null) {
throw new IllegalStateException("Stage section is null for key [" + key + "] but is in the list of retrieved keys");
}
if (!section.contains("duration-seconds")) {
log.error("Missing duration-seconds in stage {}.", key);
continue;
}
if (!section.contains("world-border-size")) {
log.error("Missing world-border-size in stage {}.", key);
continue;
}
int durationSeconds = section.getInt("duration-seconds");
int worldBorderSize = section.getInt("world-border-size");
gameStageList.add(GameStage.builder()
.duration(Duration.ofSeconds(durationSeconds))
.worldBorderSize(worldBorderSize)
.build());
}
return gameStageList;
}
}
public static class DESTINATION {
private static final String prefix = "destination.";
public static Location START_CENTER = null;
public static int START_RADIUS = 15;
public static Location FINALE_CENTER = null;
public static int FINALE_RADIUS = 10;
public static Location SPECTATOR_AREA = null;
public static int STUCK_MIN_HEIGHT = 64;
public static int STUCK_FIREWORK_COUNT = 3;
public static Duration STUCK_COOLDOWN = Duration.ofMinutes(2);
@SuppressWarnings("unused")
private static void load() {
Config.DESTINATION.START_RADIUS = config.getInt(prefix, "start-radius", START_RADIUS);
config.getLocation(prefix, "start-center")
.ifPresent(location -> DESTINATION.START_CENTER = location);
config.getLocation(prefix, "spectator-area")
.ifPresent(location -> DESTINATION.SPECTATOR_AREA = location);
DESTINATION.FINALE_RADIUS = config.getInt(prefix, "finale-radius", FINALE_RADIUS);
config.getLocation(prefix, "finale-center")
.ifPresent(location -> DESTINATION.FINALE_CENTER = location);
STUCK_MIN_HEIGHT = config.getInt(prefix, "stuck-min-height", STUCK_MIN_HEIGHT);
STUCK_FIREWORK_COUNT = config.getInt(prefix, "stuck-firework-count", STUCK_FIREWORK_COUNT);
STUCK_COOLDOWN = Duration.ofSeconds(config.getInt(prefix, "stuck-cooldown-seconds", (int) STUCK_COOLDOWN.toSeconds()));
}
}
2026-05-23 21:06:30 +00:00
}