From 3a2d9fc37b35a1b3ddd343efc3074f45e338bef9 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Fri, 22 Apr 2022 09:10:24 +0100 Subject: [PATCH] Fixed disabled worlds and requirements not working for custom enchants --- .../ConditionHasEcoEnchantRequirements.java | 25 +++++++++++++ .../custom/ConditionInEcoEnchantWorld.java | 26 ++++++++++++++ .../enchantments/custom/CustomEcoEnchant.java | 7 +++- .../custom/CustomEcoEnchantLevel.java | 35 ++++++++++++++++--- 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/ConditionHasEcoEnchantRequirements.java create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/ConditionInEcoEnchantWorld.java diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/ConditionHasEcoEnchantRequirements.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/ConditionHasEcoEnchantRequirements.java new file mode 100644 index 00000000..5e56fc7f --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/ConditionHasEcoEnchantRequirements.java @@ -0,0 +1,25 @@ +package com.willfp.ecoenchants.enchantments.custom; + +import com.willfp.eco.core.config.interfaces.Config; +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import com.willfp.ecoenchants.enchantments.EcoEnchants; +import com.willfp.libreforge.conditions.Condition; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class ConditionHasEcoEnchantRequirements extends Condition { + /** + * Create new condition. + */ + public ConditionHasEcoEnchantRequirements() { + super("has_ecoenchant_requirements"); + } + + @Override + public boolean isConditionMet(@NotNull final Player player, + @NotNull final Config config) { + EcoEnchant ecoEnchant = EcoEnchants.getByKey(NamespacedKey.minecraft(config.getString("enchant"))); + return ecoEnchant.areRequirementsMet(player); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/ConditionInEcoEnchantWorld.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/ConditionInEcoEnchantWorld.java new file mode 100644 index 00000000..4e731a58 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/ConditionInEcoEnchantWorld.java @@ -0,0 +1,26 @@ +package com.willfp.ecoenchants.enchantments.custom; + +import com.willfp.eco.core.config.interfaces.Config; +import com.willfp.eco.util.ListUtils; +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import com.willfp.ecoenchants.enchantments.EcoEnchants; +import com.willfp.libreforge.conditions.Condition; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class ConditionInEcoEnchantWorld extends Condition { + /** + * Create new condition. + */ + public ConditionInEcoEnchantWorld() { + super("in_ecoenchant_world"); + } + + @Override + public boolean isConditionMet(@NotNull final Player player, + @NotNull final Config config) { + EcoEnchant ecoEnchant = EcoEnchants.getByKey(NamespacedKey.minecraft(config.getString("enchant"))); + return !ListUtils.containsIgnoreCase(ecoEnchant.getDisabledWorldNames(), player.getWorld().getName()); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEcoEnchant.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEcoEnchant.java index 793d1226..e0fb2afb 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEcoEnchant.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEcoEnchant.java @@ -32,7 +32,7 @@ public class CustomEcoEnchant extends EcoEnchant { int i = 1; for (Config levelConfig : config.getSubsections("levels")) { - levels.put(i, new CustomEcoEnchantLevel(this, levelConfig)); + levels.put(i, new CustomEcoEnchantLevel(this, levelConfig, i)); i++; } } @@ -91,4 +91,9 @@ public class CustomEcoEnchant extends EcoEnchant { public boolean isEnabled() { return true; } + + static { + new ConditionInEcoEnchantWorld(); + new ConditionHasEcoEnchantRequirements(); + } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEcoEnchantLevel.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEcoEnchantLevel.java index aa25fa88..3b3a84bb 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEcoEnchantLevel.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEcoEnchantLevel.java @@ -1,6 +1,8 @@ package com.willfp.ecoenchants.enchantments.custom; +import com.willfp.eco.core.config.BuildableConfig; import com.willfp.eco.core.config.interfaces.Config; +import com.willfp.eco.core.placeholder.StaticPlaceholder; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.libreforge.Holder; import com.willfp.libreforge.conditions.Conditions; @@ -36,15 +38,25 @@ public class CustomEcoEnchantLevel implements Holder { @Getter private final String valuePlaceholder; + /** + * The level. + */ + private final int level; + /** * Create custom EcoEnchant level. * * @param parent The parent. * @param config The config. + * @param level The level. */ public CustomEcoEnchantLevel(@NotNull final EcoEnchant parent, - @NotNull final Config config) { + @NotNull final Config config, + final int level) { this.parent = parent; + this.level = level; + + config.injectPlaceholders(new StaticPlaceholder("level", () -> String.valueOf(level))); for (Config cfg : config.getSubsections("effects")) { effects.add(Effects.compile(cfg, "Custom EcoEnchant ID " + parent.getKey().getKey())); @@ -54,6 +66,20 @@ public class CustomEcoEnchantLevel implements Holder { conditions.add(Conditions.compile(cfg, "Custom EcoEnchant ID " + parent.getKey().getKey())); } + conditions.add(Conditions.compile( + new BuildableConfig() + .add("args.enchant", parent.getKey().toString()) + .add("id", "in_ecoenchant_world"), + "EcoEnchants Internals (world) - If you see this message, report it as a bug!" + )); + + conditions.add(Conditions.compile( + new BuildableConfig() + .add("args.enchant", parent.getKey().toString()) + .add("id", "has_ecoenchant_requirements"), + "EcoEnchants Internals (requirements) - If you see this message, report it as a bug!" + )); + this.valuePlaceholder = config.getString("value-placeholder"); } @@ -71,8 +97,9 @@ public class CustomEcoEnchantLevel implements Holder { @Override public String toString() { - return "CustomEcoEnchantLevel{" + - "parent=" + parent + - '}'; + return "CustomEcoEnchantLevel{" + + "parent=" + parent + + ",level=" + level + + '}'; } }