Fixed config load order with custom enchants

This commit is contained in:
Auxilor 2022-01-08 13:45:47 +00:00
parent abd1b4d009
commit 03327db98b
2 changed files with 27 additions and 39 deletions

View File

@ -1,6 +1,7 @@
package com.willfp.ecoenchants.enchantments;
import com.willfp.eco.core.Prerequisite;
import com.willfp.eco.core.config.interfaces.Config;
import com.willfp.eco.core.fast.FastItemStack;
import com.willfp.eco.core.requirement.Requirement;
import com.willfp.eco.core.requirement.Requirements;
@ -33,6 +34,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
@ -176,21 +178,25 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Watche
@Getter
private final List<String> requirementLore = new ArrayList<>();
/**
* Create a new EcoEnchant.
*
* @param key The key name of the enchantment
* @param type The type of the enchantment
* @param prerequisites Optional {@link Prerequisite}s that must be met
*/
protected EcoEnchant(@NotNull final String key,
@NotNull final EnchantmentType type,
@Nullable final Config overrideConfig,
@NotNull final Prerequisite... prerequisites) {
super(NamespacedKey.minecraft(key));
this.type = type;
this.permissionName = key.replace("_", "");
this.config = generateConfig();
this.config = new EnchantmentConfig(
Objects.requireNonNullElseGet(overrideConfig, () -> new BaseEnchantmentConfig(
this.permissionName,
this.getClass(),
this,
this.getPlugin()
)),
this.permissionName,
this,
this.getPlugin()
);
if (Bukkit.getPluginManager().getPermission("ecoenchants.fromtable." + permissionName) == null) {
Permission permission = new Permission(
@ -221,18 +227,17 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Watche
EcoEnchants.addNewEcoEnchant(this);
}
protected EnchantmentConfig generateConfig() {
return new EnchantmentConfig(
new BaseEnchantmentConfig(
this.permissionName,
this.getClass(),
this,
this.getPlugin()
),
this.permissionName,
this,
this.getPlugin()
);
/**
* Create a new EcoEnchant.
*
* @param key The key name of the enchantment
* @param type The type of the enchantment
* @param prerequisites Optional {@link Prerequisite}s that must be met
*/
protected EcoEnchant(@NotNull final String key,
@NotNull final EnchantmentType type,
@NotNull final Prerequisite... prerequisites) {
this(key, type, null, prerequisites);
}
/**

View File

@ -1,7 +1,6 @@
package com.willfp.ecoenchants.enchantments.custom;
import com.willfp.eco.core.config.interfaces.Config;
import com.willfp.ecoenchants.config.configs.EnchantmentConfig;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import org.jetbrains.annotations.NotNull;
@ -12,11 +11,6 @@ import java.util.Map;
import java.util.Set;
public class CustomEcoEnchant extends EcoEnchant {
/**
* The config.
*/
private final Config config;
/**
* The levels.
*/
@ -29,14 +23,13 @@ public class CustomEcoEnchant extends EcoEnchant {
*/
public CustomEcoEnchant(@NotNull final Config config) {
super(
config.getString("id"), EnchantmentType.getByName(config.getString("type").toUpperCase())
config.getString("id"), EnchantmentType.getByName(config.getString("type").toUpperCase()), config
);
this.config = config;
this.levels = new HashMap<>();
int i = 1;
for (Config levelConfig : this.config.getSubsections("levels")) {
for (Config levelConfig : config.getSubsections("levels")) {
levels.put(i, new CustomEcoEnchantLevel(this, levelConfig));
i++;
}
@ -61,16 +54,6 @@ public class CustomEcoEnchant extends EcoEnchant {
return new HashSet<>(levels.values());
}
@Override
protected EnchantmentConfig generateConfig() {
return new EnchantmentConfig(
this.config,
this.getPermissionName(),
this,
this.getPlugin()
);
}
@Override
public int getMaxLevel() {
return this.levels.size();