From d8e6628627451ecc879d7804649ddc71b8ccc45d Mon Sep 17 00:00:00 2001 From: toxicity Date: Wed, 30 Oct 2024 21:53:26 +0900 Subject: [PATCH] Placeholder fixes. --- .../api/condition/HealthBarOperation.java | 14 +++- .../api/placeholder/HealthBarPlaceholder.java | 9 ++- .../api/placeholder/PlaceholderBuilder.java | 4 +- .../api/placeholder/PlaceholderContainer.java | 64 +++++++++++-------- build.gradle.kts | 2 +- 5 files changed, 60 insertions(+), 33 deletions(-) diff --git a/api/src/main/java/kr/toxicity/healthbar/api/condition/HealthBarOperation.java b/api/src/main/java/kr/toxicity/healthbar/api/condition/HealthBarOperation.java index 28ff487..4a1b79c 100644 --- a/api/src/main/java/kr/toxicity/healthbar/api/condition/HealthBarOperation.java +++ b/api/src/main/java/kr/toxicity/healthbar/api/condition/HealthBarOperation.java @@ -44,7 +44,12 @@ default HealthBarOperation or(@NotNull HealthBarOperation other) { return HealthBarOperationCondition.find(clazz, name); } static @NotNull HealthBarCondition of(@NotNull HealthBarPlaceholder one, @NotNull HealthBarPlaceholder other, @NotNull HealthBarOperation condition) { - return t -> condition.invoke(one.value(t), other.value(t)); + return t -> { + var v1 = one.value(t); + var v2 = other.value(t); + if (v1 == null || v2 == null) return false; + return condition.invoke(v1, v2); + }; } @SuppressWarnings("unchecked") static @NotNull HealthBarCondition of(@NotNull String one, @NotNull String two, @NotNull String condition) { @@ -52,7 +57,12 @@ default HealthBarOperation or(@NotNull HealthBarOperation other) { var parseTwo = PlaceholderContainer.parse(two); if (parseOne.type() != parseTwo.type()) throw new RuntimeException("type mismatch: " + parseOne.type().getSimpleName() + " between " + parseTwo.type().getSimpleName() + "."); var operation = Objects.requireNonNull(find(parseOne.type(), condition), "Unable to find this operation: " + condition); - return p -> operation.invoke(parseOne.value(p), parseTwo.value(p)); + return p -> { + var v1 = parseOne.value(p); + var v2 = parseTwo.value(p); + if (v1 == null || v2 == null) return false; + return operation.invoke(v1, v2); + }; } static @NotNull HealthBarCondition of(@NotNull ConfigurationSection section) { diff --git a/api/src/main/java/kr/toxicity/healthbar/api/placeholder/HealthBarPlaceholder.java b/api/src/main/java/kr/toxicity/healthbar/api/placeholder/HealthBarPlaceholder.java index 52e4cea..09c3379 100644 --- a/api/src/main/java/kr/toxicity/healthbar/api/placeholder/HealthBarPlaceholder.java +++ b/api/src/main/java/kr/toxicity/healthbar/api/placeholder/HealthBarPlaceholder.java @@ -7,8 +7,13 @@ public interface HealthBarPlaceholder { @NotNull Class type(); - @NotNull - T value(@NotNull HealthBarCreateEvent player); + @Nullable + T value(@NotNull HealthBarCreateEvent event); + + default @Nullable String stringValue(@NotNull HealthBarCreateEvent event) { + var value = value(event); + return value != null ? value.toString() : ""; + } default @Nullable T cast(@NotNull Object object) { return type().isAssignableFrom(object.getClass()) ? type().cast(object) : null; diff --git a/api/src/main/java/kr/toxicity/healthbar/api/placeholder/PlaceholderBuilder.java b/api/src/main/java/kr/toxicity/healthbar/api/placeholder/PlaceholderBuilder.java index 560185a..a9762e1 100644 --- a/api/src/main/java/kr/toxicity/healthbar/api/placeholder/PlaceholderBuilder.java +++ b/api/src/main/java/kr/toxicity/healthbar/api/placeholder/PlaceholderBuilder.java @@ -24,8 +24,8 @@ public Class type() { @NotNull @Override - public T value(@NotNull HealthBarCreateEvent player) { - return get.apply(player); + public T value(@NotNull HealthBarCreateEvent event) { + return get.apply(event); } }; } diff --git a/api/src/main/java/kr/toxicity/healthbar/api/placeholder/PlaceholderContainer.java b/api/src/main/java/kr/toxicity/healthbar/api/placeholder/PlaceholderContainer.java index 6491f3b..e385ec2 100644 --- a/api/src/main/java/kr/toxicity/healthbar/api/placeholder/PlaceholderContainer.java +++ b/api/src/main/java/kr/toxicity/healthbar/api/placeholder/PlaceholderContainer.java @@ -6,6 +6,7 @@ import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.text.DecimalFormat; import java.util.*; @@ -17,7 +18,7 @@ public class PlaceholderContainer { private final @NotNull Function parser; private final @NotNull Function stringMapper; - public static final Pattern PATTERN = Pattern.compile("^(\\((?([a-zA-Z]+))\\))?((?(([a-zA-Z]|\\(|\\)|[0-9]|_|\\.)+))(:(?([a-zA-Z]|[0-9]|_|\\.|,|)+))?)$"); + public static final Pattern PATTERN = Pattern.compile("^(\\((?([a-zA-Z]+))\\))?(?(([a-zA-Z]|[0-9]|_|\\.)+))$"); private static final Map, PlaceholderContainer> CLASS_MAP = new HashMap<>(); private static final Map> STRING_MAP = new HashMap<>(); @@ -79,8 +80,8 @@ public int requiredArgsCount() { return new HealthBarPlaceholder<>() { @NotNull @Override - public T value(@NotNull HealthBarCreateEvent player) { - return function.apply(player); + public T value(@NotNull HealthBarCreateEvent event) { + return function.apply(event); } @NotNull @@ -128,8 +129,8 @@ public Class type() { @NotNull @Override - public String value(@NotNull HealthBarCreateEvent player) { - return stringMapper.apply(apply.value(player)); + public String value(@NotNull HealthBarCreateEvent event) { + return stringMapper.apply(apply.value(event)); } }; } @@ -143,7 +144,7 @@ private record PrimitivePlaceholder(@NotNull Class refer, Object value) } @Override - public @NotNull Object value(@NotNull HealthBarCreateEvent player) { + public @NotNull Object value(@NotNull HealthBarCreateEvent event) { return value; } } @@ -156,9 +157,22 @@ public static HealthBarPlaceholder primitive(@NotNull String value) { }).filter(Objects::nonNull).findFirst().orElseThrow(() -> new RuntimeException("Unable to parse this value: " + value)); } - public static HealthBarPlaceholder parse(@NotNull String pattern) { - var matcher = PATTERN.matcher(pattern); - if (!matcher.find()) return primitive(pattern); + public static @NotNull HealthBarPlaceholder parse(@NotNull String pattern) { + var index = 0; + while (index < pattern.length() && pattern.charAt(index) != ':') { + index++; + } + String main; + String argument; + if (index < pattern.length()) { + main = pattern.substring(0, index); + argument = pattern.substring(index + 1); + } else { + main = pattern; + argument = null; + } + var matcher = PATTERN.matcher(main); + if (!matcher.find()) return primitive(main); var type = matcher.group("type"); var cast = type != null ? Objects.requireNonNull(STRING_MAP.get(type), "Unsupported type: " + type) : null; var name = matcher.group("name"); @@ -167,7 +181,6 @@ public static HealthBarPlaceholder parse(@NotNull String pattern) { if (get == null) return primitive(name); - var argument = matcher.group("argument"); var list = argument != null ? Arrays.asList(argument.split(",")) : Collections.emptyList(); if (get.result.requiredArgsCount() > list.size()) throw new RuntimeException("This placeholder requires argument sized at least " + get.result.requiredArgsCount()); if (cast != null) { @@ -180,10 +193,17 @@ public Class type() { return (Class) cast.clazz; } - @NotNull + @Nullable @Override - public Object value(@NotNull HealthBarCreateEvent player) { - return cast.parser.apply(string.value(player)); + public Object value(@NotNull HealthBarCreateEvent event) { + return cast.parser.apply(string.value(event)); + } + + @Override + @SuppressWarnings("unchecked") + public @Nullable String stringValue(@NotNull HealthBarCreateEvent event) { + var value = value(event); + return value != null ? ((Function) cast.stringMapper).apply(value) : ""; } }; } else return get.value(list); @@ -202,10 +222,11 @@ public Object value(@NotNull HealthBarCreateEvent player) { } case ']' -> { var result = sb.toString(); - var name = subString(result); - var argument = result.length() > name.length() + 1 ? Arrays.asList(result.substring(name.length() + 1).split(",")) : Collections.emptyList(); - var find = CLASS_MAP.values().stream().map(f -> f.find(name)).filter(f -> f.ifPresented()).findFirst().orElseThrow(() -> new RuntimeException("Unable to find this placeholder: " + name)).stringValue(argument); - array.add(f -> legacyAdapt(find.value(f))); + var parse = parse(result); + array.add(f -> { + var value = parse.stringValue(f); + return value != null ? legacyAdapt(value) : Component.text(""); + }); sb.setLength(0); } case '\\' -> skip = true; @@ -259,13 +280,4 @@ public Object value(@NotNull HealthBarCreateEvent player) { private static @NotNull Component legacyAdapt0(@NotNull String s) { return LegacyComponentSerializer.legacySection().deserialize(s).replaceText(TO_MINI_MESSAGE); } - - private static @NotNull String subString(@NotNull String string) { - var sb = new StringBuilder(); - for (char c1 : string.toCharArray()) { - if (':' != c1) sb.append(c1); - else break; - } - return sb.toString(); - } } diff --git a/build.gradle.kts b/build.gradle.kts index 4317e51..1d3ea65 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -173,7 +173,7 @@ tasks { finalizedBy(shadowJar) } runServer { - version(minecraft) + version("1.21.1") pluginJars(fileTree("plugins")) downloadPlugins { modrinth("betterhud2", "1.7.DEV-264")