Skip to content

Commit e88e841

Browse files
committed
add ValueState to indicate a value being handled
1 parent 77b43d6 commit e88e841

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/holder/provider/NumberStringValueProvider.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.projectunified.minelib.scheduler.global.GlobalScheduler;
55
import me.hsgamer.hscore.common.MapUtils;
66
import me.hsgamer.topper.spigot.plugin.TopperPlugin;
7+
import org.jetbrains.annotations.Nullable;
78

89
import java.util.Collections;
910
import java.util.Map;
@@ -45,13 +46,18 @@ public NumberStringValueProvider(TopperPlugin plugin, Map<String, Object> map) {
4546

4647
protected abstract String getDisplayName();
4748

48-
protected abstract Optional<String> getString(UUID uuid);
49+
protected abstract ValueState getString(UUID uuid);
4950

5051
@Override
5152
public CompletableFuture<Optional<Double>> getValue(UUID uuid) {
5253
return CompletableFuture.supplyAsync(() -> {
5354
try {
54-
Optional<String> value = getString(uuid)
55+
ValueState valueState = getString(uuid);
56+
if (!valueState.handled) {
57+
return Optional.empty();
58+
}
59+
60+
Optional<String> value = Optional.ofNullable(valueState.value)
5561
.filter(s -> !s.isEmpty())
5662
.map(s -> isFormatted ? formattedSettings.clearFormat(s) : s);
5763
if (!value.isPresent()) {
@@ -60,6 +66,7 @@ public CompletableFuture<Optional<Double>> getValue(UUID uuid) {
6066
}
6167
return Optional.empty();
6268
}
69+
6370
return Optional.of(Double.parseDouble(value.get()));
6471
} catch (Exception e) {
6572
if (showErrors) {
@@ -95,4 +102,22 @@ String clearFormat(String string) {
95102
return builder.toString();
96103
}
97104
}
105+
106+
public static class ValueState {
107+
public final boolean handled;
108+
public final @Nullable String value;
109+
110+
private ValueState(boolean handled, @Nullable String value) {
111+
this.handled = handled;
112+
this.value = value;
113+
}
114+
115+
public static ValueState handled(@Nullable String value) {
116+
return new ValueState(true, value);
117+
}
118+
119+
public static ValueState unhandled() {
120+
return new ValueState(false, null);
121+
}
122+
}
98123
}

spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/hook/placeholderapi/PlaceholderValueProvider.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,18 @@ protected String getDisplayName() {
2929
}
3030

3131
@Override
32-
protected Optional<String> getString(UUID uuid) {
32+
protected ValueState getString(UUID uuid) {
3333
OfflinePlayer player;
3434
if (isOnlineOnly) {
3535
player = plugin.getServer().getPlayer(uuid);
3636
if (player == null) {
37-
return Optional.empty();
37+
return ValueState.unhandled();
3838
}
3939
} else {
4040
player = plugin.getServer().getOfflinePlayer(uuid);
4141
}
4242

4343
String parsed = PlaceholderAPI.setPlaceholders(player, placeholder).trim();
44-
if (parsed.isEmpty()) {
45-
return Optional.empty();
46-
}
47-
return Optional.of(parsed);
44+
return ValueState.handled(parsed.isEmpty() ? null : parsed);
4845
}
4946
}

0 commit comments

Comments
 (0)