Added configurable type sorting

This commit is contained in:
Auxilor 2020-12-16 11:54:13 +00:00
parent 2fea268ae6
commit 11516ee3b5
6 changed files with 36 additions and 7 deletions

View File

@ -1,13 +1,24 @@
package com.willfp.ecoenchants.display.options;
import com.willfp.ecoenchants.config.ConfigManager;
import com.willfp.ecoenchants.display.options.sorting.*;
import com.willfp.ecoenchants.display.options.sorting.AlphabeticSorter;
import com.willfp.ecoenchants.display.options.sorting.EnchantmentSorter;
import com.willfp.ecoenchants.display.options.sorting.LengthSorter;
import com.willfp.ecoenchants.display.options.sorting.TypeAlphabeticSorter;
import com.willfp.ecoenchants.display.options.sorting.TypeLengthSorter;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class DisplayOptions {
private EnchantmentSorter sorter;
private final DescriptionOptions descriptionOptions = new DescriptionOptions();
private final NumbersOptions numbersOptions = new NumbersOptions();
private final ShrinkOptions shrinkOptions = new ShrinkOptions();
private final List<EcoEnchant.EnchantmentType> sortedTypes = new ArrayList<>();
public DisplayOptions() {
update();
@ -45,6 +56,10 @@ public class DisplayOptions {
return shrinkOptions.isEnabled();
}
public List<EcoEnchant.EnchantmentType> getSortedTypes() {
return sortedTypes;
}
public EnchantmentSorter getSorter() {
return sorter;
}
@ -54,6 +69,13 @@ public class DisplayOptions {
numbersOptions.update();
shrinkOptions.update();
sortedTypes.clear();
sortedTypes.addAll(ConfigManager.getConfig().getStrings("lore.type-ordering").stream()
.map(typeName -> EcoEnchant.EnchantmentType.values().stream().filter(type -> type.getName().equalsIgnoreCase(typeName)).findFirst().orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toList()));
sortedTypes.addAll(EcoEnchant.EnchantmentType.values().stream().filter(enchantmentType -> !sortedTypes.contains(enchantmentType)).collect(Collectors.toList()));
boolean byType = ConfigManager.getConfig().getBool("lore.sort-by-type");
boolean byLength = ConfigManager.getConfig().getBool("lore.sort-by-length");
if (byType && byLength) sorter = new TypeLengthSorter();

View File

@ -1,7 +1,7 @@
package com.willfp.ecoenchants.display.options.sorting;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import org.bukkit.enchantments.Enchantment;
import java.util.ArrayList;
@ -12,7 +12,7 @@ public class TypeAlphabeticSorter implements EnchantmentSorter {
@Override
public void sortEnchantments(final List<Enchantment> toSort) {
List<Enchantment> sorted = new ArrayList<>();
EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> {
EnchantDisplay.OPTIONS.getSortedTypes().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()))

View File

@ -1,7 +1,7 @@
package com.willfp.ecoenchants.display.options.sorting;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import org.bukkit.enchantments.Enchantment;
import java.util.ArrayList;
@ -13,7 +13,7 @@ public class TypeLengthSorter implements EnchantmentSorter {
@Override
public void sortEnchantments(final List<Enchantment> toSort) {
List<Enchantment> sorted = new ArrayList<>();
EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> {
EnchantDisplay.OPTIONS.getSortedTypes().forEach(enchantmentType -> {
List<Enchantment> typeEnchants = toSort.stream()
.filter(enchantment -> EnchantmentCache.getEntry(enchantment).getType().equals(enchantmentType))
.sorted(Comparator.comparingInt(enchantment -> EnchantmentCache.getEntry(enchantment).getRawName().length()))

View File

@ -516,7 +516,7 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
values.forEach(EnchantmentType::refresh);
}
public static List<EnchantmentType> getValues() {
public static List<EnchantmentType> values() {
return new ArrayList<>(values);
}
}

View File

@ -124,7 +124,7 @@ public class AnvilMerge {
rightEnchants.forEach(((enchantment, integer) -> {
AtomicBoolean doesConflict = new AtomicBoolean(false);
EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> {
EcoEnchant.EnchantmentType.values().forEach(enchantmentType -> {
EcoEnchant enchant = EcoEnchants.getFromEnchantment(enchantment);
if (enchant == null) return;
if (enchant.getType().equals(enchantmentType) && EcoEnchants.hasAnyOfType(left, enchantmentType) && enchantmentType.isSingular())

View File

@ -27,6 +27,13 @@ lore:
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
type-ordering: # Only used if sort-by-type is enabled - top to bottom
- normal
- special
- artifact
- spell
- curse
describe: # Describe enchantments in lore
enabled: false
before-lines: 5 # Describe before or equal to number of enchantments