Config changes

This commit is contained in:
Auxilor
2022-03-28 14:21:09 +01:00
parent d09021707b
commit daab3829bc
10 changed files with 69 additions and 40 deletions

View File

@@ -23,13 +23,31 @@ public abstract class BaseConfig extends LoadableConfigWrapper {
@NotNull final PluginLike plugin,
final boolean removeUnused,
@NotNull final ConfigType type) {
this(configName, plugin, removeUnused, type, true);
}
/**
* Create new Base Config.
*
* @param plugin The plugin or extension.
* @param configName The config name (excluding extension).
* @param removeUnused If unused sections should be removed.
* @param type The config type.
* @param requiresChangeToSave If changes must be applied to save the config.
*/
protected BaseConfig(@NotNull final String configName,
@NotNull final PluginLike plugin,
final boolean removeUnused,
@NotNull final ConfigType type,
final boolean requiresChangeToSave) {
super(Eco.getHandler().getConfigFactory().createUpdatableConfig(
configName,
plugin,
"",
plugin.getClass(),
removeUnused,
type
type,
requiresChangeToSave
));
}
}

View File

@@ -30,8 +30,13 @@ import java.util.Set;
public interface Config extends Cloneable, PlaceholderInjectable {
/**
* Clears cache.
* <p>
* Configs no longer have caches as they have in previous versions.
*/
void clearCache();
@Deprecated(since = "6.31.1", forRemoval = true)
default void clearCache() {
// Do nothing.
}
/**
* Convert the config into readable text.

View File

@@ -20,13 +20,14 @@ public interface ConfigFactory {
/**
* Updatable config.
*
* @param configName The name of the config
* @param plugin The plugin.
* @param subDirectoryPath The subdirectory path.
* @param source The class that owns the resource.
* @param removeUnused Whether keys not present in the default config should be removed on update.
* @param type The config type.
* @param updateBlacklist Substring of keys to not add/remove keys for.
* @param configName The name of the config
* @param plugin The plugin.
* @param subDirectoryPath The subdirectory path.
* @param source The class that owns the resource.
* @param removeUnused Whether keys not present in the default config should be removed on update.
* @param type The config type.
* @param updateBlacklist Substring of keys to not add/remove keys for.
* @param requiresChangesToSave If the config must be changed in order to save the config.
* @return The config implementation.
*/
LoadableConfig createUpdatableConfig(@NotNull String configName,
@@ -35,23 +36,26 @@ public interface ConfigFactory {
@NotNull Class<?> source,
boolean removeUnused,
@NotNull ConfigType type,
boolean requiresChangesToSave,
@NotNull String... updateBlacklist);
/**
* Loadable config.
*
* @param configName The name of the config
* @param plugin The plugin.
* @param subDirectoryPath The subdirectory path.
* @param source The class that owns the resource.
* @param type The config type.
* @param configName The name of the config
* @param plugin The plugin.
* @param subDirectoryPath The subdirectory path.
* @param source The class that owns the resource.
* @param type The config type.
* @param requiresChangesToSave If the config must be changed in order to save the config.
* @return The config implementation.
*/
LoadableConfig createLoadableConfig(@NotNull String configName,
@NotNull PluginLike plugin,
@NotNull String subDirectoryPath,
@NotNull Class<?> source,
@NotNull ConfigType type);
@NotNull ConfigType type,
boolean requiresChangesToSave);
/**
* Create config.

View File

@@ -19,7 +19,7 @@ import java.util.Set;
*
* @param <T> The type of the handle.
*/
@SuppressWarnings("MethodDoesntCallSuperMethod")
@SuppressWarnings({"MethodDoesntCallSuperMethod", "removal"})
public abstract class ConfigWrapper<T extends Config> implements Config {
/**
* Configs from eco have an internal implementation,
@@ -43,6 +43,7 @@ public abstract class ConfigWrapper<T extends Config> implements Config {
}
@Override
@Deprecated(since = "6.31.1", forRemoval = true)
public void clearCache() {
handle.clearCache();
}

View File

@@ -59,6 +59,10 @@ public abstract class AbstractItemStackBuilder<T extends ItemMeta, U extends Abs
* @param base The ItemStack to start with.
*/
protected AbstractItemStackBuilder(@NotNull final ItemStack base) {
if (base.getType() == Material.AIR) {
base.setType(Material.STONE); // Prevents NPEs.
}
this.base = base;
this.meta = (T) base.getItemMeta();
assert meta != null;

View File

@@ -21,10 +21,6 @@ open class EcoConfig(
this.values.putAll(values.normalizeToConfig(this.type))
}
override fun clearCache() {
// No cache
}
override fun toPlaintext(): String {
return configType.toString(this.values)
}
@@ -80,7 +76,6 @@ open class EcoConfig(
path: String,
obj: Any?
) {
this.clearCache()
val nearestPath = path.split(".")[0]
if (path.contains(".")) {
@@ -90,14 +85,10 @@ open class EcoConfig(
return
}
val section = get(nearestPath)
if (section == null) {
values[nearestPath] = EcoConfigSection(this.type)
return set(path, obj)
} else if (section is Config) {
section.set(remainingPath, obj)
return
}
val section = getSubsection(nearestPath) // Creates a section if null, therefore it can be set.
section.set(remainingPath, obj)
values[nearestPath] = section // Set the value
return
}
if (obj == null) {
@@ -108,7 +99,7 @@ open class EcoConfig(
}
override fun getSubsection(path: String): Config {
return getSubsectionOrNull(path) ?: EcoConfigSection(type, mutableMapOf(), injections)
return getSubsectionOrNull(path) ?: EcoConfigSection(type, injections = injections)
}
override fun getSubsectionOrNull(path: String): Config? {
@@ -168,7 +159,6 @@ open class EcoConfig(
override fun injectPlaceholders(placeholders: Iterable<StaticPlaceholder>) {
injections.removeIf { placeholders.any { placeholder -> it.identifier == placeholder.identifier } }
injections.addAll(placeholders)
this.clearCache()
}
override fun getInjectedPlaceholders(): List<StaticPlaceholder> {
@@ -177,7 +167,6 @@ open class EcoConfig(
override fun clearInjectedPlaceholders() {
injections.clear()
this.clearCache()
}
override fun toMap(): MutableMap<String, Any?> {

View File

@@ -28,13 +28,15 @@ object EcoConfigFactory : ConfigFactory {
plugin: PluginLike,
subDirectoryPath: String,
source: Class<*>,
type: ConfigType
type: ConfigType,
requiresChangesToSave: Boolean
): LoadableConfig = EcoLoadableConfig(
type,
configName,
plugin,
subDirectoryPath,
source
source,
requiresChangesToSave
)
override fun createUpdatableConfig(
@@ -44,6 +46,7 @@ object EcoConfigFactory : ConfigFactory {
source: Class<*>,
removeUnused: Boolean,
type: ConfigType,
requiresChangesToSave: Boolean,
vararg updateBlacklist: String
): LoadableConfig = EcoUpdatableConfig(
type,
@@ -52,6 +55,7 @@ object EcoConfigFactory : ConfigFactory {
subDirectoryPath,
source,
removeUnused,
requiresChangesToSave,
*updateBlacklist
)
}

View File

@@ -19,7 +19,8 @@ open class EcoLoadableConfig(
configName: String,
private val plugin: PluginLike,
private val subDirectoryPath: String,
val source: Class<*>
val source: Class<*>,
private val requiresChangesToSave: Boolean
) : EcoConfig(type), LoadableConfig {
private val configFile: File
private val name: String = "$configName.${type.extension}"
@@ -57,8 +58,10 @@ open class EcoLoadableConfig(
@Throws(IOException::class)
override fun save() {
if (!hasChanged) { // In order to preserve comments
return
if (requiresChangesToSave) {
if (!hasChanged) { // In order to preserve comments
return
}
}
if (configFile.delete()) {

View File

@@ -14,12 +14,12 @@ open class EcoUpdatableConfig(
subDirectoryPath: String,
source: Class<*>,
private val removeUnused: Boolean,
requiresChangesToSave: Boolean,
vararg updateBlacklist: String
) : EcoLoadableConfig(type, configName, plugin, subDirectoryPath, source) {
) : EcoLoadableConfig(type, configName, plugin, subDirectoryPath, source, requiresChangesToSave) {
private val updateBlacklist = mutableListOf(*updateBlacklist)
fun update() {
super.clearCache()
this.init(configFile)
val newConfig = configInJar ?: return
if (newConfig.getKeys(true) == this.getKeys(true)) {

View File

@@ -10,5 +10,6 @@ class DataYml(
"data",
plugin,
false,
ConfigType.YAML
ConfigType.YAML,
false
)