Ignore all entity metadata for player names

The Java client ignores this; only the GameProfile of the player can set this.

Fixes #2563
This commit is contained in:
Camotoy 2021-10-10 14:54:06 -04:00
parent bf1359cf0c
commit 265c42fe09
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
3 changed files with 23 additions and 28 deletions

View file

@ -41,7 +41,6 @@ import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.geysermc.connector.entity.player.PlayerEntity;
import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.chat.MessageTranslator; import org.geysermc.connector.network.translators.chat.MessageTranslator;
@ -51,7 +50,7 @@ import org.geysermc.connector.utils.MathUtils;
@Setter @Setter
public class Entity { public class Entity {
protected long entityId; protected long entityId;
protected long geyserId; protected final long geyserId;
protected Vector3f position; protected Vector3f position;
protected Vector3f motion; protected Vector3f motion;
@ -258,8 +257,7 @@ public class Entity {
setDisplayName(session, (Component) entityMetadata.getValue()); setDisplayName(session, (Component) entityMetadata.getValue());
break; break;
case 3: // is custom name visible case 3: // is custom name visible
if (!this.is(PlayerEntity.class)) setDisplayNameVisible(entityMetadata);
metadata.put(EntityData.NAMETAG_ALWAYS_SHOW, (byte) ((boolean) entityMetadata.getValue() ? 1 : 0));
break; break;
case 4: // silent case 4: // silent
metadata.getFlags().setFlag(EntityFlag.SILENT, (boolean) entityMetadata.getValue()); metadata.getFlags().setFlag(EntityFlag.SILENT, (boolean) entityMetadata.getValue());
@ -306,19 +304,18 @@ public class Entity {
return false; return false;
} }
/** protected void setDisplayName(GeyserSession session, Component name) {
* @return the translated string display
*/
protected String setDisplayName(GeyserSession session, Component name) {
if (name != null) { if (name != null) {
String displayName = MessageTranslator.convertMessage(name, session.getLocale()); String displayName = MessageTranslator.convertMessage(name, session.getLocale());
metadata.put(EntityData.NAMETAG, displayName); metadata.put(EntityData.NAMETAG, displayName);
return displayName;
} else if (!metadata.getString(EntityData.NAMETAG).isEmpty()) { } else if (!metadata.getString(EntityData.NAMETAG).isEmpty()) {
// Clear nametag // Clear nametag
metadata.put(EntityData.NAMETAG, ""); metadata.put(EntityData.NAMETAG, "");
} }
return null; }
protected void setDisplayNameVisible(EntityMetadata entityMetadata) {
metadata.put(EntityData.NAMETAG_ALWAYS_SHOW, (byte) ((boolean) entityMetadata.getValue() ? 1 : 0));
} }
/** /**
@ -326,6 +323,7 @@ public class Entity {
*/ */
protected void setDimensions(Pose pose) { protected void setDimensions(Pose pose) {
// No flexibility options for basic entities // No flexibility options for basic entities
//TODO don't even set this for basic entities since we already set it on entity initialization
metadata.put(EntityData.BOUNDING_BOX_WIDTH, entityType.getWidth()); metadata.put(EntityData.BOUNDING_BOX_WIDTH, entityType.getWidth());
metadata.put(EntityData.BOUNDING_BOX_HEIGHT, entityType.getHeight()); metadata.put(EntityData.BOUNDING_BOX_HEIGHT, entityType.getHeight());
} }

View file

@ -40,7 +40,6 @@ import com.nukkitx.protocol.bedrock.data.entity.EntityData;
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag; import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import com.nukkitx.protocol.bedrock.data.entity.EntityLinkData; import com.nukkitx.protocol.bedrock.data.entity.EntityLinkData;
import com.nukkitx.protocol.bedrock.packet.*; import com.nukkitx.protocol.bedrock.packet.*;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -68,9 +67,6 @@ public class PlayerEntity extends LivingEntity {
private String username; private String username;
private boolean playerList = true; // Player is in the player list private boolean playerList = true; // Player is in the player list
@Getter(AccessLevel.NONE)
private String displayName;
/** /**
* Saves the parrot currently on the player's left shoulder; otherwise null * Saves the parrot currently on the player's left shoulder; otherwise null
*/ */
@ -86,7 +82,6 @@ public class PlayerEntity extends LivingEntity {
profile = gameProfile; profile = gameProfile;
uuid = gameProfile.getId(); uuid = gameProfile.getId();
username = gameProfile.getName(); username = gameProfile.getName();
displayName = username;
// For the OptionalPack, set all bits as invisible by default as this matches Java Edition behavior // For the OptionalPack, set all bits as invisible by default as this matches Java Edition behavior
metadata.put(EntityData.MARK_VARIANT, 0xff); metadata.put(EntityData.MARK_VARIANT, 0xff);
@ -100,6 +95,9 @@ public class PlayerEntity extends LivingEntity {
setBelowNameText(session, objective); setBelowNameText(session, objective);
} }
// The name can't be updated later (the entity metadata for it is ignored), so we need to check for this now
updateDisplayName(session, null, false);
AddPlayerPacket addPlayerPacket = new AddPlayerPacket(); AddPlayerPacket addPlayerPacket = new AddPlayerPacket();
addPlayerPacket.setUuid(uuid); addPlayerPacket.setUuid(uuid);
addPlayerPacket.setUsername(username); addPlayerPacket.setUsername(username);
@ -311,13 +309,8 @@ public class PlayerEntity extends LivingEntity {
} }
@Override @Override
protected String setDisplayName(GeyserSession session, Component name) { protected void setDisplayName(GeyserSession session, Component name) {
String displayName = super.setDisplayName(session, name); // Doesn't do anything for players
this.displayName = displayName != null ? displayName : username;
// Update if we know this player has a team
updateDisplayName(session, null, false);
return this.displayName;
} }
//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
@ -332,7 +325,7 @@ public class PlayerEntity extends LivingEntity {
} }
boolean needsUpdate; boolean needsUpdate;
String newDisplayName = this.displayName; String newDisplayName = this.username;
if (team != null) { if (team != null) {
if (team.isVisibleFor(session.getPlayerEntity().getUsername())) { if (team.isVisibleFor(session.getPlayerEntity().getUsername())) {
TeamColor color = team.getColor(); TeamColor color = team.getColor();
@ -345,7 +338,7 @@ public class PlayerEntity extends LivingEntity {
// We have to emulate what modern Java text already does for us and add the color to each section // We have to emulate what modern Java text already does for us and add the color to each section
String prefix = team.getCurrentData().getPrefix(); String prefix = team.getCurrentData().getPrefix();
String suffix = team.getCurrentData().getSuffix(); String suffix = team.getCurrentData().getSuffix();
newDisplayName = chatColor + prefix + chatColor + this.displayName + chatColor + suffix; newDisplayName = chatColor + prefix + chatColor + this.username + chatColor + suffix;
} else { } else {
// The name is not visible to the session player; clear name // The name is not visible to the session player; clear name
newDisplayName = ""; newDisplayName = "";
@ -355,7 +348,7 @@ public class PlayerEntity extends LivingEntity {
} else if (useGivenTeam) { } else if (useGivenTeam) {
// The name has reset, if it was previously something else // The name has reset, if it was previously something else
needsUpdate = !newDisplayName.equals(metadata.getString(EntityData.NAMETAG)); needsUpdate = !newDisplayName.equals(metadata.getString(EntityData.NAMETAG));
metadata.put(EntityData.NAMETAG, this.displayName); metadata.put(EntityData.NAMETAG, this.username);
} else { } else {
needsUpdate = false; needsUpdate = false;
} }
@ -369,6 +362,11 @@ public class PlayerEntity extends LivingEntity {
} }
} }
@Override
protected void setDisplayNameVisible(EntityMetadata entityMetadata) {
// Doesn't do anything for players
}
@Override @Override
protected void setDimensions(Pose pose) { protected void setDimensions(Pose pose) {
float height; float height;

View file

@ -25,13 +25,12 @@
package org.geysermc.connector.network.translators.java.entity; package org.geysermc.connector.network.translators.java.entity;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityMetadataPacket;
import org.geysermc.connector.entity.Entity; import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator; import org.geysermc.connector.network.translators.Translator;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityMetadataPacket;
import org.geysermc.connector.utils.InteractiveTagManager; import org.geysermc.connector.utils.InteractiveTagManager;
import org.geysermc.connector.utils.LanguageUtils; import org.geysermc.connector.utils.LanguageUtils;