Reworked sorting

This commit is contained in:
Auxilor 2020-11-28 14:15:01 +00:00
parent 1dfee3e71a
commit 3192fb0e8f
7 changed files with 103 additions and 18 deletions

View File

@ -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<Enchantment, Integer> tempEnchantments = new HashMap<>(enchantments);
if(OPTIONS.isSortByType()) {
List<Enchantment> sorted = new ArrayList<>();
EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> {
List<Enchantment> 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();
}
}
}

View File

@ -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<Enchantment> toSort) {
toSort.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName())));
}
}

View File

@ -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<Enchantment> toSort);
}

View File

@ -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<Enchantment> toSort) {
toSort.sort(Comparator.comparingInt(enchantment -> EnchantmentCache.getEntry(enchantment).getRawName().length()));
}
}

View File

@ -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<Enchantment> toSort) {
List<Enchantment> sorted = new ArrayList<>();
EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> {
List<Enchantment> 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);
}
}

View File

@ -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<Enchantment> toSort) {
List<Enchantment> sorted = new ArrayList<>();
EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> {
List<Enchantment> 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);
}
}

View File

@ -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