From f0e0cfbb8511db32c2089bc5153b5d832715714f Mon Sep 17 00:00:00 2001 From: lars Date: Fri, 19 Dec 2025 14:33:01 +0100 Subject: [PATCH] fixed scoreboard order --- .../event/appliances/deathrun/Deathrun.java | 2 +- .../scoreboard/EventScoreboardBuilder.java | 92 ++++++++++--------- 2 files changed, 52 insertions(+), 42 deletions(-) diff --git a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/deathrun/Deathrun.java b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/deathrun/Deathrun.java index 43625af..bebb36f 100644 --- a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/deathrun/Deathrun.java +++ b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/deathrun/Deathrun.java @@ -58,7 +58,7 @@ public class Deathrun extends Appliance implements Event, Scorable { @Override public void start(long durationSeconds) { this.durationSeconds = durationSeconds; - Title title = Title.title(Component.text("Start"), Component.text("Erreiche die höchste x-Koordinate!")); + Title title = Title.title(Component.text("Start"), Component.text("Laufe Richtung Osten! (positiv x)")); Bukkit.getOnlinePlayers().forEach(player -> player.showTitle(title)); World world = Bukkit.getWorlds().getFirst(); diff --git a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/scoreboard/EventScoreboardBuilder.java b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/scoreboard/EventScoreboardBuilder.java index 14a9675..282deb8 100644 --- a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/scoreboard/EventScoreboardBuilder.java +++ b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/scoreboard/EventScoreboardBuilder.java @@ -5,6 +5,7 @@ import eu.mhsl.craftattack.spawn.event.appliances.eventController.Scorable; import io.papermc.paper.scoreboard.numbers.NumberFormat; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.util.Ticks; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -37,64 +38,73 @@ public class EventScoreboardBuilder { } public Scoreboard buildFor(Player p) { - List scoreMap = this.playerScores; - Scoreboard newScoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); + List scoreList = new ArrayList<>(this.playerScores); - Objective objective = newScoreboard.registerNewObjective( + Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); + Objective objective = scoreboard.registerNewObjective( "event", "dummy", - Component.text("Scoreboard %s".formatted(this.scorable.getScoreboardName()), NamedTextColor.DARK_PURPLE) + Component.text( + "Scoreboard %s".formatted(this.scorable.getScoreboardName()), + NamedTextColor.GOLD, TextDecoration.BOLD + ) ); objective.setDisplaySlot(DisplaySlot.SIDEBAR); objective.numberFormat(NumberFormat.blank()); - if(scoreMap.isEmpty()) return newScoreboard; + UUID uuid = p.getUniqueId(); + scoreList.removeIf(e -> e.playerUuid().equals(uuid)); + scoreList.add(new EventScoreEntry(uuid, p.getName(), this.scorable.getScore(p))); - scoreMap.sort(this.scoreComparator); - System.out.print(scoreMap); + scoreList.sort(this.scoreComparator); - EventScoreEntry playerScoreEntry = scoreMap.stream() - .filter(entry -> entry.playerUuid().equals(p.getUniqueId())) + int size = scoreList.size(); + int playerIndex = IntStream.range(0, size) + .filter(i -> scoreList.get(i).playerUuid().equals(uuid)) .findFirst() - .orElse(null); - if(playerScoreEntry == null) { - playerScoreEntry = new EventScoreEntry(p.getUniqueId(), p.getName(), this.scorable.getScore(p)); - scoreMap.add(playerScoreEntry); + .orElse(0); + + IntStream top = IntStream.range(0, Math.min(this.topPlaces, size)); + + int aroundStart = Math.max(0, playerIndex - this.aroundPlaces); + int aroundEnd = Math.min(size, playerIndex + this.aroundPlaces + 1); + IntStream around = IntStream.range(aroundStart, aroundEnd); + + IntStream bottom = IntStream.range(Math.max(0, size - this.bottomPlaces), size); + + int threshold = this.topPlaces + this.bottomPlaces + this.aroundPlaces * 2 + 1; + + IntStream indices; + if (size <= threshold) { + indices = IntStream.range(0, size); + } else if (playerIndex <= this.topPlaces + this.aroundPlaces) { + indices = IntStream.concat( + IntStream.range(0, Math.min(size, playerIndex + this.aroundPlaces + 1)), + bottom + ); + } else if (playerIndex >= size - this.bottomPlaces - this.aroundPlaces - 1) { + indices = IntStream.concat( + top, + IntStream.range(Math.max(0, playerIndex - this.aroundPlaces), size) + ); + } else { + indices = IntStream.concat(IntStream.concat(top, around), bottom); } - int playerIndex = scoreMap.indexOf(playerScoreEntry); + int[] display = indices.distinct().sorted().toArray(); - IntStream topStream = IntStream.range(0, this.topPlaces); - IntStream aroundStream = IntStream.range(playerIndex - this.aroundPlaces, playerIndex + this.aroundPlaces); - IntStream bottomStream = IntStream.range(scoreMap.size() - this.bottomPlaces, scoreMap.size()); + for (int i = 0; i < display.length; i++) { + int idx = display[i]; + EventScoreEntry entry = scoreList.get(idx); - if(playerIndex <= this.topPlaces + this.aroundPlaces) { - aroundStream = IntStream.empty(); - topStream = IntStream.range(0, playerIndex + this.aroundPlaces); - } - if(playerIndex >= scoreMap.size() - this.bottomPlaces - this.aroundPlaces) { - aroundStream = IntStream.empty(); - bottomStream = IntStream.range(playerIndex - this.aroundPlaces, scoreMap.size()); - } - if(scoreMap.size() <= this.topPlaces + this.aroundPlaces * 2 + this.bottomPlaces + 2) { - topStream = IntStream.range(0, scoreMap.size()); - aroundStream = IntStream.empty(); - bottomStream = IntStream.empty(); + String line = this.formattedLine(idx, entry.name(), entry.score()); + + objective.getScore(line).setScore(display.length - i); } - IntStream indices = IntStream.concat( - IntStream.concat(topStream, aroundStream), - bottomStream - ).distinct().sorted(); - - indices.forEach(place -> { - EventScoreEntry entry = scoreMap.get(place); - String line = this.formattedLine(place, entry.name(), entry.score()); - objective.getScore(line).setScore(place); - }); - - return newScoreboard; + return scoreboard; } + public void startAutomaticUpdates() { this.scoreboardUpdateTaskId = Bukkit.getScheduler().scheduleSyncRepeatingTask( Main.instance(),