From 3192fb0e8f05e79f225a620853b7ad5a13acc4f7 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 28 Nov 2020 14:15:01 +0000 Subject: [PATCH] Reworked sorting --- .../ecoenchants/display/EnchantDisplay.java | 31 ++++++++----------- .../display/sorting/AlphabeticSorter.java | 13 ++++++++ .../display/sorting/EnchantmentSorter.java | 9 ++++++ .../display/sorting/LengthSorter.java | 14 +++++++++ .../display/sorting/TypeAlphabeticSorter.java | 26 ++++++++++++++++ .../display/sorting/TypeLengthSorter.java | 27 ++++++++++++++++ Plugin/src/main/resources/config.yml | 1 + 7 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/AlphabeticSorter.java create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/EnchantmentSorter.java create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/LengthSorter.java create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/TypeAlphabeticSorter.java create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/TypeLengthSorter.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 9eedda22..118cdb15 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java @@ -3,6 +3,7 @@ package com.willfp.ecoenchants.display; import com.google.common.collect.Lists; import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.config.ConfigManager; +import com.willfp.ecoenchants.display.sorting.*; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; @@ -167,21 +168,8 @@ public class EnchantDisplay { }); HashMap tempEnchantments = new HashMap<>(enchantments); - if(OPTIONS.isSortByType()) { - List sorted = new ArrayList<>(); - EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> { - List typeEnchants = unsorted.stream() - .filter(enchantment -> EnchantmentCache.getEntry(enchantment).getType().equals(enchantmentType)) - .sorted(Comparator.comparingInt(enchantment -> EnchantmentCache.getEntry(enchantment).getRawName().length())) - .collect(Collectors.toList()); - sorted.addAll(typeEnchants); - }); - unsorted.clear(); - unsorted.addAll(sorted); - } else { - unsorted.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))); - } + OPTIONS.getSorter().sortEnchantments(unsorted); enchantments.clear(); unsorted.forEach(enchantment -> { @@ -262,7 +250,8 @@ public class EnchantDisplay { private int shrinkThreshold; private int shrinkPerLine; private boolean useShrink; - private boolean sortByType; + + private EnchantmentSorter sorter; private DisplayOptions() { update(); @@ -300,8 +289,8 @@ public class EnchantDisplay { return useShrink; } - public boolean isSortByType() { - return sortByType; + public EnchantmentSorter getSorter() { + return sorter; } public void update() { @@ -316,7 +305,13 @@ public 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"); + + boolean byType = ConfigManager.getConfig().getBool("lore.sort-by-type"); + boolean byLength = ConfigManager.getConfig().getBool("lore.sort-by-length"); + if(byType && byLength) sorter = new TypeLengthSorter(); + if(byType && !byLength) sorter = new TypeAlphabeticSorter(); + if(!byType && byLength) sorter = new LengthSorter(); + if(!byType && !byLength) sorter = new AlphabeticSorter(); } } } diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/AlphabeticSorter.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/AlphabeticSorter.java new file mode 100644 index 00000000..b9f440e2 --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/AlphabeticSorter.java @@ -0,0 +1,13 @@ +package com.willfp.ecoenchants.display.sorting; + +import com.willfp.ecoenchants.display.EnchantmentCache; +import org.bukkit.enchantments.Enchantment; + +import java.util.List; + +public class AlphabeticSorter implements EnchantmentSorter { + @Override + public void sortEnchantments(final List toSort) { + toSort.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))); + } +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/EnchantmentSorter.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/EnchantmentSorter.java new file mode 100644 index 00000000..81d311e4 --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/EnchantmentSorter.java @@ -0,0 +1,9 @@ +package com.willfp.ecoenchants.display.sorting; + +import org.bukkit.enchantments.Enchantment; + +import java.util.List; + +public interface EnchantmentSorter { + void sortEnchantments(final List toSort); +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/LengthSorter.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/LengthSorter.java new file mode 100644 index 00000000..2d2dcfe4 --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/LengthSorter.java @@ -0,0 +1,14 @@ +package com.willfp.ecoenchants.display.sorting; + +import com.willfp.ecoenchants.display.EnchantmentCache; +import org.bukkit.enchantments.Enchantment; + +import java.util.Comparator; +import java.util.List; + +public class LengthSorter implements EnchantmentSorter { + @Override + public void sortEnchantments(final List toSort) { + toSort.sort(Comparator.comparingInt(enchantment -> EnchantmentCache.getEntry(enchantment).getRawName().length())); + } +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/TypeAlphabeticSorter.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/TypeAlphabeticSorter.java new file mode 100644 index 00000000..02644e8d --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/TypeAlphabeticSorter.java @@ -0,0 +1,26 @@ +package com.willfp.ecoenchants.display.sorting; + +import com.willfp.ecoenchants.display.EnchantmentCache; +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import org.bukkit.enchantments.Enchantment; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class TypeAlphabeticSorter implements EnchantmentSorter { + @Override + public void sortEnchantments(final List toSort) { + List sorted = new ArrayList<>(); + EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> { + List typeEnchants = toSort.stream() + .filter(enchantment -> EnchantmentCache.getEntry(enchantment).getType().equals(enchantmentType)) + .sorted((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName())) + .collect(Collectors.toList()); + sorted.addAll(typeEnchants); + }); + + toSort.clear(); + toSort.addAll(sorted); + } +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/TypeLengthSorter.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/TypeLengthSorter.java new file mode 100644 index 00000000..41917f4c --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/sorting/TypeLengthSorter.java @@ -0,0 +1,27 @@ +package com.willfp.ecoenchants.display.sorting; + +import com.willfp.ecoenchants.display.EnchantmentCache; +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import org.bukkit.enchantments.Enchantment; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class TypeLengthSorter implements EnchantmentSorter { + @Override + public void sortEnchantments(final List toSort) { + List sorted = new ArrayList<>(); + EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> { + List typeEnchants = toSort.stream() + .filter(enchantment -> EnchantmentCache.getEntry(enchantment).getType().equals(enchantmentType)) + .sorted(Comparator.comparingInt(enchantment -> EnchantmentCache.getEntry(enchantment).getRawName().length())) + .collect(Collectors.toList()); + sorted.addAll(typeEnchants); + }); + + toSort.clear(); + toSort.addAll(sorted); + } +} diff --git a/Plugin/src/main/resources/config.yml b/Plugin/src/main/resources/config.yml index 845d5b52..da24c490 100644 --- a/Plugin/src/main/resources/config.yml +++ b/Plugin/src/main/resources/config.yml @@ -25,6 +25,7 @@ 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 + sort-by-length: false # Sort enchantments by length. Any combination of this and the above option is valid describe: # Describe enchantments in lore enabled: false