Fix some instances of team-applied nametags not working

Fixes #3531
This commit is contained in:
Camotoy 2023-02-02 15:25:05 -05:00
parent c909b2b1a5
commit 0388785ea7
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
3 changed files with 25 additions and 33 deletions

View file

@ -100,6 +100,7 @@ public class PlayerEntity extends LivingEntity {
super(session, entityId, geyserId, uuid, EntityDefinitions.PLAYER, position, motion, yaw, pitch, headYaw); super(session, entityId, geyserId, uuid, EntityDefinitions.PLAYER, position, motion, yaw, pitch, headYaw);
this.username = username; this.username = username;
this.nametag = username;
this.texturesProperty = texturesProperty; this.texturesProperty = texturesProperty;
} }
@ -119,7 +120,7 @@ public class PlayerEntity extends LivingEntity {
} }
// The name can't be updated later (the entity metadata for it is ignored), so we need to check for this now // The name can't be updated later (the entity metadata for it is ignored), so we need to check for this now
updateDisplayName(null, false); updateDisplayName(session.getWorldCache().getScoreboard().getTeamFor(username));
AddPlayerPacket addPlayerPacket = new AddPlayerPacket(); AddPlayerPacket addPlayerPacket = new AddPlayerPacket();
addPlayerPacket.setUuid(uuid); addPlayerPacket.setUuid(uuid);
@ -315,19 +316,10 @@ public class PlayerEntity extends LivingEntity {
} }
//todo this will become common entity logic once UUID support is implemented for them //todo this will become common entity logic once UUID support is implemented for them
/** public void updateDisplayName(@Nullable Team team) {
* @param useGivenTeam even if there is no team, update the username in the entity metadata anyway, and don't look for a team
*/
public void updateDisplayName(@Nullable Team team, boolean useGivenTeam) {
if (team == null && !useGivenTeam) {
// Only search for the team if we are not supposed to use the given team
// If the given team is null, this is intentional that we are being removed from the team
team = session.getWorldCache().getScoreboard().getTeamFor(username);
}
boolean needsUpdate; boolean needsUpdate;
String newDisplayName = this.username;
if (team != null) { if (team != null) {
String newDisplayName;
if (team.isVisibleFor(session.getPlayerEntity().getUsername())) { if (team.isVisibleFor(session.getPlayerEntity().getUsername())) {
TeamColor color = team.getColor(); TeamColor color = team.getColor();
String chatColor = MessageTranslator.toChatColor(color); String chatColor = MessageTranslator.toChatColor(color);
@ -339,23 +331,16 @@ public class PlayerEntity extends LivingEntity {
// The name is not visible to the session player; clear name // The name is not visible to the session player; clear name
newDisplayName = ""; newDisplayName = "";
} }
needsUpdate = useGivenTeam && !newDisplayName.equals(nametag); needsUpdate = !newDisplayName.equals(this.nametag);
nametag = newDisplayName; this.nametag = newDisplayName;
dirtyMetadata.put(EntityData.NAMETAG, newDisplayName);
} else if (useGivenTeam) {
// The name has reset, if it was previously something else
needsUpdate = !newDisplayName.equals(nametag);
dirtyMetadata.put(EntityData.NAMETAG, this.username);
} else { } else {
needsUpdate = false; // The name has reset, if it was previously something else
needsUpdate = !this.nametag.equals(this.username);
this.nametag = this.username;
} }
if (needsUpdate) { if (needsUpdate) {
// Update the metadata as it won't be updated later dirtyMetadata.put(EntityData.NAMETAG, this.nametag);
SetEntityDataPacket packet = new SetEntityDataPacket();
packet.getMetadata().put(EntityData.NAMETAG, newDisplayName);
packet.setRuntimeEntityId(geyserId);
session.sendUpstreamPacket(packet);
} }
} }

View file

@ -30,6 +30,7 @@ import com.nukkitx.protocol.bedrock.data.ScoreInfo;
import com.nukkitx.protocol.bedrock.packet.RemoveObjectivePacket; import com.nukkitx.protocol.bedrock.packet.RemoveObjectivePacket;
import com.nukkitx.protocol.bedrock.packet.SetDisplayObjectivePacket; import com.nukkitx.protocol.bedrock.packet.SetDisplayObjectivePacket;
import com.nukkitx.protocol.bedrock.packet.SetScorePacket; import com.nukkitx.protocol.bedrock.packet.SetScorePacket;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import lombok.Getter; import lombok.Getter;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger; import org.geysermc.geyser.GeyserLogger;
@ -56,6 +57,13 @@ public final class Scoreboard {
@Getter @Getter
private final Map<ScoreboardPosition, Objective> objectiveSlots = new EnumMap<>(ScoreboardPosition.class); private final Map<ScoreboardPosition, Objective> objectiveSlots = new EnumMap<>(ScoreboardPosition.class);
private final Map<String, Team> teams = new ConcurrentHashMap<>(); // updated on multiple threads private final Map<String, Team> teams = new ConcurrentHashMap<>(); // updated on multiple threads
/**
* Required to preserve vanilla behavior, which also uses a map.
* Otherwise, for example, if TAB has a team for a player and vanilla has a team, "race conditions" that do not
* match vanilla could occur.
*/
@Getter
private final Map<String, Team> playerToTeam = new Object2ObjectOpenHashMap<>();
private int lastAddScoreCount = 0; private int lastAddScoreCount = 0;
private int lastRemoveScoreCount = 0; private int lastRemoveScoreCount = 0;
@ -333,12 +341,7 @@ public final class Scoreboard {
} }
public Team getTeamFor(String entity) { public Team getTeamFor(String entity) {
for (Team team : teams.values()) { return playerToTeam.get(entity);
if (team.hasEntity(entity)) {
return team;
}
}
return null;
} }
public void removeTeam(String teamName) { public void removeTeam(String teamName) {
@ -380,7 +383,8 @@ public final class Scoreboard {
for (Entity entity : session.getEntityCache().getEntities().values()) { for (Entity entity : session.getEntityCache().getEntities().values()) {
// This more complex logic is for the future to iterate over all entities, not just players // This more complex logic is for the future to iterate over all entities, not just players
if (entity instanceof PlayerEntity player && names.remove(player.getUsername())) { if (entity instanceof PlayerEntity player && names.remove(player.getUsername())) {
player.updateDisplayName(team, true); player.updateDisplayName(team);
player.updateBedrockMetadata();
if (names.isEmpty()) { if (names.isEmpty()) {
break; break;
} }
@ -396,7 +400,8 @@ public final class Scoreboard {
for (Entity entity : session.getEntityCache().getEntities().values()) { for (Entity entity : session.getEntityCache().getEntities().values()) {
if (entity instanceof PlayerEntity player) { if (entity instanceof PlayerEntity player) {
Team playerTeam = session.getWorldCache().getScoreboard().getTeamFor(player.getUsername()); Team playerTeam = session.getWorldCache().getScoreboard().getTeamFor(player.getUsername());
player.updateDisplayName(playerTeam, true); player.updateDisplayName(playerTeam);
player.updateBedrockMetadata();
} }
} }
} }

View file

@ -65,6 +65,7 @@ public final class Team {
if (entities.add(name)) { if (entities.add(name)) {
added.add(name); added.add(name);
} }
scoreboard.getPlayerToTeam().put(name, this);
} }
if (added.isEmpty()) { if (added.isEmpty()) {
@ -93,6 +94,7 @@ public final class Team {
if (entities.remove(name)) { if (entities.remove(name)) {
removed.add(name); removed.add(name);
} }
scoreboard.getPlayerToTeam().remove(name, this);
} }
return removed; return removed;
} }