Skip to content

Commit

Permalink
Finish networking pain
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Jul 4, 2024
1 parent 6ebbd01 commit ab46b39
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 170 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.client;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl;
import net.minecraft.network.Connection;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.common.extensions.ICommonPacketListener;
import org.sinytra.fabric.networking_api.client.NeoClientCommonNetworking;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.Set;

@Mixin(ClientCommonPacketListenerImpl.class)
public abstract class ClientCommonNetworkHandlerMixin {
@WrapOperation(method = "handleCustomPayload(Lnet/minecraft/network/protocol/common/ClientboundCustomPayloadPacket;)V", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/network/registration/NetworkRegistry;onMinecraftRegister(Lnet/minecraft/network/Connection;Ljava/util/Set;)V"))
public void onCustomPayloadRegisterPacket(Connection connection, Set<ResourceLocation> channels, Operation<Void> original) {
original.call(connection, channels);
NeoClientCommonNetworking.onRegisterPacket((ICommonPacketListener) this, channels);
}

@WrapOperation(method = "handleCustomPayload(Lnet/minecraft/network/protocol/common/ClientboundCustomPayloadPacket;)V", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/network/registration/NetworkRegistry;onMinecraftUnregister(Lnet/minecraft/network/Connection;Ljava/util/Set;)V"))
public void onCustomPayloadUnregisterPacket(Connection connection, Set<ResourceLocation> channels, Operation<Void> original) {
original.call(connection, channels);
NeoClientCommonNetworking.onUnregisterPacket((ICommonPacketListener) this, channels);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket;
import org.sinytra.fabric.networking_api.NeoListenableNetworkHandler;
import org.sinytra.fabric.networking_api.client.NeoClientConfigurationNetworking;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -38,13 +39,15 @@ protected ClientConfigurationNetworkHandlerMixin(Minecraft client, Connection co

@Inject(method = "<init>", at = @At("RETURN"))
private void initAddon(CallbackInfo ci) {
NeoClientConfigurationNetworking.setClientConfigurationAddon(this);
ClientConfigurationConnectionEvents.INIT.invoker().onConfigurationInit((ClientConfigurationPacketListenerImpl) (Object) this, this.minecraft);
}

@Inject(method = "handleConfigurationFinished", at = @At(value = "NEW", target = "(Lnet/minecraft/client/Minecraft;Lnet/minecraft/network/Connection;Lnet/minecraft/client/multiplayer/CommonListenerCookie;)Lnet/minecraft/client/multiplayer/ClientPacketListener;"))
public void handleComplete(ClientboundFinishConfigurationPacket packet, CallbackInfo ci) {
ClientConfigurationConnectionEvents.COMPLETE.invoker().onConfigurationComplete((ClientConfigurationPacketListenerImpl) (Object) this, this.minecraft);
ClientConfigurationConnectionEvents.READY.invoker().onConfigurationReady((ClientConfigurationPacketListenerImpl) (Object) this, this.minecraft);
NeoClientConfigurationNetworking.setClientConfigurationAddon(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"accessor.ClientLoginNetworkHandlerAccessor",
"accessor.ConnectScreenAccessor",
"accessor.MinecraftClientAccessor",
"ClientCommonNetworkHandlerMixin",
"ClientConfigurationNetworkHandlerMixin",
"ClientLoginNetworkHandlerMixin",
"ClientPlayNetworkHandlerMixin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
import net.fabricmc.fabric.impl.networking.PayloadTypeRegistryImpl;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientConfigurationPacketListenerImpl;
import net.minecraft.network.ConnectionProtocol;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
Expand All @@ -13,8 +16,10 @@
import net.minecraft.network.protocol.common.ServerCommonPacketListener;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.network.protocol.configuration.ClientConfigurationPacketListener;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.common.extensions.ICommonPacketListener;
import net.neoforged.neoforge.network.registration.NetworkPayloadSetup;
import net.neoforged.neoforge.network.registration.NetworkRegistry;
import org.sinytra.fabric.networking_api.NeoNetworkRegistrar;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -69,4 +74,9 @@ private static boolean includeFabricChannels(ICommonPacketListener listener, Res
// TODO Use original args that include the packet
return original.call(listener, location) || NeoNetworkRegistrar.hasCodecFor(listener.protocol(), listener.flow() == PacketFlow.SERVERBOUND ? PacketFlow.CLIENTBOUND : PacketFlow.SERVERBOUND, location);
}

@Inject(method = "initializeNeoForgeConnection(Lnet/minecraft/network/protocol/configuration/ClientConfigurationPacketListener;Lnet/neoforged/neoforge/network/registration/NetworkPayloadSetup;)V", at = @At("TAIL"))
private static void startConfiguration(ClientConfigurationPacketListener listener, NetworkPayloadSetup setup, CallbackInfo ci) {
ClientConfigurationConnectionEvents.START.invoker().onConfigurationStart((ClientConfigurationPacketListenerImpl) listener, Minecraft.getInstance());
}
}
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 com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.network.Connection;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
import net.neoforged.neoforge.common.extensions.ICommonPacketListener;
import org.sinytra.fabric.networking_api.server.NeoServerCommonNetworking;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.Set;

@Mixin(ServerCommonPacketListenerImpl.class)
public abstract class ServerCommonNetworkHandlerMixin {

@WrapOperation(method = "handleCustomPayload", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/network/registration/NetworkRegistry;onMinecraftRegister(Lnet/minecraft/network/Connection;Ljava/util/Set;)V"))
public void onCustomPayloadRegisterPacket(Connection connection, Set<ResourceLocation> channels, Operation<Void> original) {
original.call(connection, channels);
NeoServerCommonNetworking.onRegisterPacket((ICommonPacketListener) this, channels);
}

@WrapOperation(method = "handleCustomPayload", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/network/registration/NetworkRegistry;onMinecraftUnregister(Lnet/minecraft/network/Connection;Ljava/util/Set;)V"))
public void onCustomPayloadUnregisterPacket(Connection connection, Set<ResourceLocation> channels, Operation<Void> original) {
original.call(connection, channels);
NeoServerCommonNetworking.onUnregisterPacket((ICommonPacketListener) this, channels);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@
package net.fabricmc.fabric.mixin.networking;

import net.fabricmc.fabric.api.networking.v1.FabricServerConfigurationNetworkHandler;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents;
import net.minecraft.server.network.ConfigurationTask;
import net.minecraft.server.network.ServerConfigurationPacketListenerImpl;
import net.neoforged.neoforge.common.extensions.IServerConfigurationPacketListenerExtension;
import org.sinytra.fabric.networking_api.NeoListenableNetworkHandler;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Queue;

// We want to apply a bit earlier than other mods which may not use us in order to prevent refCount issues
@Mixin(value = ServerConfigurationPacketListenerImpl.class, priority = 900)
public abstract class ServerConfigurationNetworkHandlerMixin implements FabricServerConfigurationNetworkHandler {
public abstract class ServerConfigurationNetworkHandlerMixin implements FabricServerConfigurationNetworkHandler, NeoListenableNetworkHandler {
@Shadow
@Final
private Queue<ConfigurationTask> configurationTasks;
Expand All @@ -42,4 +47,14 @@ public void addTask(ConfigurationTask task) {
public void completeTask(ConfigurationTask.Type key) {
((IServerConfigurationPacketListenerExtension) this).finishCurrentTask(key);
}

@Inject(method = "runConfiguration", at = @At("HEAD"))
private void onPreConfiguration(CallbackInfo ci) {
ServerConfigurationConnectionEvents.BEFORE_CONFIGURE.invoker().onSendConfiguration((ServerConfigurationPacketListenerImpl) (Object) this, ((ServerConfigurationPacketListenerImpl) (Object) this).server);
}

@Override
public void handleDisconnect() {
ServerConfigurationConnectionEvents.DISCONNECT.invoker().onConfigureDisconnect((ServerConfigurationPacketListenerImpl) (Object) this, ((ServerConfigurationPacketListenerImpl) (Object) this).server);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import net.minecraft.resources.ResourceLocation;

public class NeoCommonNetworking {
public static final NeoNetworkRegistrar CONFIGURATION_REGISTRY = new NeoNetworkRegistrar(ConnectionProtocol.CONFIGURATION);
public static final NeoNetworkRegistrar PLAY_REGISTRY = new NeoNetworkRegistrar(ConnectionProtocol.PLAY);

public static final int DEFAULT_CHANNEL_NAME_MAX_LENGTH = 128;

public static void assertPayloadType(PayloadTypeRegistryImpl<?> payloadTypeRegistry, ResourceLocation channelName, PacketFlow side, ConnectionProtocol phase) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package org.sinytra.fabric.networking_api;

import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.SharedConstants;
import net.minecraft.server.commands.DebugConfigCommand;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.minecraft.server.network.ServerConfigurationPacketListenerImpl;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.OnDatapackSyncEvent;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.network.event.RegisterConfigurationTasksEvent;
import org.sinytra.fabric.networking_api.generated.GeneratedEntryPoint;
import org.sinytra.fabric.networking_api.server.NeoServerPlayNetworking;

@EventBusSubscriber
@Mod(GeneratedEntryPoint.MOD_ID)
public class NetworkingEventHooks {

@SubscribeEvent
public static void registerCommands(RegisterCommandsEvent event) {
public NetworkingEventHooks(IEventBus bus) {
bus.addListener(NetworkingEventHooks::onConfiguration);
NeoForge.EVENT_BUS.addListener(NetworkingEventHooks::registerCommands);
NeoForge.EVENT_BUS.addListener(NetworkingEventHooks::onPlayerReady);
}

private static void registerCommands(RegisterCommandsEvent event) {
if (SharedConstants.IS_RUNNING_IN_IDE) {
// Command is registered when isDevelopment is set.
return;
Expand All @@ -27,10 +37,14 @@ public static void registerCommands(RegisterCommandsEvent event) {
DebugConfigCommand.register(event.getDispatcher());
}

@SubscribeEvent
public static void onPlayerReady(OnDatapackSyncEvent event) {
private static void onPlayerReady(OnDatapackSyncEvent event) {
if (event.getPlayer() != null) {
NeoServerPlayNetworking.onClientReady(event.getPlayer());
}
}

private static void onConfiguration(RegisterConfigurationTasksEvent event) {
ServerConfigurationPacketListenerImpl listener = (ServerConfigurationPacketListenerImpl) event.getListener();
ServerConfigurationConnectionEvents.CONFIGURE.invoker().onSendConfiguration(listener, listener.server);
}
}
Loading

0 comments on commit ab46b39

Please sign in to comment.