tag fixes

This commit is contained in:
nulli0n committed 2024-06-10 15:22:09 +05:00
1 parent 2f9ba7d063
commit c76a241d09
5 files changed
+54 -22

No files matched your search

@@ -84,13 +84,13 @@ public abstract class EditorMenu<P extends NightCorePlugin, T> extends AbstractM
return this.addItem(item, locale, slot, handler); return this.addItem(item, locale, slot, handler);
} }
/*@NotNull @NotNull
@Deprecated @Deprecated
protected MenuItem addExit(int... slots) { protected MenuItem addExit(int[] slots) {
//ItemStack item = ItemUtil.getSkinHead("27548362a24c0fa8453e4d93e68c5969ddbde57bf6666c0319c1ed1e84d89065"); //ItemStack item = ItemUtil.getSkinHead("27548362a24c0fa8453e4d93e68c5969ddbde57bf6666c0319c1ed1e84d89065");
//return this.addItem(item, CoreLang.EDITOR_ITEM_CLOSE, slots).setHandler(ItemHandler.forClose(this)); //return this.addItem(item, CoreLang.EDITOR_ITEM_CLOSE, slots).setHandler(ItemHandler.forClose(this));
return this.addExit(slots[0]); return this.addExit(slots[0]);
}*/ }
@NotNull @NotNull
protected MenuItem addExit(int slot) { protected MenuItem addExit(int slot) {
@@ -3,6 +3,7 @@ package su.nightexpress.nightcore.util;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import su.nightexpress.nightcore.util.regex.TimedMatcher; import su.nightexpress.nightcore.util.regex.TimedMatcher;
import su.nightexpress.nightcore.util.text.tag.api.Tag;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -13,7 +14,7 @@ import java.util.stream.Collectors;
public class Colorizer { public class Colorizer {
public static final Pattern PATTERN_HEX = Pattern.compile("#([A-Fa-f0-9]{6})"); 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("<gradient:" + PATTERN_HEX.pattern() + ">(.*?)</gradient:" + PATTERN_HEX.pattern() + ">"); public static final Pattern PATTERN_GRADIENT = Pattern.compile("<gradient:" + PATTERN_HEX.pattern() + ">(.*?)</gradient:" + PATTERN_HEX.pattern() + ">");
@NotNull @NotNull
@@ -126,20 +127,48 @@ public class Colorizer {
@NotNull @NotNull
public static String plainHex(@NotNull String str) { public static String plainHex(@NotNull String str) {
StringBuilder buffer = new StringBuilder(str); StringBuilder builder = new StringBuilder(str);
int index; 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; int count = 0;
buffer.replace(index, index + 2, "#"); builder.replace(index, index + 2, "#");
for (int point = index + 1; count < 6; point += 1) { for (int point = index + 1; count < 6; point += 1) {
buffer.deleteCharAt(point); builder.deleteCharAt(point);
count++; 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 @NotNull
@@ -111,6 +111,8 @@ public class Lists {
@NotNull @NotNull
public static List<String> getSequentialMatches(@NotNull List<String> results, @NotNull String input) { public static List<String> getSequentialMatches(@NotNull List<String> results, @NotNull String input) {
if (input.isBlank()) return results;
char[] chars = input.toCharArray(); char[] chars = input.toCharArray();
List<String> goods = new ArrayList<>(); List<String> goods = new ArrayList<>();
@@ -75,7 +75,10 @@ public class TextGroup implements ComponentBuildable {
} }
this.childrens.forEach(child -> { this.childrens.forEach(child -> {
builder.append(child.toComponent()); BaseComponent childComponent = child.toComponent();
if (!TextRoot.isEmpty(childComponent)) {
builder.append(childComponent);
}
}); });
return builder.build(); return builder.build();
@@ -6,21 +6,17 @@ import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import su.nightexpress.nightcore.core.CoreConfig;
import su.nightexpress.nightcore.util.Colorizer; 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.TagPool;
import su.nightexpress.nightcore.util.text.tag.Tags; 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.ContentTag;
import su.nightexpress.nightcore.util.text.tag.api.PlaceholderTag; 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.api.Tag;
import su.nightexpress.nightcore.util.text.tag.decorator.Decorator; 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.ResetTag;
import su.nightexpress.nightcore.util.text.tag.impl.ShortHexColorTag;
import su.nightexpress.nightcore.util.text.tag.impl.TranslationTag; import su.nightexpress.nightcore.util.text.tag.impl.TranslationTag;
import java.util.HashSet;
import java.util.Set;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
public class TextRoot { public class TextRoot {
@@ -42,7 +38,9 @@ public class TextRoot {
} }
public void setString(@NotNull String string) { 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); TimedMatcher timedMatcher = TimedMatcher.create(Colorizer.PATTERN_HEX_LEGACY, string);
Set<String> rawCodes = new HashSet<>(); Set<String> rawCodes = new HashSet<>();
while (timedMatcher.find()) { while (timedMatcher.find()) {
@@ -52,7 +50,7 @@ public class TextRoot {
for (String hex : rawCodes) { for (String hex : rawCodes) {
string = string.replace(hex, Tag.brackets(hex)); string = string.replace(hex, Tag.brackets(hex));
} }
} }*/
this.string = string; this.string = string;
this.component = null; this.component = null;
@@ -81,13 +79,9 @@ public class TextRoot {
@NotNull @NotNull
public String toLegacy() { public String toLegacy() {
if (this.string.isBlank()) return "";
this.parseIfAbsent(); this.parseIfAbsent();
if (this.component instanceof TextComponent textComponent && textComponent.getText().isBlank() && textComponent.getExtra() == null) { if (this.string.isBlank() || isEmpty(this.component)) return "";
return "";
}
return TextComponent.toLegacyText(this.parseIfAbsent()); return TextComponent.toLegacyText(this.parseIfAbsent());
} }
@@ -282,6 +276,10 @@ public class TextRoot {
return component; 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) { public static int indexOfIgnoreEscaped(@NotNull String string, char what, int from) {
int length = string.length(); int length = string.length();
if (from >= length) return -1; if (from >= length) return -1;