mirror of
https://github.com/PaperMC/Velocity.git
synced 2026-02-17 06:27:42 +01:00
convert some more packets to records
This commit is contained in:
@@ -55,7 +55,6 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.JoinConfiguration;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.TranslatableComponent;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
|
||||
@@ -158,7 +158,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
|
||||
|
||||
@Override
|
||||
public boolean handle(KeepAlivePacket packet) {
|
||||
serverConn.getPendingPings().put(packet.getRandomId(), System.nanoTime());
|
||||
serverConn.getPendingPings().put(packet.randomId(), System.nanoTime());
|
||||
return false; // forwards on
|
||||
}
|
||||
|
||||
@@ -178,10 +178,10 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
|
||||
@Override
|
||||
public boolean handle(BossBarPacket packet) {
|
||||
if (serverConn.getPlayer().getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_20_2)) {
|
||||
if (packet.getAction() == BossBarPacket.ADD) {
|
||||
playerSessionHandler.getServerBossBars().add(packet.getUuid());
|
||||
} else if (packet.getAction() == BossBarPacket.REMOVE) {
|
||||
playerSessionHandler.getServerBossBars().remove(packet.getUuid());
|
||||
if (packet.action() == BossBarPacket.ADD) {
|
||||
playerSessionHandler.getServerBossBars().add(packet.uuid());
|
||||
} else if (packet.action() == BossBarPacket.REMOVE) {
|
||||
playerSessionHandler.getServerBossBars().remove(packet.uuid());
|
||||
}
|
||||
}
|
||||
return false; // forward
|
||||
@@ -190,13 +190,13 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
|
||||
@Override
|
||||
public boolean handle(final ResourcePackRequestPacket packet) {
|
||||
final ResourcePackInfo.Builder builder = new VelocityResourcePackInfo.BuilderImpl(
|
||||
Preconditions.checkNotNull(packet.getUrl()))
|
||||
.setId(packet.getId())
|
||||
.setPrompt(packet.getPrompt() == null ? null : packet.getPrompt().getComponent())
|
||||
Preconditions.checkNotNull(packet.url()))
|
||||
.setId(packet.id())
|
||||
.setPrompt(packet.prompt() == null ? null : packet.prompt().getComponent())
|
||||
.setShouldForce(packet.isRequired())
|
||||
.setOrigin(ResourcePackInfo.Origin.DOWNSTREAM_SERVER);
|
||||
|
||||
final String hash = packet.getHash();
|
||||
final String hash = packet.hash();
|
||||
if (hash != null && !hash.isEmpty()) {
|
||||
if (PLAUSIBLE_SHA1_HASH.matcher(hash).matches()) {
|
||||
builder.setHash(ByteBufUtil.decodeHexDump(hash));
|
||||
@@ -222,14 +222,14 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
|
||||
// Do not apply a resource pack that has already been applied
|
||||
if (serverConn.getConnection() != null) {
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(), packet.getHash(), PlayerResourcePackStatusEvent.Status.ACCEPTED));
|
||||
packet.id(), packet.hash(), PlayerResourcePackStatusEvent.Status.ACCEPTED));
|
||||
if (serverConn.getConnection().getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) {
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(), packet.getHash(),
|
||||
packet.id(), packet.hash(),
|
||||
PlayerResourcePackStatusEvent.Status.DOWNLOADED));
|
||||
}
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(), packet.getHash(),
|
||||
packet.id(), packet.hash(),
|
||||
PlayerResourcePackStatusEvent.Status.SUCCESSFUL));
|
||||
}
|
||||
if (modifiedPack) {
|
||||
@@ -241,16 +241,16 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
|
||||
}
|
||||
} else if (serverConn.getConnection() != null) {
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(),
|
||||
packet.getHash(),
|
||||
packet.id(),
|
||||
packet.hash(),
|
||||
PlayerResourcePackStatusEvent.Status.DECLINED
|
||||
));
|
||||
}
|
||||
}, playerConnection.eventLoop()).exceptionally((ex) -> {
|
||||
if (serverConn.getConnection() != null) {
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(),
|
||||
packet.getHash(),
|
||||
packet.id(),
|
||||
packet.hash(),
|
||||
PlayerResourcePackStatusEvent.Status.DECLINED
|
||||
));
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ public class ConfigSessionHandler implements MinecraftSessionHandler {
|
||||
|
||||
@Override
|
||||
public boolean handle(KeepAlivePacket packet) {
|
||||
serverConn.getPendingPings().put(packet.getRandomId(), System.nanoTime());
|
||||
serverConn.getPendingPings().put(packet.randomId(), System.nanoTime());
|
||||
serverConn.getPlayer().getConnection().write(packet);
|
||||
return true;
|
||||
}
|
||||
@@ -171,11 +171,11 @@ public class ConfigSessionHandler implements MinecraftSessionHandler {
|
||||
if (serverConn.getConnection() != null) {
|
||||
// We can technically skip these first 2 states, however, for conformity to normal state flow expectations...
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(), packet.getHash(), PlayerResourcePackStatusEvent.Status.ACCEPTED));
|
||||
packet.id(), packet.hash(), PlayerResourcePackStatusEvent.Status.ACCEPTED));
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(), packet.getHash(), PlayerResourcePackStatusEvent.Status.DOWNLOADED));
|
||||
packet.id(), packet.hash(), PlayerResourcePackStatusEvent.Status.DOWNLOADED));
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(), packet.getHash(), PlayerResourcePackStatusEvent.Status.SUCCESSFUL));
|
||||
packet.id(), packet.hash(), PlayerResourcePackStatusEvent.Status.SUCCESSFUL));
|
||||
}
|
||||
if (modifiedPack) {
|
||||
logger.warn("A plugin has tried to modify a ResourcePack provided by the backend server "
|
||||
@@ -187,12 +187,12 @@ public class ConfigSessionHandler implements MinecraftSessionHandler {
|
||||
}
|
||||
} else if (serverConn.getConnection() != null) {
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(), packet.getHash(), PlayerResourcePackStatusEvent.Status.DECLINED));
|
||||
packet.id(), packet.hash(), PlayerResourcePackStatusEvent.Status.DECLINED));
|
||||
}
|
||||
}, playerConnection.eventLoop()).exceptionally((ex) -> {
|
||||
if (serverConn.getConnection() != null) {
|
||||
serverConn.getConnection().write(new ResourcePackResponsePacket(
|
||||
packet.getId(), packet.getHash(), PlayerResourcePackStatusEvent.Status.DECLINED));
|
||||
packet.id(), packet.hash(), PlayerResourcePackStatusEvent.Status.DECLINED));
|
||||
}
|
||||
logger.error("Exception while handling resource pack send for {}", playerConnection, ex);
|
||||
return null;
|
||||
|
||||
@@ -195,7 +195,7 @@ public class AuthSessionHandler implements MinecraftSessionHandler {
|
||||
@Override
|
||||
public boolean handle(ServerboundCookieResponsePacket packet) {
|
||||
server.getEventManager()
|
||||
.fire(new CookieReceiveEvent(connectedPlayer, packet.getKey(), packet.getPayload()))
|
||||
.fire(new CookieReceiveEvent(connectedPlayer, packet.key(), packet.payload()))
|
||||
.thenAcceptAsync(event -> {
|
||||
if (event.getResult().isAllowed()) {
|
||||
// The received cookie must have been requested by a proxy plugin in login phase,
|
||||
|
||||
@@ -188,7 +188,7 @@ public class ClientConfigSessionHandler implements MinecraftSessionHandler {
|
||||
@Override
|
||||
public boolean handle(ServerboundCookieResponsePacket packet) {
|
||||
server.getEventManager()
|
||||
.fire(new CookieReceiveEvent(player, packet.getKey(), packet.getPayload()))
|
||||
.fire(new CookieReceiveEvent(player, packet.key(), packet.payload()))
|
||||
.thenAcceptAsync(event -> {
|
||||
if (event.getResult().isAllowed()) {
|
||||
final VelocityServerConnection serverConnection = player.getConnectionInFlight();
|
||||
|
||||
@@ -735,7 +735,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
|
||||
return;
|
||||
}
|
||||
|
||||
Component disconnectReason = disconnect.getReason().getComponent();
|
||||
Component disconnectReason = disconnect.reason().getComponent();
|
||||
String plainTextReason = PASS_THRU_TRANSLATE.serialize(disconnectReason);
|
||||
if (connectedServer != null && connectedServer.getServerInfo().equals(server.getServerInfo())) {
|
||||
if (this.server.getConfiguration().isLogPlayerConnections()) {
|
||||
@@ -1349,7 +1349,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
|
||||
|
||||
private boolean sendKeepAliveToBackend(final @Nullable VelocityServerConnection serverConnection, final @NotNull KeepAlivePacket packet) {
|
||||
if (serverConnection != null) {
|
||||
final Long sentTime = serverConnection.getPendingPings().remove(packet.getRandomId());
|
||||
final Long sentTime = serverConnection.getPendingPings().remove(packet.randomId());
|
||||
if (sentTime != null) {
|
||||
final MinecraftConnection smc = serverConnection.getConnection();
|
||||
if (smc != null) {
|
||||
|
||||
@@ -61,12 +61,12 @@ public final class InitialInboundConnection implements VelocityInboundConnection
|
||||
|
||||
@Override
|
||||
public Optional<InetSocketAddress> getVirtualHost() {
|
||||
return Optional.of(InetSocketAddress.createUnresolved(cleanedAddress, handshake.getPort()));
|
||||
return Optional.of(InetSocketAddress.createUnresolved(cleanedAddress, handshake.port()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getRawVirtualHost() {
|
||||
return Optional.of(handshake.getServerAddress());
|
||||
return Optional.of(handshake.serverAddress());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -101,7 +101,7 @@ public final class InitialInboundConnection implements VelocityInboundConnection
|
||||
|
||||
@Override
|
||||
public HandshakeIntent getHandshakeIntent() {
|
||||
return handshake.getIntent();
|
||||
return handshake.intent();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -120,7 +120,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
|
||||
inbound.setPlayerKey(playerKey);
|
||||
this.login = packet;
|
||||
|
||||
final PreLoginEvent event = new PreLoginEvent(inbound, login.getUsername(), login.holderUuid());
|
||||
final PreLoginEvent event = new PreLoginEvent(inbound, login.username(), login.holderUuid());
|
||||
server.getEventManager().fire(event).thenRunAsync(() -> {
|
||||
if (mcConnection.isClosed()) {
|
||||
// The player was disconnected
|
||||
@@ -146,13 +146,13 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
|
||||
&& (server.getConfiguration().isOnlineMode() || result.isOnlineModeAllowed())) {
|
||||
// Request encryption.
|
||||
EncryptionRequestPacket request = generateEncryptionRequest();
|
||||
this.verify = Arrays.copyOf(request.getVerifyToken(), 4);
|
||||
this.verify = Arrays.copyOf(request.verifyToken(), 4);
|
||||
mcConnection.write(request);
|
||||
this.currentState = LoginState.ENCRYPTION_REQUEST_SENT;
|
||||
} else {
|
||||
mcConnection.setActiveSessionHandler(StateRegistry.LOGIN,
|
||||
new AuthSessionHandler(server, inbound,
|
||||
GameProfile.forOfflinePlayer(login.getUsername()), false));
|
||||
GameProfile.forOfflinePlayer(login.username()), false));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -203,7 +203,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
|
||||
|
||||
String playerIp = ((InetSocketAddress) mcConnection.getRemoteAddress()).getHostString();
|
||||
String url = String.format(MOJANG_HASJOINED_URL,
|
||||
urlFormParameterEscaper().escape(login.getUsername()), serverId);
|
||||
urlFormParameterEscaper().escape(login.username()), serverId);
|
||||
|
||||
if (server.getConfiguration().shouldPreventClientProxyConnections()) {
|
||||
url += "&ip=" + urlFormParameterEscaper().escape(playerIp);
|
||||
@@ -263,7 +263,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
|
||||
// Something else went wrong
|
||||
logger.error(
|
||||
"Got an unexpected error code {} whilst contacting Mojang to log in {} ({})",
|
||||
response.statusCode(), login.getUsername(), playerIp);
|
||||
response.statusCode(), login.username(), playerIp);
|
||||
inbound.disconnect(Component.translatable("multiplayer.disconnect.authservers_down"));
|
||||
}
|
||||
}, mcConnection.eventLoop())
|
||||
|
||||
@@ -133,10 +133,10 @@ public class LoginInboundConnection implements LoginPhaseConnection, KeyIdentifi
|
||||
}
|
||||
|
||||
void handleLoginPluginResponse(final LoginPluginResponsePacket response) {
|
||||
final MessageConsumer consumer = this.outstandingResponses.remove(response.getId());
|
||||
final MessageConsumer consumer = this.outstandingResponses.remove(response.id());
|
||||
if (consumer != null) {
|
||||
try {
|
||||
consumer.onMessageResponse(response.isSuccess() ? ByteBufUtil.getBytes(response.content())
|
||||
consumer.onMessageResponse(response.success() ? ByteBufUtil.getBytes(response.content())
|
||||
: null);
|
||||
} finally {
|
||||
final Runnable onAllMessagesHandled = this.onAllMessagesHandled;
|
||||
|
||||
@@ -63,11 +63,11 @@ public class ConnectionRequestResults {
|
||||
}
|
||||
|
||||
public static Impl forDisconnect(DisconnectPacket disconnect, RegisteredServer server) {
|
||||
return forDisconnect(disconnect.getReason().getComponent(), server);
|
||||
return forDisconnect(disconnect.reason().getComponent(), server);
|
||||
}
|
||||
|
||||
public static Impl forUnsafeDisconnect(DisconnectPacket disconnect, RegisteredServer server) {
|
||||
return new Impl(Status.SERVER_DISCONNECTED, disconnect.getReason().getComponent(), server,
|
||||
return new Impl(Status.SERVER_DISCONNECTED, disconnect.reason().getComponent(), server,
|
||||
false);
|
||||
}
|
||||
|
||||
|
||||
@@ -65,14 +65,6 @@ public record BossBarPacket(UUID uuid, int action, @Nullable ComponentHolder nam
|
||||
public static final int UPDATE_STYLE = 4;
|
||||
public static final int UPDATE_PROPERTIES = 5;
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public int getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public static BossBarPacket createAddPacket(
|
||||
final UUID id,
|
||||
final BossBar bar,
|
||||
|
||||
@@ -28,36 +28,11 @@ import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public final class DisconnectPacket implements MinecraftPacket {
|
||||
public record DisconnectPacket(StateRegistry state, ComponentHolder reason)
|
||||
implements MinecraftPacket {
|
||||
|
||||
private final ComponentHolder reason;
|
||||
private final StateRegistry state;
|
||||
|
||||
public DisconnectPacket(StateRegistry state, ComponentHolder reason) {
|
||||
this.state = state;
|
||||
this.reason = Preconditions.checkNotNull(reason, "reason");
|
||||
}
|
||||
|
||||
public ComponentHolder reason() {
|
||||
if (reason == null) {
|
||||
throw new IllegalStateException("No reason specified");
|
||||
}
|
||||
return reason;
|
||||
}
|
||||
|
||||
public ComponentHolder getReason() {
|
||||
return reason();
|
||||
}
|
||||
|
||||
public StateRegistry state() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Disconnect{"
|
||||
+ "reason='" + reason + '\''
|
||||
+ '}';
|
||||
public DisconnectPacket {
|
||||
Preconditions.checkNotNull(reason, "reason");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,7 +24,6 @@ import com.velocitypowered.proxy.protocol.MinecraftPacket;
|
||||
import com.velocitypowered.proxy.protocol.PacketCodec;
|
||||
import com.velocitypowered.proxy.protocol.ProtocolUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.util.Arrays;
|
||||
|
||||
public final class EncryptionRequestPacket implements MinecraftPacket {
|
||||
|
||||
@@ -57,22 +56,6 @@ public final class EncryptionRequestPacket implements MinecraftPacket {
|
||||
return shouldAuthenticate;
|
||||
}
|
||||
|
||||
public byte[] getPublicKey() {
|
||||
return publicKey();
|
||||
}
|
||||
|
||||
public byte[] getVerifyToken() {
|
||||
return verifyToken();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EncryptionRequest{"
|
||||
+ "publicKey=" + Arrays.toString(publicKey)
|
||||
+ ", verifyToken=" + Arrays.toString(verifyToken)
|
||||
+ '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(MinecraftSessionHandler handler) {
|
||||
return handler.handle(this);
|
||||
|
||||
@@ -27,75 +27,19 @@ import com.velocitypowered.proxy.protocol.PacketCodec;
|
||||
import com.velocitypowered.proxy.protocol.ProtocolUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public final class HandshakePacket implements MinecraftPacket {
|
||||
public record HandshakePacket(ProtocolVersion protocolVersion, String serverAddress, int port,
|
||||
HandshakeIntent intent)
|
||||
implements MinecraftPacket {
|
||||
|
||||
// This size was chosen to ensure Forge clients can still connect even with very long hostnames.
|
||||
// While DNS technically allows any character to be used, in practice ASCII is used.
|
||||
private static final int MAXIMUM_HOSTNAME_LENGTH = 255 + HANDSHAKE_HOSTNAME_TOKEN.length() + 1;
|
||||
|
||||
private final ProtocolVersion protocolVersion;
|
||||
private final String serverAddress;
|
||||
private final int port;
|
||||
private final HandshakeIntent intent;
|
||||
private final int nextStatus;
|
||||
|
||||
public HandshakePacket() {
|
||||
this(ProtocolVersion.MINIMUM_VERSION, "", 0, HandshakeIntent.LOGIN);
|
||||
}
|
||||
|
||||
public HandshakePacket(ProtocolVersion protocolVersion, String serverAddress, int port,
|
||||
HandshakeIntent intent) {
|
||||
this.protocolVersion = protocolVersion;
|
||||
this.serverAddress = serverAddress;
|
||||
this.port = port;
|
||||
this.intent = intent;
|
||||
this.nextStatus = intent.id();
|
||||
}
|
||||
|
||||
public ProtocolVersion protocolVersion() {
|
||||
return protocolVersion;
|
||||
}
|
||||
|
||||
public String serverAddress() {
|
||||
return serverAddress;
|
||||
}
|
||||
|
||||
public int port() {
|
||||
return port;
|
||||
}
|
||||
public static final HandshakePacket DEFAULT = new HandshakePacket(ProtocolVersion.MINIMUM_VERSION, "", 0,
|
||||
HandshakeIntent.LOGIN);
|
||||
|
||||
public int nextStatus() {
|
||||
return this.nextStatus;
|
||||
}
|
||||
|
||||
public HandshakeIntent intent() {
|
||||
return this.intent;
|
||||
}
|
||||
|
||||
public ProtocolVersion getProtocolVersion() {
|
||||
return protocolVersion();
|
||||
}
|
||||
|
||||
public String getServerAddress() {
|
||||
return serverAddress();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port();
|
||||
}
|
||||
|
||||
public HandshakeIntent getIntent() {
|
||||
return intent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Handshake{"
|
||||
+ "protocolVersion=" + protocolVersion
|
||||
+ ", serverAddress='" + serverAddress + '\''
|
||||
+ ", port=" + port
|
||||
+ ", nextStatus=" + nextStatus
|
||||
+ '}';
|
||||
return intent.id();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -121,10 +65,10 @@ public final class HandshakePacket implements MinecraftPacket {
|
||||
@Override
|
||||
public void encode(HandshakePacket packet, ByteBuf buf, ProtocolUtils.Direction direction,
|
||||
ProtocolVersion ignored) {
|
||||
ProtocolUtils.writeVarInt(buf, packet.protocolVersion.getProtocol());
|
||||
ProtocolUtils.writeString(buf, packet.serverAddress);
|
||||
buf.writeShort(packet.port);
|
||||
ProtocolUtils.writeVarInt(buf, packet.nextStatus);
|
||||
ProtocolUtils.writeVarInt(buf, packet.protocolVersion().getProtocol());
|
||||
ProtocolUtils.writeString(buf, packet.serverAddress());
|
||||
buf.writeShort(packet.port());
|
||||
ProtocolUtils.writeVarInt(buf, packet.nextStatus());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -26,17 +26,6 @@ import io.netty.buffer.ByteBuf;
|
||||
|
||||
public record KeepAlivePacket(long randomId) implements MinecraftPacket {
|
||||
|
||||
public long getRandomId() {
|
||||
return randomId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "KeepAlive{"
|
||||
+ "randomId=" + randomId
|
||||
+ '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(MinecraftSessionHandler handler) {
|
||||
return handler.handle(this);
|
||||
|
||||
@@ -47,14 +47,6 @@ public final class LoginPluginResponsePacket extends DefaultByteBufHolder
|
||||
return success;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id();
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LoginPluginResponse{"
|
||||
|
||||
@@ -33,54 +33,25 @@ import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
public final class ResourcePackRequestPacket implements MinecraftPacket {
|
||||
|
||||
private final @Nullable UUID id; // 1.20.3+
|
||||
private final String url;
|
||||
private final String hash;
|
||||
private final boolean isRequired; // 1.17+
|
||||
private final @Nullable ComponentHolder prompt; // 1.17+
|
||||
public record ResourcePackRequestPacket(
|
||||
@Nullable UUID id, // 1.20.3+
|
||||
String url,
|
||||
String hash,
|
||||
boolean isRequired, // 1.17+
|
||||
@Nullable ComponentHolder prompt // 1.17+
|
||||
) implements MinecraftPacket {
|
||||
|
||||
private static final Pattern PLAUSIBLE_SHA1_HASH = Pattern.compile("^[a-z0-9]{40}$"); // 1.20.2+
|
||||
|
||||
public ResourcePackRequestPacket(@Nullable UUID id, String url, String hash,
|
||||
boolean isRequired, @Nullable ComponentHolder prompt) {
|
||||
this.id = id;
|
||||
this.url = url;
|
||||
this.hash = hash;
|
||||
this.isRequired = isRequired;
|
||||
this.prompt = prompt;
|
||||
}
|
||||
|
||||
public @Nullable UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public boolean isRequired() {
|
||||
return isRequired;
|
||||
}
|
||||
|
||||
public String getHash() {
|
||||
return hash;
|
||||
}
|
||||
|
||||
public @Nullable ComponentHolder getPrompt() {
|
||||
return prompt;
|
||||
}
|
||||
|
||||
public VelocityResourcePackInfo toServerPromptedPack() {
|
||||
final ResourcePackInfo.Builder builder =
|
||||
new VelocityResourcePackInfo.BuilderImpl(Preconditions.checkNotNull(url))
|
||||
.setId(id).setPrompt(prompt == null ? null : prompt.getComponent())
|
||||
.setShouldForce(isRequired).setOrigin(ResourcePackInfo.Origin.DOWNSTREAM_SERVER);
|
||||
new VelocityResourcePackInfo.BuilderImpl(Preconditions.checkNotNull(url()))
|
||||
.setId(id()).setPrompt(prompt() == null ? null : prompt().getComponent())
|
||||
.setShouldForce(isRequired()).setOrigin(ResourcePackInfo.Origin.DOWNSTREAM_SERVER);
|
||||
|
||||
if (hash != null && !hash.isEmpty()) {
|
||||
if (PLAUSIBLE_SHA1_HASH.matcher(hash).matches()) {
|
||||
builder.setHash(ByteBufUtil.decodeHexDump(hash));
|
||||
if (hash() != null && !hash().isEmpty()) {
|
||||
if (PLAUSIBLE_SHA1_HASH.matcher(hash()).matches()) {
|
||||
builder.setHash(ByteBufUtil.decodeHexDump(hash()));
|
||||
}
|
||||
}
|
||||
return (VelocityResourcePackInfo) builder.build();
|
||||
@@ -129,21 +100,21 @@ public final class ResourcePackRequestPacket implements MinecraftPacket {
|
||||
public void encode(ResourcePackRequestPacket packet, ByteBuf buf, Direction direction,
|
||||
ProtocolVersion protocolVersion) {
|
||||
if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) {
|
||||
if (packet.id == null) {
|
||||
if (packet.id() == null) {
|
||||
throw new IllegalStateException("Resource pack id not set yet!");
|
||||
}
|
||||
ProtocolUtils.writeUuid(buf, packet.id);
|
||||
ProtocolUtils.writeUuid(buf, packet.id());
|
||||
}
|
||||
if (packet.url == null || packet.hash == null) {
|
||||
if (packet.url() == null || packet.hash() == null) {
|
||||
throw new IllegalStateException("Packet not fully filled in yet!");
|
||||
}
|
||||
ProtocolUtils.writeString(buf, packet.url);
|
||||
ProtocolUtils.writeString(buf, packet.hash);
|
||||
ProtocolUtils.writeString(buf, packet.url());
|
||||
ProtocolUtils.writeString(buf, packet.hash());
|
||||
if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_17)) {
|
||||
buf.writeBoolean(packet.isRequired);
|
||||
if (packet.prompt != null) {
|
||||
buf.writeBoolean(packet.isRequired());
|
||||
if (packet.prompt() != null) {
|
||||
buf.writeBoolean(true);
|
||||
packet.prompt.write(buf);
|
||||
packet.prompt().write(buf);
|
||||
} else {
|
||||
buf.writeBoolean(false);
|
||||
}
|
||||
|
||||
@@ -28,55 +28,29 @@ import io.netty.buffer.ByteBuf;
|
||||
import java.util.UUID;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
public final class ServerLoginPacket implements MinecraftPacket {
|
||||
public record ServerLoginPacket(
|
||||
String username,
|
||||
@Nullable IdentifiedKey playerKey, // Introduced in 1.19.3
|
||||
@Nullable UUID holderUuid // Used for key revision 2
|
||||
) implements MinecraftPacket {
|
||||
|
||||
private static final QuietDecoderException EMPTY_USERNAME = new QuietDecoderException(
|
||||
"Empty username!");
|
||||
|
||||
private final String username;
|
||||
private final @Nullable IdentifiedKey playerKey; // Introduced in 1.19.3
|
||||
private final @Nullable UUID holderUuid; // Used for key revision 2
|
||||
|
||||
/**
|
||||
* Creates a ServerLoginPacket with a player key.
|
||||
*/
|
||||
public ServerLoginPacket(String username, @Nullable IdentifiedKey playerKey) {
|
||||
this(username, playerKey, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ServerLoginPacket with a holder UUID.
|
||||
*/
|
||||
public ServerLoginPacket(String username, @Nullable UUID holderUuid) {
|
||||
this(username, null, holderUuid);
|
||||
}
|
||||
|
||||
private ServerLoginPacket(String username, @Nullable IdentifiedKey playerKey,
|
||||
@Nullable UUID holderUuid) {
|
||||
this.username = username;
|
||||
this.playerKey = playerKey;
|
||||
this.holderUuid = holderUuid;
|
||||
}
|
||||
|
||||
public String username() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username();
|
||||
}
|
||||
|
||||
public @Nullable IdentifiedKey playerKey() {
|
||||
return this.playerKey;
|
||||
}
|
||||
|
||||
public @Nullable UUID holderUuid() {
|
||||
return holderUuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ServerLogin{"
|
||||
+ "username='" + username + '\''
|
||||
+ "playerKey='" + playerKey + '\''
|
||||
+ "holderUUID='" + holderUuid + '\''
|
||||
+ '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(MinecraftSessionHandler handler) {
|
||||
return handler.handle(this);
|
||||
@@ -127,33 +101,33 @@ public final class ServerLoginPacket implements MinecraftPacket {
|
||||
@Override
|
||||
public void encode(ServerLoginPacket packet, ByteBuf buf, ProtocolUtils.Direction direction,
|
||||
ProtocolVersion version) {
|
||||
if (packet.username == null) {
|
||||
if (packet.username() == null) {
|
||||
throw new IllegalStateException("No username found!");
|
||||
}
|
||||
ProtocolUtils.writeString(buf, packet.username);
|
||||
ProtocolUtils.writeString(buf, packet.username());
|
||||
|
||||
if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) {
|
||||
if (version.lessThan(ProtocolVersion.MINECRAFT_1_19_3)) {
|
||||
if (packet.playerKey != null) {
|
||||
if (packet.playerKey() != null) {
|
||||
buf.writeBoolean(true);
|
||||
ProtocolUtils.writePlayerKey(buf, packet.playerKey);
|
||||
ProtocolUtils.writePlayerKey(buf, packet.playerKey());
|
||||
} else {
|
||||
buf.writeBoolean(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_2)) {
|
||||
ProtocolUtils.writeUuid(buf, packet.holderUuid);
|
||||
ProtocolUtils.writeUuid(buf, packet.holderUuid());
|
||||
return;
|
||||
}
|
||||
|
||||
if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) {
|
||||
if (packet.playerKey != null && packet.playerKey.getSignatureHolder() != null) {
|
||||
if (packet.playerKey() != null && packet.playerKey().getSignatureHolder() != null) {
|
||||
buf.writeBoolean(true);
|
||||
ProtocolUtils.writeUuid(buf, packet.playerKey.getSignatureHolder());
|
||||
} else if (packet.holderUuid != null) {
|
||||
ProtocolUtils.writeUuid(buf, packet.playerKey().getSignatureHolder());
|
||||
} else if (packet.holderUuid() != null) {
|
||||
buf.writeBoolean(true);
|
||||
ProtocolUtils.writeUuid(buf, packet.holderUuid);
|
||||
ProtocolUtils.writeUuid(buf, packet.holderUuid());
|
||||
} else {
|
||||
buf.writeBoolean(false);
|
||||
}
|
||||
|
||||
@@ -30,14 +30,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
public record ServerboundCookieResponsePacket(Key key,
|
||||
byte @Nullable [] payload) implements MinecraftPacket {
|
||||
|
||||
public Key getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public byte @Nullable [] getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(MinecraftSessionHandler handler) {
|
||||
return handler.handle(this);
|
||||
|
||||
@@ -42,7 +42,7 @@ public final class TabCompleteResponsePacket implements MinecraftPacket {
|
||||
this.transactionId = transactionId;
|
||||
this.start = start;
|
||||
this.length = length;
|
||||
this.offers = offers;
|
||||
this.offers = List.copyOf(offers);
|
||||
}
|
||||
|
||||
public int transactionId() {
|
||||
|
||||
@@ -71,7 +71,7 @@ class PacketRegistryTest {
|
||||
assertNotNull(packet, "Packet was not found in registry");
|
||||
assertEquals(HandshakePacket.Codec.class, packet.getClass(), "Registry returned wrong class");
|
||||
|
||||
assertEquals(0, registry.forVersion(MINECRAFT_1_12).getPacketId(new HandshakePacket()),
|
||||
assertEquals(0, registry.forVersion(MINECRAFT_1_12).getPacketId(HandshakePacket.DEFAULT),
|
||||
"Registry did not return the correct packet ID");
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ class PacketRegistryTest {
|
||||
PacketCodec<?> packet = registry.forVersion(MINECRAFT_1_12_1).getCodec(0);
|
||||
assertNotNull(packet, "Packet was not found in registry");
|
||||
assertEquals(HandshakePacket.Codec.class, packet.getClass(), "Registry returned wrong class");
|
||||
HandshakePacket handshakePacket = new HandshakePacket();
|
||||
HandshakePacket handshakePacket = HandshakePacket.DEFAULT;
|
||||
assertEquals(0, registry.forVersion(MINECRAFT_1_12_1).getPacketId(handshakePacket),
|
||||
"Registry did not return the correct packet ID");
|
||||
assertEquals(0, registry.forVersion(MINECRAFT_1_14_2).getPacketId(handshakePacket),
|
||||
@@ -227,9 +227,9 @@ class PacketRegistryTest {
|
||||
.build();
|
||||
|
||||
// 1.12 should have ID 0x00
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_12).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_12).getPacketId(HandshakePacket.DEFAULT));
|
||||
// 1.13 should have ID 0x01
|
||||
assertEquals(0x01, registry.forVersion(MINECRAFT_1_13).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x01, registry.forVersion(MINECRAFT_1_13).getPacketId(HandshakePacket.DEFAULT));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -242,7 +242,7 @@ class PacketRegistryTest {
|
||||
VersionRange.of(MINECRAFT_1_12, 0x01))
|
||||
.build();
|
||||
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_12).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_12).getPacketId(HandshakePacket.DEFAULT));
|
||||
assertEquals(0x01, registry.forVersion(MINECRAFT_1_12).getPacketId(new StatusPingPacket(0L)));
|
||||
assertNotNull(registry.forVersion(MINECRAFT_1_12).getCodec(0x00));
|
||||
assertNotNull(registry.forVersion(MINECRAFT_1_12).getCodec(0x01));
|
||||
@@ -311,7 +311,7 @@ class PacketRegistryTest {
|
||||
@Test
|
||||
void canDecodePacketReturnsTrue() {
|
||||
MultiVersionPacketRegistry registry = setupRegistry();
|
||||
assertTrue(registry.forVersion(MINECRAFT_1_12).canDecodePacket(new HandshakePacket()),
|
||||
assertTrue(registry.forVersion(MINECRAFT_1_12).canDecodePacket(HandshakePacket.DEFAULT),
|
||||
"Should be able to decode HandshakePacket in 1.12");
|
||||
}
|
||||
|
||||
@@ -330,7 +330,7 @@ class PacketRegistryTest {
|
||||
.build();
|
||||
|
||||
// Encode-only packets can still be identified by class for encoding purposes
|
||||
assertTrue(registry.forVersion(MINECRAFT_1_12).canDecodePacket(new HandshakePacket()),
|
||||
assertTrue(registry.forVersion(MINECRAFT_1_12).canDecodePacket(HandshakePacket.DEFAULT),
|
||||
"Encode-only packets can be identified for encoding");
|
||||
// But cannot be decoded by ID (not in packetIdToCodec)
|
||||
assertNull(registry.forVersion(MINECRAFT_1_12).getCodec(0x00),
|
||||
@@ -364,7 +364,7 @@ class PacketRegistryTest {
|
||||
SimplePacketRegistry registry = new SimplePacketRegistry(ProtocolUtils.Direction.CLIENTBOUND);
|
||||
registry.register(0x42, HandshakePacket.class, new HandshakePacket.Codec());
|
||||
|
||||
int id = registry.getPacketId(new HandshakePacket());
|
||||
int id = registry.getPacketId(HandshakePacket.DEFAULT);
|
||||
assertEquals(0x42, id, "Should return correct packet ID");
|
||||
}
|
||||
|
||||
@@ -383,7 +383,7 @@ class PacketRegistryTest {
|
||||
SimplePacketRegistry registry = new SimplePacketRegistry(ProtocolUtils.Direction.CLIENTBOUND);
|
||||
registry.register(0x00, HandshakePacket.class, new HandshakePacket.Codec());
|
||||
|
||||
assertTrue(registry.canDecodePacket(new HandshakePacket()),
|
||||
assertTrue(registry.canDecodePacket(HandshakePacket.DEFAULT),
|
||||
"Should be able to decode registered packet");
|
||||
assertFalse(registry.canDecodePacket(new StatusPingPacket(0L)),
|
||||
"Should not be able to decode unregistered packet");
|
||||
@@ -405,8 +405,8 @@ class PacketRegistryTest {
|
||||
VersionRange.of(MINECRAFT_1_12, 0x01))
|
||||
.build();
|
||||
|
||||
assertEquals(0x00, clientbound.forVersion(MINECRAFT_1_12).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x01, serverbound.forVersion(MINECRAFT_1_12).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x00, clientbound.forVersion(MINECRAFT_1_12).getPacketId(HandshakePacket.DEFAULT));
|
||||
assertEquals(0x01, serverbound.forVersion(MINECRAFT_1_12).getPacketId(HandshakePacket.DEFAULT));
|
||||
}
|
||||
|
||||
// ==================== Wide Version Range Tests ====================
|
||||
@@ -420,9 +420,9 @@ class PacketRegistryTest {
|
||||
.build();
|
||||
|
||||
// Should work for all supported versions in range
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_8).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_12).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_16_2).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_8).getPacketId(HandshakePacket.DEFAULT));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_12).getPacketId(HandshakePacket.DEFAULT));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_16_2).getPacketId(HandshakePacket.DEFAULT));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -433,7 +433,7 @@ class PacketRegistryTest {
|
||||
VersionRange.of(MINECRAFT_1_15, 0x00))
|
||||
.build();
|
||||
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_15).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_16_2).getPacketId(new HandshakePacket()));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_15).getPacketId(HandshakePacket.DEFAULT));
|
||||
assertEquals(0x00, registry.forVersion(MINECRAFT_1_16_2).getPacketId(HandshakePacket.DEFAULT));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user