diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 375a85630..55d915786 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -59,6 +59,7 @@ public class Player extends LivingEntity { private ConcurrentLinkedQueue 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() { diff --git a/src/main/java/net/minestom/server/entity/hologram/Hologram.java b/src/main/java/net/minestom/server/entity/hologram/Hologram.java index 913c96709..b7e0fc8ad 100644 --- a/src/main/java/net/minestom/server/entity/hologram/Hologram.java +++ b/src/main/java/net/minestom/server/entity/hologram/Hologram.java @@ -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 { diff --git a/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java b/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java index ac3a49b90..118fce5f7 100644 --- a/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java @@ -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); diff --git a/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java index 700a1b9a7..055b454e6 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java @@ -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; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java index 4bc8fa098..5254a90fc 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java @@ -71,7 +71,6 @@ public class PlayerInfoPacket implements ServerPacket { public ArrayList 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); } } diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index a0db22a5b..33a4e24e8 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -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; } }