Read player skin from velocity

This commit is contained in:
themode 2020-11-14 22:13:01 +01:00
parent 1d1e82aeb3
commit ae8a182eb8
6 changed files with 61 additions and 9 deletions

View File

@ -220,7 +220,7 @@ public class Player extends LivingEntity implements CommandSender {
playerConnection.sendPacket(spawnPositionPacket);
// Add player to list with spawning skin
PlayerSkinInitEvent skinInitEvent = new PlayerSkinInitEvent(this);
PlayerSkinInitEvent skinInitEvent = new PlayerSkinInitEvent(this, skin);
callEvent(PlayerSkinInitEvent.class, skinInitEvent);
this.skin = skinInitEvent.getSkin();
playerConnection.sendPacket(getAddPlayerToList());
@ -1154,6 +1154,9 @@ public class Player extends LivingEntity implements CommandSender {
public synchronized void setSkin(@Nullable PlayerSkin skin) {
this.skin = skin;
if (instance == null)
return;
DestroyEntitiesPacket destroyEntitiesPacket = new DestroyEntitiesPacket();
destroyEntitiesPacket.entityIds = new int[]{getEntityId()};

View File

@ -14,8 +14,9 @@ public class PlayerSkinInitEvent extends Event {
private final Player player;
private PlayerSkin skin;
public PlayerSkinInitEvent(@NotNull Player player) {
public PlayerSkinInitEvent(@NotNull Player player, @Nullable PlayerSkin currentSkin) {
this.player = player;
this.skin = currentSkin;
}
/**

View File

@ -2,6 +2,7 @@ package net.minestom.server.extras.velocity;
import com.google.common.net.InetAddresses;
import io.netty.buffer.ByteBuf;
import net.minestom.server.entity.PlayerSkin;
import net.minestom.server.utils.binary.BinaryReader;
import org.jetbrains.annotations.NotNull;
@ -75,4 +76,27 @@ public final class VelocityProxy {
return InetAddresses.forString(reader.readSizedString(Integer.MAX_VALUE));
}
public static PlayerSkin readSkin(@NotNull BinaryReader reader) {
String skinTexture = null;
String skinSignature = null;
final int properties = reader.readVarInt();
for (int i1 = 0; i1 < properties; i1++) {
final String name = reader.readSizedString(Short.MAX_VALUE);
final String value = reader.readSizedString(Short.MAX_VALUE);
final String signature = reader.readBoolean() ? reader.readSizedString(Short.MAX_VALUE) : null;
if (name.equals("textures")) {
skinTexture = value;
skinSignature = signature;
}
}
if (skinTexture != null && skinSignature != null) {
return new PlayerSkin(skinTexture, skinSignature);
} else {
return null;
}
}
}

View File

@ -188,6 +188,8 @@ public final class ConnectionManager {
* Changes how {@link UUID} are attributed to players.
* <p>
* Shouldn't be override if already defined.
* <p>
* Be aware that it is possible for an UUID provider to be ignored, for example in the case of a proxy (eg: velocity).
*
* @param uuidProvider the new player connection uuid provider,
* setting it to null would apply a random UUID for each player connection
@ -277,10 +279,13 @@ public final class ConnectionManager {
* @param uuid the new player uuid
* @param username the new player username
* @param connection the new player connection
* @return the newly created player object
*/
public void createPlayer(@NotNull UUID uuid, @NotNull String username, @NotNull PlayerConnection connection) {
@NotNull
public Player createPlayer(@NotNull UUID uuid, @NotNull String username, @NotNull PlayerConnection connection) {
final Player player = getPlayerProvider().createPlayer(uuid, username, connection);
createPlayer(player);
return player;
}
/**
@ -306,12 +311,14 @@ public final class ConnectionManager {
* @param connection the player connection
* @param uuid the uuid of the player
* @param username the username of the player
* @return the newly created player object
*/
public void startPlayState(@NotNull PlayerConnection connection, @NotNull UUID uuid, @NotNull String username) {
@NotNull
public Player startPlayState(@NotNull PlayerConnection connection, @NotNull UUID uuid, @NotNull String username) {
LoginSuccessPacket loginSuccessPacket = new LoginSuccessPacket(uuid, username);
connection.sendPacket(loginSuccessPacket);
connection.setConnectionState(ConnectionState.PLAY);
createPlayer(uuid, username, connection);
return createPlayer(uuid, username, connection);
}
}

View File

@ -3,6 +3,8 @@ package net.minestom.server.network.packet.client.login;
import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.PlayerSkin;
import net.minestom.server.extras.velocity.VelocityProxy;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.packet.client.ClientPreplayPacket;
@ -37,7 +39,11 @@ public class LoginPluginResponsePacket implements ClientPreplayPacket {
if (channel != null) {
boolean success = false;
SocketAddress socketAddress = null;
UUID playerUuid = null;
String playerUsername = null;
PlayerSkin playerSkin = null;
// Velocity
if (VelocityProxy.isEnabled() && channel.equals(VelocityProxy.PLAYER_INFO_CHANNEL)) {
@ -49,6 +55,12 @@ public class LoginPluginResponsePacket implements ClientPreplayPacket {
final InetAddress address = VelocityProxy.readAddress(reader);
final int port = ((java.net.InetSocketAddress) connection.getRemoteAddress()).getPort();
socketAddress = new InetSocketAddress(address, port);
playerUuid = reader.readUuid();
playerUsername = reader.readSizedString(16);
playerSkin = VelocityProxy.readSkin(reader);
}
}
}
@ -57,12 +69,16 @@ public class LoginPluginResponsePacket implements ClientPreplayPacket {
if (socketAddress != null) {
nettyPlayerConnection.setRemoteAddress(socketAddress);
}
if (playerUsername != null) {
nettyPlayerConnection.UNSAFE_setLoginUsername(playerUsername);
}
// Proxy usage always mean that the server is in offline mode
final String username = nettyPlayerConnection.getLoginUsername();
final UUID playerUuid = CONNECTION_MANAGER.getPlayerConnectionUuid(connection, username);
final UUID uuid = playerUuid != null ?
playerUuid : CONNECTION_MANAGER.getPlayerConnectionUuid(connection, username);
CONNECTION_MANAGER.startPlayState(connection, playerUuid, username);
Player player = CONNECTION_MANAGER.startPlayState(connection, uuid, username);
player.setSkin(playerSkin);
} else {
LoginDisconnectPacket disconnectPacket = new LoginDisconnectPacket(INVALID_PROXY_RESPONSE);
nettyPlayerConnection.sendPacket(disconnectPacket);

View File

@ -7,6 +7,7 @@ import demo.commands.*;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandManager;
import net.minestom.server.extras.optifine.OptifineSupport;
import net.minestom.server.extras.velocity.VelocityProxy;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule;
import net.minestom.server.storage.StorageManager;
@ -51,7 +52,7 @@ public class Main {
OptifineSupport.enable();
//VelocityProxy.enable("rBeJJ79W4MVU");
VelocityProxy.enable("rBeJJ79W4MVU");
//BungeeCordProxy.enable();
// MojangAuth.init();