diff --git a/README.md b/README.md index 5a301c00..87144bfc 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Join the [Discord](https://discord.gg/v7nmTDTW8W) or create an issue for support ## Features -- Sidebars. Up to 42 characters (depends on the formatting) for 1.12.2 and below, no limit for newer versions +- Sidebars. Up to 44 characters (depends on the formatting) for 1.12.2 and below, no limit for newer versions - Teams. Supports showing different properties (display name, prefix, entries etc.) of the same team to different players - Objectives. diff --git a/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/AbstractSidebar.java b/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/AbstractSidebar.java index ada17108..0ba50306 100644 --- a/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/AbstractSidebar.java +++ b/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/AbstractSidebar.java @@ -3,6 +3,7 @@ import com.google.common.base.Preconditions; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.translation.GlobalTranslator; import net.megavex.scoreboardlibrary.api.objective.ObjectiveDisplaySlot; import net.megavex.scoreboardlibrary.api.objective.ObjectiveRenderType; @@ -16,7 +17,6 @@ import net.megavex.scoreboardlibrary.implementation.player.ScoreboardLibraryPlayer; import net.megavex.scoreboardlibrary.implementation.sidebar.line.GlobalLineInfo; import net.megavex.scoreboardlibrary.implementation.sidebar.line.LocaleLineHandler; -import net.megavex.scoreboardlibrary.implementation.sidebar.line.PlayerNameProvider; import org.apache.commons.lang.RandomStringUtils; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -32,7 +32,6 @@ public abstract class AbstractSidebar implements Sidebar, PlayerDisplayable { private final ScoreboardLibraryImpl scoreboardLibrary; private final ObjectivePacketAdapter packetAdapter; - private final List linePlayerNames; private final GlobalLineInfo[] lines; private Component title = empty(); @@ -48,7 +47,6 @@ public AbstractSidebar(@NotNull ScoreboardLibraryImpl scoreboardLibrary, int max String objectiveName = RandomStringUtils.randomAlphanumeric(16); this.packetAdapter = scoreboardLibrary.packetAdapter().createObjectiveAdapter(objectiveName); - this.linePlayerNames = PlayerNameProvider.provideLinePlayerNames(maxLines); this.lines = new GlobalLineInfo[maxLines]; } @@ -280,7 +278,10 @@ private void updateScores() { private @NotNull GlobalLineInfo getLineInfo(int line) { GlobalLineInfo globalLineInfo = lines[line]; if (globalLineInfo == null) { - globalLineInfo = lines[line] = new GlobalLineInfo(this, linePlayerNames.get(line), line); + char c = Character.toChars(128 + line)[0]; + String playerName = "" + LegacyComponentSerializer.SECTION_CHAR + c; + + globalLineInfo = lines[line] = new GlobalLineInfo(this, playerName, line); updateScores(); } diff --git a/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/PlayerNameProvider.java b/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/PlayerNameProvider.java deleted file mode 100644 index 22d9a892..00000000 --- a/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/PlayerNameProvider.java +++ /dev/null @@ -1,41 +0,0 @@ -package net.megavex.scoreboardlibrary.implementation.sidebar.line; - -import net.megavex.scoreboardlibrary.api.sidebar.Sidebar; -import org.bukkit.ChatColor; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Unmodifiable; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class PlayerNameProvider { - private static final ChatColor[] CHAT_COLORS = ChatColor.values(); - private static final List DEFAULT_NAMES = createLinePlayerNames(Sidebar.MAX_LINES); - - private PlayerNameProvider() { - } - - public static @NotNull @Unmodifiable List provideLinePlayerNames(int maxLines) { - return maxLines <= Sidebar.MAX_LINES ? DEFAULT_NAMES : createLinePlayerNames(maxLines); - } - - private static @NotNull @Unmodifiable List createLinePlayerNames(int maxLines) { - List result = new ArrayList<>(maxLines); - - int i = 0; - while (result.size() < maxLines) { - for (ChatColor color : CHAT_COLORS) { - String legacy = color.toString(); - String newStr = i == 0 ? legacy : result.get(i - 1) + legacy; - - if (!result.contains(newStr)) { - result.add(newStr); - } - } - i++; - } - - return Collections.unmodifiableList(result); - } -} diff --git a/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/locale/LegacyLocaleLine.java b/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/locale/LegacyLocaleLine.java index 2b49d231..49c3ede4 100644 --- a/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/locale/LegacyLocaleLine.java +++ b/implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/locale/LegacyLocaleLine.java @@ -82,7 +82,7 @@ public void value(@NotNull Component renderedComponent) { this.prefix = legacyValue.substring(0, prefixEnd); String last = prefix + LegacyComponentSerializer.SECTION_CHAR + (endsWithSection ? legacyValue.charAt(16) : ""); - this.player = info.player() + ChatColor.RESET + ChatColor.getLastColors(last); + this.player = info.player() + ChatColor.getLastColors(last); int playerEnd = prefixEnd; if (legacyValue.length() > 32) { @@ -93,11 +93,10 @@ public void value(@NotNull Component renderedComponent) { player += legacyValue.substring(prefixEnd, playerEnd); } - this.suffix = legacyValue.substring(playerEnd + (endsWithSection ? 2 : 0)); + this.suffix = ChatColor.getLastColors(this.player) + legacyValue.substring(playerEnd + (endsWithSection ? 2 : 0)); if (suffix.length() > 16) { String newSuffix = suffix.substring(0, 16); - if (newSuffix.endsWith(String.valueOf(LegacyComponentSerializer.SECTION_CHAR)) && - ChatColor.getByChar(suffix.charAt(16)) != null) { + if (newSuffix.endsWith(String.valueOf(LegacyComponentSerializer.SECTION_CHAR))) { newSuffix = newSuffix.substring(0, 15); } diff --git a/implementation/src/test/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/PlayerNameProviderTest.java b/implementation/src/test/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/PlayerNameProviderTest.java deleted file mode 100644 index e135ab5e..00000000 --- a/implementation/src/test/java/net/megavex/scoreboardlibrary/implementation/sidebar/line/PlayerNameProviderTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.megavex.scoreboardlibrary.implementation.sidebar.line; - -import org.junit.jupiter.api.Test; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -class PlayerNameProviderTest { - @Test - void duplicateTest() { - List names = PlayerNameProvider.provideLinePlayerNames(100); - Set uniqueNames = new HashSet<>(names.size()); - for (String name : names) { - assertTrue(uniqueNames.add(name), "duplicate player name " + name); - } - } -}