WIP: moved scores and score updates to EventScoreboardBuilder

This commit is contained in:
2025-12-12 12:09:25 +01:00
parent 2ff95f8450
commit 04cb233604
6 changed files with 59 additions and 78 deletions

View File

@@ -1,15 +1,12 @@
package eu.mhsl.craftattack.spawn.event.appliances.deathrun;
import eu.mhsl.craftattack.spawn.core.Main;
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
import eu.mhsl.craftattack.spawn.event.appliances.eventController.AbstractEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import net.kyori.adventure.util.Ticks;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
@@ -17,8 +14,6 @@ import java.util.*;
@Appliance.Flags(autoload = false)
public class Deathrun extends AbstractEvent {
private int scoreboardUpdateTaskId;
@Override
public void initialize(@NotNull JavaPlugin plugin) {
World world = Bukkit.getWorlds().getFirst();
@@ -28,11 +23,6 @@ public class Deathrun extends AbstractEvent {
this.start();
}
@Override
public void onDisable() {
if(this.scoreboardUpdateTaskId != -1) Bukkit.getScheduler().cancelTask(this.scoreboardUpdateTaskId);
}
@Override
public void start() {
Title title = Title.title(Component.text("Start"), Component.text("Erreiche die höchste x-Koordinate!"));
@@ -41,17 +31,7 @@ public class Deathrun extends AbstractEvent {
World world = Bukkit.getWorlds().getFirst();
world.getWorldBorder().setSize(world.getWorldBorder().getMaxSize());
this.updateScoreboards();
this.scoreboardUpdateTaskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(
Main.instance(),
this::updateScoreboards,
Ticks.TICKS_PER_SECOND,
Ticks.TICKS_PER_SECOND
);
}
private void updateScoreboards() {
Bukkit.getOnlinePlayers().forEach(this::updateScoreboard);
this.getScoreboardBuilder().updateScoreboards();
}
@Override
@@ -66,14 +46,12 @@ public class Deathrun extends AbstractEvent {
}
@Override
public String displayName() {
return "Deathrun";
public List<Player> getPlayers() {
return List.of();
}
@Override
protected @NotNull List<Listener> listeners() {
return List.of(
new DeathrunListener()
);
public String displayName() {
return "Deathrun";
}
}

View File

@@ -1,15 +0,0 @@
package eu.mhsl.craftattack.spawn.event.appliances.deathrun;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerMoveEvent;
class DeathrunListener extends ApplianceListener<Deathrun> {
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
if(!event.hasChangedPosition()) return;
if(event.getTo().clone().subtract(event.getFrom()).x() == 0) return;
this.getAppliance().updateScore(event.getPlayer());
}
}

View File

@@ -11,17 +11,7 @@ import java.util.*;
public abstract class AbstractEvent extends Appliance implements Event {
private final Map<UUID, Scoreboard> playerScoreboards = new HashMap<>();
private final List<EventScoreEntry> playerScores = new ArrayList<>();
private final EventScoreboardBuilder scoreboardBuilder = new EventScoreboardBuilder(3, 2, 3);
@Override
public Map<UUID, Scoreboard> getPlayerScoreboards() {
return this.playerScoreboards;
}
@Override
public List<EventScoreEntry> getPlayerScores() {
return this.playerScores;
}
private final EventScoreboardBuilder scoreboardBuilder = new EventScoreboardBuilder(this, 3, 2, 3);
@Override
public EventScoreboardBuilder getScoreboardBuilder() {

View File

@@ -1,31 +1,16 @@
package eu.mhsl.craftattack.spawn.event.appliances.eventController;
import eu.mhsl.craftattack.spawn.event.appliances.eventController.scoreboard.EventScoreEntry;
import eu.mhsl.craftattack.spawn.event.appliances.eventController.scoreboard.EventScoreboardBuilder;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Scoreboard;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public interface Event {
String displayName();
Map<UUID, Scoreboard> getPlayerScoreboards();
List<EventScoreEntry> getPlayerScores();
EventScoreboardBuilder getScoreboardBuilder();
void start();
void stop();
List<Player> getPlayers();
int getScore(Player p);
default void updateScore(Player p) {
System.out.println(this.getPlayerScores().removeIf(entry -> entry.playerUuid().equals(p.getUniqueId())));
this.getPlayerScores().add(new EventScoreEntry(p.getUniqueId(), p.getName(), this.getScore(p)));
}
default void updateScoreboard(Player p) {
this.updateScore(p);
Scoreboard scoreboard = this.getScoreboardBuilder().buildFor(p, this);
this.getPlayerScoreboards().put(p.getUniqueId(), scoreboard);
p.setScoreboard(scoreboard);
}
}

View File

@@ -26,7 +26,6 @@ public class EventController extends Appliance {
public void enableEvent(Appliance event) {
this.getEventAppliances().forEach(appliance -> appliance.disableSequence(Main.instance()));
Main.instance().restartAppliance(event.getClass());
// Main.instance().getAppliance(event.getClass())
}
public void disableEvent(Appliance event) {

View File

@@ -1,42 +1,58 @@
package eu.mhsl.craftattack.spawn.event.appliances.eventController.scoreboard;
import eu.mhsl.craftattack.spawn.core.Main;
import eu.mhsl.craftattack.spawn.event.appliances.eventController.Event;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.util.Ticks;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import java.util.stream.IntStream;
public class EventScoreboardBuilder {
private final int topPlaces;
private final int aroundPlaces;
private final int bottomPlaces;
private final Event event;
private final Comparator<EventScoreEntry> scoreComparator;
private final List<EventScoreEntry> playerScores = new ArrayList<>();
private final Map<UUID, Scoreboard> playerScoreboardMap = new HashMap<>();
private int scoreboardUpdateTaskId = -1;
public EventScoreboardBuilder(int topPlaces, int aroundPlaces, int bottomPlaces) {
public EventScoreboardBuilder(Event event) {
this(event, 3, 2, 3);
}
public EventScoreboardBuilder(Event event, int topPlaces, int aroundPlaces, int bottomPlaces) {
this(event, topPlaces, aroundPlaces, bottomPlaces, Comparator.comparingInt(EventScoreEntry::score).reversed());
}
public EventScoreboardBuilder(Event event, int topPlaces, int aroundPlaces, int bottomPlaces, Comparator<EventScoreEntry> scoreComparator) {
this.event = event;
this.topPlaces = topPlaces;
this.aroundPlaces = aroundPlaces;
this.bottomPlaces = bottomPlaces;
this.scoreComparator = scoreComparator;
}
public Scoreboard buildFor(Player p, Event event) {
List<EventScoreEntry> scoreMap = event.getPlayerScores();
public Scoreboard buildFor(Player p) {
List<EventScoreEntry> scoreMap = this.playerScores;
Scoreboard newScoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
Objective objective = newScoreboard.registerNewObjective(
"event", "dummy",
Component.text("Scoreboard %s".formatted(event.displayName()), NamedTextColor.GOLD)
Component.text("Scoreboard %s".formatted(this.event.displayName()), NamedTextColor.GOLD)
);
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
if(scoreMap.isEmpty()) return newScoreboard;
scoreMap.sort(Comparator.comparingInt(EventScoreEntry::score).reversed());
scoreMap.sort(this.scoreComparator);
System.out.print(scoreMap);
EventScoreEntry playerScoreEntry = scoreMap.stream()
@@ -44,7 +60,7 @@ public class EventScoreboardBuilder {
.findFirst()
.orElse(null);
if(playerScoreEntry == null) {
playerScoreEntry = new EventScoreEntry(p.getUniqueId(), p.getName(), event.getScore(p));
playerScoreEntry = new EventScoreEntry(p.getUniqueId(), p.getName(), this.event.getScore(p));
scoreMap.add(playerScoreEntry);
}
@@ -79,9 +95,37 @@ public class EventScoreboardBuilder {
objective.getScore(line).setScore(place);
});
this.playerScoreboardMap.put(p.getUniqueId(), newScoreboard);
return newScoreboard;
}
public void startAutomaticUpdates() {
this.scoreboardUpdateTaskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(
Main.instance(),
this::updateScoreboards,
Ticks.TICKS_PER_SECOND,
Ticks.TICKS_PER_SECOND
);
}
public void stopAutomaticUpdates() {
if(this.scoreboardUpdateTaskId != -1) Bukkit.getScheduler().cancelTask(this.scoreboardUpdateTaskId);
this.scoreboardUpdateTaskId = -1;
}
private void updateScore(Player p) {
this.playerScores.removeIf(entry -> entry.playerUuid().equals(p.getUniqueId()));
this.playerScores.add(new EventScoreEntry(p.getUniqueId(), p.getName(), this.event.getScore(p)));
}
public void updateScoreboards() {
Bukkit.getOnlinePlayers().forEach(player -> {
this.updateScore(player);
Scoreboard scoreboard = this.buildFor(player);
player.setScoreboard(scoreboard);
});
}
private String formattedLine(int place, String name, int score) {
name = this.trimName(name);
return name + ": " + score;