diff --git a/src/main/java/su/nightexpress/nightcore/menu/impl/EditorMenu.java b/src/main/java/su/nightexpress/nightcore/menu/impl/EditorMenu.java index f265ffa..00a8b98 100644 --- a/src/main/java/su/nightexpress/nightcore/menu/impl/EditorMenu.java +++ b/src/main/java/su/nightexpress/nightcore/menu/impl/EditorMenu.java @@ -84,13 +84,13 @@ public abstract class EditorMenu

extends AbstractM return this.addItem(item, locale, slot, handler); } - /*@NotNull + @NotNull @Deprecated - protected MenuItem addExit(int... slots) { + protected MenuItem addExit(int[] slots) { //ItemStack item = ItemUtil.getSkinHead("27548362a24c0fa8453e4d93e68c5969ddbde57bf6666c0319c1ed1e84d89065"); //return this.addItem(item, CoreLang.EDITOR_ITEM_CLOSE, slots).setHandler(ItemHandler.forClose(this)); return this.addExit(slots[0]); - }*/ + } @NotNull protected MenuItem addExit(int slot) { diff --git a/src/main/java/su/nightexpress/nightcore/util/Colorizer.java b/src/main/java/su/nightexpress/nightcore/util/Colorizer.java index f75d4b9..8b52c3b 100644 --- a/src/main/java/su/nightexpress/nightcore/util/Colorizer.java +++ b/src/main/java/su/nightexpress/nightcore/util/Colorizer.java @@ -3,6 +3,7 @@ package su.nightexpress.nightcore.util; import net.md_5.bungee.api.ChatColor; import org.jetbrains.annotations.NotNull; import su.nightexpress.nightcore.util.regex.TimedMatcher; +import su.nightexpress.nightcore.util.text.tag.api.Tag; import java.util.List; import java.util.Set; @@ -13,7 +14,7 @@ import java.util.stream.Collectors; public class Colorizer { public static final Pattern PATTERN_HEX = Pattern.compile("#([A-Fa-f0-9]{6})"); - public static final Pattern PATTERN_HEX_LEGACY = Pattern.compile("(?:^|[^<])(#[A-Fa-f0-9]{6})(?:$|[^:>])"); + //public static final Pattern PATTERN_HEX_LEGACY = Pattern.compile("(?:^|[^<])(#[A-Fa-f0-9]{6})(?:$|[^:>])"); public static final Pattern PATTERN_GRADIENT = Pattern.compile("(.*?)"); @NotNull @@ -126,20 +127,48 @@ public class Colorizer { @NotNull public static String plainHex(@NotNull String str) { - StringBuilder buffer = new StringBuilder(str); + StringBuilder builder = new StringBuilder(str); int index; - while ((index = buffer.toString().indexOf(ChatColor.COLOR_CHAR + "x")) >= 0) { + while ((index = builder.toString().indexOf(ChatColor.COLOR_CHAR + "x")) >= 0) { int count = 0; - buffer.replace(index, index + 2, "#"); + builder.replace(index, index + 2, "#"); for (int point = index + 1; count < 6; point += 1) { - buffer.deleteCharAt(point); + builder.deleteCharAt(point); count++; } } - return buffer.toString(); + return tagPlainHex(builder.toString()); + } + + @NotNull + public static String tagPlainHex(@NotNull String str) { + StringBuilder builder = new StringBuilder(str); + + int index; + int lastIndex = 0; + + while ((index = builder.toString().indexOf("#", lastIndex)) >= 0) { + lastIndex = index + 1; + + if (builder.length() < 7) break; + if (index > 0 && builder.charAt(index - 1) == Tag.OPEN_BRACKET) continue; + + String sub = builder.substring(index, index + 7); + try { + Integer.decode(sub); + } + catch (NumberFormatException exception) { + continue; + } + + builder.insert(index, Tag.OPEN_BRACKET); + builder.insert(index + 8, Tag.CLOSE_BRACKET); + } + + return builder.toString(); } @NotNull diff --git a/src/main/java/su/nightexpress/nightcore/util/Lists.java b/src/main/java/su/nightexpress/nightcore/util/Lists.java index 29a9fd3..563da82 100644 --- a/src/main/java/su/nightexpress/nightcore/util/Lists.java +++ b/src/main/java/su/nightexpress/nightcore/util/Lists.java @@ -111,6 +111,8 @@ public class Lists { @NotNull public static List getSequentialMatches(@NotNull List results, @NotNull String input) { + if (input.isBlank()) return results; + char[] chars = input.toCharArray(); List goods = new ArrayList<>(); diff --git a/src/main/java/su/nightexpress/nightcore/util/text/TextGroup.java b/src/main/java/su/nightexpress/nightcore/util/text/TextGroup.java index 1972900..45ac190 100644 --- a/src/main/java/su/nightexpress/nightcore/util/text/TextGroup.java +++ b/src/main/java/su/nightexpress/nightcore/util/text/TextGroup.java @@ -75,7 +75,10 @@ public class TextGroup implements ComponentBuildable { } this.childrens.forEach(child -> { - builder.append(child.toComponent()); + BaseComponent childComponent = child.toComponent(); + if (!TextRoot.isEmpty(childComponent)) { + builder.append(childComponent); + } }); return builder.build(); diff --git a/src/main/java/su/nightexpress/nightcore/util/text/TextRoot.java b/src/main/java/su/nightexpress/nightcore/util/text/TextRoot.java index b5c25f8..1b2e7a3 100644 --- a/src/main/java/su/nightexpress/nightcore/util/text/TextRoot.java +++ b/src/main/java/su/nightexpress/nightcore/util/text/TextRoot.java @@ -6,21 +6,17 @@ import net.md_5.bungee.chat.ComponentSerializer; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import su.nightexpress.nightcore.core.CoreConfig; import su.nightexpress.nightcore.util.Colorizer; -import su.nightexpress.nightcore.util.regex.TimedMatcher; import su.nightexpress.nightcore.util.text.tag.TagPool; import su.nightexpress.nightcore.util.text.tag.Tags; import su.nightexpress.nightcore.util.text.tag.api.ContentTag; import su.nightexpress.nightcore.util.text.tag.api.PlaceholderTag; import su.nightexpress.nightcore.util.text.tag.api.Tag; import su.nightexpress.nightcore.util.text.tag.decorator.Decorator; -import su.nightexpress.nightcore.util.text.tag.impl.ShortHexColorTag; import su.nightexpress.nightcore.util.text.tag.impl.ResetTag; +import su.nightexpress.nightcore.util.text.tag.impl.ShortHexColorTag; import su.nightexpress.nightcore.util.text.tag.impl.TranslationTag; -import java.util.HashSet; -import java.util.Set; import java.util.function.UnaryOperator; public class TextRoot { @@ -42,7 +38,9 @@ public class TextRoot { } public void setString(@NotNull String string) { - if (CoreConfig.LEGACY_COLOR_SUPPORT.get()) { + string = Colorizer.plain(string); + + /*if (CoreConfig.LEGACY_COLOR_SUPPORT.get()) { TimedMatcher timedMatcher = TimedMatcher.create(Colorizer.PATTERN_HEX_LEGACY, string); Set rawCodes = new HashSet<>(); while (timedMatcher.find()) { @@ -52,7 +50,7 @@ public class TextRoot { for (String hex : rawCodes) { string = string.replace(hex, Tag.brackets(hex)); } - } + }*/ this.string = string; this.component = null; @@ -81,13 +79,9 @@ public class TextRoot { @NotNull public String toLegacy() { - if (this.string.isBlank()) return ""; - this.parseIfAbsent(); - if (this.component instanceof TextComponent textComponent && textComponent.getText().isBlank() && textComponent.getExtra() == null) { - return ""; - } + if (this.string.isBlank() || isEmpty(this.component)) return ""; return TextComponent.toLegacyText(this.parseIfAbsent()); } @@ -282,6 +276,10 @@ public class TextRoot { return component; } + public static boolean isEmpty(@NotNull BaseComponent component) { + return component instanceof TextComponent textComponent && textComponent.getText().isBlank() && textComponent.getExtra() == null; + } + public static int indexOfIgnoreEscaped(@NotNull String string, char what, int from) { int length = string.length(); if (from >= length) return -1;