From 69f74b74517dc4a11d4b8ebf444b63b988d6a191 Mon Sep 17 00:00:00 2001 From: nulli0n Date: Wed, 20 Nov 2024 19:40:15 +0500 Subject: [PATCH] v2.7.2 --- .../nightcore/util/StringUtil.java | 5 +++ .../nightcore/util/bukkit/NightItem.java | 41 ++++++++++++++++--- .../nightcore/util/placeholder/Replacer.java | 29 +++++++++++-- .../nightcore/util/text/tag/api/Tag.java | 15 ++++--- .../util/text/tag/impl/ColorTag.java | 7 ++++ 5 files changed, 83 insertions(+), 14 deletions(-) diff --git a/src/main/java/su/nightexpress/nightcore/util/StringUtil.java b/src/main/java/su/nightexpress/nightcore/util/StringUtil.java index 3f7f4c4..c7cc939 100644 --- a/src/main/java/su/nightexpress/nightcore/util/StringUtil.java +++ b/src/main/java/su/nightexpress/nightcore/util/StringUtil.java @@ -262,6 +262,11 @@ public class StringUtil { return Color.fromRGB(red, green, blue); } + @NotNull + public static String transformForID(@NotNull String str) { + return transformForID(str, -1); + } + @NotNull public static String transformForID(@NotNull String str, int length) { char[] chars = str.toCharArray(); diff --git a/src/main/java/su/nightexpress/nightcore/util/bukkit/NightItem.java b/src/main/java/su/nightexpress/nightcore/util/bukkit/NightItem.java index fc82e4f..3bb61a8 100644 --- a/src/main/java/su/nightexpress/nightcore/util/bukkit/NightItem.java +++ b/src/main/java/su/nightexpress/nightcore/util/bukkit/NightItem.java @@ -6,16 +6,16 @@ import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.*; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.LeatherArmorMeta; +import org.bukkit.inventory.meta.PotionMeta; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import su.nightexpress.nightcore.config.FileConfig; import su.nightexpress.nightcore.config.Writeable; import su.nightexpress.nightcore.core.CoreLogger; -import su.nightexpress.nightcore.util.BukkitThing; -import su.nightexpress.nightcore.util.ItemUtil; -import su.nightexpress.nightcore.util.StringUtil; -import su.nightexpress.nightcore.util.Version; +import su.nightexpress.nightcore.util.*; import su.nightexpress.nightcore.util.placeholder.Replacer; import su.nightexpress.nightcore.util.text.NightMessage; @@ -258,7 +258,7 @@ public class NightItem implements Writeable { List replacedLore = replacer == null || this.lore == null ? this.lore : replacer.apply(this.lore); meta.setDisplayName(replacedDisplayName == null ? null : (legacy ? NightMessage.asLegacy(replacedDisplayName) : replacedDisplayName)); - meta.setLore(replacedLore == null ? null : (legacy ? NightMessage.asLegacy(replacedLore) : replacedLore)); + meta.setLore(replacedLore == null ? null : (legacy ? NightMessage.asLegacy(this.addEmptyLines(replacedLore)) : this.addEmptyLines(replacedLore))); meta.setCustomModelData(this.modelData); meta.setUnbreakable(this.unbreakable); @@ -288,6 +288,35 @@ public class NightItem implements Writeable { return itemStack; } + @NotNull + private List addEmptyLines(@NotNull List lore) { + for (int index = 0; index < lore.size(); index++) { + String line = lore.get(index); + if (line.equalsIgnoreCase(Placeholders.EMPTY_IF_ABOVE)) { + if (index == 0 || this.isEmpty(lore.get(index - 1))) { + lore.remove(index); + } + else lore.set(index, ""); + + return addEmptyLines(lore); + } + else if (line.equalsIgnoreCase(Placeholders.EMPTY_IF_BELOW)) { + if (index == lore.size() - 1 || this.isEmpty(lore.get(index + 1))) { + lore.remove(index); + } + else lore.set(index, ""); + + return addEmptyLines(lore); + } + } + + return lore; + } + + private boolean isEmpty(@NotNull String line) { + return line.isBlank() || line.equalsIgnoreCase(Placeholders.EMPTY_IF_ABOVE) || line.equalsIgnoreCase(Placeholders.EMPTY_IF_BELOW); + } + @NotNull public NightItem removeNameAndLore() { this.setItemName(null); diff --git a/src/main/java/su/nightexpress/nightcore/util/placeholder/Replacer.java b/src/main/java/su/nightexpress/nightcore/util/placeholder/Replacer.java index 7c2af60..1f2cd6a 100644 --- a/src/main/java/su/nightexpress/nightcore/util/placeholder/Replacer.java +++ b/src/main/java/su/nightexpress/nightcore/util/placeholder/Replacer.java @@ -12,6 +12,7 @@ import su.nightexpress.nightcore.util.text.NightMessage; import su.nightexpress.nightcore.util.text.TextRoot; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.function.Consumer; import java.util.function.Supplier; @@ -89,9 +90,16 @@ public class Replacer { @NotNull public List apply(@NotNull List list) { - List replaced = new ArrayList<>(); - list.forEach(line -> replaced.add(this.apply(line))); - return replaced; +// List replaced = new ArrayList<>(); +// list.forEach(line -> replaced.add(this.apply(line))); +// return replaced; + + List result = new ArrayList<>(list); + for (UnaryOperator operator : this.getReplacers()) { + result = replaceList(result, operator); + } + + return result; } @NotNull @@ -115,6 +123,21 @@ public class Replacer { return meta; } + @NotNull + private static List replaceList(@NotNull List lore, @NotNull UnaryOperator operator) { + List replaced = new ArrayList<>(); + for (String line : lore) { + if (!line.isBlank()) { + line = operator.apply(line); + if (line.isBlank()) continue; + + replaced.addAll(Arrays.asList(line.split("\n"))); + } + else replaced.add(line); + } + return replaced; + } + @NotNull public Replacer replacePlaceholderAPI(@NotNull Player player) { if (!Plugins.hasPlaceholderAPI()) return this; diff --git a/src/main/java/su/nightexpress/nightcore/util/text/tag/api/Tag.java b/src/main/java/su/nightexpress/nightcore/util/text/tag/api/Tag.java index 5db08f2..6982fa3 100644 --- a/src/main/java/su/nightexpress/nightcore/util/text/tag/api/Tag.java +++ b/src/main/java/su/nightexpress/nightcore/util/text/tag/api/Tag.java @@ -33,12 +33,21 @@ public class Tag { return OPEN_BRACKET + str + CLOSE_BRACKET; } + @NotNull + public static String closedBrackets(@NotNull String str) { + return brackets(CLOSE_SLASH + str); + } + /*@NotNull @Deprecated public String enclose(@NotNull String text) { return this.getBracketsName() + text + this.getClosingName(); }*/ + public final boolean isNamed(@NotNull String name) { + return this.name.equalsIgnoreCase(name) || this.aliases.contains(name.toLowerCase()); + } + @NotNull @Deprecated public final String getFullName() { @@ -47,11 +56,7 @@ public class Tag { @NotNull public final String getClosingName() { - return brackets(CLOSE_SLASH + this.getName()); - } - - public final boolean isNamed(@NotNull String name) { - return this.name.equalsIgnoreCase(name) || this.aliases.contains(name.toLowerCase()); + return closedBrackets(this.getName()); } @NotNull diff --git a/src/main/java/su/nightexpress/nightcore/util/text/tag/impl/ColorTag.java b/src/main/java/su/nightexpress/nightcore/util/text/tag/impl/ColorTag.java index 35695f2..8c1f36d 100644 --- a/src/main/java/su/nightexpress/nightcore/util/text/tag/impl/ColorTag.java +++ b/src/main/java/su/nightexpress/nightcore/util/text/tag/impl/ColorTag.java @@ -43,6 +43,13 @@ public class ColorTag extends SimpleTag implements ColorDecorator { return color; } + @NotNull + public String encloseHex(@NotNull String text) { + String hex = this.toHexString(); + + return brackets(hex) + text + closedBrackets(hex); + } + @Override public void decorate(@NotNull BaseComponent component) { component.setColor(ChatColor.of(this.getColor()));