fixed scoreboard order

This commit is contained in:
2025-12-19 14:33:01 +01:00
parent de112f7e13
commit f0e0cfbb85
2 changed files with 52 additions and 42 deletions

View File

@@ -58,7 +58,7 @@ public class Deathrun extends Appliance implements Event, Scorable {
@Override @Override
public void start(long durationSeconds) { public void start(long durationSeconds) {
this.durationSeconds = 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)); Bukkit.getOnlinePlayers().forEach(player -> player.showTitle(title));
World world = Bukkit.getWorlds().getFirst(); World world = Bukkit.getWorlds().getFirst();

View File

@@ -5,6 +5,7 @@ import eu.mhsl.craftattack.spawn.event.appliances.eventController.Scorable;
import io.papermc.paper.scoreboard.numbers.NumberFormat; import io.papermc.paper.scoreboard.numbers.NumberFormat;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.util.Ticks; import net.kyori.adventure.util.Ticks;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -37,64 +38,73 @@ public class EventScoreboardBuilder {
} }
public Scoreboard buildFor(Player p) { public Scoreboard buildFor(Player p) {
List<EventScoreEntry> scoreMap = this.playerScores; List<EventScoreEntry> scoreList = new ArrayList<>(this.playerScores);
Scoreboard newScoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
Objective objective = newScoreboard.registerNewObjective( Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
Objective objective = scoreboard.registerNewObjective(
"event", "dummy", "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.setDisplaySlot(DisplaySlot.SIDEBAR);
objective.numberFormat(NumberFormat.blank()); 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); scoreList.sort(this.scoreComparator);
System.out.print(scoreMap);
EventScoreEntry playerScoreEntry = scoreMap.stream() int size = scoreList.size();
.filter(entry -> entry.playerUuid().equals(p.getUniqueId())) int playerIndex = IntStream.range(0, size)
.filter(i -> scoreList.get(i).playerUuid().equals(uuid))
.findFirst() .findFirst()
.orElse(null); .orElse(0);
if(playerScoreEntry == null) {
playerScoreEntry = new EventScoreEntry(p.getUniqueId(), p.getName(), this.scorable.getScore(p)); IntStream top = IntStream.range(0, Math.min(this.topPlaces, size));
scoreMap.add(playerScoreEntry);
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); for (int i = 0; i < display.length; i++) {
IntStream aroundStream = IntStream.range(playerIndex - this.aroundPlaces, playerIndex + this.aroundPlaces); int idx = display[i];
IntStream bottomStream = IntStream.range(scoreMap.size() - this.bottomPlaces, scoreMap.size()); EventScoreEntry entry = scoreList.get(idx);
if(playerIndex <= this.topPlaces + this.aroundPlaces) { String line = this.formattedLine(idx, entry.name(), entry.score());
aroundStream = IntStream.empty();
topStream = IntStream.range(0, playerIndex + this.aroundPlaces); objective.getScore(line).setScore(display.length - i);
}
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();
} }
IntStream indices = IntStream.concat( return scoreboard;
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;
} }
public void startAutomaticUpdates() { public void startAutomaticUpdates() {
this.scoreboardUpdateTaskId = Bukkit.getScheduler().scheduleSyncRepeatingTask( this.scoreboardUpdateTaskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(
Main.instance(), Main.instance(),