Skip to content

Commit

Permalink
3.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Nov 2, 2024
1 parent 87a1d89 commit 4c482be
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 62 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ betterhealthbar.reload - Access to reload command
./gradlew build

# API
[![](https://jitpack.io/v/toxicity188/BetterHealthBar3.svg)](https://jitpack.io/#toxicity188/BetterHealthBar3)
``` kotlin
repositories {
mavenCenteral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import kr.toxicity.healthbar.api.placeholder.HealthBarPlaceholder;
import kr.toxicity.healthbar.api.placeholder.PlaceholderContainer;
import kr.toxicity.healthbar.api.placeholder.PlaceholderOption;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -53,8 +54,8 @@ default HealthBarOperation<T> or(@NotNull HealthBarOperation<T> other) {
}
@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);
var parseOne = (HealthBarPlaceholder<Object>) PlaceholderContainer.parse(PlaceholderOption.EMPTY, one);
var parseTwo = PlaceholderContainer.parse(PlaceholderOption.EMPTY, 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 -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.toxicity.healthbar.api.layout;

import kr.toxicity.healthbar.api.event.HealthBarCreateEvent;
import kr.toxicity.healthbar.api.placeholder.PlaceholderOption;
import kr.toxicity.healthbar.api.renderer.TextRenderer;
import kr.toxicity.healthbar.api.text.TextAlign;
import net.kyori.adventure.text.Component;
Expand All @@ -18,6 +19,8 @@ public interface TextLayout extends Layout {
@NotNull
TextAlign align();

@NotNull PlaceholderOption.Property property();

@NotNull
Function<HealthBarCreateEvent, Component> pattern();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.function.Function;

public interface PlaceholderBuilder<T> {
@NotNull HealthBarPlaceholder<T> build(@NotNull List<String> strings);
@NotNull HealthBarPlaceholder<T> build(@NotNull PlaceholderOption.Property property, @NotNull List<String> strings);
int requiredArgsCount();

static <R> @NotNull Builder<R> builder(@NotNull PlaceholderContainer<R> container) {
Expand All @@ -22,17 +22,16 @@ public interface PlaceholderBuilder<T> {
}

class Builder<R> {
private final Class<R> clazz;
private final PlaceholderContainer<R> container;
private Function<R, String> stringMapper;
private Function<List<String>, Function<HealthBarCreateEvent, R>> parser;
private int argsLength;
private Builder(@NotNull PlaceholderContainer<R> container) {
clazz = container.clazz();
stringMapper = container.stringMapper();
this.container = container;
}

public @NotNull Builder<R> stringMapper(@NotNull Function<R, String> mapper) {
stringMapper = Objects.requireNonNull(mapper);
public @NotNull Builder<R> stringMapper(@Nullable Function<R, String> mapper) {
stringMapper = mapper;
return this;
}
public @NotNull Builder<R> parser(@NotNull Function<List<String>, Function<HealthBarCreateEvent, R>> parser) {
Expand All @@ -47,12 +46,14 @@ private Builder(@NotNull PlaceholderContainer<R> container) {
public @NotNull PlaceholderBuilder<R> build() {
return new PlaceholderBuilder<>() {
@Override
public @NotNull HealthBarPlaceholder<R> build(@NotNull List<String> strings) {
public @NotNull HealthBarPlaceholder<R> build(@NotNull PlaceholderOption.Property property, @NotNull List<String> strings) {
var valueFunction = parser.apply(strings);
var result = container.propertyResult(property);
var mapper = stringMapper != null ? stringMapper : result.stringMapper();
return new HealthBarPlaceholder<>() {
@Override
public @NotNull Class<R> type() {
return clazz;
return container.clazz();
}

@Override
Expand All @@ -63,7 +64,7 @@ private Builder(@NotNull PlaceholderContainer<R> container) {
@Override
public @Nullable String stringValue(@NotNull HealthBarCreateEvent event) {
var parsed = value(event);
return parsed != null ? stringMapper.apply(parsed) : null;
return parsed != null ? mapper.apply(parsed) : null;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Pattern;

public class PlaceholderContainer<T> {
private final @NotNull Class<T> clazz;
private final @NotNull Function<String, T> parser;
private final @NotNull Function<T, String> stringMapper;
private final @NotNull BiFunction<PlaceholderOption.Property, PlaceholderContainer<T>, PropertyResult<T>> propertyParser;

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<>();

public PlaceholderContainer(@NotNull Class<T> clazz, String name, @NotNull Function<String, T> parser, @NotNull Function<T, String> stringMapper) {
public PlaceholderContainer(
@NotNull Class<T> clazz,
String name,
@NotNull Function<String, T> parser,
@NotNull Function<T, String> stringMapper,
@NotNull BiFunction<PlaceholderOption.Property, PlaceholderContainer<T>, PropertyResult<T>> propertyParser
) {
this.clazz = clazz;
this.parser = parser;
this.stringMapper = stringMapper;
this.propertyParser = propertyParser;

CLASS_MAP.put(clazz, this);
STRING_MAP.put(name, this);
Expand All @@ -44,10 +54,15 @@ public PlaceholderContainer(@NotNull Class<T> clazz, String name, @NotNull Funct
return clazz;
}

@NotNull Function<T, String> stringMapper() {
return stringMapper;
@NotNull PropertyResult<T> propertyResult(@NotNull PlaceholderOption.Property property) {
return propertyParser.apply(property, this);
}

public record PropertyResult<R>(
@NotNull Function<String, R> parser,
@NotNull Function<R, String> stringMapper
) {}

public static final PlaceholderContainer<Number> NUMBER = new PlaceholderContainer<>(
Number.class,
"number",
Expand All @@ -58,7 +73,16 @@ public PlaceholderContainer(@NotNull Class<T> clazz, String name, @NotNull Funct
return null;
}
},
s -> BetterHealthBar.inst().configManager().numberFormat().format(s)
s -> BetterHealthBar.inst().configManager().numberFormat().format(s),
(p, c) -> {
var mapper = c.stringMapper;
var format = p.get(PlaceholderOption.NUMBER_FORMAT);
if (format != null) {
var decimal = new DecimalFormat(format);
mapper = decimal::format;
}
return new PropertyResult<>(c.parser, mapper);
}
);
public static final PlaceholderContainer<String> STRING = new PlaceholderContainer<>(
String.class,
Expand All @@ -70,7 +94,8 @@ public PlaceholderContainer(@NotNull Class<T> clazz, String name, @NotNull Funct
}
return null;
},
d -> d
d -> d,
(p, c) -> new PropertyResult<>(c.parser, c.stringMapper)
);
public static final PlaceholderContainer<Boolean> BOOL = new PlaceholderContainer<>(
Boolean.class,
Expand All @@ -80,7 +105,8 @@ public PlaceholderContainer(@NotNull Class<T> clazz, String name, @NotNull Funct
case "false" -> false;
default -> null;
},
s -> Boolean.toString(s)
s -> Boolean.toString(s),
(p, c) -> new PropertyResult<>(c.parser, c.stringMapper)
);

private final Map<String, PlaceholderBuilder<T>> map = new HashMap<>();
Expand All @@ -93,7 +119,8 @@ public int requiredArgsCount() {
}

@Override
public @NotNull HealthBarPlaceholder<T> build(@NotNull List<String> strings) {
public @NotNull HealthBarPlaceholder<T> build(@NotNull PlaceholderOption.Property property, @NotNull List<String> strings) {
var result = propertyResult(property);
return new HealthBarPlaceholder<>() {
@Nullable
@Override
Expand All @@ -110,7 +137,7 @@ public Class<T> type() {
@Override
public @Nullable String stringValue(@NotNull HealthBarCreateEvent event) {
var value = value(event);
return value != null ? stringMapper.apply(value) : null;
return value != null ? result.stringMapper.apply(value) : null;
}
};
}
Expand All @@ -132,20 +159,20 @@ private FindResult(@NotNull String name) {
result = map.get(name);
}

public @NotNull HealthBarPlaceholder<T> value(@NotNull List<String> strings) {
public @NotNull HealthBarPlaceholder<T> value(@NotNull PlaceholderOption.Property property, @NotNull List<String> strings) {
Objects.requireNonNull(result);
Objects.requireNonNull(strings);
return result.build(strings);
return result.build(property, strings);
}

public boolean ifPresented() {
return result != null;
}

public @NotNull HealthBarPlaceholder<String> stringValue(@NotNull List<String> strings) {
public @NotNull HealthBarPlaceholder<String> stringValue(@NotNull PlaceholderOption.Property property, @NotNull List<String> strings) {
Objects.requireNonNull(result);
Objects.requireNonNull(strings);
var apply = result.build(strings);
var apply = result.build(property, strings);
return new HealthBarPlaceholder<>() {
@NotNull
@Override
Expand Down Expand Up @@ -184,7 +211,7 @@ public static HealthBarPlaceholder<?> primitive(@NotNull String value) {
}).filter(Objects::nonNull).findFirst().orElseThrow(() -> new RuntimeException("Unable to parse this value: " + value));
}

public static @NotNull HealthBarPlaceholder<?> parse(@NotNull String pattern) {
public static @NotNull HealthBarPlaceholder<?> parse(@NotNull PlaceholderOption.Property property, @NotNull String pattern) {
var index = 0;
while (index < pattern.length() && pattern.charAt(index) != ':') {
index++;
Expand All @@ -211,7 +238,7 @@ public static HealthBarPlaceholder<?> primitive(@NotNull String value) {
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) {
var string = get.stringValue(list);
var string = get.stringValue(property, list);
return new HealthBarPlaceholder<>() {
@NotNull
@Override
Expand All @@ -234,10 +261,10 @@ public Object value(@NotNull HealthBarCreateEvent event) {
return value != null ? ((Function<Object, String>) cast.stringMapper).apply(value) : "<error>";
}
};
} else return get.value(list);
} else return get.value(property, list);
}

public static @NotNull Function<HealthBarCreateEvent, Component> toString(@NotNull String pattern) {
public static @NotNull Function<HealthBarCreateEvent, Component> toString(@NotNull PlaceholderOption.Property property, @NotNull String pattern) {
var array = new ArrayList<Function<HealthBarCreateEvent, Component>>();
var sb = new StringBuilder();
var skip = false;
Expand All @@ -250,7 +277,7 @@ public Object value(@NotNull HealthBarCreateEvent event) {
}
case ']' -> {
var result = sb.toString();
var parse = parse(result);
var parse = parse(property, result);
array.add(f -> {
var value = parse.stringValue(f);
return value != null ? legacyAdapt(value) : Component.text("<error>");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package kr.toxicity.healthbar.api.placeholder;

import lombok.RequiredArgsConstructor;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
public enum PlaceholderOption {
NUMBER_FORMAT("#,###")
;
private final @NotNull String defaultValue;

public static final Property EMPTY = new Property(Arrays.stream(values()).collect(Collectors.toMap(v -> v, v -> v.defaultValue)));

public static @NotNull Property of(@NotNull ConfigurationSection section) {
return new Property(Arrays.stream(values()).collect(Collectors.toMap(v -> v, v -> section.getString(v.configName(), v.defaultValue))));
}

public @NotNull String configName() {
return name().toLowerCase().replace("_", "-");
}

@RequiredArgsConstructor
public static final class Property {
private final Map<PlaceholderOption, String> map;

public @Nullable String get(@NotNull PlaceholderOption option) {
return map.get(option);
}
}
}
Loading

0 comments on commit 4c482be

Please sign in to comment.