WIP: moved scores and score updates to EventScoreboardBuilder
This commit is contained in:
@@ -1,15 +1,12 @@
|
|||||||
package eu.mhsl.craftattack.spawn.event.appliances.deathrun;
|
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.core.appliance.Appliance;
|
||||||
import eu.mhsl.craftattack.spawn.event.appliances.eventController.AbstractEvent;
|
import eu.mhsl.craftattack.spawn.event.appliances.eventController.AbstractEvent;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.title.Title;
|
import net.kyori.adventure.title.Title;
|
||||||
import net.kyori.adventure.util.Ticks;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@@ -17,8 +14,6 @@ import java.util.*;
|
|||||||
|
|
||||||
@Appliance.Flags(autoload = false)
|
@Appliance.Flags(autoload = false)
|
||||||
public class Deathrun extends AbstractEvent {
|
public class Deathrun extends AbstractEvent {
|
||||||
private int scoreboardUpdateTaskId;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(@NotNull JavaPlugin plugin) {
|
public void initialize(@NotNull JavaPlugin plugin) {
|
||||||
World world = Bukkit.getWorlds().getFirst();
|
World world = Bukkit.getWorlds().getFirst();
|
||||||
@@ -28,11 +23,6 @@ public class Deathrun extends AbstractEvent {
|
|||||||
this.start();
|
this.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDisable() {
|
|
||||||
if(this.scoreboardUpdateTaskId != -1) Bukkit.getScheduler().cancelTask(this.scoreboardUpdateTaskId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
Title title = Title.title(Component.text("Start"), Component.text("Erreiche die höchste x-Koordinate!"));
|
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 world = Bukkit.getWorlds().getFirst();
|
||||||
world.getWorldBorder().setSize(world.getWorldBorder().getMaxSize());
|
world.getWorldBorder().setSize(world.getWorldBorder().getMaxSize());
|
||||||
|
|
||||||
this.updateScoreboards();
|
this.getScoreboardBuilder().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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -66,14 +46,12 @@ public class Deathrun extends AbstractEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String displayName() {
|
public List<Player> getPlayers() {
|
||||||
return "Deathrun";
|
return List.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected @NotNull List<Listener> listeners() {
|
public String displayName() {
|
||||||
return List.of(
|
return "Deathrun";
|
||||||
new DeathrunListener()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,17 +11,7 @@ import java.util.*;
|
|||||||
public abstract class AbstractEvent extends Appliance implements Event {
|
public abstract class AbstractEvent extends Appliance implements Event {
|
||||||
private final Map<UUID, Scoreboard> playerScoreboards = new HashMap<>();
|
private final Map<UUID, Scoreboard> playerScoreboards = new HashMap<>();
|
||||||
private final List<EventScoreEntry> playerScores = new ArrayList<>();
|
private final List<EventScoreEntry> playerScores = new ArrayList<>();
|
||||||
private final EventScoreboardBuilder scoreboardBuilder = new EventScoreboardBuilder(3, 2, 3);
|
private final EventScoreboardBuilder scoreboardBuilder = new EventScoreboardBuilder(this, 3, 2, 3);
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<UUID, Scoreboard> getPlayerScoreboards() {
|
|
||||||
return this.playerScoreboards;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<EventScoreEntry> getPlayerScores() {
|
|
||||||
return this.playerScores;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EventScoreboardBuilder getScoreboardBuilder() {
|
public EventScoreboardBuilder getScoreboardBuilder() {
|
||||||
|
|||||||
@@ -1,31 +1,16 @@
|
|||||||
package eu.mhsl.craftattack.spawn.event.appliances.eventController;
|
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 eu.mhsl.craftattack.spawn.event.appliances.eventController.scoreboard.EventScoreboardBuilder;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.scoreboard.Scoreboard;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public interface Event {
|
public interface Event {
|
||||||
String displayName();
|
String displayName();
|
||||||
Map<UUID, Scoreboard> getPlayerScoreboards();
|
|
||||||
List<EventScoreEntry> getPlayerScores();
|
|
||||||
EventScoreboardBuilder getScoreboardBuilder();
|
EventScoreboardBuilder getScoreboardBuilder();
|
||||||
|
|
||||||
void start();
|
void start();
|
||||||
void stop();
|
void stop();
|
||||||
|
List<Player> getPlayers();
|
||||||
int getScore(Player p);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ public class EventController extends Appliance {
|
|||||||
public void enableEvent(Appliance event) {
|
public void enableEvent(Appliance event) {
|
||||||
this.getEventAppliances().forEach(appliance -> appliance.disableSequence(Main.instance()));
|
this.getEventAppliances().forEach(appliance -> appliance.disableSequence(Main.instance()));
|
||||||
Main.instance().restartAppliance(event.getClass());
|
Main.instance().restartAppliance(event.getClass());
|
||||||
// Main.instance().getAppliance(event.getClass())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disableEvent(Appliance event) {
|
public void disableEvent(Appliance event) {
|
||||||
|
|||||||
@@ -1,42 +1,58 @@
|
|||||||
package eu.mhsl.craftattack.spawn.event.appliances.eventController.scoreboard;
|
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 eu.mhsl.craftattack.spawn.event.appliances.eventController.Event;
|
||||||
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.util.Ticks;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.scoreboard.DisplaySlot;
|
import org.bukkit.scoreboard.DisplaySlot;
|
||||||
import org.bukkit.scoreboard.Objective;
|
import org.bukkit.scoreboard.Objective;
|
||||||
import org.bukkit.scoreboard.Scoreboard;
|
import org.bukkit.scoreboard.Scoreboard;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class EventScoreboardBuilder {
|
public class EventScoreboardBuilder {
|
||||||
private final int topPlaces;
|
private final int topPlaces;
|
||||||
private final int aroundPlaces;
|
private final int aroundPlaces;
|
||||||
private final int bottomPlaces;
|
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.topPlaces = topPlaces;
|
||||||
this.aroundPlaces = aroundPlaces;
|
this.aroundPlaces = aroundPlaces;
|
||||||
this.bottomPlaces = bottomPlaces;
|
this.bottomPlaces = bottomPlaces;
|
||||||
|
this.scoreComparator = scoreComparator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Scoreboard buildFor(Player p, Event event) {
|
public Scoreboard buildFor(Player p) {
|
||||||
List<EventScoreEntry> scoreMap = event.getPlayerScores();
|
List<EventScoreEntry> scoreMap = this.playerScores;
|
||||||
Scoreboard newScoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
|
Scoreboard newScoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
|
||||||
|
|
||||||
Objective objective = newScoreboard.registerNewObjective(
|
Objective objective = newScoreboard.registerNewObjective(
|
||||||
"event", "dummy",
|
"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);
|
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
|
||||||
|
|
||||||
if(scoreMap.isEmpty()) return newScoreboard;
|
if(scoreMap.isEmpty()) return newScoreboard;
|
||||||
|
|
||||||
scoreMap.sort(Comparator.comparingInt(EventScoreEntry::score).reversed());
|
scoreMap.sort(this.scoreComparator);
|
||||||
System.out.print(scoreMap);
|
System.out.print(scoreMap);
|
||||||
|
|
||||||
EventScoreEntry playerScoreEntry = scoreMap.stream()
|
EventScoreEntry playerScoreEntry = scoreMap.stream()
|
||||||
@@ -44,7 +60,7 @@ public class EventScoreboardBuilder {
|
|||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
if(playerScoreEntry == 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);
|
scoreMap.add(playerScoreEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,9 +95,37 @@ public class EventScoreboardBuilder {
|
|||||||
objective.getScore(line).setScore(place);
|
objective.getScore(line).setScore(place);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.playerScoreboardMap.put(p.getUniqueId(), newScoreboard);
|
||||||
return 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) {
|
private String formattedLine(int place, String name, int score) {
|
||||||
name = this.trimName(name);
|
name = this.trimName(name);
|
||||||
return name + ": " + score;
|
return name + ": " + score;
|
||||||
|
|||||||
Reference in New Issue
Block a user