From f9159cf245a8b3527346fdc88cbfe8a8a68c3707 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 3 Apr 2021 14:26:27 +0100 Subject: [PATCH] Added setters to config --- .../eco/internal/config/ConfigWrapper.java | 44 ++++++++++--------- .../internal/config/LoadableYamlConfig.java | 10 ++--- .../internal/config/UpdatableYamlConfig.java | 14 +++--- .../com/willfp/eco/util/config/Config.java | 12 ++++- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/eco-api/src/main/java/com/willfp/eco/internal/config/ConfigWrapper.java b/eco-api/src/main/java/com/willfp/eco/internal/config/ConfigWrapper.java index 1623f145..7dbcbeb1 100644 --- a/eco-api/src/main/java/com/willfp/eco/internal/config/ConfigWrapper.java +++ b/eco-api/src/main/java/com/willfp/eco/internal/config/ConfigWrapper.java @@ -2,11 +2,9 @@ package com.willfp.eco.internal.config; import com.willfp.eco.util.StringUtils; import com.willfp.eco.util.config.Config; -import lombok.AccessLevel; import lombok.Getter; import org.apache.commons.lang.Validate; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.MemorySection; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -19,10 +17,10 @@ import java.util.Objects; @SuppressWarnings({"unchecked", "unused"}) public abstract class ConfigWrapper implements Config { /** - * The linked {@link MemorySection} where values are physically stored. + * The linked {@link ConfigurationSection} where values are physically stored. */ - @Getter(AccessLevel.PROTECTED) - private T config = null; + @Getter + private T handle = null; /** * Cached values for faster reading. @@ -37,7 +35,7 @@ public abstract class ConfigWrapper implements C } protected Config init(@NotNull final T config) { - this.config = config; + this.handle = config; return this; } @@ -48,19 +46,25 @@ public abstract class ConfigWrapper implements C @Override public boolean has(@NotNull final String path) { - return config.contains(path); + return handle.contains(path); } @NotNull @Override public List getKeys(final boolean deep) { - return new ArrayList<>(config.getKeys(deep)); + return new ArrayList<>(handle.getKeys(deep)); } @Override @Nullable - public Object getRaw(@NotNull final String path) { - return config.get(path); + public Object get(@NotNull final String path) { + return handle.get(path); + } + + @Override + public void set(@NotNull final String path, + @Nullable final Object object) { + handle.set(path, object); } @Override @@ -77,7 +81,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (Config) cache.get(path); } else { - cache.put(path, new ConfigSection(Objects.requireNonNull(config.getConfigurationSection(path)))); + cache.put(path, new ConfigSection(Objects.requireNonNull(handle.getConfigurationSection(path)))); return getSubsectionOrNull(path); } } @@ -87,7 +91,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (int) cache.get(path); } else { - cache.put(path, config.getInt(path, 0)); + cache.put(path, handle.getInt(path, 0)); return getInt(path); } } @@ -108,7 +112,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (int) cache.get(path); } else { - cache.put(path, config.getInt(path, def)); + cache.put(path, handle.getInt(path, def)); return getInt(path); } } @@ -119,7 +123,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (List) cache.get(path); } else { - cache.put(path, has(path) ? new ArrayList<>(config.getIntegerList(path)) : new ArrayList<>()); + cache.put(path, has(path) ? new ArrayList<>(handle.getIntegerList(path)) : new ArrayList<>()); return getInts(path); } } @@ -139,7 +143,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (boolean) cache.get(path); } else { - cache.put(path, config.getBoolean(path)); + cache.put(path, handle.getBoolean(path)); return getBool(path); } } @@ -160,7 +164,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (List) cache.get(path); } else { - cache.put(path, has(path) ? new ArrayList<>(config.getBooleanList(path)) : new ArrayList<>()); + cache.put(path, has(path) ? new ArrayList<>(handle.getBooleanList(path)) : new ArrayList<>()); return getBools(path); } } @@ -181,7 +185,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (String) cache.get(path); } else { - cache.put(path, StringUtils.translate(Objects.requireNonNull(config.getString(path, "")))); + cache.put(path, StringUtils.translate(Objects.requireNonNull(handle.getString(path, "")))); return getString(path); } } @@ -202,7 +206,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (List) cache.get(path); } else { - cache.put(path, has(path) ? new ArrayList<>(config.getStringList(path)) : new ArrayList<>()); + cache.put(path, has(path) ? new ArrayList<>(handle.getStringList(path)) : new ArrayList<>()); return getStrings(path); } } @@ -222,7 +226,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (double) cache.get(path); } else { - cache.put(path, config.getDouble(path)); + cache.put(path, handle.getDouble(path)); return getDouble(path); } } @@ -243,7 +247,7 @@ public abstract class ConfigWrapper implements C if (cache.containsKey(path)) { return (List) cache.get(path); } else { - cache.put(path, has(path) ? new ArrayList<>(config.getDoubleList(path)) : new ArrayList<>()); + cache.put(path, has(path) ? new ArrayList<>(handle.getDoubleList(path)) : new ArrayList<>()); return getDoubles(path); } } diff --git a/eco-api/src/main/java/com/willfp/eco/internal/config/LoadableYamlConfig.java b/eco-api/src/main/java/com/willfp/eco/internal/config/LoadableYamlConfig.java index 5b5d3895..ce45f08e 100644 --- a/eco-api/src/main/java/com/willfp/eco/internal/config/LoadableYamlConfig.java +++ b/eco-api/src/main/java/com/willfp/eco/internal/config/LoadableYamlConfig.java @@ -5,7 +5,6 @@ import lombok.AccessLevel; import lombok.Getter; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; -import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import java.io.BufferedReader; @@ -147,12 +146,11 @@ public abstract class LoadableYamlConfig extends ConfigWrapper { - if (!this.getConfig().getKeys(true).contains(s)) { + if (!this.getHandle().getKeys(true).contains(s)) { if (updateBlacklist.stream().noneMatch(s::contains)) { - this.getConfig().set(s, newConfig.get(s)); + this.getHandle().set(s, newConfig.get(s)); } } })); if (this.removeUnused) { - this.getConfig().getKeys(true).forEach((s -> { + this.getHandle().getKeys(true).forEach((s -> { if (!newConfig.getKeys(true).contains(s)) { if (updateBlacklist.stream().noneMatch(s::contains)) { - this.getConfig().set(s, null); + this.getHandle().set(s, null); } } })); } - this.getConfig().save(this.getConfigFile()); + this.getHandle().save(this.getConfigFile()); } catch (IOException | InvalidConfigurationException e) { e.printStackTrace(); } diff --git a/eco-api/src/main/java/com/willfp/eco/util/config/Config.java b/eco-api/src/main/java/com/willfp/eco/util/config/Config.java index 7cd91920..c9f53d87 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/config/Config.java +++ b/eco-api/src/main/java/com/willfp/eco/util/config/Config.java @@ -36,7 +36,17 @@ public interface Config { * @return The object. */ @Nullable - Object getRaw(@NotNull String path); + Object get(@NotNull String path); + + /** + * Set an object in config. + * Default implementations call {@link org.bukkit.configuration.file.YamlConfiguration#set(String, Object)} + * + * @param path The path. + * @param object The object. + */ + void set(@NotNull String path, + @Nullable Object object); /** * Get subsection from config.