Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass handler to clientside ConfigurationPacketHandler #3305

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.listener.ServerCommonPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.thread.ThreadExecutor;

Expand Down Expand Up @@ -105,10 +106,10 @@ public void receive(MinecraftClient client, ClientConfigurationNetworkHandler ne
// Normally, packets are handled on the network IO thread - though it is
// not guaranteed (for example, with 1.19.4 S2C packet bundling)
// Since we're handling it right now, connection check is redundant.
handler.receive(packet, sender);
handler.receive(packet, networkHandler, sender);
} else {
client.execute(() -> {
if (((ClientCommonNetworkHandlerAccessor) networkHandler).getConnection().isOpen()) handler.receive(packet, sender);
if (((ClientCommonNetworkHandlerAccessor) networkHandler).getConnection().isOpen()) handler.receive(packet, networkHandler, sender);
});
}
}
Expand Down Expand Up @@ -218,10 +219,10 @@ public void receive(MinecraftClient client, ClientConfigurationNetworkHandler ne
// Normally, packets are handled on the network IO thread - though it is
// not guaranteed (for example, with 1.19.4 S2C packet bundling)
// Since we're handling it right now, connection check is redundant.
handler.receive(packet, sender);
handler.receive(packet, networkHandler, sender);
} else {
client.execute(() -> {
if (((ClientCommonNetworkHandlerAccessor) networkHandler).getConnection().isOpen()) handler.receive(packet, sender);
if (((ClientCommonNetworkHandlerAccessor) networkHandler).getConnection().isOpen()) handler.receive(packet, networkHandler, sender);
});
}
}
Expand Down Expand Up @@ -393,6 +394,14 @@ public static <T extends FabricPacket> void send(T packet) {
throw new IllegalStateException("Cannot send packet while not configuring!");
}

/**
* Disconnects from the server.
* @param handler the network handler
*/
public static void disconnect(ClientConfigurationNetworkHandler handler, Text reason) {
((ClientCommonNetworkHandlerAccessor) handler).getConnection().disconnect(reason);
}

private ClientConfigurationNetworking() {
}

Expand Down Expand Up @@ -451,9 +460,10 @@ public interface ConfigurationPacketHandler<T extends FabricPacket> {
*
*
* @param packet the packet
* @param networkHandler the network handler
* @param responseSender the packet sender
* @see FabricPacket
*/
void receive(T packet, PacketSender responseSender);
void receive(T packet, ClientConfigurationNetworkHandler networkHandler, PacketSender responseSender);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class NetworkingConfigurationClientTest implements ClientModInitializer {
@Override
public void onInitializeClient() {
ClientConfigurationNetworking.registerGlobalReceiver(NetworkingConfigurationTest.ConfigurationPacket.PACKET_TYPE, (packet, responseSender) -> {
ClientConfigurationNetworking.registerGlobalReceiver(NetworkingConfigurationTest.ConfigurationPacket.PACKET_TYPE, (packet, handler, responseSender) -> {
// Handle stuff here

// Respond back to the server that the task is complete
Expand Down