Skip to content

Commit 30e2288

Browse files
authored
Merge pull request #681 from deyoyk/fixing-scoreboard-again
fixing scoreboard, tested it too
2 parents ff11ddd + a7282d5 commit 30e2288

15 files changed

Lines changed: 713 additions & 680 deletions

File tree

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
package net.swofty.type.bedwarsgame;
22

3-
import net.kyori.adventure.text.Component;
43
import net.minestom.server.MinecraftServer;
54
import net.minestom.server.entity.Player;
6-
import net.minestom.server.scoreboard.Sidebar;
75
import net.minestom.server.timer.Scheduler;
86
import net.minestom.server.timer.TaskSchedule;
97
import net.swofty.type.bedwarsgame.game.Game;
108
import net.swofty.type.bedwarsgame.game.GameStatus;
11-
import net.swofty.type.generic.data.handlers.BedWarsDataHandler;
12-
import net.swofty.commons.bedwars.map.BedWarsMapsConfig.MapTeam;
139
import net.swofty.commons.bedwars.map.BedWarsMapsConfig.TeamKey;
1410
import net.swofty.type.generic.HypixelConst;
1511
import net.swofty.type.generic.data.HypixelDataHandler;
12+
import net.swofty.type.generic.data.handlers.BedWarsDataHandler;
13+
import net.swofty.type.generic.scoreboard.HypixelScoreboard;
1614
import net.swofty.type.generic.user.HypixelPlayer;
1715

1816
import java.text.SimpleDateFormat;
17+
import java.util.ArrayList;
1918
import java.util.Date;
20-
import java.util.HashMap;
21-
import java.util.Map;
22-
import java.util.UUID;
19+
import java.util.List;
2320

2421
public class BedWarsGameScoreboard {
25-
private static final Map<UUID, Sidebar> sidebarCache = new HashMap<>();
22+
private static final HypixelScoreboard scoreboard = new HypixelScoreboard();
2623
private static Integer prototypeName = 0;
2724

2825
public static void start(Game game) {
@@ -38,7 +35,7 @@ public static void start(Game game) {
3835
}
3936

4037
for (HypixelPlayer player : game.getPlayers()) {
41-
if (player.joined - System.currentTimeMillis() > 5000) { // for now let's not show scoreboard too early
38+
if (player.joined - System.currentTimeMillis() > 5000) {
4239
continue;
4340
}
4441
HypixelDataHandler dataHandler = player.getDataHandler();
@@ -48,18 +45,18 @@ public static void start(Game game) {
4845
continue;
4946
}
5047

51-
Sidebar sidebar = new Sidebar(Component.text(getSidebarName(prototypeName)));
48+
List<String> lines = new ArrayList<>();
49+
lines.add("§7" + new SimpleDateFormat("MM/dd/yy").format(new Date()) + " §8" + HypixelConst.getServerName());
50+
lines.add("§7 ");
5251

53-
addLine("§7" + new SimpleDateFormat("MM/dd/yy").format(new Date()) + " §8" + HypixelConst.getServerName(), sidebar);
54-
addLine("§7 ", sidebar);
5552
if (game.getGameStatus() == GameStatus.WAITING) {
56-
addLine("§fMap: §a" + game.getMapEntry().getName(), sidebar);
57-
addLine("§fPlayers: §a" + game.getPlayers().size() + "/" + game.getMapEntry().getConfiguration().getTeams().size(), sidebar);
58-
addLine("§7 ", sidebar);
59-
addLine("§fStarting in §a" + game.getCountdown().getRemainingSeconds() + "s", sidebar);
60-
addLine("§7 ", sidebar);
61-
addLine("§fMode: §a" + game.getBedwarsGameType().getDisplayName(), sidebar);
62-
addLine("§fVersion: §7v1.9", sidebar);
53+
lines.add("§fMap: §a" + game.getMapEntry().getName());
54+
lines.add("§fPlayers: §a" + game.getPlayers().size() + "/" + game.getMapEntry().getConfiguration().getTeams().size());
55+
lines.add("§7 ");
56+
lines.add("§fStarting in §a" + game.getCountdown().getRemainingSeconds() + "s");
57+
lines.add("§7 ");
58+
lines.add("§fMode: §a" + game.getBedwarsGameType().getDisplayName());
59+
lines.add("§fVersion: §7v1.9");
6360
} else {
6461
String eventName = game.getEventManager().getNextEvent() != null
6562
? game.getEventManager().getNextEvent().getDisplayName()
@@ -68,57 +65,33 @@ public static void start(Game game) {
6865
long minutesPart = seconds / 60;
6966
long secondsPart = seconds % 60;
7067
String timeLeft = String.format("%d:%02d", minutesPart, secondsPart);
71-
addLine("§f" + eventName + " in §a" + timeLeft, sidebar);
72-
addLine("§7 ", sidebar);
73-
for (java.util.Map.Entry<TeamKey, MapTeam> entry : game.getMapEntry().getConfiguration().getTeams().entrySet()) {
68+
lines.add("§f" + eventName + " in §a" + timeLeft);
69+
lines.add("§7 ");
70+
for (java.util.Map.Entry<TeamKey, net.swofty.commons.bedwars.map.BedWarsMapsConfig.MapTeam> entry : game.getMapEntry().getConfiguration().getTeams().entrySet()) {
7471
TeamKey teamKey = entry.getKey();
7572
String teamName = teamKey.getName();
7673
String teamInitial = teamName.substring(0, 1).toUpperCase();
7774

7875
String bedStatus = game.getTeamManager().isBedAlive(teamKey) ? "§a✔" : "§c✖";
79-
addLine(String.format("%s%s §f%s %s", teamKey.chatColor(), teamInitial, teamName, bedStatus), sidebar);
76+
lines.add(String.format("%s%s §f%s %s", teamKey.chatColor(), teamInitial, teamName, bedStatus));
8077
}
8178
}
82-
addLine("§7 ", sidebar);
83-
addLine("§ewww.hypixel.net", sidebar);
84-
85-
Sidebar oldSidebar = sidebarCache.get(player.getUuid());
86-
87-
sidebar.addViewer(player);
88-
89-
sidebarCache.put(player.getUuid(), sidebar);
79+
lines.add("§7 ");
80+
lines.add("§ewww.hypixel.net");
9081

91-
if (oldSidebar != null && oldSidebar != sidebar) {
92-
final Sidebar finalOldSidebar = oldSidebar;
93-
scheduler.scheduleNextTick(() -> {
94-
if (sidebarCache.get(player.getUuid()) == sidebar) {
95-
try {
96-
finalOldSidebar.removeViewer(player);
97-
} catch (Exception e) {
98-
99-
}
100-
}
101-
});
82+
if (!scoreboard.hasScoreboard(player)) {
83+
scoreboard.createScoreboard(player, getSidebarName(prototypeName));
10284
}
85+
86+
scoreboard.updateLines(player, lines);
87+
scoreboard.updateTitle(player, getSidebarName(prototypeName));
10388
}
104-
return TaskSchedule.tick(10);
89+
return TaskSchedule.tick(10);
10590
});
10691
}
10792

10893
public static void removeCache(Player player) {
109-
sidebarCache.remove(player.getUuid());
110-
}
111-
112-
private static void addLine(String text, Sidebar sidebar) {
113-
114-
int score = sidebar.getLines().size();
115-
sidebar.createLine(new Sidebar.ScoreboardLine(UUID.randomUUID().toString(), Component.text(text), score));
116-
}
117-
118-
private static void addLine(Component text, Sidebar sidebar) {
119-
120-
int score = sidebar.getLines().size();
121-
sidebar.createLine(new Sidebar.ScoreboardLine(UUID.randomUUID().toString(), text, score));
94+
scoreboard.removeScoreboard(player);
12295
}
12396

12497
private static String getSidebarName(int counter) {
@@ -138,5 +111,4 @@ private static String getSidebarName(int counter) {
138111
return colors[2] + baseText + endColor;
139112
}
140113
}
141-
142114
}

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/events/ActionPlayerDisconnect.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import lombok.SneakyThrows;
44
import net.minestom.server.event.player.PlayerDisconnectEvent;
5-
import net.swofty.type.bedwarsgame.TypeBedWarsGameLoader;
5+
import net.swofty.type.bedwarsgame.BedWarsGameScoreboard;
66
import net.swofty.type.bedwarsgame.game.Game;
77
import net.swofty.type.bedwarsgame.game.GameStatus;
88
import net.swofty.type.bedwarsgame.user.BedWarsPlayer;
@@ -19,13 +19,12 @@ public void run(PlayerDisconnectEvent event) {
1919
Game game = player.getGame();
2020
if (game != null) {
2121
if (game.getGameStatus() == GameStatus.IN_PROGRESS) {
22-
// Use handleDisconnect for active games to enable rejoin
2322
game.handleDisconnect(player);
2423
} else {
25-
// Normal leave for waiting/ending games
2624
game.leave(player);
2725
}
2826
}
27+
BedWarsGameScoreboard.removeCache(player);
2928
}
3029
}
3130

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package net.swofty.type.bedwarslobby;
22

3-
import net.kyori.adventure.text.Component;
43
import net.minestom.server.MinecraftServer;
54
import net.minestom.server.entity.Player;
6-
import net.minestom.server.scoreboard.Sidebar;
75
import net.minestom.server.timer.Scheduler;
86
import net.minestom.server.timer.TaskSchedule;
97
import net.swofty.commons.bedwars.BedwarsLevelColor;
@@ -13,24 +11,23 @@
1311
import net.swofty.type.generic.data.HypixelDataHandler;
1412
import net.swofty.type.generic.data.datapoints.DatapointLeaderboardLong;
1513
import net.swofty.type.generic.data.handlers.BedWarsDataHandler;
14+
import net.swofty.type.generic.scoreboard.HypixelScoreboard;
1615
import net.swofty.type.generic.user.HypixelPlayer;
1716

1817
import java.text.SimpleDateFormat;
18+
import java.util.ArrayList;
1919
import java.util.Date;
20-
import java.util.HashMap;
21-
import java.util.Map;
22-
import java.util.UUID;
20+
import java.util.List;
2321

2422
import static net.swofty.commons.bedwars.BedwarsLevelUtil.suffix;
2523

2624
public class BedWarsLobbyScoreboard {
27-
private static final Map<UUID, Sidebar> sidebarCache = new HashMap<>();
25+
private static final HypixelScoreboard scoreboard = new HypixelScoreboard();
2826
private static Integer prototypeName = 0;
2927

3028
public static void start() {
3129
Scheduler scheduler = MinecraftServer.getSchedulerManager();
3230

33-
// Scoreboard Updater
3431
scheduler.submitTask(() -> {
3532
prototypeName++;
3633
if (prototypeName > 50) {
@@ -61,45 +58,38 @@ public static void start() {
6158
}
6259
progressBar.append("§8]");
6360

64-
65-
Sidebar sidebar = sidebarCache.get(player.getUuid());
66-
67-
68-
if (sidebar == null) {
69-
sidebar = new Sidebar(Component.text(getSidebarName(prototypeName)));
70-
71-
addLine("§7" + new SimpleDateFormat("MM/dd/yy").format(new Date()) + " §8" + HypixelConst.getServerName(), sidebar);
72-
addLine("§7 ", sidebar);
73-
addLine("§fLevel: §7" + BedwarsLevelColor.constructLevelString(BedwarsLevelUtil.calculateLevel(experience)), sidebar);
74-
addLine("§7 ", sidebar);
75-
addLine("§fProgress: §b" + suffix(progress) + "§7/§a" + suffix(maxExperience), sidebar);
76-
addLine(progressBar.toString(), sidebar);
77-
addLine("§7 ", sidebar);
78-
addLine("§fTokens: §2" + bwDataHandler.get(BedWarsDataHandler.Data.TOKENS, DatapointLeaderboardLong.class).getValue(), sidebar);
79-
addLine("§fTickets: §b" + bwDataHandler.get(BedWarsDataHandler.Data.SLUMBER_TICKETS, DatapointLeaderboardLong.class).getValue() + "§7/75", sidebar);
80-
addLine("§7 ", sidebar);
81-
addLine("§fTotal Kills: §a0", sidebar);
82-
addLine("§fTotal Wins: §a0", sidebar);
83-
addLine("§7 ", sidebar);
84-
85-
addLine("§ewww.hypixel.net", sidebar);
86-
87-
sidebar.addViewer(player);
88-
sidebarCache.put(player.getUuid(), sidebar);
61+
long tokens = bwDataHandler.get(BedWarsDataHandler.Data.TOKENS, DatapointLeaderboardLong.class).getValue();
62+
long tickets = bwDataHandler.get(BedWarsDataHandler.Data.SLUMBER_TICKETS, DatapointLeaderboardLong.class).getValue();
63+
64+
List<String> lines = new ArrayList<>();
65+
lines.add("§7" + new SimpleDateFormat("MM/dd/yy").format(new Date()) + " §8" + HypixelConst.getServerName());
66+
lines.add("§7 ");
67+
lines.add("§fLevel: §7" + BedwarsLevelColor.constructLevelString(BedwarsLevelUtil.calculateLevel(experience)));
68+
lines.add("§7 ");
69+
lines.add("§fProgress: §b" + suffix(progress) + "§7/§a" + suffix(maxExperience));
70+
lines.add(progressBar.toString());
71+
lines.add("§7 ");
72+
lines.add("§fTokens: §2" + tokens);
73+
lines.add("§fTickets: §b" + tickets + "§7/75");
74+
lines.add("§7 ");
75+
lines.add("§fTotal Kills: §a0");
76+
lines.add("§fTotal Wins: §a0");
77+
lines.add("§7 ");
78+
lines.add("§ewww.hypixel.net");
79+
80+
if (!scoreboard.hasScoreboard(player)) {
81+
scoreboard.createScoreboard(player, getSidebarName(prototypeName));
8982
}
9083

84+
scoreboard.updateLines(player, lines);
85+
scoreboard.updateTitle(player, getSidebarName(prototypeName));
9186
}
92-
return TaskSchedule.tick(5);
87+
return TaskSchedule.tick(5);
9388
});
9489
}
9590

9691
public static void removeCache(Player player) {
97-
sidebarCache.remove(player.getUuid());
98-
}
99-
100-
private static void addLine(String text, Sidebar sidebar) {
101-
int score = sidebar.getLines().size();
102-
sidebar.createLine(new Sidebar.ScoreboardLine(UUID.randomUUID().toString(), Component.text(text), score));
92+
scoreboard.removeScoreboard(player);
10393
}
10494

10595
private static String getSidebarName(int counter) {
@@ -119,5 +109,4 @@ private static String getSidebarName(int counter) {
119109
return colors[2] + baseText + endColor;
120110
}
121111
}
122-
123112
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.swofty.type.bedwarslobby.events;
2+
3+
import net.minestom.server.event.player.PlayerDisconnectEvent;
4+
import net.swofty.type.bedwarslobby.BedWarsLobbyScoreboard;
5+
import net.swofty.type.generic.event.EventNodes;
6+
import net.swofty.type.generic.event.HypixelEvent;
7+
import net.swofty.type.generic.event.HypixelEventClass;
8+
import net.swofty.type.generic.user.HypixelPlayer;
9+
10+
public class ActionPlayerDisconnect implements HypixelEventClass {
11+
12+
@HypixelEvent(node = EventNodes.PLAYER, requireDataLoaded = false)
13+
public void run(PlayerDisconnectEvent event) {
14+
HypixelPlayer player = (HypixelPlayer) event.getPlayer();
15+
BedWarsLobbyScoreboard.removeCache(player);
16+
}
17+
}
18+

0 commit comments

Comments
 (0)