Skip to content

Commit 084a3c2

Browse files
committed
feat: leaderboard placeholders for the built-in score leaderboard
port of 98afb31
1 parent 60797ee commit 084a3c2

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

plugin/common/src/main/java/org/screamingsandals/bedwars/placeholderapi/BedwarsExpansion.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,75 @@ public BedwarsExpansion() {
285285
}
286286
}
287287

288+
if (identifier.startsWith("leaderboard_score_")) {
289+
if (!PlayerStatisticManager.isEnabled()) {
290+
return null;
291+
}
292+
293+
String remainder = identifier.substring(18);
294+
String[] split = remainder.split("_", 2);
295+
if (split.length != 2) {
296+
return null;
297+
}
298+
299+
int index;
300+
try {
301+
index = Integer.parseInt(split[0]);
302+
} catch (NumberFormatException e) {
303+
return null;
304+
}
305+
306+
index--; // 1 -> 0
307+
if (index < 0) {
308+
return null;
309+
}
310+
311+
var entry = PlayerStatisticManager.getInstance().getLeaderboardEntry(index);
312+
313+
if (entry == null) {
314+
switch (split[1].toLowerCase(Locale.ROOT)) {
315+
case "uuid":
316+
case "name":
317+
return Component.text("---");
318+
case "score":
319+
case "deaths":
320+
case "destroyed_beds":
321+
case "kills":
322+
case "loses":
323+
case "wins":
324+
case "games":
325+
case "kd":
326+
return Component.text("0");
327+
}
328+
} else {
329+
switch (split[1].toLowerCase(Locale.ROOT)) {
330+
case "uuid":
331+
return Component.text(entry.getPlayer().getUuid().toString());
332+
case "name":
333+
return Component.text(entry.getLastKnownName() != null ? entry.getLastKnownName() : entry.getPlayer().getUuid().toString());
334+
case "score":
335+
return Component.text(entry.getTotalScore());
336+
case "deaths":
337+
return Component.text(entry.fetchStatistics().getDeaths());
338+
case "destroyed_beds":
339+
return Component.text(entry.fetchStatistics().getDestroyedBeds());
340+
case "kills":
341+
return Component.text(entry.fetchStatistics().getKills());
342+
case "loses":
343+
return Component.text(entry.fetchStatistics().getLoses());
344+
case "wins":
345+
return Component.text(entry.fetchStatistics().getWins());
346+
case "games":
347+
return Component.text(entry.fetchStatistics().getGames());
348+
case "kd":
349+
return Component.text(entry.fetchStatistics().getKD());
350+
}
351+
}
352+
}
353+
288354
// Player
289355
if (player == null) {
290-
return Component.empty();
356+
return null;
291357
}
292358

293359
if (identifier.startsWith("current_")) {

plugin/common/src/main/java/org/screamingsandals/bedwars/statistics/PlayerStatisticManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ public List<LeaderboardEntryImpl> getLeaderboard(int count) {
207207
.collect(Collectors.toList());
208208
}
209209

210+
public @Nullable LeaderboardEntryImpl getLeaderboardEntry(int index) {
211+
return allScores.entrySet()
212+
.stream()
213+
.sorted((c1, c2) -> Comparator.<Integer>reverseOrder().compare(c1.getValue().getValue(), c2.getValue().getValue()))
214+
.skip(index)
215+
.findFirst()
216+
.map(entry -> new LeaderboardEntryImpl(Players.getOfflinePlayer(entry.getKey()), entry.getValue().getValue(), entry.getValue().getKey()))
217+
.orElse(null);
218+
}
219+
210220
private PlayerStatisticImpl loadDatabaseStatistic(UUID uuid) {
211221
if (this.playerStatistic.containsKey(uuid)) {
212222
return this.playerStatistic.get(uuid);

0 commit comments

Comments
 (0)