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 04b15fc2..7289845e 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; +import java.util.stream.Collectors; /** * All methods and fields pertaining to showing players the enchantments on their items. @@ -69,6 +70,7 @@ public final class EnchantDisplay { static int shrinkThreshold; static int shrinkPerLine; static boolean useShrink; + static boolean sortByType; /** * Update config values @@ -90,6 +92,7 @@ public final class EnchantDisplay { 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"); EnchantmentCache.update(); } @@ -210,7 +213,28 @@ public final class EnchantDisplay { }); HashMap tempEnchantments = new HashMap<>(enchantments); - unsorted.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))); + if(sortByType) { + List normalEnchants = unsorted.stream().filter(enchantment -> EnchantmentCache.getEntry(enchantment).getType().equals(EcoEnchant.EnchantmentType.NORMAL)).collect(Collectors.toList()); + List specialEnchants = unsorted.stream().filter(enchantment -> EnchantmentCache.getEntry(enchantment).getType().equals(EcoEnchant.EnchantmentType.SPECIAL)).collect(Collectors.toList()); + List artifactEnchants = unsorted.stream().filter(enchantment -> EnchantmentCache.getEntry(enchantment).getType().equals(EcoEnchant.EnchantmentType.ARTIFACT)).collect(Collectors.toList()); + List spellEnchants = unsorted.stream().filter(enchantment -> EnchantmentCache.getEntry(enchantment).getType().equals(EcoEnchant.EnchantmentType.SPELL)).collect(Collectors.toList()); + List curseEnchants = unsorted.stream().filter(enchantment -> EnchantmentCache.getEntry(enchantment).getType().equals(EcoEnchant.EnchantmentType.CURSE)).collect(Collectors.toList()); + + normalEnchants.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))); + specialEnchants.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))); + artifactEnchants.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))); + spellEnchants.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))); + curseEnchants.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))); + + unsorted.clear(); + unsorted.addAll(normalEnchants); + unsorted.addAll(specialEnchants); + unsorted.addAll(artifactEnchants); + unsorted.addAll(spellEnchants); + unsorted.addAll(curseEnchants); + } else { + unsorted.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))); + } enchantments.clear(); unsorted.forEach(enchantment -> { 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 99c1eac4..a8e4e324 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java @@ -20,7 +20,7 @@ public class EnchantmentCache { public static CacheEntry getEntry(Enchantment enchantment) { Optional matching = CACHE.stream().filter(entry -> entry.getEnchantment().getKey().getKey().equals(enchantment.getKey().getKey())).findFirst(); - return matching.orElse(new CacheEntry(enchantment, EnchantDisplay.PREFIX + "§7" + enchantment.getKey().getKey(), enchantment.getKey().getKey(), Collections.singletonList(EnchantDisplay.PREFIX + "No Description Found"))); + return matching.orElse(new CacheEntry(enchantment, EnchantDisplay.PREFIX + "§7" + enchantment.getKey().getKey(), enchantment.getKey().getKey(), Collections.singletonList(EnchantDisplay.PREFIX + "No Description Found"), EcoEnchant.EnchantmentType.NORMAL)); } public static Set getCache() { @@ -83,7 +83,7 @@ public class EnchantmentCache { String rawName = name; name = color + name; description.replaceAll(line -> EnchantDisplay.PREFIX + EnchantDisplay.descriptionColor + line); - CACHE.add(new CacheEntry(enchantment, name, rawName, description)); + CACHE.add(new CacheEntry(enchantment, name, rawName, description, type)); }); } @@ -94,12 +94,14 @@ public class EnchantmentCache { private final String rawName; private final List description; private final String stringDescription; + private final EcoEnchant.EnchantmentType type; - public CacheEntry(Enchantment enchantment, String name, String rawName, List description) { + public CacheEntry(Enchantment enchantment, String name, String rawName, List description, EcoEnchant.EnchantmentType type) { this.enchantment = enchantment; this.name = name; this.rawName = rawName; this.description = description; + this.type = type; StringBuilder descriptionBuilder = new StringBuilder(); @@ -134,6 +136,10 @@ public class EnchantmentCache { return stringDescription; } + public EcoEnchant.EnchantmentType getType() { + return type; + } + @Override public String toString() { return "CacheEntry{" + diff --git a/Plugin/src/main/resources/config.yml b/Plugin/src/main/resources/config.yml index 75e42467..845d5b52 100644 --- a/Plugin/src/main/resources/config.yml +++ b/Plugin/src/main/resources/config.yml @@ -24,6 +24,7 @@ anvil: lore: use-numerals: true use-numbers-above-threshold: 10 #After level 10, enchantments will display as Name Number, eg: Sharpness 25 instead of Sharpness XXV + sort-by-type: false # Sort enchantments by type describe: # Describe enchantments in lore enabled: false