Skip to content

Commit fb679c7

Browse files
committed
move getDisplayLine to ValueDisplay template
1 parent 175e4ad commit fb679c7

File tree

2 files changed

+25
-22
lines changed
  • query/display-number/src/main/java/me/hsgamer/topper/query/display/number
  • template/top-player-number/src/main/java/me/hsgamer/topper/template/topplayernumber/holder/display

2 files changed

+25
-22
lines changed

query/display-number/src/main/java/me/hsgamer/topper/query/display/number/NumberDisplay.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,18 @@
1616
import java.util.concurrent.ConcurrentHashMap;
1717
import java.util.concurrent.TimeUnit;
1818
import java.util.function.Function;
19-
import java.util.regex.Matcher;
2019
import java.util.regex.Pattern;
2120
import java.util.stream.Collectors;
2221

2322
public abstract class NumberDisplay<K, V extends Number> implements SimpleQueryDisplay<K, V> {
24-
private static final Pattern VALUE_PLACEHOLDER_PATTERN = Pattern.compile("\\{value(?:_(.*))?}");
2523
private static final String FORMAT_QUERY_DECIMAL = "decimal";
2624
private static final String FORMAT_QUERY_TIME = "time";
2725
private static final String FORMAT_QUERY_SHORTEN = "shorten";
2826
private static final Map<String, Function<Number, String>> displayByQueryCache = new ConcurrentHashMap<>();
2927

30-
private final String line;
3128
private final String displayNullValue;
3229

33-
protected NumberDisplay(String line, String displayNullValue) {
34-
this.line = line;
30+
protected NumberDisplay(String displayNullValue) {
3531
this.displayNullValue = displayNullValue;
3632
}
3733

@@ -232,20 +228,4 @@ private static Function<Number, String> createDisplayFunction(String formatQuery
232228

233229
return displayByQueryCache.computeIfAbsent(formatQuery, NumberDisplay::createDisplayFunction).apply(value);
234230
}
235-
236-
public String getDisplayLine(int index /* 1-based */, @Nullable Map.Entry<K, V> entry) {
237-
String line = this.line
238-
.replace("{index}", String.valueOf(index))
239-
.replace("{uuid}", getDisplayKey(entry == null ? null : entry.getKey()))
240-
.replace("{name}", getDisplayName(entry == null ? null : entry.getKey()));
241-
242-
V value = entry == null ? null : entry.getValue();
243-
Matcher matcher = VALUE_PLACEHOLDER_PATTERN.matcher(line);
244-
while (matcher.find()) {
245-
String formatType = matcher.group(1);
246-
line = line.replace(matcher.group(), getDisplayValue(value, formatType));
247-
}
248-
249-
return line;
250-
}
251231
}

template/top-player-number/src/main/java/me/hsgamer/topper/template/topplayernumber/holder/display/ValueDisplay.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
77

8+
import java.util.Map;
89
import java.util.Optional;
910
import java.util.UUID;
1011
import java.util.function.Function;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
1114

1215
public class ValueDisplay extends NumberDisplay<UUID, Double> {
16+
private static final Pattern VALUE_PLACEHOLDER_PATTERN = Pattern.compile("\\{value(?:_(.*))?}");
17+
18+
public final String defaultLine;
1319
public final String displayNullName;
1420
public final String displayNullUuid;
1521
private final Function<UUID, String> nameFunction;
1622

1723
public ValueDisplay(Function<UUID, String> nameFunction, Settings settings) {
18-
super(settings.defaultLine(), settings.displayNullValue());
24+
super(settings.displayNullValue());
1925
this.nameFunction = nameFunction;
26+
this.defaultLine = settings.defaultLine();
2027
this.displayNullName = settings.displayNullName();
2128
this.displayNullUuid = settings.displayNullUuid();
2229
}
@@ -29,6 +36,22 @@ public ValueDisplay(Function<UUID, String> nameFunction, Settings settings) {
2936
return Optional.ofNullable(uuid).map(nameFunction).orElse(displayNullName);
3037
}
3138

39+
public String getDisplayLine(int index /* 1-based */, @Nullable Map.Entry<UUID, Double> entry) {
40+
String line = this.defaultLine
41+
.replace("{index}", String.valueOf(index))
42+
.replace("{uuid}", getDisplayKey(entry == null ? null : entry.getKey()))
43+
.replace("{name}", getDisplayName(entry == null ? null : entry.getKey()));
44+
45+
Double value = entry == null ? null : entry.getValue();
46+
Matcher matcher = VALUE_PLACEHOLDER_PATTERN.matcher(line);
47+
while (matcher.find()) {
48+
String formatType = matcher.group(1);
49+
line = line.replace(matcher.group(), getDisplayValue(value, formatType));
50+
}
51+
52+
return line;
53+
}
54+
3255
public String getDisplayLine(int index /* 1-based */, NumberTopHolder holder) {
3356
return getDisplayLine(index, holder.getSnapshotAgent().getSnapshotByIndex(index - 1).orElse(null));
3457
}

0 commit comments

Comments
 (0)