Skip to content

Commit

Permalink
Fix DisplaySlot tracking when overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Jun 22, 2024
1 parent c82553a commit cbeb5cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private VelocityObjective(@NotNull VelocityScoreboard scoreboard, @NotNull Strin
this.healthDisplay = healthDisplay;
this.numberFormat = numberFormat;
this.displaySlot = displaySlot;
if (displaySlot != null) scoreboard.setDisplaySlot(displaySlot, this);
}

@NotNull
Expand Down Expand Up @@ -94,6 +95,7 @@ public DisplaySlot getDisplaySlot() {
public void setDisplaySlot(@NotNull DisplaySlot displaySlot) {
checkState();
if (this.displaySlot == displaySlot) return;
scoreboard.setDisplaySlot(displaySlot, this);
this.displaySlot = displaySlot;
for (ConnectedPlayer player : scoreboard.getPlayers()) {
player.getConnection().write(new DisplayObjectivePacket(scoreboard.getPriority(), displaySlot, name));
Expand Down Expand Up @@ -191,6 +193,10 @@ public void unregister() {
sendUnregister(scoreboard.getPlayers());
}

public void clearDisplaySlot() {
this.displaySlot = null;
}

@NotNull
public Collection<VelocityScore> getScores() {
return scores.values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.scoreboard.DisplaySlot;
import com.velocitypowered.api.scoreboard.Objective;
import com.velocitypowered.api.scoreboard.Scoreboard;
import com.velocitypowered.api.scoreboard.Team;
Expand All @@ -45,6 +46,7 @@ public class VelocityScoreboard implements Scoreboard {
private final Collection<ConnectedPlayer> players = new HashSet<>();
private final Map<String, VelocityObjective> objectives = new ConcurrentHashMap<>();
private final Map<String, VelocityTeam> teams = new ConcurrentHashMap<>();
private final EnumMap<DisplaySlot, VelocityObjective> displaySlots = new EnumMap<>(DisplaySlot.class);

public VelocityScoreboard(int priority, @NotNull ProxyServer server) {
this.priority = priority;
Expand Down Expand Up @@ -151,4 +153,8 @@ public void unregisterTeam(@NotNull String teamName) {
teams.remove(teamName).sendUnregister(players);
}

public void setDisplaySlot(@NotNull DisplaySlot displaySlot, @NotNull VelocityObjective objective) {
VelocityObjective previous = displaySlots.put(displaySlot, objective);
if (previous != null) previous.clearDisplaySlot();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public DownstreamObjective(@NotNull String objectiveName, @NotNull TextHolder ti
this.displaySlot = displaySlot;
}

public void setDisplaySlot(@NotNull DisplaySlot displaySlot) {
public void setDisplaySlot(@Nullable DisplaySlot displaySlot) {
this.displaySlot = displaySlot;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@
package com.velocitypowered.proxy.scoreboard.downstream;

import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.scoreboard.DisplaySlot;
import com.velocitypowered.api.scoreboard.NumberFormat;
import com.velocitypowered.proxy.data.PacketLogger;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import com.velocitypowered.proxy.protocol.packet.scoreboard.*;
import com.velocitypowered.proxy.scoreboard.VelocityObjective;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

public class DownstreamScoreboard {

private final Map<String, DownstreamObjective> objectives = new HashMap<>();
private final Map<String, DownstreamTeam> teams = new HashMap<>();
private final EnumMap<DisplaySlot, DownstreamObjective> displaySlots = new EnumMap<>(DisplaySlot.class);
@NotNull private final Player viewer;

public DownstreamScoreboard(@NotNull Player viewer) {
Expand Down Expand Up @@ -76,6 +80,8 @@ public void handle(@NotNull DisplayObjectivePacket packet) {
if (objective == null) {
PacketLogger.invalidDownstreamPacket("Cannot set display slot of unknown objective " + packet.getObjectiveName());
} else {
DownstreamObjective previous = displaySlots.put(packet.getPosition(), objective);
if (previous != null) previous.setDisplaySlot(null);
objective.setDisplaySlot(packet.getPosition());
}
}
Expand Down

0 comments on commit cbeb5cb

Please sign in to comment.