Added rarity sorting

This commit is contained in:
Auxilor
2020-12-17 12:36:17 +00:00
parent 78cfb963f1
commit 69309935a6
17 changed files with 285 additions and 32 deletions

View File

@@ -14,9 +14,10 @@ import java.util.*;
public class EnchantmentCache {
private static final Set<CacheEntry> CACHE = new HashSet<>();
@SuppressWarnings("OptionalGetWithoutIsPresent")
public static CacheEntry getEntry(Enchantment enchantment) {
Optional<CacheEntry> 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"), EcoEnchant.EnchantmentType.NORMAL));
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, EnchantmentRarity.values().stream().findFirst().get()));
}
public static Set<CacheEntry> getCache() {
@@ -29,12 +30,14 @@ public class EnchantmentCache {
String name;
String color;
EcoEnchant.EnchantmentType type;
EnchantmentRarity rarity;
List<String> description;
if (EcoEnchants.getFromEnchantment(enchantment) != null) {
EcoEnchant ecoEnchant = EcoEnchants.getFromEnchantment(enchantment);
description = ecoEnchant.getDescription();
name = ecoEnchant.getName();
type = ecoEnchant.getType();
rarity = ecoEnchant.getRarity();
} else {
description = Arrays.asList(
WordUtils.wrap(
@@ -45,25 +48,23 @@ public class EnchantmentCache {
);
name = String.valueOf(ConfigManager.getLang().getString("enchantments." + enchantment.getKey().getKey().toLowerCase() + ".name"));
type = enchantment.isCursed() ? EcoEnchant.EnchantmentType.CURSE : EcoEnchant.EnchantmentType.NORMAL;
rarity = EnchantmentRarity.getByName(ConfigManager.getConfig().getString("lore.vanilla-rarity"));
}
color = type.getColor();
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");
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.OPTIONS.getDescriptionColor() + line);
CACHE.add(new CacheEntry(enchantment, name, rawName, description, type));
CACHE.add(new CacheEntry(enchantment, name, rawName, description, type, rarity));
});
}
@@ -75,14 +76,15 @@ public class EnchantmentCache {
private final List<String> description;
private final String stringDescription;
private final EcoEnchant.EnchantmentType type;
private final EnchantmentRarity rarity;
public CacheEntry(Enchantment enchantment, String name, String rawName, List<String> description, EcoEnchant.EnchantmentType type) {
public CacheEntry(Enchantment enchantment, String name, String rawName, List<String> description, EcoEnchant.EnchantmentType type, EnchantmentRarity rarity) {
this.enchantment = enchantment;
this.name = name;
this.rawName = rawName;
this.description = description;
this.type = type;
this.rarity = rarity;
StringBuilder descriptionBuilder = new StringBuilder();
@@ -120,6 +122,10 @@ public class EnchantmentCache {
return type;
}
public EnchantmentRarity getRarity() {
return rarity;
}
@Override
public String toString() {
return "CacheEntry{" +

View File

@@ -1,16 +1,17 @@
package com.willfp.ecoenchants.display.options;
import com.willfp.ecoenchants.config.ConfigManager;
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.display.options.sorting.SortParameters;
import com.willfp.ecoenchants.display.options.sorting.SorterManager;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class DisplayOptions {
@@ -19,6 +20,7 @@ public class DisplayOptions {
private final NumbersOptions numbersOptions = new NumbersOptions();
private final ShrinkOptions shrinkOptions = new ShrinkOptions();
private final List<EcoEnchant.EnchantmentType> sortedTypes = new ArrayList<>();
private final List<EnchantmentRarity> sortedRarities = new ArrayList<>();
public DisplayOptions() {
update();
@@ -60,6 +62,10 @@ public class DisplayOptions {
return sortedTypes;
}
public List<EnchantmentRarity> getSortedRarities() {
return sortedRarities;
}
public EnchantmentSorter getSorter() {
return sorter;
}
@@ -76,11 +82,21 @@ public class DisplayOptions {
.collect(Collectors.toList()));
sortedTypes.addAll(EcoEnchant.EnchantmentType.values().stream().filter(enchantmentType -> !sortedTypes.contains(enchantmentType)).collect(Collectors.toList()));
sortedRarities.clear();
sortedRarities.addAll(ConfigManager.getConfig().getStrings("lore.rarity-ordering").stream()
.map(rarityName -> EnchantmentRarity.values().stream().filter(rarity -> rarity.getName().equalsIgnoreCase(rarityName)).findFirst().orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toList()));
sortedRarities.addAll(EnchantmentRarity.values().stream().filter(enchantmentRarity -> !sortedRarities.contains(enchantmentRarity)).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();
if (byType && !byLength) sorter = new TypeAlphabeticSorter();
if (!byType && byLength) sorter = new LengthSorter();
if (!byType && !byLength) sorter = new AlphabeticSorter();
boolean byRarity = ConfigManager.getConfig().getBool("lore.sort-by-rarity");
Set<SortParameters> params = new HashSet<>();
if(byType) params.add(SortParameters.TYPE);
if(byLength) params.add(SortParameters.LENGTH);
if(byRarity) params.add(SortParameters.RARITY);
sorter = SorterManager.getSorter(params.toArray(new SortParameters[]{}));
}
}

View File

@@ -6,4 +6,5 @@ import java.util.List;
public interface EnchantmentSorter {
void sortEnchantments(final List<Enchantment> toSort);
SortParameters[] getParameters();
}

View File

@@ -0,0 +1,7 @@
package com.willfp.ecoenchants.display.options.sorting;
public enum SortParameters {
TYPE,
RARITY,
LENGTH
}

View File

@@ -0,0 +1,36 @@
package com.willfp.ecoenchants.display.options.sorting;
import com.willfp.ecoenchants.display.options.sorting.implementations.AlphabeticSorter;
import com.willfp.ecoenchants.display.options.sorting.implementations.LengthSorter;
import com.willfp.ecoenchants.display.options.sorting.implementations.RarityAlphabeticSorter;
import com.willfp.ecoenchants.display.options.sorting.implementations.RarityLengthSorter;
import com.willfp.ecoenchants.display.options.sorting.implementations.RarityTypeAlphabeticSorter;
import com.willfp.ecoenchants.display.options.sorting.implementations.RarityTypeLengthSorter;
import com.willfp.ecoenchants.display.options.sorting.implementations.TypeAlphabeticSorter;
import com.willfp.ecoenchants.display.options.sorting.implementations.TypeLengthSorter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SorterManager {
private static final Set<EnchantmentSorter> values = new HashSet<>();
public static EnchantmentSorter getSorter(SortParameters... parameters) {
return values.stream()
.filter(enchantmentSorter -> Arrays.asList(enchantmentSorter.getParameters()).containsAll(Arrays.asList(parameters)) && enchantmentSorter.getParameters().length == parameters.length)
.findFirst()
.orElse(new AlphabeticSorter());
}
static {
values.add(new AlphabeticSorter());
values.add(new LengthSorter());
values.add(new TypeAlphabeticSorter());
values.add(new TypeLengthSorter());
values.add(new RarityAlphabeticSorter());
values.add(new RarityLengthSorter());
values.add(new RarityTypeAlphabeticSorter());
values.add(new RarityTypeLengthSorter());
}
}

View File

@@ -1,6 +1,8 @@
package com.willfp.ecoenchants.display.options.sorting;
package com.willfp.ecoenchants.display.options.sorting.implementations;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.display.options.sorting.EnchantmentSorter;
import com.willfp.ecoenchants.display.options.sorting.SortParameters;
import org.bukkit.enchantments.Enchantment;
import java.util.List;
@@ -10,4 +12,9 @@ public class AlphabeticSorter implements EnchantmentSorter {
public void sortEnchantments(final List<Enchantment> toSort) {
toSort.sort(((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName())));
}
@Override
public SortParameters[] getParameters() {
return new SortParameters[0];
}
}

View File

@@ -1,6 +1,8 @@
package com.willfp.ecoenchants.display.options.sorting;
package com.willfp.ecoenchants.display.options.sorting.implementations;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.display.options.sorting.EnchantmentSorter;
import com.willfp.ecoenchants.display.options.sorting.SortParameters;
import org.bukkit.enchantments.Enchantment;
import java.util.Comparator;
@@ -11,4 +13,9 @@ public class LengthSorter implements EnchantmentSorter {
public void sortEnchantments(final List<Enchantment> toSort) {
toSort.sort(Comparator.comparingInt(enchantment -> EnchantmentCache.getEntry(enchantment).getRawName().length()));
}
@Override
public SortParameters[] getParameters() {
return new SortParameters[]{SortParameters.LENGTH};
}
}

View File

@@ -0,0 +1,33 @@
package com.willfp.ecoenchants.display.options.sorting.implementations;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.display.options.sorting.EnchantmentSorter;
import com.willfp.ecoenchants.display.options.sorting.SortParameters;
import org.bukkit.enchantments.Enchantment;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RarityAlphabeticSorter implements EnchantmentSorter {
@Override
public void sortEnchantments(final List<Enchantment> toSort) {
List<Enchantment> sorted = new ArrayList<>();
EnchantDisplay.OPTIONS.getSortedRarities().forEach(enchantmentRarity -> {
List<Enchantment> rarityEnchants = toSort.stream()
.filter(enchantment -> EnchantmentCache.getEntry(enchantment).getRarity().equals(enchantmentRarity))
.sorted((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))
.collect(Collectors.toList());
sorted.addAll(rarityEnchants);
});
toSort.clear();
toSort.addAll(sorted);
}
@Override
public SortParameters[] getParameters() {
return new SortParameters[]{SortParameters.RARITY};
}
}

View File

@@ -0,0 +1,34 @@
package com.willfp.ecoenchants.display.options.sorting.implementations;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.display.options.sorting.EnchantmentSorter;
import com.willfp.ecoenchants.display.options.sorting.SortParameters;
import org.bukkit.enchantments.Enchantment;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class RarityLengthSorter implements EnchantmentSorter {
@Override
public void sortEnchantments(final List<Enchantment> toSort) {
List<Enchantment> sorted = new ArrayList<>();
EnchantDisplay.OPTIONS.getSortedRarities().forEach(enchantmentRarity -> {
List<Enchantment> rarityEnchants = toSort.stream()
.filter(enchantment -> EnchantmentCache.getEntry(enchantment).getRarity().equals(enchantmentRarity))
.sorted(Comparator.comparingInt(enchantment -> EnchantmentCache.getEntry(enchantment).getRawName().length()))
.collect(Collectors.toList());
sorted.addAll(rarityEnchants);
});
toSort.clear();
toSort.addAll(sorted);
}
@Override
public SortParameters[] getParameters() {
return new SortParameters[]{SortParameters.RARITY, SortParameters.LENGTH};
}
}

View File

@@ -0,0 +1,39 @@
package com.willfp.ecoenchants.display.options.sorting.implementations;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.display.options.sorting.EnchantmentSorter;
import com.willfp.ecoenchants.display.options.sorting.SortParameters;
import org.bukkit.enchantments.Enchantment;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RarityTypeAlphabeticSorter implements EnchantmentSorter {
@Override
public void sortEnchantments(final List<Enchantment> toSort) {
List<Enchantment> sorted = new ArrayList<>();
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()))
.collect(Collectors.toList());
EnchantDisplay.OPTIONS.getSortedRarities().forEach(enchantmentRarity -> {
List<Enchantment> rarityEnchants = typeEnchants.stream()
.filter(enchantment -> EnchantmentCache.getEntry(enchantment).getRarity().equals(enchantmentRarity))
.sorted((enchantment1, enchantment2) -> EnchantmentCache.getEntry(enchantment1).getRawName().compareToIgnoreCase(EnchantmentCache.getEntry(enchantment2).getRawName()))
.collect(Collectors.toList());
sorted.addAll(rarityEnchants);
});
});
toSort.clear();
toSort.addAll(sorted);
}
@Override
public SortParameters[] getParameters() {
return new SortParameters[]{SortParameters.RARITY, SortParameters.TYPE};
}
}

View File

@@ -0,0 +1,40 @@
package com.willfp.ecoenchants.display.options.sorting.implementations;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.display.options.sorting.EnchantmentSorter;
import com.willfp.ecoenchants.display.options.sorting.SortParameters;
import org.bukkit.enchantments.Enchantment;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class RarityTypeLengthSorter implements EnchantmentSorter {
@Override
public void sortEnchantments(final List<Enchantment> toSort) {
List<Enchantment> sorted = new ArrayList<>();
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()))
.collect(Collectors.toList());
EnchantDisplay.OPTIONS.getSortedRarities().forEach(enchantmentRarity -> {
List<Enchantment> rarityEnchants = typeEnchants.stream()
.filter(enchantment -> EnchantmentCache.getEntry(enchantment).getRarity().equals(enchantmentRarity))
.sorted(Comparator.comparingInt(enchantment -> EnchantmentCache.getEntry(enchantment).getRawName().length()))
.collect(Collectors.toList());
sorted.addAll(rarityEnchants);
});
});
toSort.clear();
toSort.addAll(sorted);
}
@Override
public SortParameters[] getParameters() {
return new SortParameters[]{SortParameters.RARITY, SortParameters.TYPE, SortParameters.LENGTH};
}
}

View File

@@ -1,7 +1,9 @@
package com.willfp.ecoenchants.display.options.sorting;
package com.willfp.ecoenchants.display.options.sorting.implementations;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.display.options.sorting.EnchantmentSorter;
import com.willfp.ecoenchants.display.options.sorting.SortParameters;
import org.bukkit.enchantments.Enchantment;
import java.util.ArrayList;
@@ -23,4 +25,9 @@ public class TypeAlphabeticSorter implements EnchantmentSorter {
toSort.clear();
toSort.addAll(sorted);
}
@Override
public SortParameters[] getParameters() {
return new SortParameters[]{SortParameters.TYPE};
}
}

View File

@@ -1,7 +1,9 @@
package com.willfp.ecoenchants.display.options.sorting;
package com.willfp.ecoenchants.display.options.sorting.implementations;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.display.options.sorting.EnchantmentSorter;
import com.willfp.ecoenchants.display.options.sorting.SortParameters;
import org.bukkit.enchantments.Enchantment;
import java.util.ArrayList;
@@ -24,4 +26,9 @@ public class TypeLengthSorter implements EnchantmentSorter {
toSort.clear();
toSort.addAll(sorted);
}
@Override
public SortParameters[] getParameters() {
return new SortParameters[]{SortParameters.TYPE, SortParameters.LENGTH};
}
}

View File

@@ -173,7 +173,7 @@ public class EnchantmentRarity implements Registerable {
*
* @return A set of all rarities
*/
public static Set<EnchantmentRarity> getAll() {
public static Set<EnchantmentRarity> values() {
return rarities;
}
}

View File

@@ -93,7 +93,7 @@ public class EnchantmentTarget implements Registerable {
*
* @return A set of all rarities
*/
public static Set<EnchantmentTarget> getAll() {
public static Set<EnchantmentTarget> values() {
return targets;
}
}

View File

@@ -166,20 +166,20 @@ public class Loader {
Logger.info("Adding Enchantments to API...");
EnchantmentRarity.update();
EnchantmentTarget.update();
if (EnchantmentRarity.getAll().size() == 0 || EnchantmentTarget.getAll().size() == 0) {
if (EnchantmentRarity.values().size() == 0 || EnchantmentTarget.values().size() == 0) {
Logger.error("&cError loading rarities or targets! Aborting...");
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
return;
} else {
Logger.info(EnchantmentRarity.getAll().size() + " Rarities Loaded:");
EnchantmentRarity.getAll().forEach((rarity -> {
Logger.info(EnchantmentRarity.values().size() + " Rarities Loaded:");
EnchantmentRarity.values().forEach((rarity -> {
Logger.info("- " + rarity.getName() + ": Table Probability=" + rarity.getProbability() + ", Minimum Level=" + rarity.getMinimumLevel() + ", Villager Probability=" + rarity.getVillagerProbability() + ", Loot Probability=" + rarity.getLootProbability() + ", Has Custom Color=" + rarity.hasCustomColor());
}));
Logger.info("");
Logger.info(EnchantmentTarget.getAll().size() + " Targets Loaded:");
EnchantmentTarget.getAll().forEach((target) -> {
Logger.info(EnchantmentTarget.values().size() + " Targets Loaded:");
EnchantmentTarget.values().forEach((target) -> {
Logger.info("- " + target.getName() + ": Materials=" + target.getMaterials().toString());
});
}

View File

@@ -24,8 +24,11 @@ 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
sort-by-length: false # Sort enchantments by length. Any combination of this and the above option is valid
sort-by-length: false # Sort enchantments by length
sort-by-rarity: false # Sort enchantments by rarity.
# Any combination of the above options is valid
type-ordering: # Only used if sort-by-type is enabled - top to bottom
- normal
@@ -34,6 +37,16 @@ lore:
- spell
- curse
rarity-ordering: # Only used if sort-by-rarity is enabled - top to bottom
- common
- uncommon
- rare
- epic
- legendary
- special
- veryspecial
vanilla-rarity: uncommon # Vanilla enchantments do not have an EcoEnchants rarity - what rarity should they have?
describe: # Describe enchantments in lore
enabled: false
before-lines: 5 # Describe before or equal to number of enchantments