From 1dfee3e71a26c227f648f37b81f58bf474fe80cb Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 28 Nov 2020 13:59:07 +0000 Subject: [PATCH] Refactored display settings into internal class --- .../ecoenchants/display/EnchantDisplay.java | 113 +++++++++++++----- .../ecoenchants/display/EnchantmentCache.java | 4 +- 2 files changed, 85 insertions(+), 32 deletions(-) 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 0e73762d..9eedda22 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java @@ -41,38 +41,21 @@ public class EnchantDisplay { */ public static final NamespacedKey KEY_V = new NamespacedKey(EcoEnchantsPlugin.getInstance(), "ecoenchantlore-v"); + /** + * The prefix for all enchantment lines to have in lore + */ public static final String PREFIX = "§w"; - static String descriptionColor; - - static int numbersThreshold; - static boolean useNumerals; - - static int describeThreshold; - static boolean useDescribe; - - static int shrinkThreshold; - static int shrinkPerLine; - static boolean useShrink; - static boolean sortByType; + /** + * The configurable options for displaying enchantments + */ + public static final DisplayOptions OPTIONS = new DisplayOptions(); /** * Update config values */ public static void update() { - descriptionColor = StringUtils.translate(ConfigManager.getLang().getString("description-color")); - - useNumerals = ConfigManager.getConfig().getBool("lore.use-numerals"); - numbersThreshold = ConfigManager.getConfig().getInt("lore.use-numbers-above-threshold"); - - describeThreshold = ConfigManager.getConfig().getInt("lore.describe.before-lines"); - useDescribe = ConfigManager.getConfig().getBool("lore.describe.enabled"); - - shrinkThreshold = ConfigManager.getConfig().getInt("lore.shrink.after-lines"); - useShrink = ConfigManager.getConfig().getBool("lore.shrink.enabled"); - shrinkPerLine = ConfigManager.getConfig().getInt("lore.shrink.maximum-per-line"); - sortByType = ConfigManager.getConfig().getBool("lore.sort-by-type"); - + OPTIONS.update(); EnchantmentCache.update(); } @@ -184,7 +167,7 @@ public class EnchantDisplay { }); HashMap tempEnchantments = new HashMap<>(enchantments); - if(sortByType) { + if(OPTIONS.isSortByType()) { List sorted = new ArrayList<>(); EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> { List typeEnchants = unsorted.stream() @@ -227,7 +210,7 @@ public class EnchantDisplay { String name = EnchantmentCache.getEntry(enchantment).getName(); if(!(enchantment.getMaxLevel() == 1 && level == 1)) { - if(useNumerals && item.getEnchantmentLevel(enchantment) < numbersThreshold) { + if(OPTIONS.isUseNumerals() && item.getEnchantmentLevel(enchantment) < OPTIONS.getNumbersThreshold()) { name += " " + NumberUtils.toNumeral(level); } else { name += " " + level; @@ -235,12 +218,12 @@ public class EnchantDisplay { } lore.add(PREFIX + name); - if(enchantments.size() <= describeThreshold && useDescribe) + if(enchantments.size() <= OPTIONS.getDescribeThreshold() && OPTIONS.isUseDescribe()) lore.addAll(EnchantmentCache.getEntry(enchantment).getDescription()); }); - if (useShrink && (enchantments.size() > shrinkThreshold)) { - List> partitionedCombinedLoreList = Lists.partition(lore, shrinkPerLine); + if (OPTIONS.isUseShrink() && (enchantments.size() > OPTIONS.getShrinkThreshold())) { + List> partitionedCombinedLoreList = Lists.partition(lore, OPTIONS.getShrinkPerLine()); List newLore = new ArrayList<>(); partitionedCombinedLoreList.forEach((list) -> { StringBuilder builder = new StringBuilder(); @@ -266,4 +249,74 @@ public class EnchantDisplay { return item; } + + public static class DisplayOptions { + private String descriptionColor; + + private int numbersThreshold; + private boolean useNumerals; + + private int describeThreshold; + private boolean useDescribe; + + private int shrinkThreshold; + private int shrinkPerLine; + private boolean useShrink; + private boolean sortByType; + + private DisplayOptions() { + update(); + } + + public String getDescriptionColor() { + return descriptionColor; + } + + public int getNumbersThreshold() { + return numbersThreshold; + } + + public boolean isUseNumerals() { + return useNumerals; + } + + public int getDescribeThreshold() { + return describeThreshold; + } + + public boolean isUseDescribe() { + return useDescribe; + } + + public int getShrinkThreshold() { + return shrinkThreshold; + } + + public int getShrinkPerLine() { + return shrinkPerLine; + } + + public boolean isUseShrink() { + return useShrink; + } + + public boolean isSortByType() { + return sortByType; + } + + public void update() { + descriptionColor = StringUtils.translate(ConfigManager.getLang().getString("description-color")); + + useNumerals = ConfigManager.getConfig().getBool("lore.use-numerals"); + numbersThreshold = ConfigManager.getConfig().getInt("lore.use-numbers-above-threshold"); + + describeThreshold = ConfigManager.getConfig().getInt("lore.describe.before-lines"); + useDescribe = ConfigManager.getConfig().getBool("lore.describe.enabled"); + + shrinkThreshold = ConfigManager.getConfig().getInt("lore.shrink.after-lines"); + useShrink = ConfigManager.getConfig().getBool("lore.shrink.enabled"); + shrinkPerLine = ConfigManager.getConfig().getInt("lore.shrink.maximum-per-line"); + sortByType = ConfigManager.getConfig().getBool("lore.sort-by-type"); + } + } } 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 7ec40f31..9aebef33 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java @@ -62,7 +62,7 @@ public class EnchantmentCache { String rawName = name; name = color + name; - description.replaceAll(line -> EnchantDisplay.PREFIX + EnchantDisplay.descriptionColor + line); + description.replaceAll(line -> EnchantDisplay.PREFIX + EnchantDisplay.OPTIONS.getDescriptionColor() + line); CACHE.add(new CacheEntry(enchantment, name, rawName, description, type)); }); } @@ -93,7 +93,7 @@ public class EnchantmentCache { String stringDescription = descriptionBuilder.toString(); stringDescription = stringDescription.replaceAll("§w", ""); - this.stringDescription = stringDescription.replaceAll(EnchantDisplay.descriptionColor, ""); + this.stringDescription = stringDescription.replaceAll(EnchantDisplay.OPTIONS.getDescriptionColor(), ""); } public Enchantment getEnchantment() {