Skip to content

Commit

Permalink
Fix Unknown player colors leaking between uses
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed Apr 27, 2024
1 parent 0c3afe9 commit c54dc21
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tc.oc.pgm.namedecorations;

import static net.kyori.adventure.text.Component.text;
import static tc.oc.pgm.util.player.PlayerRenderer.OFFLINE_COLOR;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
Expand Down Expand Up @@ -30,6 +31,7 @@
import tc.oc.pgm.events.PlayerPartyChangeEvent;
import tc.oc.pgm.util.named.NameDecorationProvider;
import tc.oc.pgm.util.player.PlayerComponent;
import tc.oc.pgm.util.player.PlayerRenderer;
import tc.oc.pgm.util.text.TextFormatter;

@SuppressWarnings("UnstableApiUsage")
Expand Down Expand Up @@ -124,7 +126,7 @@ public String getSuffix(UUID uuid) {

public TextColor getColor(UUID uuid) {
MatchPlayer player = PGM.get().getMatchManager().getPlayer(uuid);
if (player == null) return PlayerComponent.OFFLINE_COLOR;
if (player == null) return OFFLINE_COLOR;
return TextFormatter.convert(player.getParty().getColor());
}

Expand Down
29 changes: 13 additions & 16 deletions core/src/main/java/tc/oc/pgm/util/player/PlayerComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,23 @@
/** PlayerComponent is used to format player names in a consistent manner with optional styling */
public final class PlayerComponent implements RenderableComponent {

public static final TextColor OFFLINE_COLOR = NamedTextColor.DARK_AQUA;

public static final Component UNKNOWN =
translatable("misc.unknown", OFFLINE_COLOR, TextDecoration.ITALIC);
public static final Component CONSOLE = translatable("misc.console", OFFLINE_COLOR);
translatable("misc.unknown", PlayerRenderer.OFFLINE_COLOR, TextDecoration.ITALIC);
public static final Component CONSOLE = translatable("misc.console", PlayerRenderer.OFFLINE_COLOR);
public static final PlayerComponent UNKNOWN_PLAYER =
new PlayerComponent(null, new PlayerData(null, null, NameStyle.PLAIN));
new PlayerComponent(null, new PlayerData(null, null, NameStyle.SIMPLE_COLOR), Style.empty());

public static final PlayerRenderer RENDERER = new PlayerRenderer();

// The data for player being rendered.
private final @Nullable Player player;
private final @NotNull PlayerData data;
private final Style style;

private Style style = Style.empty();

private PlayerComponent(@Nullable Player player, @NotNull PlayerData data) {
private PlayerComponent(@Nullable Player player, @NotNull PlayerData data, Style style) {
this.player = player;
this.data = data;
this.style = style;
}

public static Component player(@Nullable UUID playerId, @NotNull NameStyle style) {
Expand All @@ -59,24 +57,24 @@ public static Component player(CommandSender sender, @NotNull NameStyle style) {

public static PlayerComponent player(Player player, @NotNull NameStyle style) {
if (player == null) return UNKNOWN_PLAYER;
return new PlayerComponent(player, new PlayerData(player, style));
return new PlayerComponent(player, new PlayerData(player, style), Style.empty());
}

public static Component player(
@Nullable Player player, @Nullable String username, @NotNull NameStyle style) {
if (player == null && username == null) return UNKNOWN_PLAYER;
return new PlayerComponent(player, new PlayerData(player, username, style));
return new PlayerComponent(player, new PlayerData(player, username, style), Style.empty());
}

public static PlayerComponent player(
@Nullable MatchPlayerState player, @NotNull NameStyle style) {
if (player == null) return UNKNOWN_PLAYER;
return new PlayerComponent(Bukkit.getPlayer(player.getId()), new PlayerData(player, style));
return new PlayerComponent(Bukkit.getPlayer(player.getId()), new PlayerData(player, style), Style.empty());
}

public static PlayerComponent player(@Nullable MatchPlayer player, @NotNull NameStyle style) {
if (player == null) return UNKNOWN_PLAYER;
return new PlayerComponent(player.getBukkit(), new PlayerData(player, style));
return new PlayerComponent(player.getBukkit(), new PlayerData(player, style), Style.empty());
}

public Component render(CommandSender viewer) {
Expand All @@ -90,8 +88,7 @@ public Component render(CommandSender viewer) {

@Override
public @NotNull RenderableComponent style(@NotNull Style style) {
this.style = style;
return this;
return new PlayerComponent(player, data, style);
}

@Override
Expand All @@ -101,7 +98,7 @@ public Component render(CommandSender viewer) {

@Override
public @NotNull RenderableComponent colorIfAbsent(@Nullable TextColor color) {
if (!data.style.has(NameStyle.Flag.COLOR)) style = style.colorIfAbsent(color);
return this;
if (data.style.has(NameStyle.Flag.COLOR) || style.color() != null) return this;
return new PlayerComponent(player, data, style.colorIfAbsent(color));
}
}
8 changes: 5 additions & 3 deletions core/src/main/java/tc/oc/pgm/util/player/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import tc.oc.pgm.api.player.MatchPlayerState;
import tc.oc.pgm.util.named.NameStyle;

import static tc.oc.pgm.util.player.PlayerRenderer.OFFLINE_COLOR;

class PlayerData {
public final @Nullable UUID uuid;

Expand All @@ -29,7 +31,7 @@ public PlayerData(@NotNull Player player, @NotNull NameStyle style) {
this.name = player.getName();
this.nick = Integration.getNick(player);
MatchPlayer mp = PGM.get().getMatchManager().getPlayer(player);
this.teamColor = mp == null ? PlayerComponent.OFFLINE_COLOR : mp.getParty().getTextColor();
this.teamColor = mp == null ? OFFLINE_COLOR : mp.getParty().getTextColor();
this.dead = mp != null && mp.isDead();
this.vanish = Integration.isVanished(player);
this.online = player.isOnline();
Expand All @@ -43,7 +45,7 @@ public PlayerData(@NotNull MatchPlayer mp, @NotNull NameStyle style) {
this.name = mp.getNameLegacy();
this.nick = Integration.getNick(mp.getBukkit());
this.teamColor =
mp.getParty() == null ? PlayerComponent.OFFLINE_COLOR : mp.getParty().getTextColor();
mp.getParty() == null ? OFFLINE_COLOR : mp.getParty().getTextColor();
this.dead = mp.isDead();
this.vanish = Integration.isVanished(mp.getBukkit());
this.online = mp.getBukkit().isOnline();
Expand Down Expand Up @@ -71,7 +73,7 @@ public PlayerData(@Nullable Player player, @Nullable String username, @NotNull N
this.nick = player != null ? Integration.getNick(player) : null;
// Null-check is relevant as MatchManager will be null when loading author names.
MatchPlayer mp = player != null ? PGM.get().getMatchManager().getPlayer(player) : null;
this.teamColor = mp == null ? PlayerComponent.OFFLINE_COLOR : mp.getParty().getTextColor();
this.teamColor = mp == null ? OFFLINE_COLOR : mp.getParty().getTextColor();
this.dead = mp != null && mp.isDead();
this.vanish = mp != null && Integration.isVanished(mp.getBukkit());
this.online = player != null && player.isOnline();
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/tc/oc/pgm/util/player/PlayerRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

@SuppressWarnings("UnstableApiUsage")
public class PlayerRenderer {
private static final TextColor DEAD_COLOR = NamedTextColor.DARK_GRAY;
public static final TextColor DEAD_COLOR = NamedTextColor.DARK_GRAY;
public static final TextColor OFFLINE_COLOR = NamedTextColor.DARK_AQUA;
private static final Style NICK_STYLE =
Style.style(TextDecoration.ITALIC).decoration(TextDecoration.STRIKETHROUGH, false);

Expand All @@ -44,7 +45,7 @@ public Component load(@NotNull PlayerCacheKey key) {
});
}

public Component render(PlayerData data, PlayerRelationship relation) {
Component render(PlayerData data, PlayerRelationship relation) {
return nameCache.getUnchecked(new PlayerCacheKey(data, relation));
}

Expand All @@ -70,7 +71,7 @@ private Component render(PlayerCacheKey key) {
boolean disguised = (data.nick != null || data.vanish);

if (!data.online || (data.conceal && disguised && !relation.reveal)) {
return text(data.name, PlayerComponent.OFFLINE_COLOR);
return text(data.name, OFFLINE_COLOR);
}

String plName = relation.reveal || data.nick == null ? data.name : data.nick;
Expand Down

0 comments on commit c54dc21

Please sign in to comment.