From 96c6e946f9340f6dca3fb162ae175bbccbd29981 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Wed, 28 Aug 2024 20:18:54 -0500 Subject: [PATCH] Add support for storing additional options in Arena config --- .../java/org/battleplugins/arena/Arena.java | 11 ++++++++- .../arena/config/ArenaConfigParser.java | 12 ++++++++++ .../arena/config/ConfigHolder.java | 23 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 plugin/src/main/java/org/battleplugins/arena/config/ConfigHolder.java diff --git a/plugin/src/main/java/org/battleplugins/arena/Arena.java b/plugin/src/main/java/org/battleplugins/arena/Arena.java index 4c6cfc76..27de1a81 100644 --- a/plugin/src/main/java/org/battleplugins/arena/Arena.java +++ b/plugin/src/main/java/org/battleplugins/arena/Arena.java @@ -9,6 +9,7 @@ import org.battleplugins.arena.competition.phase.CompetitionPhaseType; import org.battleplugins.arena.competition.victory.VictoryConditionType; import org.battleplugins.arena.config.ArenaOption; +import org.battleplugins.arena.config.ConfigHolder; import org.battleplugins.arena.config.DocumentationSource; import org.battleplugins.arena.config.Scoped; import org.battleplugins.arena.config.context.EventContextProvider; @@ -26,9 +27,11 @@ import org.battleplugins.arena.resolver.Resolver; import org.battleplugins.arena.resolver.ResolverKeys; import org.battleplugins.arena.resolver.ResolverProvider; +import org.bukkit.configuration.ConfigurationSection; import org.jetbrains.annotations.Nullable; import java.nio.file.Path; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -46,7 +49,7 @@ * active game actions will occur (i.e. game timer, score, etc.) */ @DocumentationSource("https://docs.battleplugins.org/books/user-guide/chapter/configuration") -public class Arena implements ArenaLike, ArenaListener, Resolvable { +public class Arena implements ArenaLike, ArenaListener, Resolvable, ConfigHolder { @Scoped private BattleArena plugin; @@ -103,6 +106,7 @@ public class Arena implements ArenaLike, ArenaListener, Resolvable { private List modules; private final ArenaEventManager eventManager; + private final Map config = new HashMap<>(); public Arena() { this.eventManager = new ArenaEventManager(this); @@ -316,6 +320,11 @@ public final E getOption return (E) this.options.get(type); } + @Override + public final Map getConfig() { + return this.config; + } + @Override public final Arena getArena() { return this; diff --git a/plugin/src/main/java/org/battleplugins/arena/config/ArenaConfigParser.java b/plugin/src/main/java/org/battleplugins/arena/config/ArenaConfigParser.java index b7f7d67b..ed657275 100644 --- a/plugin/src/main/java/org/battleplugins/arena/config/ArenaConfigParser.java +++ b/plugin/src/main/java/org/battleplugins/arena/config/ArenaConfigParser.java @@ -61,6 +61,7 @@ public static T newInstance(@Nullable Path sourceFile, Class type, Config } private static void populateFields(@Nullable Path sourceFile, Object instance, ConfigurationSection configuration, @Nullable Object scope, @Nullable Object id) throws ParseException { + List sectionsParsed = new ArrayList<>(); for (Field field : FieldUtils.getAllFieldsList(instance.getClass())) { field.setAccessible(true); if (field.isAnnotationPresent(Scoped.class)) { @@ -121,6 +122,17 @@ private static void populateFields(@Nullable Path sourceFile, Object instance, C // Get the type from the configuration populateType(sourceFile, field, arenaOption, instance, configuration, scope); + + sectionsParsed.add(name); + } + + // Check if there are any sections that were not parsed + if (instance instanceof ConfigHolder holder) { + for (String key : configuration.getKeys(false)) { + if (!sectionsParsed.contains(key) && configuration.isConfigurationSection(key)) { + holder.getConfig().put(key, configuration.getConfigurationSection(key)); + } + } } } diff --git a/plugin/src/main/java/org/battleplugins/arena/config/ConfigHolder.java b/plugin/src/main/java/org/battleplugins/arena/config/ConfigHolder.java new file mode 100644 index 00000000..06f46b53 --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/config/ConfigHolder.java @@ -0,0 +1,23 @@ +package org.battleplugins.arena.config; + +import org.bukkit.configuration.ConfigurationSection; + +import java.util.Map; + +/** + * A holder for a set of config sections. + *

+ * This is typically implemented by classes in which + * there are unparsed config sections, typically accessed + * by third parties, that need to be stored and accessed + * in a less verbose manner. + */ +public interface ConfigHolder { + + /** + * Gets the config sections stored in this holder. + * + * @return the config sections stored in this holder + */ + Map getConfig(); +}