Skip to content

Commit

Permalink
Plugin doesn't send anything during reset. Disconnect handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
adde0109 committed Apr 13, 2023
1 parent 08bdfe9 commit 7cb4890
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "org.adde0109"
version = "1.3.2-beta-rc8"
version = "1.3.2-beta-rc10"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/adde0109/ambassador/Ambassador.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_19_3;
import static com.velocitypowered.proxy.protocol.packet.brigadier.ArgumentIdentifier.mapSet;

@Plugin(id = "ambassador", name = "Ambassador", version = "1.3.2-beta-rc4", authors = {"adde0109"})
@Plugin(id = "ambassador", name = "Ambassador", version = "1.3.2-beta-rc", authors = {"adde0109"})
public class Ambassador {

public ProxyServer server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
VelocityForgeBackendConnectionPhase nextPhase() {
return WAITING_FOR_ACK;
}

@Override
public boolean consideredComplete() {
//Safe if the server hasn't initiated the handshake yet.
return true;
}
},
WAITING_FOR_ACK() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.adde0109.ambassador.velocity.client.FML2CRPMResetCompleteDecoder;
import org.adde0109.ambassador.velocity.client.OutboundForgeHandshakeQueue;
import org.adde0109.ambassador.velocity.client.OutboundSuccessHolder;
import org.adde0109.ambassador.velocity.client.PluginLoginPacketQueue;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -49,11 +50,14 @@ public void resetConnectionPhase(ConnectedPlayer player) {
}
player.getConnectionInFlight().getConnection().getChannel().config().setAutoRead(false);

//Prepare to receive reset ACK and Forge Handshake.
connection.getChannel().pipeline().addBefore(Connections.MINECRAFT_DECODER, ForgeConstants.RESET_LISTENER,new FML2CRPMResetCompleteDecoder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER, ForgeConstants.FORGE_HANDSHAKE_HOLDER,new OutboundForgeHandshakeQueue());
((ForgeLoginWrapperDecoder) connection.getChannel().pipeline().get(ForgeConstants.FORGE_HANDSHAKE_DECODER)).registerLoginWrapperID(98);

//No more PLAY packets past this point should be sent to the client in case the reset works.
connection.write(new PluginMessage("fml:handshake", Unpooled.wrappedBuffer(ForgeHandshakeUtils.generatePluginResetPacket())));
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.PLUGIN_PACKET_QUEUE, new PluginLoginPacketQueue());

player.setPhase(WAITING_RESET);
WAITING_RESET.onTransitionToNewPhase(player);
Expand Down Expand Up @@ -94,6 +98,7 @@ public boolean handle(ConnectedPlayer player, IForgeLoginWrapperPacket msg, Velo
((OutboundSuccessHolder) connection.getChannel().pipeline().get(ForgeConstants.SERVER_SUCCESS_LISTENER))
.sendPacket();
connection.setState(StateRegistry.PLAY);
connection.getChannel().pipeline().remove(ForgeConstants.PLUGIN_PACKET_QUEUE);
((VelocityServer) Ambassador.getInstance().server).registerConnection(player);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.config.PlayerInfoForwarding;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.connection.backend.*;
Expand All @@ -11,6 +12,7 @@
import com.velocitypowered.proxy.protocol.packet.Disconnect;
import com.velocitypowered.proxy.protocol.packet.LoginPluginMessage;
import com.velocitypowered.proxy.protocol.packet.ServerLoginSuccess;
import com.velocitypowered.proxy.util.except.QuietRuntimeException;
import net.kyori.adventure.text.Component;
import org.adde0109.ambassador.Ambassador;
import org.adde0109.ambassador.forge.ForgeConstants;
Expand Down Expand Up @@ -59,12 +61,12 @@ public boolean handle(ServerLoginSuccess packet) {
} else if (player.getConnection().getState() == StateRegistry.LOGIN) {
//Initial vanilla
//Vanilla -> Forge
//Forge -> Forge
MinecraftConnection connection = player.getConnection();
((OutboundSuccessHolder) connection.getChannel().pipeline().get(ForgeConstants.SERVER_SUCCESS_LISTENER))
.sendPacket();
connection.setState(StateRegistry.PLAY);
if (connection.getChannel().pipeline().toMap().containsKey(ForgeConstants.PLUGIN_PACKET_QUEUE))
connection.getChannel().pipeline().remove(ForgeConstants.PLUGIN_PACKET_QUEUE);
connection.getChannel().pipeline().remove(ForgeConstants.PLUGIN_PACKET_QUEUE);
((VelocityServer) Ambassador.getInstance().server).registerConnection(player);
}

Expand All @@ -78,28 +80,32 @@ public boolean handle(ServerLoginSuccess packet) {

@Override
public boolean handle(Disconnect packet) {
if (!serverConnection.getPlayer().getPhase().consideredComplete()) {
serverConnection.getPlayer().handleConnectionException(serverConnection.getServer(), packet,false);
if (!serverConnection.getPhase().consideredComplete()) {
serverConnection.getPlayer().handleConnectionException(serverConnection.getServer(), packet, false);
return true;
}
return original.handle(packet);
}

@Override
public void disconnected() {
if (!serverConnection.getPhase().consideredComplete()
&& serverConnection.getPlayer().getPhase() != VelocityForgeClientConnectionPhase.NOT_STARTED) {
int protocolVersion = serverConnection.getConnection().getProtocolVersion().getProtocol();
if (protocolVersion <= ProtocolVersion.MINECRAFT_1_16_4.getProtocol()) {
//Same as default just not safe.
if (!serverConnection.getPhase().consideredComplete()) {
if (server.getConfiguration().getPlayerInfoForwardingMode() == PlayerInfoForwarding.LEGACY) {
serverConnection.getPlayer().handleConnectionException(serverConnection.getServer(),
Disconnect.create(Component.text("Ambassador: Backend server disconnected during handshake." +
((protocolVersion <= ProtocolVersion.MINECRAFT_1_16_4.getProtocol()) ?
"Could be mismatched mods." : "")),
serverConnection.getPlayer().getProtocolVersion()),false);
return;
}
new QuietRuntimeException("The connection to the remote server was unexpectedly closed.\n"
+ "This is usually because the remote server does not have BungeeCord IP forwarding "
+ "correctly enabled.\nSee https://velocitypowered.com/wiki/users/forwarding/ "
+ "for instructions on how to configure player info forwarding correctly."),
false);
} else {
serverConnection.getPlayer().handleConnectionException(serverConnection.getServer(),
new QuietRuntimeException("The connection to the remote server was unexpectedly closed."),
false);
}
return;
}
original.disconnected();
original.disconnected();
}

public void handleGeneric(MinecraftPacket packet) {
Expand Down

0 comments on commit 7cb4890

Please sign in to comment.