Skip to content

Commit

Permalink
Allow usage of RawDataChannel during EngineConnectionState.Intent
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Oct 5, 2024
1 parent 6fc3fc1 commit c49fa9f
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 245 deletions.

This file was deleted.

1 change: 0 additions & 1 deletion forge/src/mixins/resources/mixins.spongeforge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"server.commands.SpreadPlayersCommandMixin_Forge",
"server.level.ServerPlayerMixin_Forge",
"server.network.ServerGamePacketListenerImplMixin_Forge",
"server.network.ServerLoginPacketListenerImplMixin_Forge",
"world.entity.LivingEntityMixin_Forge",
"world.entity.LivingEntityMixin_Forge_Attack_Impl",
"world.entity.item.ItemEntityMixin_Forge",
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion neoforge/src/mixins/resources/mixins.spongeneo.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"server.level.ServerEntityMixin_Neo",
"server.level.ServerPlayerMixin_Neo",
"server.network.ServerGamePacketListenerImplMixin_Neo",
"server.network.ServerLoginPacketListenerImplMixin_Neo",
"world.entity.LivingEntityMixin_Neo",
"world.entity.LivingEntityMixin_Neo_Attack_Impl",
"world.entity.item.ItemEntityMixin_Neo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@

import net.minecraft.network.Connection;

import java.util.concurrent.ExecutorService;

public interface ServerLoginPacketListenerImplBridge {

ExecutorService bridge$getExecutor();

Connection bridge$getConnection();

boolean bridge$isIntentDone();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.adventure.SpongeAdventure;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.bridge.network.ServerLoginPacketListenerImplBridge;
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.common.profile.SpongeGameProfile;

Expand Down Expand Up @@ -66,7 +67,11 @@ public boolean active() {
@Override
public Optional<EngineConnectionState> state() {
if (this.active()) {
return Optional.of((EngineConnectionState) this.connection.getPacketListener());
final EngineConnectionState state = (EngineConnectionState) this.connection.getPacketListener();
if (!(state instanceof ServerLoginPacketListenerImplBridge loginBridge) || loginBridge.bridge$isIntentDone()) {
return Optional.of(state);
}
return Optional.of(DummyIntent.of(state.transferred()));
}
return Optional.empty();
}
Expand Down Expand Up @@ -134,4 +139,21 @@ 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 @@ -35,6 +35,11 @@

public final class ConnectionUtil {

public static boolean isIntentPhase(final EngineConnection connection) {
final EngineConnectionState state = (EngineConnectionState) ((SpongeEngineConnection) connection).connection().getPacketListener();
return state instanceof EngineConnectionState.Intent;
}

public static boolean isLoginPhase(final EngineConnection connection) {
final EngineConnectionState state = (EngineConnectionState) ((SpongeEngineConnection) connection).connection().getPacketListener();
return state instanceof EngineConnectionState.Login;
Expand All @@ -50,6 +55,12 @@ public static TransactionStore getTransactionStore(final EngineConnection connec
return ((ConnectionBridge) networkManager).bridge$getTransactionStore();
}

public static void checkHandshakeOrIntentPhase(final EngineConnection connection) {
if (!ConnectionUtil.isIntentPhase(connection) && !ConnectionUtil.isLoginPhase(connection)) {
throw new IllegalStateException("This dispatcher may only be used for connections in the handshake phase.");
}
}

public static void checkHandshakePhase(final EngineConnection connection) {
if (!ConnectionUtil.isLoginPhase(connection)) {
throw new IllegalStateException("This dispatcher may only be used for connections in the handshake phase.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void handleTransactionResponse(final EngineConnection connection, final Object s

@Override
public CompletableFuture<ChannelBuf> sendTo(final EngineConnection connection, final Consumer<ChannelBuf> payload) {
ConnectionUtil.checkHandshakePhase(connection);
ConnectionUtil.checkHandshakeOrIntentPhase(connection);

final EngineConnectionState state = (EngineConnectionState) ((SpongeEngineConnection) connection).connection().getPacketListener();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import net.kyori.adventure.text.Component;
import net.minecraft.network.Connection;
import net.minecraft.network.ProtocolInfo;
import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket;
import net.minecraft.network.protocol.login.LoginProtocols;
import net.minecraft.server.network.MemoryServerHandshakePacketListenerImpl;
Expand All @@ -37,6 +38,7 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.adventure.SpongeAdventure;
import org.spongepowered.common.bridge.network.ConnectionBridge;
Expand All @@ -58,16 +60,21 @@ public abstract class MemoryServerHandshakePacketListenerImplMixin implements Se
target = "Lnet/minecraft/network/Connection;setupInboundProtocol(Lnet/minecraft/network/ProtocolInfo;Lnet/minecraft/network/PacketListener;)V"),
cancellable = true)
private void impl$onLogin(final CallbackInfo ci) {
this.connection.setupOutboundProtocol(LoginProtocols.CLIENTBOUND);
final SpongeEngineConnection connection = ((ConnectionBridge) this.connection).bridge$getEngineConnection();
final Component message = Component.text("You are not allowed to log in to this server.");
final ServerSideConnectionEvent.Intent event = SpongeEventFactory.createServerSideConnectionEventIntent(
PhaseTracker.getCauseStackManager().currentCause(), message, message, (ServerSideConnection) connection, false);
if (connection.postGuardedEvent(event)) {
ci.cancel();
final net.minecraft.network.chat.Component kickReason = SpongeAdventure.asVanilla(event.message());
this.connection.setupOutboundProtocol(LoginProtocols.CLIENTBOUND);
this.connection.send(new ClientboundLoginDisconnectPacket(kickReason));
this.connection.disconnect(kickReason);
}
}

@Redirect(method = "handleIntention", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/Connection;setupOutboundProtocol(Lnet/minecraft/network/ProtocolInfo;)V"))
private void impl$onSetupOutboundProtocol(final Connection instance, final ProtocolInfo<?> $$0) {
//Moved to impl$onLogin
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import net.kyori.adventure.text.Component;
import net.minecraft.network.Connection;
import net.minecraft.network.ProtocolInfo;
import net.minecraft.network.protocol.handshake.ClientIntentionPacket;
import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket;
import net.minecraft.network.protocol.login.LoginProtocols;
Expand Down Expand Up @@ -78,13 +79,13 @@ public abstract class ServerHandshakePacketListenerImplMixin implements ServerHa
target = "Lnet/minecraft/server/MinecraftServer;getStatus()Lnet/minecraft/network/protocol/status/ServerStatus;")),
cancellable = true)
private void impl$onLogin(final ClientIntentionPacket $$0, final CallbackInfo ci) {
this.connection.setupOutboundProtocol(LoginProtocols.CLIENTBOUND);
final SpongeEngineConnection connection = ((ConnectionBridge) this.connection).bridge$getEngineConnection();
final Component message = Component.text("You are not allowed to log in to this server.");
final ServerSideConnectionEvent.Intent event = SpongeEventFactory.createServerSideConnectionEventIntent(
PhaseTracker.getCauseStackManager().currentCause(), message, message, (ServerSideConnection) connection, false);
if (connection.postGuardedEvent(event)) {
final net.minecraft.network.chat.Component kickReason = SpongeAdventure.asVanilla(event.message());
this.connection.setupOutboundProtocol(LoginProtocols.CLIENTBOUND);
this.connection.send(new ClientboundLoginDisconnectPacket(kickReason));
this.connection.disconnect(kickReason);
ci.cancel();
Expand All @@ -93,6 +94,7 @@ public abstract class ServerHandshakePacketListenerImplMixin implements ServerHa

@Redirect(method = "handleIntention", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;acceptsTransfers()Z"))
private boolean impl$onTransfer(final MinecraftServer instance) {
this.connection.setupOutboundProtocol(LoginProtocols.CLIENTBOUND);
this.impl$transferred = true;
final SpongeEngineConnection connection = ((ConnectionBridge) this.connection).bridge$getEngineConnection();
final Component message = Component.translatable("multiplayer.disconnect.transfers_disabled");
Expand All @@ -109,4 +111,17 @@ public abstract class ServerHandshakePacketListenerImplMixin implements ServerHa
private net.minecraft.network.chat.Component impl$setTransferDisconnectMessage(final net.minecraft.network.chat.Component component) {
return ((ConnectionBridge) this.connection).bridge$getKickReason();
}

@Redirect(method = "handleIntention", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/Connection;setupOutboundProtocol(Lnet/minecraft/network/ProtocolInfo;)V"),
slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;acceptsTransfers()Z")))
private void impl$onSetupOutboundProtocol(final Connection instance, final ProtocolInfo<?> $$0) {
//Moved to impl$onTransfer
}

@Redirect(method = "beginLogin", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/Connection;setupOutboundProtocol(Lnet/minecraft/network/ProtocolInfo;)V"))
private void impl$onSetupOutboundProtocol2(final Connection instance, final ProtocolInfo<?> $$0) {
//Moved to impl$onLogin
}
}
Loading

0 comments on commit c49fa9f

Please sign in to comment.