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

[1.20.2] Support common registration packets. Add configuration task API #3244

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ allprojects {

testImplementation "net.fabricmc:fabric-loader-junit:${project.loader_version}"
testImplementation sourceSets.testmodClient.output
testImplementation 'org.mockito:mockito-core:5.4.0'
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import net.minecraft.util.thread.ThreadExecutor;

import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
Expand Down Expand Up @@ -384,9 +383,14 @@ public static <T extends FabricPacket> void send(T packet) {
Objects.requireNonNull(packet, "Packet cannot be null");
Objects.requireNonNull(packet.getType(), "Packet#getType cannot return null");

PacketByteBuf buf = PacketByteBufs.create();
packet.write(buf);
send(packet.getType().getId(), buf);
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
addon.sendPacket(packet);
return;
}

throw new IllegalStateException("Cannot send packet while not configuring!");
}

private ClientConfigurationNetworking() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import net.minecraft.util.thread.ThreadExecutor;

import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
Expand Down Expand Up @@ -339,6 +338,16 @@ public static Packet<ServerCommonPacketListener> createC2SPacket(Identifier chan
return ClientNetworkingImpl.createC2SPacket(channelName, buf);
}

/**
* Creates a packet which may be sent to the connected server.
*
* @param packet the fabric packet
* @return a new packet
*/
public static <T extends FabricPacket> Packet<ServerCommonPacketListener> createC2SPacket(T packet) {
return ClientNetworkingImpl.createC2SPacket(packet);
}

/**
* Gets the packet sender which sends packets to the connected server.
*
Expand Down Expand Up @@ -381,9 +390,13 @@ public static <T extends FabricPacket> void send(T packet) {
Objects.requireNonNull(packet, "Packet cannot be null");
Objects.requireNonNull(packet.getType(), "Packet#getType cannot return null");

PacketByteBuf buf = PacketByteBufs.create();
packet.write(buf);
send(packet.getType().getId(), buf);
// You cant send without a client player, so this is fine
if (MinecraftClient.getInstance().getNetworkHandler() != null) {
MinecraftClient.getInstance().getNetworkHandler().sendPacket(createC2SPacket(packet));
return;
}

throw new IllegalStateException("Cannot send packets when not in game!");
}

private ClientPlayNetworking() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon;
import net.fabricmc.fabric.impl.networking.ChannelInfoHolder;
import net.fabricmc.fabric.impl.networking.NetworkingImpl;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
import net.fabricmc.fabric.mixin.networking.client.accessor.ClientCommonNetworkHandlerAccessor;
import net.fabricmc.fabric.mixin.networking.client.accessor.ClientConfigurationNetworkHandlerAccessor;
import net.fabricmc.fabric.mixin.networking.client.accessor.ClientLoginNetworkHandlerAccessor;

public final class ClientConfigurationNetworkAddon extends AbstractChanneledNetworkAddon<ClientConfigurationNetworking.ConfigurationChannelHandler> {
private final ClientConfigurationNetworkHandler handler;
Expand Down Expand Up @@ -65,8 +67,17 @@ public void lateInit() {
}

public void onServerReady() {
this.sendInitialChannelRegistrationPacket();
this.sentInitialRegisterPacket = true;
// Do nothing for now
}

@Override
protected void receiveRegistration(boolean register, PacketByteBuf buf) {
super.receiveRegistration(register, buf);

if (register && !this.sentInitialRegisterPacket) {
this.sendInitialChannelRegistrationPacket();
this.sentInitialRegisterPacket = true;
}
}

/**
Expand Down Expand Up @@ -100,6 +111,11 @@ public Packet<?> createPacket(Identifier channelName, PacketByteBuf buf) {
return ClientPlayNetworking.createC2SPacket(channelName, buf);
}

@Override
public Packet<?> createPacket(FabricPacket packet) {
return ClientPlayNetworking.createC2SPacket(packet);
}

@Override
protected void invokeRegisterEvent(List<Identifier> ids) {
C2SPlayChannelEvents.REGISTER_CONFIGURATION.invoker().onChannelRegister(this.handler, this, this.client, ids);
Expand Down Expand Up @@ -147,6 +163,10 @@ protected void invokeDisconnectEvent() {

@Override
protected boolean isReservedChannel(Identifier channelName) {
return NetworkingImpl.isReservedPlayChannel(channelName);
return NetworkingImpl.isReservedCommonChannel(channelName);
}

public ChannelInfoHolder getChannelInfoHolder() {
return (ChannelInfoHolder) ((ClientLoginNetworkHandlerAccessor) handler).getConnection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package net.fabricmc.fabric.impl.networking.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.Objects;

import org.jetbrains.annotations.Nullable;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ConnectScreen;
import net.minecraft.client.network.ClientConfigurationNetworkHandler;
import net.minecraft.client.network.ClientLoginNetworkHandler;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.ClientConnection;
Expand All @@ -40,27 +38,35 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.impl.networking.ChannelInfoHolder;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.impl.networking.CommonPacketsImpl;
import net.fabricmc.fabric.impl.networking.CommonRegisterPayload;
import net.fabricmc.fabric.impl.networking.CommonVersionPayload;
import net.fabricmc.fabric.impl.networking.GlobalReceiverRegistry;
import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions;
import net.fabricmc.fabric.impl.networking.NetworkingImpl;
import net.fabricmc.fabric.impl.networking.payload.FabricPacketPayload;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
import net.fabricmc.fabric.mixin.networking.client.accessor.ClientLoginNetworkHandlerAccessor;
import net.fabricmc.fabric.mixin.networking.client.accessor.ConnectScreenAccessor;
import net.fabricmc.fabric.mixin.networking.client.accessor.MinecraftClientAccessor;

public final class ClientNetworkingImpl {
public static final GlobalReceiverRegistry<ClientLoginNetworking.LoginQueryRequestHandler> LOGIN = new GlobalReceiverRegistry<>();
public static final GlobalReceiverRegistry<ClientConfigurationNetworking.ConfigurationChannelHandler> CONFIGURATION = new GlobalReceiverRegistry<>();
public static final GlobalReceiverRegistry<ClientPlayNetworking.PlayChannelHandler> PLAY = new GlobalReceiverRegistry<>();
public static final GlobalReceiverRegistry<ClientLoginNetworking.LoginQueryRequestHandler> LOGIN = new GlobalReceiverRegistry<>(NetworkState.LOGIN);
public static final GlobalReceiverRegistry<ClientConfigurationNetworking.ConfigurationChannelHandler> CONFIGURATION = new GlobalReceiverRegistry<>(NetworkState.CONFIGURATION);
public static final GlobalReceiverRegistry<ClientPlayNetworking.PlayChannelHandler> PLAY = new GlobalReceiverRegistry<>(NetworkState.PLAY);
private static ClientPlayNetworkAddon currentPlayAddon;
private static ClientConfigurationNetworkAddon currentConfigurationAddon;

public static ClientPlayNetworkAddon getAddon(ClientPlayNetworkHandler handler) {
return (ClientPlayNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon();
}

public static ClientConfigurationNetworkAddon getAddon(ClientConfigurationNetworkHandler handler) {
return (ClientConfigurationNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon();
}

public static ClientLoginNetworkAddon getAddon(ClientLoginNetworkHandler handler) {
return (ClientLoginNetworkAddon) ((NetworkHandlerExtensions) handler).getAddon();
}
Expand All @@ -69,6 +75,19 @@ public static Packet<ServerCommonPacketListener> createC2SPacket(Identifier chan
return new CustomPayloadC2SPacket(new PacketByteBufPayload(channelName, buf));
}

public static Packet<ServerCommonPacketListener> createC2SPacket(FabricPacket packet) {
Objects.requireNonNull(packet, "Packet cannot be null");
Objects.requireNonNull(packet.getType(), "Packet#getType cannot return null");

if (NetworkingImpl.WRITE_FABRIC_PACKET_CALLING_THREAD) {
PacketByteBuf buf = PacketByteBufs.create();
packet.write(buf);
return createC2SPacket(packet.getType().getId(), buf);
}

return new CustomPayloadC2SPacket(new FabricPacketPayload(packet));
}

/**
* Due to the way logging into a integrated or remote dedicated server will differ, we need to obtain the login client connection differently.
*/
Expand Down Expand Up @@ -134,29 +153,44 @@ public static void clientInit() {
currentConfigurationAddon = null;
});

// Register a login query handler for early channel registration.
ClientLoginNetworking.registerGlobalReceiver(NetworkingImpl.EARLY_REGISTRATION_CHANNEL, (client, handler, buf, listenerAdder) -> {
int n = buf.readVarInt();
List<Identifier> ids = new ArrayList<>(n);
// Version packet
ClientConfigurationNetworking.registerGlobalReceiver(CommonVersionPayload.PACKET_ID, (client, handler, buf, responseSender) -> {
var payload = new CommonVersionPayload(buf);
int negotiatedVersion = handleVersionPacket(payload, responseSender);
ClientNetworkingImpl.getAddon(handler).onCommonVersionPacket(negotiatedVersion);
});

for (int i = 0; i < n; i++) {
ids.add(buf.readIdentifier());
// Register packet
ClientConfigurationNetworking.registerGlobalReceiver(CommonRegisterPayload.PACKET_ID, (client, handler, buf, responseSender) -> {
var payload = new CommonRegisterPayload(buf);
ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getAddon(handler);

if (CommonRegisterPayload.PLAY_PHASE.equals(payload.phase())) {
if (payload.version() != addon.getNegotiatedVersion()) {
throw new IllegalStateException("Negotiated common packet version: %d but received packet with version: %d".formatted(addon.getNegotiatedVersion(), payload.version()));
}

addon.getChannelInfoHolder().getPendingChannelsNames(NetworkState.PLAY).addAll(payload.channels());
NetworkingImpl.LOGGER.debug("Received accepted channels from the server");
responseSender.sendPacket(new CommonRegisterPayload(addon.getNegotiatedVersion(), CommonRegisterPayload.PLAY_PHASE, ClientPlayNetworking.getGlobalReceivers()));
} else {
addon.onCommonRegisterPacket(payload);
responseSender.sendPacket(addon.createRegisterPayload());
}
});
}

ClientConnection connection = ((ClientLoginNetworkHandlerAccessor) handler).getConnection();
((ChannelInfoHolder) connection).getPendingChannelsNames(NetworkState.PLAY).addAll(ids);
NetworkingImpl.LOGGER.debug("Received accepted channels from the server");

PacketByteBuf response = PacketByteBufs.create();
Collection<Identifier> channels = ClientPlayNetworking.getGlobalReceivers();
response.writeVarInt(channels.size());
// Disconnect if there are no commonly supported versions.
// Client responds with the intersection of supported versions.
// Return the highest supported version
private static int handleVersionPacket(CommonVersionPayload payload, PacketSender packetSender) {
int[] commonlySupportedVersion = CommonPacketsImpl.intersection(payload.versions(), CommonPacketsImpl.SUPPORTED_COMMON_PACKET_VERSIONS);

for (Identifier id : channels) {
response.writeIdentifier(id);
}
if (commonlySupportedVersion.length == 0) {
throw new UnsupportedOperationException("Client does not support any requested versions from server");
}

NetworkingImpl.LOGGER.debug("Sent accepted channels to the server");
return CompletableFuture.completedFuture(response);
});
packetSender.sendPacket(new CommonVersionPayload(commonlySupportedVersion));
return CommonPacketsImpl.largest(commonlySupportedVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.fabricmc.fabric.api.client.networking.v1.C2SPlayChannelEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon;
import net.fabricmc.fabric.impl.networking.ChannelInfoHolder;
import net.fabricmc.fabric.impl.networking.NetworkingImpl;
Expand Down Expand Up @@ -109,6 +110,11 @@ public Packet<?> createPacket(Identifier channelName, PacketByteBuf buf) {
return ClientPlayNetworking.createC2SPacket(channelName, buf);
}

@Override
public Packet<?> createPacket(FabricPacket packet) {
return ClientPlayNetworking.createC2SPacket(packet);
}

@Override
protected void invokeRegisterEvent(List<Identifier> ids) {
C2SPlayChannelEvents.REGISTER.invoker().onChannelRegister(this.handler, this, this.client, ids);
Expand Down Expand Up @@ -151,6 +157,6 @@ protected void invokeDisconnectEvent() {

@Override
protected boolean isReservedChannel(Identifier channelName) {
return NetworkingImpl.isReservedPlayChannel(channelName);
return NetworkingImpl.isReservedCommonChannel(channelName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
*
* @see ServerPlayNetworking#registerGlobalReceiver(PacketType, ServerPlayNetworking.PlayPacketHandler)
* @see ServerPlayNetworking#send(ServerPlayerEntity, PacketType, FabricPacket)
* @see PacketSender#sendPacket(PacketType, FabricPacket)
* @see PacketSender#sendPacket(FabricPacket)
*/
public interface FabricPacket {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.networking.v1;

import net.minecraft.server.network.ServerConfigurationNetworkHandler;
import net.minecraft.server.network.ServerPlayerConfigurationTask;
import net.minecraft.util.Identifier;

/**
* Fabric-provided extensions for {@link ServerConfigurationNetworkHandler}.
* This interface is automatically implemented via Mixin and interface injection.
*/
public interface FabricServerConfigurationNetworkHandler {
/**
* Enqueue a {@link ServerPlayerConfigurationTask} task to be processed.
*
modmuss50 marked this conversation as resolved.
Show resolved Hide resolved
* <p>Before adding a task use {@link ServerConfigurationNetworking#canSend(ServerConfigurationNetworkHandler, Identifier)}
* to ensure that the client can process this task.
*
* <p>Once the client has handled the task a packet should be sent to the server.
* Upon receiving this packet the server should call {@link FabricServerConfigurationNetworkHandler#completeTask(ServerPlayerConfigurationTask.Key)}
*
* @param task the task
*/
default void addTask(ServerPlayerConfigurationTask task) {
throw new UnsupportedOperationException("Implemented via mixin");
}

/**
*
* @param key the key
*/
default void completeTask(ServerPlayerConfigurationTask.Key key) {
throw new UnsupportedOperationException("Implemented via mixin");
}
}
Loading