From 4f03b2e4e379581a144029d979c995686e217a61 Mon Sep 17 00:00:00 2001 From: filoghost Date: Sun, 15 Aug 2021 09:54:22 +0200 Subject: [PATCH] Make animation options required --- .../plugin/config/AnimationConfig.java | 16 ++++++------ .../plugin/config/ConfigManager.java | 25 +++++++++++-------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/AnimationConfig.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/AnimationConfig.java index e2c288ae..3f6677aa 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/AnimationConfig.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/AnimationConfig.java @@ -5,18 +5,20 @@ */ package me.filoghost.holographicdisplays.plugin.config; -import me.filoghost.fcommons.config.mapped.MappedConfig; -import me.filoghost.fcommons.config.mapped.Path; +import me.filoghost.fcommons.config.ConfigSection; +import me.filoghost.fcommons.config.exception.ConfigValueException; import java.util.List; -public class AnimationConfig implements MappedConfig { +public class AnimationConfig { - @Path("interval-seconds") - private double intervalSeconds; + private final double intervalSeconds; + private final List frames; - @Path("animation-frames") - private List frames; + public AnimationConfig(ConfigSection configSection) throws ConfigValueException { + this.intervalSeconds = configSection.getRequiredDouble("interval-seconds"); + this.frames = configSection.getRequiredStringList("animation-frames"); + } public int getIntervalTicks() { return Math.max((int) (intervalSeconds * 20.0), 1); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/ConfigManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/ConfigManager.java index ab500700..00b0c200 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/ConfigManager.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/ConfigManager.java @@ -13,6 +13,7 @@ import me.filoghost.fcommons.config.FileConfig; import me.filoghost.fcommons.config.exception.ConfigException; import me.filoghost.fcommons.config.exception.ConfigLoadException; import me.filoghost.fcommons.config.exception.ConfigSaveException; +import me.filoghost.fcommons.config.exception.ConfigValueException; import me.filoghost.fcommons.config.mapped.MappedConfigLoader; import me.filoghost.fcommons.logging.ErrorCollector; import me.filoghost.fcommons.logging.Log; @@ -47,7 +48,7 @@ public class ConfigManager extends BaseConfigManager { try { mainConfig = mainConfigLoader.init(); } catch (ConfigException e) { - logConfigInitException(errorCollector, mainConfigLoader.getFile(), e); + logConfigException(errorCollector, mainConfigLoader.getFile(), e); mainConfig = new SettingsModel(); // Fallback: use default values } @@ -60,7 +61,7 @@ public class ConfigManager extends BaseConfigManager { try { databaseConfig = databaseConfigLoader.init(); } catch (ConfigException e) { - logConfigInitException(errorCollector, databaseConfigLoader.getFile(), e); + logConfigException(errorCollector, databaseConfigLoader.getFile(), e); databaseConfig = new Config(); // Fallback: empty config } @@ -96,10 +97,13 @@ public class ConfigManager extends BaseConfigManager { animationFiles.filter(this::isYamlFile).forEach(file -> { try { String fileName = file.getFileName().toString(); - AnimationPlaceholder animationPlaceholder = loadAnimation(file); + AnimationConfig animationConfig = loadAnimation(file); + AnimationPlaceholder animationPlaceholder = new AnimationPlaceholder( + animationConfig.getIntervalTicks(), + animationConfig.getFrames()); animationsByFileName.put(fileName, animationPlaceholder); - } catch (ConfigLoadException e) { - logConfigInitException(errorCollector, file, e); + } catch (ConfigException e) { + logConfigException(errorCollector, file, e); } }); } @@ -110,10 +114,9 @@ public class ConfigManager extends BaseConfigManager { return new AnimationPlaceholderFactory(animationsByFileName); } - private AnimationPlaceholder loadAnimation(Path animationFile) throws ConfigLoadException { - MappedConfigLoader animationConfigLoader = getMappedConfigLoader(animationFile, AnimationConfig.class); - AnimationConfig animationConfig = animationConfigLoader.load(); - return new AnimationPlaceholder(animationConfig.getIntervalTicks(), animationConfig.getFrames()); + private AnimationConfig loadAnimation(Path animationFile) throws ConfigLoadException, ConfigValueException { + FileConfig animationFileConfig = getConfigLoader(animationFile).load(); + return new AnimationConfig(animationFileConfig); } public void reloadStaticReplacements(ErrorCollector errorCollector) { @@ -122,7 +125,7 @@ public class ConfigManager extends BaseConfigManager { try { staticReplacementsConfig = staticReplacementsConfigLoader.init(); } catch (ConfigException e) { - logConfigInitException(errorCollector, staticReplacementsConfigLoader.getFile(), e); + logConfigException(errorCollector, staticReplacementsConfigLoader.getFile(), e); staticReplacementsConfig = new FileConfig(staticReplacementsConfigLoader.getFile()); // Fallback: empty config } @@ -141,7 +144,7 @@ public class ConfigManager extends BaseConfigManager { return FileUtils.hasFileExtension(file, "yml", "yaml"); } - private void logConfigInitException(ErrorCollector errorCollector, Path file, ConfigException e) { + private void logConfigException(ErrorCollector errorCollector, Path file, ConfigException e) { errorCollector.add(e, "error while loading config file \"" + formatPath(file) + "\""); }