Skip to content

Commit

Permalink
Implement EngineConnectionState#connection
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Jan 7, 2025
1 parent 341d518 commit f668e0d
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,27 @@
package org.spongepowered.common.network;

import net.minecraft.network.Connection;
import org.spongepowered.api.network.ClientConnectionState;
import org.spongepowered.api.network.ClientSideConnection;

public final class SpongeClientEngineConnection extends SpongeEngineConnection implements ClientSideConnection {

public SpongeClientEngineConnection(final Connection connection) {
super(connection);
}

@Override
protected ClientConnectionState.Intent createIntentState(final boolean transferred) {
return new ClientConnectionState.Intent() {
@Override
public ClientSideConnection connection() {
return SpongeClientEngineConnection.this;
}

@Override
public boolean transferred() {
return transferred;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Optional<EngineConnectionState> state() {
if (!(state instanceof ServerLoginPacketListenerImplBridge loginBridge) || loginBridge.bridge$isIntentDone()) {
return Optional.of(state);
}
return Optional.of(DummyIntent.of(state.transferred()));
return Optional.of(this.createIntentState(state.transferred()));
}
return Optional.empty();
}
Expand Down Expand Up @@ -129,6 +129,8 @@ private void fireDisconnectEvent() {
SpongeCommon.post(event);
}

protected abstract EngineConnectionState.Intent createIntentState(boolean transferred);

private enum EventFireState {
NONE,
IN_FLIGHT,
Expand All @@ -139,21 +141,4 @@ boolean shouldFireDisconnectionImmediately() {
return this == EventFireState.POST;
}
}

private record DummyIntent(boolean transferred) implements EngineConnectionState.Intent {

private static final DummyIntent TRANSFERRED_FALSE = new DummyIntent(false);
private static final DummyIntent TRANSFERRED_TRUE = new DummyIntent(true);

@Override
public boolean transferred() {
return this.transferred;
}

static DummyIntent of(final boolean transferred) {
return transferred
? DummyIntent.TRANSFERRED_TRUE
: DummyIntent.TRANSFERRED_FALSE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.common.network;

import net.minecraft.network.Connection;
import org.spongepowered.api.network.ServerConnectionState;
import org.spongepowered.api.network.ServerSideConnection;

public final class SpongeServerEngineConnection extends SpongeEngineConnection implements ServerSideConnection {
Expand All @@ -33,4 +34,18 @@ public SpongeServerEngineConnection(final Connection connection) {
super(connection);
}

@Override
protected ServerConnectionState.Intent createIntentState(final boolean transferred) {
return new ServerConnectionState.Intent() {
@Override
public ServerSideConnection connection() {
return SpongeServerEngineConnection.this;
}

@Override
public boolean transferred() {
return transferred;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl;
import net.minecraft.network.Connection;
import org.spongepowered.api.network.ClientConnectionState;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -35,6 +36,7 @@
public abstract class ClientCommonPacketListenerImplMixin_API implements ClientConnectionState {

// @formatter:off
@Shadow @Final protected Connection connection;
@Shadow @Final protected Minecraft minecraft;
@Shadow protected boolean isTransferring;
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@

import net.minecraft.client.multiplayer.ClientConfigurationPacketListenerImpl;
import org.spongepowered.api.network.ClientConnectionState;
import org.spongepowered.api.network.ClientSideConnection;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.profile.SpongeGameProfile;

@Mixin(ClientConfigurationPacketListenerImpl.class)
Expand All @@ -39,6 +41,11 @@ public abstract class ClientConfigurationPacketListenerImplMixin_API extends Cli
@Shadow @Final private com.mojang.authlib.GameProfile localGameProfile;
// @formatter:on

@Override
public ClientSideConnection connection() {
return (ClientSideConnection) ((ConnectionBridge) this.connection).bridge$getEngineConnection();
}

@Override
public GameProfile profile() {
return SpongeGameProfile.of(this.localGameProfile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,30 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientHandshakePacketListenerImpl;
import net.minecraft.network.Connection;
import org.spongepowered.api.network.ClientConnectionState;
import org.spongepowered.api.network.ClientSideConnection;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.profile.SpongeGameProfile;

@Mixin(ClientHandshakePacketListenerImpl.class)
public abstract class ClientHandshakePacketListenerImplMixin_API implements ClientConnectionState.Login {

// @formatter:off
@Shadow @Final private Connection connection;
@Shadow @Final private Minecraft minecraft;
@Shadow @Final private boolean wasTransferredTo;
// @formatter:on

@Override
public ClientSideConnection connection() {
return (ClientSideConnection) ((ConnectionBridge) this.connection).bridge$getEngineConnection();
}

@Override
public boolean transferred() {
return this.wasTransferredTo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,26 @@
import net.minecraft.client.multiplayer.ClientPacketListener;
import org.spongepowered.api.entity.living.player.client.LocalPlayer;
import org.spongepowered.api.network.ClientConnectionState;
import org.spongepowered.api.network.ClientSideConnection;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.profile.SpongeGameProfile;

@Mixin(ClientPacketListener.class)
public class ClientPacketListenerMixin_API extends ClientCommonPacketListenerImplMixin_API implements ClientConnectionState.Game {
public abstract class ClientPacketListenerMixin_API extends ClientCommonPacketListenerImplMixin_API implements ClientConnectionState.Game {

// @formatter:off
@Shadow @Final private com.mojang.authlib.GameProfile localGameProfile;
// @formatter:on

@Override
public ClientSideConnection connection() {
return (ClientSideConnection) ((ConnectionBridge) this.connection).bridge$getEngineConnection();
}

@Override
public GameProfile profile() {
return SpongeGameProfile.of(this.localGameProfile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,28 @@
*/
package org.spongepowered.common.mixin.api.minecraft.server.network;

import net.minecraft.network.Connection;
import net.minecraft.server.network.MemoryServerHandshakePacketListenerImpl;
import org.spongepowered.api.network.ServerConnectionState;
import org.spongepowered.api.network.ServerSideConnection;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.bridge.server.network.ServerHandshakePacketListenerImplBridge;

@Mixin(MemoryServerHandshakePacketListenerImpl.class)
public abstract class MemoryServerHandshakePacketListenerImplMixin_API implements ServerConnectionState.Intent {

// @formatter:off
@Shadow @Final private Connection connection;
// @formatter:on

@Override
public ServerSideConnection connection() {
return (ServerSideConnection) ((ConnectionBridge) this.connection).bridge$getEngineConnection();
}

@Override
public boolean transferred() {
return ((ServerHandshakePacketListenerImplBridge) this).bridge$transferred();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.common.mixin.api.minecraft.server.network;

import com.mojang.authlib.GameProfile;
import net.minecraft.network.Connection;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
import org.spongepowered.api.network.EngineConnectionState;
import org.spongepowered.asm.mixin.Final;
Expand All @@ -36,6 +37,7 @@ public abstract class ServerCommonPacketListenerImplMixin_API implements EngineC

// @formatter:off
@Shadow @Final private boolean transferred;
@Shadow @Final protected Connection connection;

@Shadow protected abstract GameProfile shadow$playerProfile();
@Shadow public abstract int shadow$latency();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@

import net.minecraft.server.network.ServerConfigurationPacketListenerImpl;
import org.spongepowered.api.network.ServerConnectionState;
import org.spongepowered.api.network.ServerSideConnection;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.profile.SpongeGameProfile;

@Mixin(ServerConfigurationPacketListenerImpl.class)
public abstract class ServerConfigurationPacketListenerImplMixin_API extends ServerCommonPacketListenerImplMixin_API implements ServerConnectionState.Configuration {

@Override
public ServerSideConnection connection() {
return (ServerSideConnection) ((ConnectionBridge) this.connection).bridge$getEngineConnection();
}

@Override
public GameProfile profile() {
return SpongeGameProfile.of(this.shadow$playerProfile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.network.ServerConnectionState;
import org.spongepowered.api.network.ServerSideConnection;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.profile.SpongeGameProfile;

@Mixin(ServerGamePacketListenerImpl.class)
Expand All @@ -42,6 +44,11 @@ public abstract class ServerGamePacketListenerImplMixin_API extends ServerCommon
@Shadow public net.minecraft.server.level.ServerPlayer player;
// @formatter:on

@Override
public ServerSideConnection connection() {
return (ServerSideConnection) ((ConnectionBridge) this.connection).bridge$getEngineConnection();
}

@Override
public GameProfile profile() {
return SpongeGameProfile.of(this.shadow$playerProfile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,28 @@
*/
package org.spongepowered.common.mixin.api.minecraft.server.network;

import net.minecraft.network.Connection;
import net.minecraft.server.network.ServerHandshakePacketListenerImpl;
import org.spongepowered.api.network.ServerConnectionState;
import org.spongepowered.api.network.ServerSideConnection;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.bridge.server.network.ServerHandshakePacketListenerImplBridge;

@Mixin(ServerHandshakePacketListenerImpl.class)
public abstract class ServerHandshakePacketListenerImplMixin_API implements ServerConnectionState.Intent {

// @formatter:off
@Shadow @Final private Connection connection;
// @formatter:on

@Override
public ServerSideConnection connection() {
return (ServerSideConnection) ((ConnectionBridge) this.connection).bridge$getEngineConnection();
}

@Override
public boolean transferred() {
return ((ServerHandshakePacketListenerImplBridge) this).bridge$transferred();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
*/
package org.spongepowered.common.mixin.api.minecraft.server.network;

import net.minecraft.network.Connection;
import net.minecraft.server.network.ServerLoginPacketListenerImpl;
import org.spongepowered.api.network.ServerConnectionState;
import org.spongepowered.api.network.ServerSideConnection;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.profile.SpongeGameProfile;

@Mixin(ServerLoginPacketListenerImpl.class)
Expand All @@ -38,8 +41,14 @@ public abstract class ServerLoginPacketListenerImplMixin_API implements ServerCo
// @formatter:off
@Shadow private com.mojang.authlib.GameProfile authenticatedProfile;
@Shadow @Final private boolean transferred;
@Shadow @Final Connection connection;
// @formatter:on

@Override
public ServerSideConnection connection() {
return (ServerSideConnection) ((ConnectionBridge) this.connection).bridge$getEngineConnection();
}

@Override
public boolean transferred() {
return this.transferred;
Expand Down

0 comments on commit f668e0d

Please sign in to comment.