Added Player#getDisplayName + some optimizations/improvements

This commit is contained in:
Felix Cravic 2020-05-26 15:35:48 +02:00
parent fc751acb75
commit b797967151
6 changed files with 54 additions and 16 deletions

View File

@ -59,6 +59,7 @@ public class Player extends LivingEntity {
private ConcurrentLinkedQueue<ClientPlayPacket> packets = new ConcurrentLinkedQueue<>();
private int latency;
private String displayName;
private Dimension dimension;
private GameMode gameMode;
@ -156,6 +157,10 @@ public class Player extends LivingEntity {
MinecraftServer.getEntityManager().addWaitingPlayer(this);
}
/**
* Used when the player is created
* Init the player and spawn him
*/
protected void init() {
GameMode gameMode = GameMode.SURVIVAL;
Dimension dimension = Dimension.OVERWORLD;
@ -194,11 +199,13 @@ public class Player extends LivingEntity {
playerConnection.sendPacket(spawnPositionPacket);
// Add player to list
String property = "eyJ0aW1lc3RhbXAiOjE1NjU0ODMwODQwOTYsInByb2ZpbGVJZCI6ImFiNzBlY2I0MjM0NjRjMTRhNTJkN2EwOTE1MDdjMjRlIiwicHJvZmlsZU5hbWUiOiJUaGVNb2RlOTExIiwidGV4dHVyZXMiOnsiU0tJTiI6eyJ1cmwiOiJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2RkOTE2NzJiNTE0MmJhN2Y3MjA2ZTRjN2IwOTBkNzhlM2Y1ZDc2NDdiNWFmZDIyNjFhZDk4OGM0MWI2ZjcwYTEifX19";
String jsonDisplayName = displayName != null ? Chat.toJsonString(Chat.fromLegacyText(displayName)) : null;
String property = "";
PlayerInfoPacket playerInfoPacket = new PlayerInfoPacket(PlayerInfoPacket.Action.ADD_PLAYER);
PlayerInfoPacket.AddPlayer addPlayer = new PlayerInfoPacket.AddPlayer(getUuid(), username, getGameMode(), 10);
PlayerInfoPacket.AddPlayer.Property prop = new PlayerInfoPacket.AddPlayer.Property("textures", property); //new PlayerInfoPacket.AddPlayer.Property("textures", properties.get(username));
addPlayer.properties.add(prop);
PlayerInfoPacket.AddPlayer addPlayer = new PlayerInfoPacket.AddPlayer(getUuid(), getUsername(), getGameMode(), getLatency());
addPlayer.displayName = jsonDisplayName;
//PlayerInfoPacket.AddPlayer.Property prop = new PlayerInfoPacket.AddPlayer.Property("textures", property);
//addPlayer.properties.add(prop);
playerInfoPacket.playerInfos.add(addPlayer);
playerConnection.sendPacket(playerInfoPacket);
@ -780,6 +787,31 @@ public class Player extends LivingEntity {
Check.notNull(gameMode, "GameMode cannot be null");
this.gameMode = gameMode;
sendChangeGameStatePacket(ChangeGameStatePacket.Reason.CHANGE_GAMEMODE, gameMode.getId());
PlayerInfoPacket infoPacket = new PlayerInfoPacket(PlayerInfoPacket.Action.UPDATE_GAMEMODE);
infoPacket.playerInfos.add(new PlayerInfoPacket.UpdateGamemode(getUuid(), gameMode));
sendPacketToViewersAndSelf(infoPacket);
}
/**
* @return the displayed name of the player in the tab-list,
* null means that {@link #getUsername()} is display
*/
public String getDisplayName() {
return displayName;
}
/**
* @param displayName the new displayed name of the player in the tab-list,
* set to null to show the player username
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
String jsonDisplayName = displayName != null ? Chat.toJsonString(Chat.fromLegacyText(displayName)) : null;
PlayerInfoPacket infoPacket = new PlayerInfoPacket(PlayerInfoPacket.Action.UPDATE_DISPLAY_NAME);
infoPacket.playerInfos.add(new PlayerInfoPacket.UpdateDisplayName(getUuid(), jsonDisplayName));
sendPacketToViewersAndSelf(infoPacket);
}
public boolean isEnableRespawnScreen() {

View File

@ -34,6 +34,7 @@ public class Hologram {
}
public void setPosition(Position position) {
checkRemoved();
position = position.add(0, OFFSET_Y, 0);
this.position = position;
this.entity.teleport(position);
@ -54,8 +55,12 @@ public class Hologram {
this.entity.remove();
}
public boolean isRemoved() {
return removed;
}
private void checkRemoved() {
Check.stateCondition(removed, "You cannot interact with a removed Hologram");
Check.stateCondition(isRemoved(), "You cannot interact with a removed Hologram");
}
private class HologramEntity extends EntityArmorStand {

View File

@ -16,9 +16,10 @@ public class LoginStartPacket implements ClientPreplayPacket {
@Override
public void process(PlayerConnection connection, ConnectionManager connectionManager) {
// TODO send encryption request OR directly login success
UUID playerUuid = UUID.randomUUID();//UUID.fromString("OfflinePlayer:" + username);
UUID adam = UUID.fromString("58ffa9d8-aee1-4587-8b79-41b754f6f238");
UUID playerUuid = UUID.fromString("ab70ecb4-2346-4c14-a52d-7a091507c24e");//UUID.randomUUID();
LoginSuccessPacket successPacket = new LoginSuccessPacket(playerUuid, username);//new LoginSuccessPacket(uuids.get(username), username);
LoginSuccessPacket successPacket = new LoginSuccessPacket(playerUuid, username);
connection.sendPacket(successPacket);
connection.setConnectionState(ConnectionState.PLAY);

View File

@ -10,11 +10,6 @@ public class LoginSuccessPacket implements ServerPacket {
public UUID uuid;
public String username;
public LoginSuccessPacket(String username) {
this.uuid = UUID.randomUUID();
this.username = username;
}
public LoginSuccessPacket(UUID uuid, String username) {
this.uuid = uuid;
this.username = username;

View File

@ -71,7 +71,6 @@ public class PlayerInfoPacket implements ServerPacket {
public ArrayList<Property> properties;
public GameMode gameMode;
public int ping;
public boolean hasDisplayName = false;
public String displayName;
public AddPlayer(UUID uuid, String name, GameMode gameMode, int ping) {
@ -91,6 +90,8 @@ public class PlayerInfoPacket implements ServerPacket {
}
writer.writeVarInt(gameMode.getId());
writer.writeVarInt(ping);
final boolean hasDisplayName = displayName != null;
writer.writeBoolean(hasDisplayName);
if (hasDisplayName)
writer.writeSizedString(displayName);
@ -159,8 +160,10 @@ public class PlayerInfoPacket implements ServerPacket {
@Override
public void write(PacketWriter writer) {
writer.writeBoolean(true); // ????
writer.writeSizedString(displayName);
final boolean hasDisplayName = displayName != null;
writer.writeBoolean(hasDisplayName);
if (hasDisplayName)
writer.writeSizedString(displayName);
}
}

View File

@ -24,9 +24,11 @@ public class PacketUtils {
Utils.writeVarIntBuf(buffer, length);
buffer.writeBytes(bytes);
//if(!(serverPacket instanceof ChunkDataPacket) && !(serverPacket instanceof PlayerListHeaderAndFooterPacket))
//System.out.println("WRITE PACKET: " + id + " " + serverPacket.getClass().getSimpleName());
return Unpooled.copiedBuffer(buffer);
//Unpooled.copiedBuffer(buffer);
return buffer;
}
}