Skip to content

Commit

Permalink
Placeholder fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Oct 30, 2024
1 parent fd2f43d commit d8e6628
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,25 @@ default HealthBarOperation<T> or(@NotNull HealthBarOperation<T> other) {
return HealthBarOperationCondition.find(clazz, name);
}
static <T> @NotNull HealthBarCondition of(@NotNull HealthBarPlaceholder<T> one, @NotNull HealthBarPlaceholder<T> other, @NotNull HealthBarOperation<T> 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) {
var parseOne = (HealthBarPlaceholder<Object>) PlaceholderContainer.parse(one);
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
public interface HealthBarPlaceholder<T> {
@NotNull
Class<T> 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() : "<error>";
}

default @Nullable T cast(@NotNull Object object) {
return type().isAssignableFrom(object.getClass()) ? type().cast(object) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public Class<T> type() {

@NotNull
@Override
public T value(@NotNull HealthBarCreateEvent player) {
return get.apply(player);
public T value(@NotNull HealthBarCreateEvent event) {
return get.apply(event);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -17,7 +18,7 @@ public class PlaceholderContainer<T> {
private final @NotNull Function<String, T> parser;
private final @NotNull Function<T, String> stringMapper;

public static final Pattern PATTERN = Pattern.compile("^(\\((?<type>([a-zA-Z]+))\\))?((?<name>(([a-zA-Z]|\\(|\\)|[0-9]|_|\\.)+))(:(?<argument>([a-zA-Z]|[0-9]|_|\\.|,|)+))?)$");
public static final Pattern PATTERN = Pattern.compile("^(\\((?<type>([a-zA-Z]+))\\))?(?<name>(([a-zA-Z]|[0-9]|_|\\.)+))$");
private static final Map<Class<?>, PlaceholderContainer<?>> CLASS_MAP = new HashMap<>();
private static final Map<String, PlaceholderContainer<?>> STRING_MAP = new HashMap<>();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -128,8 +129,8 @@ public Class<String> 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));
}
};
}
Expand All @@ -143,7 +144,7 @@ private record PrimitivePlaceholder(@NotNull Class<Object> refer, Object value)
}

@Override
public @NotNull Object value(@NotNull HealthBarCreateEvent player) {
public @NotNull Object value(@NotNull HealthBarCreateEvent event) {
return value;
}
}
Expand All @@ -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");
Expand All @@ -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.<String>emptyList();
if (get.result.requiredArgsCount() > list.size()) throw new RuntimeException("This placeholder requires argument sized at least " + get.result.requiredArgsCount());
if (cast != null) {
Expand All @@ -180,10 +193,17 @@ public Class<Object> type() {
return (Class<Object>) 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<Object, String>) cast.stringMapper).apply(value) : "<error>";
}
};
} else return get.value(list);
Expand All @@ -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.<String>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("<error>");
});
sb.setLength(0);
}
case '\\' -> skip = true;
Expand Down Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ tasks {
finalizedBy(shadowJar)
}
runServer {
version(minecraft)
version("1.21.1")
pluginJars(fileTree("plugins"))
downloadPlugins {
modrinth("betterhud2", "1.7.DEV-264")
Expand Down

0 comments on commit d8e6628

Please sign in to comment.