Skip to content

Commit

Permalink
Begin Networking API impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Jan 5, 2024
1 parent c975348 commit f77bd0e
Show file tree
Hide file tree
Showing 58 changed files with 447 additions and 1,844 deletions.
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def moduleDependencies(project, List<String> depNames) {

project.dependencies {
deps.each {
api it
implementation it
}

clientOutputs.each {
Expand Down Expand Up @@ -240,6 +240,13 @@ allprojects {

repositories {
maven { url "https://maven.neoforged.net/releases/" }
maven {
name 'Maven for PR #277' // https://github.com/neoforged/NeoForge/pull/277
url 'https://prmaven.neoforged.net/NeoForge/pr277'
content {
includeModule('net.neoforged', 'neoforge')
}
}
}

dependencies {
Expand Down
14 changes: 9 additions & 5 deletions fabric-networking-api-v1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ version = getSubprojectVersion(project)

moduleDependencies(project, ['fabric-api-base'])

testDependencies(project, [
':fabric-command-api-v2',
':fabric-lifecycle-events-v1',
':fabric-key-binding-api-v1'
])
//testDependencies(project, [
// ':fabric-command-api-v2',
// ':fabric-lifecycle-events-v1',
// ':fabric-key-binding-api-v1'
//])

dependencies {
compileOnly "net.fabricmc:fabric-loader:${rootProject.loader_version}"
}

loom {
accessWidenerPath = file('src/main/resources/fabric-networking-api-v1.accesswidener')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Objects;
import java.util.Set;

import net.fabricmc.fabric.impl.networking.client.ClientNeoNetworking;

import org.jetbrains.annotations.Nullable;

import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -73,7 +75,7 @@ public final class ClientConfigurationNetworking {
* @see ClientConfigurationNetworking#registerReceiver(Identifier, ConfigurationChannelHandler)
*/
public static boolean registerGlobalReceiver(Identifier channelName, ConfigurationChannelHandler channelHandler) {
return ClientNetworkingImpl.CONFIGURATION.registerGlobalReceiver(channelName, wrapUntyped(channelHandler));
return ClientNeoNetworking.CONFIGURATION.registerGlobalReceiver(channelName, wrapUntyped(channelHandler));
}

/**
Expand All @@ -90,7 +92,7 @@ public static boolean registerGlobalReceiver(Identifier channelName, Configurati
* @see ClientConfigurationNetworking#registerReceiver(PacketType, ConfigurationPacketHandler)
*/
public static <T extends FabricPacket> boolean registerGlobalReceiver(PacketType<T> type, ConfigurationPacketHandler<T> handler) {
return ClientNetworkingImpl.CONFIGURATION.registerGlobalReceiver(type.getId(), wrapTyped(type, handler));
return ClientNeoNetworking.CONFIGURATION.registerGlobalReceiver(type.getId(), wrapTyped(type, handler));
}

/**
Expand All @@ -106,7 +108,7 @@ public static <T extends FabricPacket> boolean registerGlobalReceiver(PacketType
*/
@Nullable
public static ClientConfigurationNetworking.ConfigurationChannelHandler unregisterGlobalReceiver(Identifier channelName) {
return unwrapUntyped(ClientNetworkingImpl.CONFIGURATION.unregisterGlobalReceiver(channelName));
return unwrapUntyped(ClientNeoNetworking.CONFIGURATION.unregisterGlobalReceiver(channelName));
}

/**
Expand All @@ -123,7 +125,7 @@ public static ClientConfigurationNetworking.ConfigurationChannelHandler unregist
*/
@Nullable
public static <T extends FabricPacket> ClientConfigurationNetworking.ConfigurationPacketHandler<T> unregisterGlobalReceiver(PacketType<T> type) {
return unwrapTyped(ClientNetworkingImpl.CONFIGURATION.unregisterGlobalReceiver(type.getId()));
return unwrapTyped(ClientNeoNetworking.CONFIGURATION.unregisterGlobalReceiver(type.getId()));
}

/**
Expand All @@ -133,7 +135,7 @@ public static <T extends FabricPacket> ClientConfigurationNetworking.Configurati
* @return all channel names which global receivers are registered for.
*/
public static Set<Identifier> getGlobalReceivers() {
return ClientNetworkingImpl.CONFIGURATION.getChannels();
return ClientNeoNetworking.CONFIGURATION.getChannels();
}

/**
Expand All @@ -154,10 +156,10 @@ public static Set<Identifier> getGlobalReceivers() {
* @see ClientPlayConnectionEvents#INIT
*/
public static boolean registerReceiver(Identifier channelName, ConfigurationChannelHandler channelHandler) {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();
final ClientConfigurationNetworkHandler addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return addon.registerChannel(channelName, wrapUntyped(channelHandler));
return ClientNeoNetworking.CONFIGURATION.registerReceiver(addon, channelName, wrapUntyped(channelHandler));
}

throw new IllegalStateException("Cannot register receiver while not configuring!");
Expand All @@ -179,10 +181,10 @@ public static boolean registerReceiver(Identifier channelName, ConfigurationChan
* @see ClientPlayConnectionEvents#INIT
*/
public static <T extends FabricPacket> boolean registerReceiver(PacketType<T> type, ConfigurationPacketHandler<T> handler) {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();
final ClientConfigurationNetworkHandler addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return addon.registerChannel(type.getId(), wrapTyped(type, handler));
return ClientNeoNetworking.CONFIGURATION.registerReceiver(addon, type.getId(), wrapTyped(type, handler));
}

throw new IllegalStateException("Cannot register receiver while not configuring!");
Expand All @@ -199,10 +201,10 @@ public static <T extends FabricPacket> boolean registerReceiver(PacketType<T> ty
*/
@Nullable
public static ClientConfigurationNetworking.ConfigurationChannelHandler unregisterReceiver(Identifier channelName) throws IllegalStateException {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();
final ClientConfigurationNetworkHandler addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return unwrapUntyped(addon.unregisterChannel(channelName));
return unwrapUntyped(ClientNeoNetworking.CONFIGURATION.unregisterReceiver(addon, channelName));
}

throw new IllegalStateException("Cannot unregister receiver while not configuring!");
Expand All @@ -220,10 +222,10 @@ public static ClientConfigurationNetworking.ConfigurationChannelHandler unregist
*/
@Nullable
public static <T extends FabricPacket> ClientConfigurationNetworking.ConfigurationPacketHandler<T> unregisterReceiver(PacketType<T> type) {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();
final ClientConfigurationNetworkHandler addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return unwrapTyped(addon.unregisterChannel(type.getId()));
return unwrapTyped(ClientNeoNetworking.CONFIGURATION.unregisterReceiver(addon, type.getId()));
}

throw new IllegalStateException("Cannot unregister receiver while not configuring!");
Expand All @@ -236,10 +238,10 @@ public static <T extends FabricPacket> ClientConfigurationNetworking.Configurati
* @throws IllegalStateException if the client is not connected to a server
*/
public static Set<Identifier> getReceived() throws IllegalStateException {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();
final ClientConfigurationNetworkHandler addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return addon.getReceivableChannels();
return ClientNeoNetworking.CONFIGURATION.getReceived(addon);
}

throw new IllegalStateException("Cannot get a list of channels the client can receive packets on while not configuring!");
Expand All @@ -252,10 +254,10 @@ public static Set<Identifier> getReceived() throws IllegalStateException {
* @throws IllegalStateException if the client is not connected to a server
*/
public static Set<Identifier> getSendable() throws IllegalStateException {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();
final ClientConfigurationNetworkHandler addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return addon.getSendableChannels();
return ClientNeoNetworking.CONFIGURATION.getSendable(addon);
}

throw new IllegalStateException("Cannot get a list of channels the server can receive packets on while not configuring!");
Expand All @@ -269,13 +271,7 @@ public static Set<Identifier> getSendable() throws IllegalStateException {
* False if the client is not in game.
*/
public static boolean canSend(Identifier channelName) throws IllegalArgumentException {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return addon.getSendableChannels().contains(channelName);
}

throw new IllegalStateException("Cannot get a list of channels the server can receive packets on while not configuring!");
return getSendable().contains(channelName);
}

/**
Expand Down Expand Up @@ -310,10 +306,10 @@ public static Packet<ServerCommonPacketListener> createC2SPacket(Identifier chan
* @throws IllegalStateException if the client is not connected to a server
*/
public static PacketSender getSender() throws IllegalStateException {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();
final ClientConfigurationNetworkHandler addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return addon;
return new ClientConfigurationNetworkAddon(addon);
}

throw new IllegalStateException("Cannot get PacketSender while not configuring!");
Expand All @@ -327,7 +323,7 @@ public static PacketSender getSender() throws IllegalStateException {
* @throws IllegalStateException if the client is not connected to a server
*/
public static void send(Identifier channelName, PacketByteBuf buf) throws IllegalStateException {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();
final ClientConfigurationNetworkHandler addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
addon.sendPacket(createC2SPacket(channelName, buf));
Expand All @@ -347,10 +343,10 @@ 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");

final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();
final ClientConfigurationNetworkHandler addon = ClientNetworkingImpl.getClientConfigurationAddon();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.util.Objects;
import java.util.Set;

import net.fabricmc.fabric.impl.networking.client.ClientNeoNetworking;

import net.fabricmc.fabric.impl.networking.client.ClientPlayNetworkAddon;

import org.jetbrains.annotations.Nullable;

import net.minecraft.client.MinecraftClient;
Expand All @@ -35,7 +39,6 @@
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl;
import net.fabricmc.fabric.impl.networking.client.ClientPlayNetworkAddon;
import net.fabricmc.fabric.impl.networking.payload.ResolvablePayload;
import net.fabricmc.fabric.impl.networking.payload.TypedPayload;
import net.fabricmc.fabric.impl.networking.payload.UntypedPayload;
Expand Down Expand Up @@ -76,7 +79,7 @@ public final class ClientPlayNetworking {
* @see ClientPlayNetworking#registerReceiver(Identifier, PlayChannelHandler)
*/
public static boolean registerGlobalReceiver(Identifier channelName, PlayChannelHandler channelHandler) {
return ClientNetworkingImpl.PLAY.registerGlobalReceiver(channelName, wrapUntyped(channelHandler));
return ClientNeoNetworking.PLAY.registerGlobalReceiver(channelName, wrapUntyped(channelHandler));
}

/**
Expand All @@ -93,7 +96,7 @@ public static boolean registerGlobalReceiver(Identifier channelName, PlayChannel
* @see ClientPlayNetworking#registerReceiver(PacketType, PlayPacketHandler)
*/
public static <T extends FabricPacket> boolean registerGlobalReceiver(PacketType<T> type, PlayPacketHandler<T> handler) {
return ClientNetworkingImpl.PLAY.registerGlobalReceiver(type.getId(), wrapTyped(type, handler));
return ClientNeoNetworking.PLAY.registerGlobalReceiver(type.getId(), wrapTyped(type, handler));
}

/**
Expand All @@ -109,7 +112,7 @@ public static <T extends FabricPacket> boolean registerGlobalReceiver(PacketType
*/
@Nullable
public static PlayChannelHandler unregisterGlobalReceiver(Identifier channelName) {
return unwrapUntyped(ClientNetworkingImpl.PLAY.unregisterGlobalReceiver(channelName));
return unwrapUntyped(ClientNeoNetworking.PLAY.unregisterGlobalReceiver(channelName));
}

/**
Expand All @@ -126,7 +129,7 @@ public static PlayChannelHandler unregisterGlobalReceiver(Identifier channelName
*/
@Nullable
public static <T extends FabricPacket> PlayPacketHandler<T> unregisterGlobalReceiver(PacketType<T> type) {
return unwrapTyped(ClientNetworkingImpl.PLAY.unregisterGlobalReceiver(type.getId()));
return unwrapTyped(ClientNeoNetworking.PLAY.unregisterGlobalReceiver(type.getId()));
}

/**
Expand All @@ -136,7 +139,7 @@ public static <T extends FabricPacket> PlayPacketHandler<T> unregisterGlobalRece
* @return all channel names which global receivers are registered for.
*/
public static Set<Identifier> getGlobalReceivers() {
return ClientNetworkingImpl.PLAY.getChannels();
return ClientNeoNetworking.PLAY.getChannels();
}

/**
Expand All @@ -157,10 +160,10 @@ public static Set<Identifier> getGlobalReceivers() {
* @see ClientPlayConnectionEvents#INIT
*/
public static boolean registerReceiver(Identifier channelName, PlayChannelHandler channelHandler) {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();
final ClientPlayNetworkHandler addon = MinecraftClient.getInstance().getNetworkHandler();

if (addon != null) {
return addon.registerChannel(channelName, wrapUntyped(channelHandler));
return ClientNeoNetworking.PLAY.registerReceiver(addon, channelName, wrapUntyped(channelHandler));
}

throw new IllegalStateException("Cannot register receiver while not in game!");
Expand All @@ -182,10 +185,10 @@ public static boolean registerReceiver(Identifier channelName, PlayChannelHandle
* @see ClientPlayConnectionEvents#INIT
*/
public static <T extends FabricPacket> boolean registerReceiver(PacketType<T> type, PlayPacketHandler<T> handler) {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();
final ClientPlayNetworkHandler addon = MinecraftClient.getInstance().getNetworkHandler();

if (addon != null) {
return addon.registerChannel(type.getId(), wrapTyped(type, handler));
return ClientNeoNetworking.PLAY.registerReceiver(addon, type.getId(), wrapTyped(type, handler));
}

throw new IllegalStateException("Cannot register receiver while not in game!");
Expand All @@ -202,10 +205,10 @@ public static <T extends FabricPacket> boolean registerReceiver(PacketType<T> ty
*/
@Nullable
public static PlayChannelHandler unregisterReceiver(Identifier channelName) throws IllegalStateException {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();
final ClientPlayNetworkHandler addon = MinecraftClient.getInstance().getNetworkHandler();

if (addon != null) {
return unwrapUntyped(addon.unregisterChannel(channelName));
return unwrapUntyped(ClientNeoNetworking.PLAY.unregisterReceiver(addon, channelName));
}

throw new IllegalStateException("Cannot unregister receiver while not in game!");
Expand All @@ -223,10 +226,10 @@ public static PlayChannelHandler unregisterReceiver(Identifier channelName) thro
*/
@Nullable
public static <T extends FabricPacket> PlayPacketHandler<T> unregisterReceiver(PacketType<T> type) {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();
final ClientPlayNetworkHandler addon = MinecraftClient.getInstance().getNetworkHandler();

if (addon != null) {
return unwrapTyped(addon.unregisterChannel(type.getId()));
return unwrapTyped(ClientNeoNetworking.PLAY.unregisterReceiver(addon, type.getId()));
}

throw new IllegalStateException("Cannot unregister receiver while not in game!");
Expand All @@ -239,10 +242,10 @@ public static <T extends FabricPacket> PlayPacketHandler<T> unregisterReceiver(P
* @throws IllegalStateException if the client is not connected to a server
*/
public static Set<Identifier> getReceived() throws IllegalStateException {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();
final ClientPlayNetworkHandler addon = MinecraftClient.getInstance().getNetworkHandler();

if (addon != null) {
return addon.getReceivableChannels();
return ClientNeoNetworking.PLAY.getReceived(addon);
}

throw new IllegalStateException("Cannot get a list of channels the client can receive packets on while not in game!");
Expand All @@ -255,10 +258,10 @@ public static Set<Identifier> getReceived() throws IllegalStateException {
* @throws IllegalStateException if the client is not connected to a server
*/
public static Set<Identifier> getSendable() throws IllegalStateException {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();
final ClientPlayNetworkHandler addon = MinecraftClient.getInstance().getNetworkHandler();

if (addon != null) {
return addon.getSendableChannels();
return ClientNeoNetworking.PLAY.getSendable(addon);
}

throw new IllegalStateException("Cannot get a list of channels the server can receive packets on while not in game!");
Expand All @@ -274,7 +277,7 @@ public static Set<Identifier> getSendable() throws IllegalStateException {
public static boolean canSend(Identifier channelName) throws IllegalArgumentException {
// You cant send without a client player, so this is fine
if (MinecraftClient.getInstance().getNetworkHandler() != null) {
return ClientNetworkingImpl.getAddon(MinecraftClient.getInstance().getNetworkHandler()).getSendableChannels().contains(channelName);
return getSendable().contains(channelName);
}

return false;
Expand Down Expand Up @@ -324,7 +327,7 @@ public static <T extends FabricPacket> Packet<ServerCommonPacketListener> create
public static PacketSender getSender() throws IllegalStateException {
// You cant send without a client player, so this is fine
if (MinecraftClient.getInstance().getNetworkHandler() != null) {
return ClientNetworkingImpl.getAddon(MinecraftClient.getInstance().getNetworkHandler());
return new ClientPlayNetworkAddon(MinecraftClient.getInstance().getNetworkHandler());
}

throw new IllegalStateException("Cannot get packet sender when not in game!");
Expand Down
Loading

0 comments on commit f77bd0e

Please sign in to comment.