Skip to content

Commit 98afb31

Browse files
committed
feat: leaderboard placeholders for the built-in score leaderboard
for other types of leaderboard ajLeaderboards is still needed
1 parent 7eb7756 commit 98afb31

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.bukkit.entity.Player;
2626
import org.screamingsandals.bedwars.Main;
2727
import org.screamingsandals.bedwars.api.game.GameStatus;
28+
import org.screamingsandals.bedwars.api.statistics.LeaderboardEntry;
2829
import org.screamingsandals.bedwars.api.statistics.PlayerStatistic;
2930
import org.screamingsandals.bedwars.game.CurrentTeam;
3031
import org.screamingsandals.bedwars.game.Game;
@@ -239,9 +240,75 @@ public String onPlaceholderRequest(Player player, String identifier) {
239240
}
240241
}
241242

243+
if (identifier.startsWith("leaderboard_score_")) {
244+
if (!Main.isPlayerStatisticsEnabled()) {
245+
return null;
246+
}
247+
248+
String remainder = identifier.substring(18);
249+
String[] split = remainder.split("_", 2);
250+
if (split.length != 2) {
251+
return null;
252+
}
253+
254+
int index;
255+
try {
256+
index = Integer.parseInt(split[0]);
257+
} catch (NumberFormatException e) {
258+
return null;
259+
}
260+
261+
index--; // 1 -> 0
262+
if (index < 0) {
263+
return null;
264+
}
265+
266+
LeaderboardEntry entry = Main.getPlayerStatisticsManager().getLeaderboardEntry(index);
267+
268+
if (entry == null) {
269+
switch (split[1].toLowerCase(Locale.ROOT)) {
270+
case "uuid":
271+
case "name":
272+
return "---";
273+
case "score":
274+
case "deaths":
275+
case "destroyed_beds":
276+
case "kills":
277+
case "loses":
278+
case "wins":
279+
case "games":
280+
case "kd":
281+
return "0";
282+
}
283+
} else {
284+
switch (split[1].toLowerCase(Locale.ROOT)) {
285+
case "uuid":
286+
return entry.getPlayer().getUniqueId().toString();
287+
case "name":
288+
return entry.getLatestKnownName();
289+
case "score":
290+
return Integer.toString(entry.getTotalScore());
291+
case "deaths":
292+
return Integer.toString(entry.fetchStatistics().getDeaths());
293+
case "destroyed_beds":
294+
return Integer.toString(entry.fetchStatistics().getDestroyedBeds());
295+
case "kills":
296+
return Integer.toString(entry.fetchStatistics().getKills());
297+
case "loses":
298+
return Integer.toString(entry.fetchStatistics().getLoses());
299+
case "wins":
300+
return Integer.toString(entry.fetchStatistics().getWins());
301+
case "games":
302+
return Integer.toString(entry.fetchStatistics().getGames());
303+
case "kd":
304+
return Double.toString(entry.fetchStatistics().getKD());
305+
}
306+
}
307+
}
308+
242309
// Player
243310
if (player == null) {
244-
return "";
311+
return null;
245312
}
246313

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ public List<LeaderboardEntry> getLeaderboard(int count) {
143143
return entries;
144144
}
145145

146+
public LeaderboardEntry getLeaderboardEntry(int index) {
147+
return allScores.entrySet().stream()
148+
.sorted((c1, c2) -> Comparator.<Integer>reverseOrder().compare(c1.getValue().getValue(), c2.getValue().getValue()))
149+
.skip(index)
150+
.findFirst()
151+
.map(entry -> new org.screamingsandals.bedwars.statistics.LeaderboardEntry(Bukkit.getOfflinePlayer(entry.getKey()), entry.getValue().getValue(), entry.getValue().getKey()))
152+
.orElse(null);
153+
}
154+
146155
private PlayerStatistic loadDatabaseStatistic(UUID uuid) {
147156
if (this.playerStatistic.containsKey(uuid)) {
148157
return this.playerStatistic.get(uuid);

0 commit comments

Comments
 (0)