Skip to content

Commit 862738d

Browse files
committed
Change handshake intent from transfer when connecting first
1 parent 478a1b8 commit 862738d

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

src/main/java/net/lenni0451/miniconnect/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void onEnable() {
4545
ViaProxy.EVENT_MANAGER.register(new HAProxyEnableHandler());
4646
ViaProxy.EVENT_MANAGER.register(new TargetOnlineModeHandler());
4747
ViaProxy.EVENT_MANAGER.register(new ProxyOnlineModeHandler());
48+
ViaProxy.EVENT_MANAGER.register(new HandshakeIntentListener());
4849
}
4950

5051
}

src/main/java/net/lenni0451/miniconnect/proxy/StateRegistry.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class StateRegistry {
1616
private final Map<InetAddress, ConnectionInfo> reconnectTargets = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).<InetAddress, ConnectionInfo>build().asMap();
1717
private final Map<InetAddress, ConnectionInfo> lobbyTargets = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).<InetAddress, ConnectionInfo>build().asMap();
1818
private final Set<UUID> verificationQueue = Collections.newSetFromMap(CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).<UUID, Boolean>build().asMap());
19+
private final Set<InetAddress> changeHandshakeIntent = Collections.newSetFromMap(CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).<InetAddress, Boolean>build().asMap());
1920

2021
public Map<InetAddress, ConnectionInfo> getConnectionTargets() {
2122
return this.connectionTargets;
@@ -33,4 +34,8 @@ public Set<UUID> getVerificationQueue() {
3334
return this.verificationQueue;
3435
}
3536

37+
public Set<InetAddress> getChangeHandshakeIntent() {
38+
return this.changeHandshakeIntent;
39+
}
40+
3641
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.lenni0451.miniconnect.proxy.event;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.SimpleChannelInboundHandler;
5+
import net.lenni0451.lambdaevents.EventHandler;
6+
import net.lenni0451.miniconnect.Main;
7+
import net.lenni0451.miniconnect.utils.ChannelUtils;
8+
import net.raphimc.netminecraft.constants.IntendedState;
9+
import net.raphimc.netminecraft.constants.MCPipeline;
10+
import net.raphimc.netminecraft.packet.Packet;
11+
import net.raphimc.netminecraft.packet.impl.handshaking.C2SHandshakingClientIntentionPacket;
12+
import net.raphimc.viaproxy.plugins.events.Client2ProxyChannelInitializeEvent;
13+
import net.raphimc.viaproxy.plugins.events.types.ITyped;
14+
15+
public class HandshakeIntentListener {
16+
17+
@EventHandler
18+
public void onClient2ProxyChannelInitialize(final Client2ProxyChannelInitializeEvent event) {
19+
if (!event.getType().equals(ITyped.Type.POST)) return;
20+
event.getChannel().pipeline().addBefore(MCPipeline.HANDLER_HANDLER_NAME, "miniconnect-intent-changer", new SimpleChannelInboundHandler<Packet>() {
21+
@Override
22+
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Packet packet) {
23+
if (packet instanceof C2SHandshakingClientIntentionPacket clientIntention) {
24+
if (clientIntention.intendedState.equals(IntendedState.TRANSFER)) {
25+
if (Main.getInstance().getStateRegistry().getChangeHandshakeIntent().remove(ChannelUtils.getChannelAddress(event.getChannel()))) {
26+
clientIntention.intendedState = IntendedState.LOGIN;
27+
}
28+
}
29+
}
30+
channelHandlerContext.fireChannelRead(packet);
31+
}
32+
});
33+
}
34+
35+
}

src/main/java/net/lenni0451/miniconnect/server/states/play/screen/impl/MainScreen.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ public void init(ScreenHandler screenHandler, ItemList itemList) {
144144
Logger.LOGGER.error("Failed to save player config", e);
145145
}
146146
}
147-
Main.getInstance().getStateRegistry().getConnectionTargets().put(
148-
ChannelUtils.getChannelAddress(screenHandler.getStateHandler().getChannel()),
149-
playerConfig.toConnectionInfo()
150-
);
147+
InetAddress channelAddress = ChannelUtils.getChannelAddress(screenHandler.getStateHandler().getChannel());
148+
Main.getInstance().getStateRegistry().getConnectionTargets().put(channelAddress, playerConfig.toConnectionInfo());
149+
Main.getInstance().getStateRegistry().getChangeHandshakeIntent().add(channelAddress);
151150
screenHandler.getStateHandler().send(new S2CTransferPacket(playerConfig.handshakeAddress, playerConfig.handshakePort));
152151
} else {
153152
screenHandler.getStateHandler().send(new S2CSystemChatPacket(new StringComponent("§cYou need to set all options before connecting"), false));

0 commit comments

Comments
 (0)