Skip to content

Commit

Permalink
Add internal API to get the configuration channels the client accepts…
Browse files Browse the repository at this point in the history
… during play. (#4489)

* Add internal API to get the configuration channels the client accpets during play.

* Actually rerun configuration when using the debugconfig command.
  • Loading branch information
modmuss50 authored Mar 9, 2025
1 parent 4a1de89 commit 97e245f
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.impl.networking;

import java.util.Set;

import org.jetbrains.annotations.Nullable;

import net.minecraft.util.Identifier;

public interface FabricRegistryByteBuf {
void fabric_setSendableConfigurationChannels(Set<Identifier> globalChannels);

@Nullable
Set<Identifier> fabric_getSendableConfigurationChannels();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.mixin.networking;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.server.command.DebugConfigCommand;
import net.minecraft.server.network.ServerConfigurationNetworkHandler;

@Mixin(DebugConfigCommand.class)
public class DebugConfigCommandMixin {
// endConfiguration() does not re-run the configuration tasks. This means we loose the state such as what channels we can send on when in the play phase.
@Redirect(method = "executeUnconfig", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerConfigurationNetworkHandler;endConfiguration()V"))
private static void sendConfigurations(ServerConfigurationNetworkHandler networkHandler) {
networkHandler.sendConfigurations();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.mixin.networking;

import java.util.Objects;
import java.util.Set;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.network.RegistryByteBuf;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.impl.networking.FabricRegistryByteBuf;

@Mixin(RegistryByteBuf.class)
public class RegistryByteBufMixin implements FabricRegistryByteBuf {
@Unique
private Set<Identifier> sendableConfigurationChannels = null;

@Override
public void fabric_setSendableConfigurationChannels(Set<Identifier> globalChannels) {
this.sendableConfigurationChannels = Objects.requireNonNull(globalChannels);
}

@Override
public @Nullable Set<Identifier> fabric_getSendableConfigurationChannels() {
return this.sendableConfigurationChannels;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
package net.fabricmc.fabric.mixin.networking;

import java.util.Queue;
import java.util.Set;
import java.util.function.Function;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -28,13 +33,16 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.network.ClientConnection;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ConnectedClientData;
import net.minecraft.server.network.ServerCommonNetworkHandler;
import net.minecraft.server.network.ServerConfigurationNetworkHandler;
import net.minecraft.server.network.ServerPlayerConfigurationTask;

import net.fabricmc.fabric.api.networking.v1.FabricServerConfigurationNetworkHandler;
import net.fabricmc.fabric.impl.networking.FabricRegistryByteBuf;
import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions;
import net.fabricmc.fabric.impl.networking.server.ServerConfigurationNetworkAddon;

Expand Down Expand Up @@ -163,4 +171,13 @@ public void completeTask(ServerPlayerConfigurationTask.Key key) {
this.currentTask = null;
sendConfigurations();
}

@WrapOperation(method = "onReady", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/RegistryByteBuf;makeFactory(Lnet/minecraft/registry/DynamicRegistryManager;)Ljava/util/function/Function;"))
private Function<ByteBuf, RegistryByteBuf> bindChannelInfo(DynamicRegistryManager registryManager, Operation<Function<ByteBuf, RegistryByteBuf>> original) {
return original.call(registryManager).andThen(registryByteBuf -> {
FabricRegistryByteBuf fabricRegistryByteBuf = (FabricRegistryByteBuf) registryByteBuf;
fabricRegistryByteBuf.fabric_setSendableConfigurationChannels(Set.copyOf(addon.getSendableChannels()));
return registryByteBuf;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"CustomPayloadC2SPacketMixin",
"CustomPayloadS2CPacketMixin",
"CustomPayloadPacketCodecMixin",
"DebugConfigCommandMixin",
"EntityTrackerEntryMixin",
"LoginQueryRequestS2CPacketMixin",
"LoginQueryResponseC2SPacketMixin",
"PlayerManagerMixin",
"RegistryByteBufMixin",
"ServerCommonNetworkHandlerMixin",
"ServerConfigurationNetworkHandlerMixin",
"ServerLoginNetworkHandlerMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
Expand All @@ -37,6 +39,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.text.TextCodecs;
import net.minecraft.util.Identifier;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
Expand All @@ -45,7 +48,9 @@
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.impl.networking.FabricRegistryByteBuf;
import net.fabricmc.fabric.test.networking.NetworkingTestmods;
import net.fabricmc.fabric.test.networking.common.NetworkingCommonTest;
import net.fabricmc.loader.api.FabricLoader;

public final class NetworkingPlayPacketTest implements ModInitializer {
Expand Down Expand Up @@ -132,6 +137,15 @@ public OverlayPacket(RegistryByteBuf buf) {
}

public void write(RegistryByteBuf buf) {
// Test that we can get the configuration channels that the client accepts
FabricRegistryByteBuf fabricRegistryByteBuf = (FabricRegistryByteBuf) buf;
Collection<Identifier> channels = fabricRegistryByteBuf.fabric_getSendableConfigurationChannels();
Objects.requireNonNull(channels);

if (!channels.contains(NetworkingCommonTest.CommonPayload.ID.id())) {
throw new IllegalStateException("Expected common payload channel to be sent");
}

TextCodecs.REGISTRY_PACKET_CODEC.encode(buf, this.message);
}

Expand Down

0 comments on commit 97e245f

Please sign in to comment.