From 7c0e17f1f33051614934b0e8844adea86d2b9ffc Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 28 Nov 2020 09:40:53 +0000 Subject: [PATCH] Reworked EnchantmentType into instantiatable system --- .../ecoenchants/display/EnchantDisplay.java | 10 ---- .../ecoenchants/display/EnchantmentCache.java | 18 +----- .../ecoenchants/enchantments/EcoEnchant.java | 56 +++++++++++++------ .../support/merging/anvil/AnvilMerge.java | 2 +- .../placeholder/PlaceholderEntry.java | 8 +-- .../util/interfaces/ObjectBiCallable.java | 6 ++ .../util/interfaces/ObjectCallable.java | 4 +- 7 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/util/interfaces/ObjectBiCallable.java diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java index a4118186..895d7fa0 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java @@ -55,12 +55,7 @@ public class EnchantDisplay { public static final String PREFIX = "§w"; - static String normalColor; - static String curseColor; - static String specialColor; - static String artifactColor; static String descriptionColor; - static String spellColor; static int numbersThreshold; static boolean useNumerals; @@ -78,11 +73,6 @@ public class EnchantDisplay { */ public static void update() { descriptionColor = StringUtils.translate(ConfigManager.getLang().getString("description-color")); - curseColor = StringUtils.translate(ConfigManager.getLang().getString("curse-color")); - specialColor = StringUtils.translate(ConfigManager.getLang().getString("special-color")); - artifactColor = StringUtils.translate(ConfigManager.getLang().getString("artifact-color")); - spellColor = StringUtils.translate(ConfigManager.getLang().getString("spell-color")); - normalColor = StringUtils.translate(ConfigManager.getLang().getString("not-curse-color")); useNumerals = ConfigManager.getConfig().getBool("lore.use-numerals"); numbersThreshold = ConfigManager.getConfig().getInt("lore.use-numbers-above-threshold"); diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java index 097120e4..0df5e36d 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java @@ -52,23 +52,7 @@ public class EnchantmentCache { type = enchantment.isCursed() ? EcoEnchant.EnchantmentType.CURSE : EcoEnchant.EnchantmentType.NORMAL; } - switch(type) { - case ARTIFACT: - color = EnchantDisplay.artifactColor; - break; - case SPECIAL: - color = EnchantDisplay.specialColor; - break; - case CURSE: - color = EnchantDisplay.curseColor; - break; - case SPELL: - color = EnchantDisplay.spellColor; - break; - default: - color = EnchantDisplay.normalColor; - break; - } + color = type.getColor(); if(EcoEnchants.getFromEnchantment(enchantment) != null) { EnchantmentRarity rarity = EcoEnchants.getFromEnchantment(enchantment).getRarity(); diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java index 7bd573f4..de807705 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java @@ -10,8 +10,10 @@ import com.willfp.ecoenchants.integrations.placeholder.PlaceholderEntry; import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager; import com.willfp.ecoenchants.util.NumberUtils; import com.willfp.ecoenchants.util.StringUtils; +import com.willfp.ecoenchants.util.interfaces.ObjectCallable; import com.willfp.ecoenchants.util.interfaces.Registerable; import com.willfp.ecoenchants.util.optional.Prerequisite; +import jdk.nashorn.internal.codegen.ObjectClassGenerator; import org.apache.commons.lang.WordUtils; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -433,24 +435,43 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist return targetMaterials.contains(itemStack.getType()) || itemStack.getType().equals(Material.BOOK) || itemStack.getType().equals(Material.ENCHANTED_BOOK); } - /** - * The types of {@link EcoEnchant} - */ - public enum EnchantmentType { - NORMAL(false), - CURSE(false), - SPECIAL(true), - ARTIFACT(true), - SPELL(true); + public static class EnchantmentType { + public static final EnchantmentType NORMAL = new EnchantmentType(false, () -> ConfigManager.getLang().getString("not-curse-color")); + public static final EnchantmentType CURSE = new EnchantmentType(false, () -> ConfigManager.getLang().getString("curse-color")); + public static final EnchantmentType SPECIAL = new EnchantmentType(() -> !ConfigManager.getConfig().getBool("types.special.allow-multiple"), () -> ConfigManager.getLang().getString("special-color")); + public static final EnchantmentType ARTIFACT = new EnchantmentType(() -> !ConfigManager.getConfig().getBool("types.artifact.allow-multiple"), () -> ConfigManager.getLang().getString("artifact-color")); + public static final EnchantmentType SPELL = new EnchantmentType(true, () -> ConfigManager.getLang().getString("spell-color")); - static { - update(); + private static final Set values = new HashSet<>(); + + private boolean singular; + private String color; + private final ObjectCallable colorCallable; + private final ObjectCallable singularCallable; + + public EnchantmentType(boolean singular, String color) { + this(() -> singular, () -> color); } - boolean singular; + public EnchantmentType(boolean singular, ObjectCallable colorCallable) { + this(() -> singular, colorCallable); + } - EnchantmentType(boolean singular) { - this.singular = singular; + public EnchantmentType(ObjectCallable singularCallable, ObjectCallable colorCallable) { + this.singularCallable = singularCallable; + this.colorCallable = colorCallable; + color = colorCallable.call(); + singular = singularCallable.call(); + values.add(this); + } + + private void refresh() { + this.color = colorCallable.call(); + this.singular = singularCallable.call(); + } + + public String getColor() { + return color; } public boolean isSingular() { @@ -458,8 +479,11 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist } public static void update() { - SPECIAL.singular = !ConfigManager.getConfig().getBool("types.special.allow-multiple"); - ARTIFACT.singular = !ConfigManager.getConfig().getBool("types.artifact.allow-multiple"); + values.forEach(EnchantmentType::refresh); + } + + public static Set getValues() { + return values; } } } diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilMerge.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilMerge.java index 92f6910a..24932f3e 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilMerge.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilMerge.java @@ -121,7 +121,7 @@ public class AnvilMerge { rightEnchants.forEach(((enchantment, integer) -> { AtomicBoolean doesConflict = new AtomicBoolean(false); - Arrays.stream(EcoEnchant.EnchantmentType.values()).forEach(enchantmentType -> { + EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> { EcoEnchant enchant = EcoEnchants.getFromEnchantment(enchantment); if(enchant == null) return; if(enchant.getType().equals(enchantmentType) && EcoEnchants.hasAnyOfType(left, enchantmentType) && enchantmentType.isSingular()) doesConflict.set(true); diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/integrations/placeholder/PlaceholderEntry.java b/Plugin/src/main/java/com/willfp/ecoenchants/integrations/placeholder/PlaceholderEntry.java index 0dcfce89..81d1b683 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/integrations/placeholder/PlaceholderEntry.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/integrations/placeholder/PlaceholderEntry.java @@ -1,18 +1,18 @@ package com.willfp.ecoenchants.integrations.placeholder; -import com.willfp.ecoenchants.util.interfaces.ObjectCallable; +import com.willfp.ecoenchants.util.interfaces.ObjectBiCallable; import org.bukkit.entity.Player; public class PlaceholderEntry { private final String identifier; - private final ObjectCallable function; + private final ObjectBiCallable function; private final boolean requiresPlayer; - public PlaceholderEntry(String identifier, ObjectCallable function) { + public PlaceholderEntry(String identifier, ObjectBiCallable function) { this(identifier, function, false); } - public PlaceholderEntry(String identifier, ObjectCallable function, boolean requiresPlayer) { + public PlaceholderEntry(String identifier, ObjectBiCallable function, boolean requiresPlayer) { this.identifier = identifier; this.function = function; this.requiresPlayer = requiresPlayer; diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/util/interfaces/ObjectBiCallable.java b/Plugin/src/main/java/com/willfp/ecoenchants/util/interfaces/ObjectBiCallable.java new file mode 100644 index 00000000..0892aab2 --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/util/interfaces/ObjectBiCallable.java @@ -0,0 +1,6 @@ +package com.willfp.ecoenchants.util.interfaces; + +@FunctionalInterface +public interface ObjectBiCallable { + A call(B object); +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/util/interfaces/ObjectCallable.java b/Plugin/src/main/java/com/willfp/ecoenchants/util/interfaces/ObjectCallable.java index 58b555a1..01b48fa8 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/util/interfaces/ObjectCallable.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/util/interfaces/ObjectCallable.java @@ -1,6 +1,6 @@ package com.willfp.ecoenchants.util.interfaces; @FunctionalInterface -public interface ObjectCallable { - A call(B object); +public interface ObjectCallable { + A call(); }