diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandEnchantinfo.java b/Plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandEnchantinfo.java index 6d9ffa63..abc447ae 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandEnchantinfo.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandEnchantinfo.java @@ -4,7 +4,7 @@ import com.willfp.ecoenchants.command.AbstractCommand; import com.willfp.ecoenchants.command.AbstractTabCompleter; import com.willfp.ecoenchants.command.tabcompleters.TabCompleterEnchantinfo; import com.willfp.ecoenchants.config.ConfigManager; -import com.willfp.ecoenchants.display.EnchantDisplay; +import com.willfp.ecoenchants.display.EnchantmentCache; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import org.apache.commons.lang.WordUtils; @@ -115,19 +115,8 @@ public final class CommandEnchantinfo extends AbstractCommand { String maxLevel = String.valueOf(enchantment.getMaxLevel()); - StringBuilder descriptionBuilder = new StringBuilder(); - - EnchantDisplay.CACHE.get(enchantment).getValue().forEach(s -> { - descriptionBuilder.append(s); - descriptionBuilder.append(" "); - }); - - String description = descriptionBuilder.toString(); - description = description.replaceAll("§w", ""); - description = description.replaceAll(EnchantDisplay.descriptionColor, ""); - - final String finalName = EnchantDisplay.CACHE.get(enchantment).getKey(); - final String finalDescription = description; + final String finalName = EnchantmentCache.getEntry(enchantment).getName(); + final String finalDescription = EnchantmentCache.getEntry(enchantment).getStringDescription(); final String finalTargets = allTargets; final String finalConflicts = allConflicts; final String finalMaxLevel = maxLevel; 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 da5ce1fe..c897d449 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java @@ -5,12 +5,8 @@ import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.config.ConfigManager; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; -import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity; import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; -import com.willfp.ecoenchants.util.Logger; import com.willfp.ecoenchants.util.NumberUtils; -import com.willfp.ecoenchants.util.Pair; -import org.apache.commons.lang.WordUtils; import org.bukkit.ChatColor; import org.bukkit.NamespacedKey; import org.bukkit.enchantments.Enchantment; @@ -20,7 +16,10 @@ import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataType; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; /** * All methods and fields pertaining to showing players the enchantments on their items. @@ -52,28 +51,23 @@ public final class EnchantDisplay { */ public static final NamespacedKey KEY_V = new NamespacedKey(EcoEnchantsPlugin.getInstance(), "ecoenchantlore-v"); - /** - * Cached enchantment descriptions and names - */ - public static final Map>> CACHE = new HashMap<>(); + public static final String PREFIX = "§w"; - private static final String prefix = "§w"; + static String normalColor; + static String curseColor; + static String specialColor; + static String artifactColor; + static String descriptionColor; - private static String normalColor; - private static String curseColor; - private static String specialColor; - private static String artifactColor; - public static String descriptionColor; + static int numbersThreshold; + static boolean useNumerals; - private static int numbersThreshold; - private static boolean useNumerals; + static int describeThreshold; + static boolean useDescribe; - private static int describeThreshold; - private static boolean useDescribe; - - private static int shrinkThreshold; - private static int shrinkPerLine; - private static boolean useShrink; + static int shrinkThreshold; + static int shrinkPerLine; + static boolean useShrink; /** * Update config values @@ -85,60 +79,6 @@ public final class EnchantDisplay { artifactColor = ChatColor.translateAlternateColorCodes('&', ConfigManager.getLang().getString("artifact-color")); normalColor = ChatColor.translateAlternateColorCodes('&', ConfigManager.getLang().getString("not-curse-color")); - CACHE.clear(); - Arrays.asList(Enchantment.values()).parallelStream().forEach(enchantment -> { - String name; - String color; - EcoEnchant.EnchantmentType type; - List description; - if(EcoEnchants.getFromEnchantment(enchantment) != null) { - EcoEnchant ecoEnchant = EcoEnchants.getFromEnchantment(enchantment); - description = ecoEnchant.getDescription(); - name = ecoEnchant.getName(); - type = ecoEnchant.getType(); - } else { - description = Arrays.asList( - WordUtils.wrap( - String.valueOf(ConfigManager.getLang().getString("enchantments." + enchantment.getKey().getKey().toLowerCase() + ".description")), - ConfigManager.getConfig().getInt("lore.describe.wrap"), - "\n", false - ).split("\\r?\\n") - ); - name = String.valueOf(ConfigManager.getLang().getString("enchantments." + enchantment.getKey().getKey().toLowerCase() + ".name")); - type = enchantment.isCursed() ? EcoEnchant.EnchantmentType.CURSE : EcoEnchant.EnchantmentType.NORMAL; - } - - switch(type) { - case ARTIFACT: - color = artifactColor; - break; - case SPECIAL: - color = specialColor; - break; - case CURSE: - color = curseColor; - break; - default: - color = normalColor; - break; - } - - if(EcoEnchants.getFromEnchantment(enchantment) != null) { - EnchantmentRarity rarity = EcoEnchants.getFromEnchantment(enchantment).getRarity(); - if(rarity != null) { - if (rarity.hasCustomColor() && type != EcoEnchant.EnchantmentType.CURSE) { - color = rarity.getCustomColor(); - } - } else { - Logger.warn("Enchantment " + enchantment.getKey().getKey() + " has an invalid rarity"); - } - } - - name = color + name; - description.replaceAll(line -> prefix + descriptionColor + line); - CACHE.put(enchantment, new Pair<>(name, description)); - }); - useNumerals = ConfigManager.getConfig().getBool("lore.use-numerals"); numbersThreshold = ConfigManager.getConfig().getInt("lore.use-numbers-above-threshold"); @@ -194,7 +134,7 @@ public final class EnchantDisplay { } catch(NullPointerException ignored) { } meta.getPersistentDataContainer().remove(KEY); - itemLore.removeIf((s) -> s.startsWith(prefix)); + itemLore.removeIf((s) -> s.startsWith(PREFIX)); if(!meta.getPersistentDataContainer().has(KEY_SKIP, PersistentDataType.INTEGER)) { if (meta instanceof EnchantmentStorageMeta) @@ -247,15 +187,47 @@ public final class EnchantDisplay { List lore = new ArrayList<>(); - Map enchantments; + LinkedHashMap enchantments = new LinkedHashMap<>(); List forRemoval = new ArrayList<>(); if(meta instanceof EnchantmentStorageMeta) { - enchantments = ((EnchantmentStorageMeta) meta).getStoredEnchants(); + enchantments.putAll(((EnchantmentStorageMeta) meta).getStoredEnchants()); } else { - enchantments = meta.getEnchants(); + enchantments.putAll(meta.getEnchants()); } + List unsorted = new ArrayList<>(); + enchantments.forEach((enchantment, integer) -> { + unsorted.add(enchantment); + }); + + HashMap tempEnchantments = new HashMap<>(enchantments); + unsorted.sort(((enchantment1, enchantment2) -> { + String name1; + String name2; + + if(EcoEnchants.getFromEnchantment(enchantment1) != null) { + EcoEnchant ecoEnchant = EcoEnchants.getFromEnchantment(enchantment1); + name1 = ecoEnchant.getName(); + } else { + name1 = String.valueOf(ConfigManager.getLang().getString("enchantments." + enchantment1.getKey().getKey().toLowerCase() + ".name")); + } + + if(EcoEnchants.getFromEnchantment(enchantment2) != null) { + EcoEnchant ecoEnchant = EcoEnchants.getFromEnchantment(enchantment2); + name2 = ecoEnchant.getName(); + } else { + name2 = String.valueOf(ConfigManager.getLang().getString("enchantments." + enchantment2.getKey().getKey().toLowerCase() + ".name")); + } + + return name1.compareToIgnoreCase(name2); + })); + + enchantments.clear(); + unsorted.forEach(enchantment -> { + enchantments.put(enchantment, tempEnchantments.get(enchantment)); + }); + enchantments.forEach((enchantment, level) -> { if(EcoEnchants.getFromEnchantment(enchantment) == null) return; @@ -265,18 +237,18 @@ public final class EnchantDisplay { forRemoval.add(enchantment); }); - forRemoval.forEach(enchantments::remove); - if(meta instanceof EnchantmentStorageMeta) { - forRemoval.forEach(((EnchantmentStorageMeta) meta)::removeStoredEnchant); - } else { - forRemoval.forEach(meta::removeEnchant); - } + forRemoval.forEach(enchantment -> { + enchantments.remove(enchantment); + if(meta instanceof EnchantmentStorageMeta) { + ((EnchantmentStorageMeta) meta).removeStoredEnchant(enchantment); + } else { + meta.removeEnchant(enchantment); + } + }); final ItemStack finalItem = item; enchantments.forEach((enchantment, level) -> { - if(CACHE.get(enchantment) == null) return; - - String name = CACHE.get(enchantment).getKey(); + String name = EnchantmentCache.getEntry(enchantment).getName(); if(!(enchantment.getMaxLevel() == 1 && level == 1)) { if(useNumerals && finalItem.getEnchantmentLevel(enchantment) < numbersThreshold) { @@ -286,9 +258,9 @@ public final class EnchantDisplay { } } - lore.add(prefix + name); + lore.add(PREFIX + name); if(enchantments.size() <= describeThreshold && useDescribe) - lore.addAll(CACHE.get(enchantment).getValue()); + lore.addAll(EnchantmentCache.getEntry(enchantment).getDescription()); }); if (useShrink && (enchantments.size() > shrinkThreshold)) { diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java new file mode 100644 index 00000000..dca54f46 --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java @@ -0,0 +1,139 @@ +package com.willfp.ecoenchants.display; + +import com.willfp.ecoenchants.config.ConfigManager; +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import com.willfp.ecoenchants.enchantments.EcoEnchants; +import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity; +import com.willfp.ecoenchants.util.Logger; +import org.apache.commons.lang.WordUtils; +import org.bukkit.enchantments.Enchantment; + +import java.util.*; + +public class EnchantmentCache { + private static final Set CACHE = new HashSet<>(); + + static { + update(); + } + + public static CacheEntry getEntry(Enchantment enchantment) { + Optional matching = CACHE.stream().filter(enchant -> enchant.getEnchantment().getKey().equals(enchantment.getKey())).findFirst(); + return matching.orElse(new CacheEntry(enchantment, enchantment.getKey().getKey(), enchantment.getKey().getKey(), Collections.singletonList("No Description Found"))); + } + + public static void update() { + CACHE.clear(); + Arrays.asList(Enchantment.values()).parallelStream().forEach(enchantment -> { + String name; + String color; + EcoEnchant.EnchantmentType type; + List description; + if(EcoEnchants.getFromEnchantment(enchantment) != null) { + EcoEnchant ecoEnchant = EcoEnchants.getFromEnchantment(enchantment); + description = ecoEnchant.getDescription(); + name = ecoEnchant.getName(); + type = ecoEnchant.getType(); + } else { + description = Arrays.asList( + WordUtils.wrap( + String.valueOf(ConfigManager.getLang().getString("enchantments." + enchantment.getKey().getKey().toLowerCase() + ".description")), + ConfigManager.getConfig().getInt("lore.describe.wrap"), + "\n", false + ).split("\\r?\\n") + ); + name = String.valueOf(ConfigManager.getLang().getString("enchantments." + enchantment.getKey().getKey().toLowerCase() + ".name")); + 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; + default: + color = EnchantDisplay.normalColor; + break; + } + + if(EcoEnchants.getFromEnchantment(enchantment) != null) { + EnchantmentRarity rarity = EcoEnchants.getFromEnchantment(enchantment).getRarity(); + if(rarity != null) { + if (rarity.hasCustomColor() && type != EcoEnchant.EnchantmentType.CURSE) { + color = rarity.getCustomColor(); + } + } else { + Logger.warn("Enchantment " + enchantment.getKey().getKey() + " has an invalid rarity"); + } + } + + String rawName = name; + name = color + name; + description.replaceAll(line -> EnchantDisplay.PREFIX + EnchantDisplay.descriptionColor + line); + CACHE.add(new CacheEntry(enchantment, name, rawName, description)); + }); + } + + + public static class CacheEntry { + private final Enchantment enchantment; + private String name; + private String rawName; + private List description; + + public CacheEntry(Enchantment enchantment, String name, String rawName, List description) { + this.enchantment = enchantment; + this.name = name; + this.rawName = rawName; + this.description = description; + } + + public Enchantment getEnchantment() { + return enchantment; + } + + public String getName() { + return name; + } + + public String getRawName() { + return rawName; + } + + public List getDescription() { + return description; + } + + public String getStringDescription() { + StringBuilder descriptionBuilder = new StringBuilder(); + + EnchantmentCache.getEntry(enchantment).getDescription().forEach(s -> { + descriptionBuilder.append(s); + descriptionBuilder.append(" "); + }); + + String description = descriptionBuilder.toString(); + description = description.replaceAll("§w", ""); + description = description.replaceAll(EnchantDisplay.descriptionColor, ""); + + return description; + } + + public void setName(String name) { + this.name = name; + } + + public void setRawName(String rawName) { + this.rawName = rawName; + } + + public void setDescription(List description) { + this.description = description; + } + } +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilListeners.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilListeners.java index e9b1d602..2c1ffe7e 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilListeners.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilListeners.java @@ -1,7 +1,7 @@ package com.willfp.ecoenchants.enchantments.support.merging.anvil; import com.willfp.ecoenchants.EcoEnchantsPlugin; -import com.willfp.ecoenchants.util.Pair; +import com.willfp.ecoenchants.util.tuplets.Pair; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; 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 1d034566..f533f1e6 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 @@ -4,7 +4,7 @@ import com.willfp.ecoenchants.config.ConfigManager; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; -import com.willfp.ecoenchants.util.Pair; +import com.willfp.ecoenchants.util.tuplets.Pair; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/util/Pair.java b/Plugin/src/main/java/com/willfp/ecoenchants/util/tuplets/Pair.java similarity index 95% rename from Plugin/src/main/java/com/willfp/ecoenchants/util/Pair.java rename to Plugin/src/main/java/com/willfp/ecoenchants/util/tuplets/Pair.java index 04b6d7e9..72b9a1b8 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/util/Pair.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/util/tuplets/Pair.java @@ -1,4 +1,4 @@ -package com.willfp.ecoenchants.util; +package com.willfp.ecoenchants.util.tuplets; import java.util.Map; diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/util/tuplets/Triplet.java b/Plugin/src/main/java/com/willfp/ecoenchants/util/tuplets/Triplet.java new file mode 100644 index 00000000..799d70e8 --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/util/tuplets/Triplet.java @@ -0,0 +1,49 @@ +package com.willfp.ecoenchants.util.tuplets; + +/** + * Spigot doesn't include javafx + */ +public class Triplet { + private A first; + private B second; + private C third; + + public Triplet(A first, B second, C third) { + this.first = first; + this.second = second; + this.third = third; + } + + public A getFirst() { + return first; + } + + public B getSecond() { + return second; + } + + public C getThird() { + return third; + } + + public void setFirst(A first) { + this.first = first; + } + + public void setSecond(B second) { + this.second = second; + } + + public void setThird(C third) { + this.third = third; + } + + @Override + public String toString() { + return "Triplet{" + + "first=" + first + + ", second=" + second + + ", third=" + third + + '}'; + } +}