Skip to content

Commit

Permalink
[Scoreboard] Add startup warn if trying to use NumberFormat on <1.20.…
Browse files Browse the repository at this point in the history
…3 server and runtime warn when trying to send it to <1.20.3 player
  • Loading branch information
NEZNAMY committed Nov 3, 2024
1 parent c15a600 commit 380b3dd
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ public TabList createTabList(@NotNull TabPlayer player) {
return TabListBase.getInstance().apply((BukkitTabPlayer) player);
}

@Override
public boolean supportsNumberFormat() {
return serverVersion.getNetworkId() >= ProtocolVersion.V1_20_3.getNetworkId();
}

@Override
public boolean supportsListOrder() {
return serverVersion.getNetworkId() >= ProtocolVersion.V1_21_2.getNetworkId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ public TabList createTabList(@NotNull TabPlayer player) {
}
}

@Override
public boolean supportsNumberFormat() {
return true;
}

@Override
public boolean supportsListOrder() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ public TabList createTabList(@NotNull TabPlayer player) {
return new FabricTabList((FabricTabPlayer) player);
}

@Override
public boolean supportsNumberFormat() {
return serverVersion.getNetworkId() >= ProtocolVersion.V1_20_3.getNetworkId();
}

@Override
public boolean supportsListOrder() {
return serverVersion.getNetworkId() >= ProtocolVersion.V1_21_2.getNetworkId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.config.file.ConfigurationSection;
import me.neznamy.tab.shared.features.PlaceholderManagerImpl;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -120,6 +121,19 @@ public static ScoreboardDefinition fromSection(@NotNull String name, @NotNull Co
name, lines.size(), alwaysVisibleLines));
}

// Check if using NumberFormat on <1.20.3 server
boolean found = false;
for (String line : lines) {
if (line.contains("||")) {
found = true;
break;
}
}
if (found && !TAB.getInstance().getPlatform().supportsNumberFormat()) {
section.startupWarn("Scoreboard \"" + name + "\" is using right-side text alignment (using ||) in the lines, however, your server does not " +
"support this feature. It was added into the game in version 1.20.3. Any text defined after || in lines will not be displayed.");
}

return new ScoreboardDefinition(
section.getString("display-condition"),
section.getString("title", "<Title is not defined>"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.NonNull;
import me.neznamy.tab.api.scoreboard.Line;
import me.neznamy.tab.shared.Property;
import me.neznamy.tab.shared.ProtocolVersion;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.chat.SimpleComponent;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class ScoreboardImpl extends RefreshableFeature implements me.neznamy.tab
//lines of scoreboard
private final List<Line> lines = new ArrayList<>();

private boolean containsNumberFormat;

//players currently seeing this scoreboard
private final Set<TabPlayer> players = Collections.newSetFromMap(new WeakHashMap<>());

Expand Down Expand Up @@ -83,6 +86,7 @@ public ScoreboardImpl(@NonNull ScoreboardManagerImpl manager, @NonNull String na
for (int i = 0; i< definition.getLines().size(); i++) {
String line = definition.getLines().get(i);
if (line == null) line = "";
if (line.contains("||")) containsNumberFormat = true;
ScoreboardLine score;
if (dynamicLinesOnly) {
score = new StableDynamicLine(this, i+1, line);
Expand Down Expand Up @@ -149,6 +153,11 @@ public void addPlayer(@NonNull TabPlayer p) {
p.scoreboardData.activeScoreboard = this;
recalculateScores(p);
TAB.getInstance().getPlaceholderManager().getTabExpansion().setScoreboardName(p, name);
if (containsNumberFormat && p.getVersion().getNetworkId() < ProtocolVersion.V1_20_3.getNetworkId()) {
TAB.getInstance().getConfigHelper().runtime().error("Scoreboard \"" + name + "\" contains right-side text alignment (using ||), however, this feature " +
"was added in 1.20.3, but player \"" + p.getName() + "\" is using version " + p.getVersion().getFriendlyName() + ". Right-side text " +
"will not be visible for them.");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public interface Platform {
@NotNull
TabList createTabList(@NotNull TabPlayer player);

/**
* Returns {@code true} if server is able to use {@code NumberFormat} scoreboard feature (1.20.3+). Returns {@code false}
* if server is running below this version (backend) or server API does not support it yet.
*
* @return {@code true} if server is able to use {@code NumberFormat} scoreboard feature, {@code false} if not
*/
boolean supportsNumberFormat();

/**
* Returns {@code true} if server is able to use {@code listOrder} field in tablist (1.21.2+). Returns {@code false}
* if server is running below this version (backend) or server API does not support it yet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public TabList createTabList(@NotNull TabPlayer player) {
return new SpongeTabList((SpongeTabPlayer) player);
}

@Override
public boolean supportsNumberFormat() {
return false; // Sponge 7 only goes up to 1.12.2
}

@Override
public boolean supportsListOrder() {
return false; // Sponge 7 only goes up to 1.12.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public TabList createTabList(@NotNull TabPlayer player) {
return new SpongeTabList((SpongeTabPlayer) player);
}

@Override
public boolean supportsNumberFormat() {
return false; // TODO implement it
}

@Override
public boolean supportsListOrder() {
return false; // TODO when they add API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public TabList createTabList(@NotNull TabPlayer player) {
return new VelocityTabList((VelocityTabPlayer) player);
}

@Override
public boolean supportsNumberFormat() {
return true;
}

@Override
public boolean supportsListOrder() {
return false; // TODO when they add API
Expand Down

0 comments on commit 380b3dd

Please sign in to comment.