mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-05 19:47:00 -04:00
Add ServerPlayNetworking.reconfigure (#4493)
* Add ServerPlayNetworking.reconfigure * Add ServerConfigurationNetworking.isReconfiguring, allowing tasks to be skipped if not required again.
This commit is contained in:
parent
a5c5487b68
commit
db5e66823d
7 changed files with 87 additions and 2 deletions
fabric-networking-api-v1/src
main/java/net/fabricmc/fabric
api/networking/v1
impl/networking/server
mixin/networking
testmod/java/net/fabricmc/fabric/test/networking
|
@ -235,6 +235,18 @@ public final class ServerConfigurationNetworking {
|
|||
return ((ServerCommonNetworkHandlerAccessor) handler).getServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the client has previously completed configuration, and has re-entered the configuration phase.
|
||||
*
|
||||
* @param handler the server configuration network handler
|
||||
* @return {@code true} if the client is reconfiguring
|
||||
*/
|
||||
public static boolean isReconfiguring(ServerConfigurationNetworkHandler handler) {
|
||||
Objects.requireNonNull(handler, "Server configuration network handler cannot be null");
|
||||
|
||||
return ServerNetworkingImpl.getAddon(handler).isReconfiguring();
|
||||
}
|
||||
|
||||
private ServerConfigurationNetworking() {
|
||||
}
|
||||
|
||||
|
|
|
@ -293,6 +293,28 @@ public final class ServerPlayNetworking {
|
|||
player.networkHandler.sendPacket(createS2CPacket(payload));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the player back into configuration phase and re-run all of the configuration tasks.
|
||||
*
|
||||
* @param player the player
|
||||
*/
|
||||
public static void reconfigure(ServerPlayerEntity player) {
|
||||
Objects.requireNonNull(player, "Server player entity cannot be null");
|
||||
|
||||
reconfigure(player.networkHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the player back into configuration phase and re-run all of the configuration tasks.
|
||||
*
|
||||
* @param handler the network handler
|
||||
*/
|
||||
public static void reconfigure(ServerPlayNetworkHandler handler) {
|
||||
Objects.requireNonNull(handler, "Server play network handler cannot be null");
|
||||
|
||||
ServerNetworkingImpl.getAddon(handler).reconfigure();
|
||||
}
|
||||
|
||||
private ServerPlayNetworking() {
|
||||
}
|
||||
|
||||
|
|
|
@ -49,6 +49,7 @@ public final class ServerConfigurationNetworkAddon extends AbstractChanneledNetw
|
|||
private RegisterState registerState = RegisterState.NOT_SENT;
|
||||
@Nullable
|
||||
private String clientBrand = null;
|
||||
private boolean isReconfiguring = false;
|
||||
|
||||
public ServerConfigurationNetworkAddon(ServerConfigurationNetworkHandler handler, MinecraftServer server) {
|
||||
super(ServerNetworkingImpl.CONFIGURATION, ((ServerCommonNetworkHandlerAccessor) handler).getConnection(), "ServerConfigurationNetworkAddon for " + handler.getDebugProfile().getName());
|
||||
|
@ -188,6 +189,14 @@ public final class ServerConfigurationNetworkAddon extends AbstractChanneledNetw
|
|||
return clientBrand;
|
||||
}
|
||||
|
||||
public boolean isReconfiguring() {
|
||||
return isReconfiguring;
|
||||
}
|
||||
|
||||
public void setReconfiguring() {
|
||||
isReconfiguring = true;
|
||||
}
|
||||
|
||||
private enum RegisterState {
|
||||
NOT_SENT,
|
||||
SENT,
|
||||
|
|
|
@ -41,9 +41,11 @@ import net.fabricmc.fabric.impl.networking.RegistrationPayload;
|
|||
public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon<ServerPlayNetworking.PlayPayloadHandler<?>> {
|
||||
private final ServerPlayNetworkHandler handler;
|
||||
private final MinecraftServer server;
|
||||
private boolean sentInitialRegisterPacket;
|
||||
private final ServerPlayNetworking.Context context;
|
||||
|
||||
private boolean sentInitialRegisterPacket;
|
||||
private boolean requestedReconfigure = false;
|
||||
|
||||
public ServerPlayNetworkAddon(ServerPlayNetworkHandler handler, ClientConnection connection, MinecraftServer server) {
|
||||
super(ServerNetworkingImpl.PLAY, connection, "ServerPlayNetworkAddon for " + handler.player.getDisplayName());
|
||||
this.handler = handler;
|
||||
|
@ -129,6 +131,19 @@ public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon<
|
|||
return NetworkingImpl.isReservedCommonChannel(channelName);
|
||||
}
|
||||
|
||||
public void reconfigure() {
|
||||
if (requestedReconfigure) {
|
||||
throw new IllegalStateException("Already requested reconfigure");
|
||||
}
|
||||
|
||||
requestedReconfigure = true;
|
||||
handler.reconfigure();
|
||||
}
|
||||
|
||||
public boolean requestedReconfigure() {
|
||||
return requestedReconfigure;
|
||||
}
|
||||
|
||||
private record ContextImpl(MinecraftServer server, ServerPlayNetworkHandler handler, PacketSender responseSender) implements ServerPlayNetworking.Context {
|
||||
private ContextImpl {
|
||||
Objects.requireNonNull(server, "server");
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.networking;
|
||||
|
||||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
||||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Unique;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
|
@ -23,14 +25,18 @@ import org.spongepowered.asm.mixin.injection.Inject;
|
|||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import net.minecraft.network.NetworkState;
|
||||
import net.minecraft.network.listener.PacketListener;
|
||||
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
|
||||
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.ServerPlayNetworkHandler;
|
||||
|
||||
import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions;
|
||||
import net.fabricmc.fabric.impl.networking.UntrackedNetworkHandler;
|
||||
import net.fabricmc.fabric.impl.networking.server.ServerNetworkingImpl;
|
||||
import net.fabricmc.fabric.impl.networking.server.ServerPlayNetworkAddon;
|
||||
|
||||
// We want to apply a bit earlier than other mods which may not use us in order to prevent refCount issues
|
||||
|
@ -60,6 +66,18 @@ abstract class ServerPlayNetworkHandlerMixin extends ServerCommonNetworkHandler
|
|||
}
|
||||
}
|
||||
|
||||
@WrapOperation(method = "onAcknowledgeReconfiguration", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;transitionInbound(Lnet/minecraft/network/NetworkState;Lnet/minecraft/network/listener/PacketListener;)V"))
|
||||
private <T extends PacketListener> void onAcknowledgeReconfiguration(ClientConnection instance, NetworkState<T> state, T packetListener, Operation<Void> original) {
|
||||
original.call(instance, state, packetListener);
|
||||
|
||||
ServerConfigurationNetworkHandler networkHandler = (ServerConfigurationNetworkHandler) packetListener;
|
||||
ServerNetworkingImpl.getAddon(networkHandler).setReconfiguring();
|
||||
|
||||
if (addon.requestedReconfigure()) {
|
||||
networkHandler.sendConfigurations();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerPlayNetworkAddon getAddon() {
|
||||
return this.addon;
|
||||
|
|
|
@ -50,6 +50,10 @@ public class NetworkingConfigurationTest implements ModInitializer {
|
|||
PayloadTypeRegistry.configurationC2S().register(ConfigurationStartPacket.ID, ConfigurationStartPacket.CODEC);
|
||||
|
||||
ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> {
|
||||
if (ServerConfigurationNetworking.isReconfiguring(handler)) {
|
||||
LOGGER.info("Reconfiguring client");
|
||||
}
|
||||
|
||||
// You must check to see if the client can handle your config task
|
||||
if (ServerConfigurationNetworking.canSend(handler, ConfigurationPacket.ID)) {
|
||||
handler.addTask(new TestConfigurationTask("Example data"));
|
||||
|
|
|
@ -90,7 +90,12 @@ public final class NetworkingPlayPacketTest implements ModInitializer {
|
|||
));
|
||||
ServerPlayNetworking.getSender(ctx.getSource().getPlayer()).sendPacket(packet);
|
||||
return Command.SINGLE_SUCCESS;
|
||||
})));
|
||||
}))
|
||||
.then(literal("reconfigure").executes(ctx -> {
|
||||
ServerPlayNetworking.reconfigure(ctx.getSource().getPlayer());
|
||||
return Command.SINGLE_SUCCESS;
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Add table
Reference in a new issue