Skip to content

Commit

Permalink
Add support for storing additional options in Arena config
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Aug 29, 2024
1 parent f33cc23 commit 96c6e94
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
11 changes: 10 additions & 1 deletion plugin/src/main/java/org/battleplugins/arena/Arena.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -103,6 +106,7 @@ public class Arena implements ArenaLike, ArenaListener, Resolvable {
private List<String> modules;

private final ArenaEventManager eventManager;
private final Map<String, ConfigurationSection> config = new HashMap<>();

public Arena() {
this.eventManager = new ArenaEventManager(this);
Expand Down Expand Up @@ -316,6 +320,11 @@ public final <E extends org.battleplugins.arena.options.ArenaOption> E getOption
return (E) this.options.get(type);
}

@Override
public final Map<String, ConfigurationSection> getConfig() {
return this.config;
}

@Override
public final Arena getArena() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static <T> T newInstance(@Nullable Path sourceFile, Class<T> type, Config
}

private static void populateFields(@Nullable Path sourceFile, Object instance, ConfigurationSection configuration, @Nullable Object scope, @Nullable Object id) throws ParseException {
List<String> sectionsParsed = new ArrayList<>();
for (Field field : FieldUtils.getAllFieldsList(instance.getClass())) {
field.setAccessible(true);
if (field.isAnnotationPresent(Scoped.class)) {
Expand Down Expand Up @@ -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));
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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<String, ConfigurationSection> getConfig();
}

0 comments on commit 96c6e94

Please sign in to comment.