Minor performance boost

This commit is contained in:
nulli0n 2023-01-29 18:02:29 +06:00
parent 390f7d0149
commit 7d82eaa31f
4 changed files with 37 additions and 31 deletions

View File

@ -24,11 +24,11 @@ import su.nightexpress.excellentenchants.api.enchantment.meta.Potioned;
import su.nightexpress.excellentenchants.api.enchantment.util.EnchantPriority;
import su.nightexpress.excellentenchants.config.Config;
import su.nightexpress.excellentenchants.enchantment.EnchantManager;
import su.nightexpress.excellentenchants.enchantment.config.EnchantScaler;
import su.nightexpress.excellentenchants.enchantment.impl.meta.ChanceImplementation;
import su.nightexpress.excellentenchants.enchantment.impl.meta.PotionImplementation;
import su.nightexpress.excellentenchants.enchantment.type.FitItemType;
import su.nightexpress.excellentenchants.enchantment.type.ObtainType;
import su.nightexpress.excellentenchants.enchantment.config.EnchantScaler;
import su.nightexpress.excellentenchants.tier.Tier;
import java.util.*;
@ -155,20 +155,8 @@ public abstract class ExcellentEnchant extends Enchantment implements IEnchantme
}
@NotNull
public UnaryOperator<String> replacePlaceholders(int level) {
return str -> {
str = str.replace(Placeholders.ENCHANTMENT_DESCRIPTION, String.join("\n", this.getDescription()));
if (this instanceof Chanced chanced) {
str = str.replace(ChanceImplementation.PLACEHOLDER_CHANCE, NumberUtil.format(chanced.getTriggerChance(level)));
}
if (this instanceof Potioned potioned) {
str = str
.replace(PotionImplementation.PLACEHOLDER_POTION_LEVEL, NumberUtil.toRoman(potioned.getEffectAmplifier(level)))
.replace(PotionImplementation.PLACEHOLDER_POTION_DURATION, NumberUtil.format((double) potioned.getEffectDuration(level) / 20D))
.replace(PotionImplementation.PLACEHOLDER_POTION_TYPE, LangManager.getPotionType(potioned.getEffectType()));
}
return str
public UnaryOperator<String> replaceAllPlaceholders(int level) {
return str -> this.replacePlaceholders(level).apply(str)
.replace(Placeholders.ENCHANTMENT_NAME, this.getDisplayName())
.replace(Placeholders.ENCHANTMENT_NAME_FORMATTED, this.getNameFormatted(level))
.replace(Placeholders.ENCHANTMENT_LEVEL, NumberUtil.toRoman(level))
@ -186,6 +174,23 @@ public abstract class ExcellentEnchant extends Enchantment implements IEnchantme
.replace(Placeholders.ENCHANTMENT_CHARGES_RECHARGE_AMOUNT, String.valueOf(this.getChargesRechargeAmount(level)))
.replace(Placeholders.ENCHANTMENT_CHARGES_FUEL_ITEM, ItemUtil.getItemName(this.getChargesFuel()))
;
}
@NotNull
public UnaryOperator<String> replacePlaceholders(int level) {
return str -> {
str = str.replace(Placeholders.ENCHANTMENT_DESCRIPTION, String.join("\n", this.getDescription()));
if (this instanceof Chanced chanced) {
str = str.replace(ChanceImplementation.PLACEHOLDER_CHANCE, NumberUtil.format(chanced.getTriggerChance(level)));
}
if (this instanceof Potioned potioned) {
str = str
.replace(PotionImplementation.PLACEHOLDER_POTION_LEVEL, NumberUtil.toRoman(potioned.getEffectAmplifier(level)))
.replace(PotionImplementation.PLACEHOLDER_POTION_DURATION, NumberUtil.format((double) potioned.getEffectDuration(level) / 20D))
.replace(PotionImplementation.PLACEHOLDER_POTION_TYPE, LangManager.getPotionType(potioned.getEffectType()));
}
return str;
};
}

View File

@ -275,7 +275,7 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
return enchant == null ? null : Pair.of(enchant, entry.getValue());
})
.filter(Objects::nonNull)
.sorted(Comparator.comparing(p -> p.getFirst().getPriority(), Comparator.reverseOrder()))
//.sorted(Comparator.comparing(p -> p.getFirst().getPriority(), Comparator.reverseOrder()))
.collect(Collectors.toMap(Pair::getFirst, Pair::getSecond, (old, nev) -> nev, LinkedHashMap::new));
}

View File

@ -139,7 +139,7 @@ public class EnchantmentsListMenu extends AbstractMenuAuto<ExcellentEnchants, Ex
meta.setLore(lore);
icon.setItemMeta(meta);
ItemUtil.replace(icon, enchant.replacePlaceholders(level));
ItemUtil.replace(icon, enchant.replaceAllPlaceholders(level));
return icon;
}

View File

@ -84,20 +84,21 @@ public class ProtocolHook {
ItemMeta meta = copy.getItemMeta();
if (meta == null) return item;
Map<ExcellentEnchant, Integer> enchants = EnchantManager.getExcellentEnchantments(item);
Map<ExcellentEnchant, Integer> enchants = EnchantManager.getExcellentEnchantments(item)
.entrySet().stream()
.sorted(Comparator.comparing(e -> e.getKey().getTier().getPriority()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (old,nev) -> nev, LinkedHashMap::new));
if (enchants.isEmpty()) return item;
List<String> lore = meta.getLore() == null ? new ArrayList<>() : meta.getLore();
if (!lore.isEmpty()) {
enchants.keySet().forEach(enchant -> lore.removeIf(line -> line.contains(enchant.getDisplayName())));
if (isCreative) {
enchants.forEach((enchant, level) -> {
lore.removeAll(enchant.formatDescription(level));
});
}
enchants = enchants.entrySet().stream()
.sorted(Comparator.comparing(e -> e.getKey().getTier().getPriority()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (old,nev) -> nev, LinkedHashMap::new));
}
if (Config.ENCHANTMENTS_DESCRIPTION_ENABLED.get() && !isCreative) {
enchants.forEach((enchant, level) -> {
lore.addAll(0, enchant.formatDescription(level));