Skip to content

Commit

Permalink
Updates number-format in config.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Nov 2, 2024
1 parent 348cab5 commit 87a1d89
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.jetbrains.annotations.Unmodifiable;

import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Set;

public interface ConfigManager {
Expand All @@ -28,6 +30,7 @@ public interface ConfigManager {
boolean createPackMcmeta();
boolean enableSelfHost();
int selfHostPort();
@NotNull NumberFormat numberFormat();
@NotNull
@Unmodifiable
Set<EntityType> blacklistEntityType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,78 @@

import kr.toxicity.healthbar.api.event.HealthBarCreateEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;

public interface PlaceholderBuilder<T> {
@NotNull HealthBarPlaceholder<T> build(@NotNull List<String> strings);
int requiredArgsCount();

static <T> PlaceholderBuilder<T> of(int length, @NotNull Class<T> clazz, @NotNull Function<List<String>, Function<HealthBarCreateEvent, T>> tFunction) {
return new PlaceholderBuilder<>() {
@Override
public @NotNull HealthBarPlaceholder<T> build(@NotNull List<String> strings) {
var get = tFunction.apply(strings);
return new HealthBarPlaceholder<>() {
@NotNull
@Override
public Class<T> type() {
return clazz;
}

@NotNull
@Override
public T value(@NotNull HealthBarCreateEvent event) {
return get.apply(event);
}
};
}

@Override
public int requiredArgsCount() {
return length;
}
};
static <R> @NotNull Builder<R> builder(@NotNull PlaceholderContainer<R> container) {
return new Builder<>(Objects.requireNonNull(container));
}

static <R> @Nullable Builder<R> builder(@NotNull Class<R> clazz) {
var get = PlaceholderContainer.find(clazz);
return get != null ? get.create() : null;
}

class Builder<R> {
private final Class<R> clazz;
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();
}

public @NotNull Builder<R> stringMapper(@NotNull Function<R, String> mapper) {
stringMapper = Objects.requireNonNull(mapper);
return this;
}
public @NotNull Builder<R> parser(@NotNull Function<List<String>, Function<HealthBarCreateEvent, R>> parser) {
this.parser = Objects.requireNonNull(parser);
return this;
}
public @NotNull Builder<R> argsLength(int argsLength) {
this.argsLength = argsLength;
return this;
}

public @NotNull PlaceholderBuilder<R> build() {
return new PlaceholderBuilder<>() {
@Override
public @NotNull HealthBarPlaceholder<R> build(@NotNull List<String> strings) {
var valueFunction = parser.apply(strings);
return new HealthBarPlaceholder<>() {
@Override
public @NotNull Class<R> type() {
return clazz;
}

@Override
public @Nullable R value(@NotNull HealthBarCreateEvent event) {
return valueFunction.apply(event);
}

@Override
public @Nullable String stringValue(@NotNull HealthBarCreateEvent event) {
var parsed = value(event);
return parsed != null ? stringMapper.apply(parsed) : null;
}
};
}

@Override
public int requiredArgsCount() {
return argsLength;
}
};
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.toxicity.healthbar.api.placeholder;

import kr.toxicity.healthbar.api.BetterHealthBar;
import kr.toxicity.healthbar.api.event.HealthBarCreateEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
Expand All @@ -8,7 +9,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
Expand All @@ -31,6 +31,23 @@ public PlaceholderContainer(@NotNull Class<T> clazz, String name, @NotNull Funct
STRING_MAP.put(name, this);
}

@SuppressWarnings("unchecked")
public static <R> @Nullable PlaceholderContainer<R> find(@NotNull Class<R> clazz) {
return (PlaceholderContainer<R>) CLASS_MAP.get(clazz);
}

public @NotNull PlaceholderBuilder.Builder<T> create() {
return PlaceholderBuilder.builder(this);
}

@NotNull Class<T> clazz() {
return clazz;
}

@NotNull Function<T, String> stringMapper() {
return stringMapper;
}

public static final PlaceholderContainer<Number> NUMBER = new PlaceholderContainer<>(
Number.class,
"number",
Expand All @@ -41,7 +58,7 @@ public PlaceholderContainer(@NotNull Class<T> clazz, String name, @NotNull Funct
return null;
}
},
s -> DecimalFormat.getInstance().format(s)
s -> BetterHealthBar.inst().configManager().numberFormat().format(s)
);
public static final PlaceholderContainer<String> STRING = new PlaceholderContainer<>(
String.class,
Expand Down Expand Up @@ -78,7 +95,7 @@ public int requiredArgsCount() {
@Override
public @NotNull HealthBarPlaceholder<T> build(@NotNull List<String> strings) {
return new HealthBarPlaceholder<>() {
@NotNull
@Nullable
@Override
public T value(@NotNull HealthBarCreateEvent event) {
return function.apply(event);
Expand All @@ -89,10 +106,19 @@ public T value(@NotNull HealthBarCreateEvent event) {
public Class<T> type() {
return clazz;
}

@Override
public @Nullable String stringValue(@NotNull HealthBarCreateEvent event) {
var value = value(event);
return value != null ? stringMapper.apply(value) : null;
}
};
}
});
}
public void addPlaceholder(@NotNull String name, @NotNull PlaceholderBuilder.Builder<T> builder) {
addPlaceholder(name, builder.build());
}
public void addPlaceholder(@NotNull String name, @NotNull PlaceholderBuilder<T> builder) {
map.put(name, builder);
}
Expand Down Expand Up @@ -127,10 +153,11 @@ public Class<String> type() {
return String.class;
}

@NotNull
@Nullable
@Override
public String value(@NotNull HealthBarCreateEvent event) {
return stringMapper.apply(apply.value(event));
var value = apply.value(event);
return value != null ? value.toString() : null;
}
};
}
Expand Down Expand Up @@ -196,7 +223,8 @@ public Class<Object> type() {
@Nullable
@Override
public Object value(@NotNull HealthBarCreateEvent event) {
return cast.parser.apply(string.value(event));
var value = string.value(event);
return value != null ? cast.parser.apply(value) : null;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ tasks {
version("1.21.1") //TODO set this to 'minecraft' when other plugins support the latest version.
pluginJars(fileTree("plugins"))
downloadPlugins {
modrinth("betterhud2", "1.7.DEV-266")
modrinth("betterhud2", "1.8")
hangar("PlaceholderAPI", "2.11.6")
hangar("Skript", "2.9.3")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import kr.toxicity.healthbar.util.*
import org.bstats.bukkit.Metrics
import org.bukkit.entity.EntityType
import java.io.File
import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.Collections
import java.util.EnumSet

Expand All @@ -27,6 +29,7 @@ object ConfigManagerImpl : ConfigManager, BetterHealthBerManager {
private var mergeOtherFolder = emptySet<String>()
private var createPackMemeta = true
private var enableSelfHost = false
private var numberFormat = DecimalFormat.getNumberInstance()
private var selfHostPort = 8163
private var blackListEntityType = emptySet<EntityType>()
private var disableToInvulnerableMob = true
Expand Down Expand Up @@ -67,6 +70,11 @@ object ConfigManagerImpl : ConfigManager, BetterHealthBerManager {
createPackMemeta = config.getBoolean("create-pack-mcmeta", true)
enableSelfHost = config.getBoolean("enable-self-host", false)
selfHostPort = config.getInt("self-host-port", 8163)
numberFormat = runCatching {
DecimalFormat(config.getString("number-format", "#,###"))
}.getOrElse {
DecimalFormat.getNumberInstance()
}
blackListEntityType = Collections.unmodifiableSet(EnumSet.copyOf(config.getStringList("blacklist-entity-type").mapNotNull {
runCatching {
EntityType.valueOf(it.uppercase())
Expand All @@ -81,6 +89,7 @@ object ConfigManagerImpl : ConfigManager, BetterHealthBerManager {
}
useCoreShaders = config.getBoolean("use-core-shaders", true)
showMeHealthBar = config.getBoolean("show-me-healthbar", true)

if (!metrics) {
bstats?.shutdown()
bstats = null
Expand All @@ -106,6 +115,7 @@ object ConfigManagerImpl : ConfigManager, BetterHealthBerManager {
override fun createPackMcmeta(): Boolean = createPackMemeta
override fun enableSelfHost(): Boolean = enableSelfHost
override fun selfHostPort(): Int = selfHostPort
override fun numberFormat(): NumberFormat = numberFormat
override fun blacklistEntityType(): Set<EntityType> = blackListEntityType
override fun disableToInvulnerableMob(): Boolean = disableToInvulnerableMob
override fun shaders(): CoreShadersOption = shaders
Expand Down
6 changes: 4 additions & 2 deletions dist/src/main/kotlin/kr/toxicity/healthbar/util/Functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ fun Throwable.handleException(log: String): Array<String> {
)
}

inline fun <reified T> placeholder(length: Int, function: Function<List<String>, Function<HealthBarCreateEvent, T>>): PlaceholderBuilder<T> {
return PlaceholderBuilder.of(length, T::class.java, function)
inline fun <reified T : Any> placeholder(length: Int, function: Function<List<String>, Function<HealthBarCreateEvent, T>>): PlaceholderBuilder.Builder<T> {
return PlaceholderBuilder.builder(T::class.java).ifNull("Unable to find this type: ${T::class.java.simpleName}")
.argsLength(length)
.parser(function)
}
1 change: 1 addition & 0 deletions dist/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ merge-other-folder: []
create-pack-mcmeta: true
enable-self-host: true
self-host-port: 8163
number-format: "#,###"
blacklist-entity-type:
- ARMOR_STAND
- TEXT_DISPLAY
Expand Down

0 comments on commit 87a1d89

Please sign in to comment.