Skip to content

Commit

Permalink
Fix login disconnect from 1.20.4 (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d authored Feb 16, 2024
1 parent ecf936f commit 2cf18b0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ public void disconnected() {
if (server.getConfiguration().isFailoverOnUnexpectedServerDisconnect()) {
serverConn.getPlayer().handleConnectionException(serverConn.getServer(),
DisconnectPacket.create(ConnectionMessages.INTERNAL_SERVER_CONNECTION_ERROR,
serverConn.getPlayer().getProtocolVersion(), false), true);
serverConn.getPlayer().getProtocolVersion(),
serverConn.getPlayer().getConnection().getState()), true);
} else {
serverConn.getPlayer().disconnect(ConnectionMessages.INTERNAL_SERVER_CONNECTION_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ public void disconnect0(Component reason, boolean duringLogin) {
logger.info(Component.text(this + " has disconnected: ").append(translated));
}
connection.closeWith(DisconnectPacket.create(translated,
this.getProtocolVersion(), duringLogin));
this.getProtocolVersion(), connection.getState()));
}

public @Nullable VelocityServerConnection getConnectedServer() {
Expand Down Expand Up @@ -775,7 +775,7 @@ private void handleKickEvent(KickedFromServerEvent originalEvent, Component frie
Component reason = status.getReasonComponent()
.orElse(ConnectionMessages.INTERNAL_SERVER_CONNECTION_ERROR);
handleConnectionException(res.getServer(),
DisconnectPacket.create(reason, getProtocolVersion(), false),
DisconnectPacket.create(reason, getProtocolVersion(), connection.getState()),
((Impl) status).isSafe());
break;
case SUCCESS:
Expand Down Expand Up @@ -1276,7 +1276,7 @@ public CompletableFuture<Boolean> connectWithIndication() {
Component reason = status.getReasonComponent()
.orElse(ConnectionMessages.INTERNAL_SERVER_CONNECTION_ERROR);
handleConnectionException(toConnect,
DisconnectPacket.create(reason, getProtocolVersion(), false), status.isSafe());
DisconnectPacket.create(reason, getProtocolVersion(), connection.getState()), status.isSafe());
break;
default:
// The only remaining value is successful (no need to do anything!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void disconnect(Component reason) {
if (connection.server.getConfiguration().isLogPlayerConnections()) {
logger.info(Component.text(this + " has disconnected: ").append(translated));
}
connection.closeWith(DisconnectPacket.create(translated, getProtocolVersion(), true));
connection.closeWith(DisconnectPacket.create(translated, getProtocolVersion(), connection.getState()));
}

/**
Expand All @@ -109,6 +109,6 @@ public void disconnect(Component reason) {
public void disconnectQuietly(Component reason) {
Component translated = GlobalTranslator.render(reason, ClosestLocaleMatcher.INSTANCE
.lookupClosest(Locale.getDefault()));
connection.closeWith(DisconnectPacket.create(translated, getProtocolVersion(), true));
connection.closeWith(DisconnectPacket.create(translated, getProtocolVersion(), connection.getState()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public enum StateRegistry {
PluginMessagePacket.class, PluginMessagePacket::new,
map(0x00, MINECRAFT_1_20_2, false));
clientbound.register(
DisconnectPacket.class, () -> new DisconnectPacket(false),
DisconnectPacket.class, () -> new DisconnectPacket(this),
map(0x01, MINECRAFT_1_20_2, false));
clientbound.register(
FinishedUpdatePacket.class, () -> FinishedUpdatePacket.INSTANCE,
Expand Down Expand Up @@ -354,7 +354,7 @@ public enum StateRegistry {
map(0x18, MINECRAFT_1_20_2, false));
clientbound.register(
DisconnectPacket.class,
() -> new DisconnectPacket(false),
() -> new DisconnectPacket(this),
map(0x40, MINECRAFT_1_7_2, false),
map(0x1A, MINECRAFT_1_9, false),
map(0x1B, MINECRAFT_1_13, false),
Expand Down Expand Up @@ -595,7 +595,7 @@ public enum StateRegistry {
map(0x03, MINECRAFT_1_20_2, false));

clientbound.register(
DisconnectPacket.class, () -> new DisconnectPacket(true),
DisconnectPacket.class, () -> new DisconnectPacket(this),
map(0x00, MINECRAFT_1_7_2, false));
clientbound.register(
EncryptionRequestPacket.class, EncryptionRequestPacket::new,
Expand Down Expand Up @@ -795,7 +795,8 @@ public static final class PacketMapping {
private final @Nullable ProtocolVersion lastValidProtocolVersion;

PacketMapping(int id, ProtocolVersion protocolVersion,
ProtocolVersion lastValidProtocolVersion, boolean packetDecoding) {
@Nullable ProtocolVersion lastValidProtocolVersion,
boolean packetDecoding) {
this.id = id;
this.protocolVersion = protocolVersion;
this.lastValidProtocolVersion = lastValidProtocolVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import io.netty.buffer.ByteBuf;
import net.kyori.adventure.text.Component;
Expand All @@ -30,14 +31,14 @@
public class DisconnectPacket implements MinecraftPacket {

private @Nullable ComponentHolder reason;
private final boolean login;
private final StateRegistry state;

public DisconnectPacket(boolean login) {
this.login = login;
public DisconnectPacket(StateRegistry state) {
this.state = state;
}

private DisconnectPacket(boolean login, ComponentHolder reason) {
this.login = login;
private DisconnectPacket(StateRegistry state, ComponentHolder reason) {
this.state = state;
this.reason = Preconditions.checkNotNull(reason, "reason");
}

Expand All @@ -61,7 +62,8 @@ public String toString() {

@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
reason = ComponentHolder.read(buf, login ? ProtocolVersion.MINECRAFT_1_20_2 : version);
reason = ComponentHolder.read(buf, state == StateRegistry.LOGIN
? ProtocolVersion.MINECRAFT_1_20_2 : version);
}

@Override
Expand All @@ -74,8 +76,9 @@ public boolean handle(MinecraftSessionHandler handler) {
return handler.handle(this);
}

public static DisconnectPacket create(Component component, ProtocolVersion version, boolean login) {
public static DisconnectPacket create(Component component, ProtocolVersion version, StateRegistry state) {
Preconditions.checkNotNull(component, "component");
return new DisconnectPacket(login, new ComponentHolder(login ? ProtocolVersion.MINECRAFT_1_20_2 : version, component));
return new DisconnectPacket(state, new ComponentHolder(state == StateRegistry.LOGIN
? ProtocolVersion.MINECRAFT_1_20_2 : version, component));
}
}

0 comments on commit 2cf18b0

Please sign in to comment.