Skip to content

Commit

Permalink
slightly refactor sidebar line logic, fixes several instances of usel…
Browse files Browse the repository at this point in the history
…ess packets being sent & a rare NPE
  • Loading branch information
vytskalt committed Mar 26, 2024
1 parent 20245b8 commit 5f89a47
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.megavex.scoreboardlibrary.implementation.commons;

/**
* Represents different strategies of rendering sidebar lines.
*/
public enum LineRenderingStrategy {
/**
* For versions older than 1.13, where team properties are stored as strings and have a limit of 16 characters.
*/
LEGACY,
/**
* For versions 1.13-1.20.2, where team properties are stored as components with no limits.
*/
MODERN,
/**
* For versions 1.20.3+, where the score display name can be used instead of team prefix team.
* Not currently being used.
*/
POST_MODERN
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public final class LocaleProvider {
return provider.apply(player);
}


private static @NotNull Function<Player, Locale> get() {
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -45,7 +46,7 @@ public abstract class AbstractSidebar implements Sidebar, PlayerDisplayable {
public AbstractSidebar(@NotNull ScoreboardLibraryImpl scoreboardLibrary, int maxLines) {
this.scoreboardLibrary = scoreboardLibrary;

String objectiveName = UUID.randomUUID().toString().substring(0, 5);
String objectiveName = RandomStringUtils.randomAlphanumeric(16);
this.packetAdapter = scoreboardLibrary.packetAdapter().createObjectiveAdapter(objectiveName);
this.linePlayerNames = PlayerNameProvider.provideLinePlayerNames(maxLines);
this.lines = new GlobalLineInfo[maxLines];
Expand Down Expand Up @@ -168,12 +169,13 @@ public final ObjectivePacketAdapter packetAdapter() {

@Override
public final void display(@NotNull Player player) {
packetAdapter.sendProperties(players, PropertiesPacketType.CREATE, title, ObjectiveRenderType.INTEGER, ScoreFormat.blank());
Collection<Player> singleton = Collections.singleton(player);
packetAdapter.sendProperties(singleton, PropertiesPacketType.CREATE, title, ObjectiveRenderType.INTEGER, ScoreFormat.blank());

LocaleLineHandler lineHandler = Objects.requireNonNull(addPlayer0(player));
lineHandler.addPlayer(player);
lineHandler.show(player);
packetAdapter.display(Collections.singleton(player), ObjectiveDisplaySlot.sidebar());
packetAdapter.display(singleton, ObjectiveDisplaySlot.sidebar());
}

public final boolean tick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SingleLocaleSidebar extends AbstractSidebar {
public SingleLocaleSidebar(@NotNull ScoreboardLibraryImpl scoreboardLibrary, int size, @NotNull Locale locale) {
super(scoreboardLibrary, size);
this.locale = locale;
this.sidebar = new LocaleLineHandler(this, locale());
this.sidebar = new LocaleLineHandler(this, locale);
this.internalPlayers = CollectionProvider.set(8);
}

Expand All @@ -36,14 +36,12 @@ public SingleLocaleSidebar(@NotNull ScoreboardLibraryImpl scoreboardLibrary, int
@Override
protected @Nullable LocaleLineHandler addPlayer0(@NotNull Player player) {
if (!internalPlayers.add(player)) return null;

return sidebar;
}

@Override
protected @Nullable LocaleLineHandler removePlayer0(@NotNull Player player) {
if (!internalPlayers.remove(player)) return null;

return sidebar;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.megavex.scoreboardlibrary.implementation.sidebar.line;

import net.kyori.adventure.text.Component;
import net.megavex.scoreboardlibrary.implementation.commons.LineRenderingStrategy;
import net.megavex.scoreboardlibrary.implementation.sidebar.AbstractSidebar;
import net.megavex.scoreboardlibrary.implementation.sidebar.line.locale.LineType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

Expand All @@ -12,111 +12,98 @@
public class LocaleLineHandler {
private final AbstractSidebar sidebar;
private final Locale locale;
private SidebarLineHandler modernLineHandler, legacyLineHandler;
private final SidebarLineHandler[] lineHandlers;

public LocaleLineHandler(AbstractSidebar sidebar, Locale locale) {
public LocaleLineHandler(@NotNull AbstractSidebar sidebar, @NotNull Locale locale) {
this.sidebar = sidebar;
this.locale = locale;
this.lineHandlers = new SidebarLineHandler[LineRenderingStrategy.values().length];
}

public AbstractSidebar sidebar() {
public @NotNull AbstractSidebar sidebar() {
return sidebar;
}

public Locale locale() {
public @NotNull Locale locale() {
return locale;
}

public boolean hasPlayers() {
return (modernLineHandler != null && !modernLineHandler.players().isEmpty()) || (legacyLineHandler != null && !legacyLineHandler.players().isEmpty());
for (SidebarLineHandler lineHandler : lineHandlers) {
if (lineHandler != null && !lineHandler.players().isEmpty()) {
return true;
}
}
return false;
}

public void addPlayer(@NotNull Player player) {
boolean isLegacy = sidebar.scoreboardLibrary().packetAdapter().isLegacy(player);
LineType lineType = isLegacy ? LineType.LEGACY : LineType.MODERN;
lineHandler(lineType).players().add(player);
LineRenderingStrategy strategy = sidebar.scoreboardLibrary().packetAdapter().lineRenderingStrategy(player);
lineHandler(strategy).players().add(player);
}

public void removePlayer(@NotNull Player player) {
if (modernLineHandler != null && modernLineHandler.players().remove(player)) {
return;
}

if (legacyLineHandler != null) {
legacyLineHandler.players().remove(player);
for (SidebarLineHandler lineHandler : lineHandlers) {
if (lineHandler != null && lineHandler.players().remove(player)) {
return;
}
}
}

public @NotNull SidebarLineHandler lineHandler(@NotNull LineType lineType) {
switch (lineType) {
case MODERN: {
if (modernLineHandler == null) {
modernLineHandler = new SidebarLineHandler(LineType.MODERN, this);
}

return modernLineHandler;
}
case LEGACY: {
if (legacyLineHandler == null) {
legacyLineHandler = new SidebarLineHandler(LineType.LEGACY, this);
}

return legacyLineHandler;
}
public @NotNull SidebarLineHandler lineHandler(@NotNull LineRenderingStrategy lineType) {
SidebarLineHandler lineHandler = lineHandlers[lineType.ordinal()];
if (lineHandler == null) {
lineHandler = lineHandlers[lineType.ordinal()] = new SidebarLineHandler(lineType, this);
}

throw new RuntimeException();
return lineHandler;
}

public void updateScoreFormat(int lineIndex) {
modernLineHandler.updateScore(lineIndex);
SidebarLineHandler lineHandler = lineHandlers[LineRenderingStrategy.MODERN.ordinal()];
if (lineHandler != null) {
lineHandler.updateScore(lineIndex);
}
}

public void updateLine(int lineIndex, Component renderedValue) {
if (modernLineHandler != null) {
modernLineHandler.setLine(lineIndex, renderedValue);
modernLineHandler.updateLine(lineIndex);
}

if (legacyLineHandler != null) {
legacyLineHandler.setLine(lineIndex, renderedValue);
legacyLineHandler.updateLine(lineIndex);
for (SidebarLineHandler lineHandler : lineHandlers) {
if (lineHandler != null) {
lineHandler.setLine(lineIndex, renderedValue);
}
}
}

public void updateScores() {
if (modernLineHandler != null) {
modernLineHandler.updateScores();
}

if (legacyLineHandler != null) {
legacyLineHandler.updateScores();
for (SidebarLineHandler lineHandler : lineHandlers) {
if (lineHandler != null) {
lineHandler.updateScores();
}
}
}

public void show(Player player) {
if (modernLineHandler != null && modernLineHandler.players().contains(player)) {
modernLineHandler.show(Collections.singleton(player));
} else if (legacyLineHandler != null && legacyLineHandler.players().contains(player)) {
legacyLineHandler.show(Collections.singleton(player));
for (SidebarLineHandler lineHandler : lineHandlers) {
if (lineHandler != null && lineHandler.players().contains(player)) {
lineHandler.show(Collections.singleton(player));
return;
}
}
}

public void hide(Player player) {
if (modernLineHandler != null && modernLineHandler.players().contains(player)) {
modernLineHandler.hide(Collections.singleton(player));
} else if (legacyLineHandler != null && legacyLineHandler.players().contains(player)) {
legacyLineHandler.hide(Collections.singleton(player));
for (SidebarLineHandler lineHandler : lineHandlers) {
if (lineHandler != null && lineHandler.players().contains(player)) {
lineHandler.hide(Collections.singleton(player));
return;
}
}
}

public void hide() {
if (modernLineHandler != null) {
modernLineHandler.hide(modernLineHandler.players());
}

if (legacyLineHandler != null) {
legacyLineHandler.hide(legacyLineHandler.players());
for (SidebarLineHandler lineHandler : lineHandlers) {
if (lineHandler != null) {
lineHandler.hide(lineHandler.players());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.translation.GlobalTranslator;
import net.megavex.scoreboardlibrary.implementation.commons.CollectionProvider;
import net.megavex.scoreboardlibrary.implementation.sidebar.line.locale.LineType;
import net.megavex.scoreboardlibrary.implementation.commons.LineRenderingStrategy;
import net.megavex.scoreboardlibrary.implementation.sidebar.line.locale.LegacyLocaleLine;
import net.megavex.scoreboardlibrary.implementation.sidebar.line.locale.LocaleLine;
import net.megavex.scoreboardlibrary.implementation.sidebar.line.locale.ModernLocaleLine;
import net.megavex.scoreboardlibrary.implementation.sidebar.line.locale.PostModernLocaleLine;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Set;

public class SidebarLineHandler {
private final LineType lineType;
private final LineRenderingStrategy strategy;
private final LocaleLineHandler localeLineHandler;
private final Set<Player> players = CollectionProvider.set(1);
private final LocaleLine<?>[] lines;
private final LocaleLine[] lines;

public SidebarLineHandler(@NotNull LineType lineType, @NotNull LocaleLineHandler localeLineHandler) {
this.lineType = lineType;
public SidebarLineHandler(@NotNull LineRenderingStrategy strategy, @NotNull LocaleLineHandler localeLineHandler) {
this.strategy = strategy;
this.localeLineHandler = localeLineHandler;
this.lines = new LocaleLine[localeLineHandler.sidebar().maxLines()];

for (GlobalLineInfo line : localeLineHandler.sidebar().lines()) {
if (line != null) {
Component value = line.value();
if (value != null) {
setLine(line.line(), GlobalTranslator.render(value, localeLineHandler.locale()), false);
setLine(line.line(), GlobalTranslator.render(value, localeLineHandler.locale()));
}
}
}
Expand All @@ -40,15 +43,8 @@ public SidebarLineHandler(@NotNull LineType lineType, @NotNull LocaleLineHandler
return players;
}

public void updateLine(int lineIndex) {
LocaleLine<?> line = lines[lineIndex];
if (line != null) {
line.updateTeam();
}
}

public void updateScores() {
for (LocaleLine<?> line : lines) {
for (LocaleLine line : lines) {
if (line != null && line.info().updateScore()) {
line.sendScore(players);
line.info().updateScore(false);
Expand All @@ -57,23 +53,19 @@ public void updateScores() {
}

public void updateScore(int line) {
LocaleLine<?> localeLine = lines[line];
LocaleLine localeLine = lines[line];
localeLine.sendScore(players);
}

public void setLine(int line, Component renderedLine) {
setLine(line, renderedLine, true);
}

private void setLine(int line, Component renderedLine, boolean sendPackets) {
LocaleLine<?> localeLine = lines[line];
LocaleLine localeLine = lines[line];
if (renderedLine == null && localeLine == null) {
return;
}

boolean newlyCreated = false;
if (localeLine == null) {
lines[line] = localeLine = lineType.create(localeLineHandler.sidebar().lines()[line], this);
lines[line] = localeLine = createLine(strategy, localeLineHandler.sidebar().lines()[line]);
newlyCreated = true;
}

Expand All @@ -83,28 +75,42 @@ private void setLine(int line, Component renderedLine, boolean sendPackets) {
localeLine.value(renderedLine);
}

if (sendPackets) {
if (renderedLine == null) {
localeLine.hide(players);
} else if (newlyCreated) {
localeLine.show(players);
}
if (renderedLine == null) {
localeLine.hide(players);
} else if (newlyCreated) {
localeLine.resetOldPlayer();
localeLine.show(players);
} else {
localeLine.updateTeam();
}
}

public void show(Collection<Player> players) {
for (LocaleLine<?> line : lines) {
for (LocaleLine line : lines) {
if (line != null) {
line.show(players);
}
}
}

public void hide(Collection<Player> players) {
for (LocaleLine<?> line : lines) {
for (LocaleLine line : lines) {
if (line != null) {
line.hide(players);
}
}
}

private @NotNull LocaleLine createLine(@NotNull LineRenderingStrategy strategy, @NotNull GlobalLineInfo line) {
switch (strategy) {
case LEGACY:
return new LegacyLocaleLine(line, this);
case MODERN:
return new ModernLocaleLine(line, this);
case POST_MODERN:
return new PostModernLocaleLine(line, this);
default:
throw new IllegalArgumentException();
}
}
}
Loading

0 comments on commit 5f89a47

Please sign in to comment.