Skip to content

Commit

Permalink
Networking step one
Browse files Browse the repository at this point in the history
Play and login networking tested and operational
  • Loading branch information
Su5eD committed Jul 4, 2024
1 parent f2e7b01 commit 6ebbd01
Show file tree
Hide file tree
Showing 53 changed files with 1,006 additions and 2,694 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

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

import org.jetbrains.annotations.ApiStatus;
Expand All @@ -25,11 +24,10 @@
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.impl.networking.client.ClientConfigurationNetworkAddon;
import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.thread.BlockableEventLoop;
import org.sinytra.fabric.networking_api.client.NeoClientConfigurationNetworking;

/**
* Offers access to configuration stage client-side networking functionalities.
Expand Down Expand Up @@ -65,7 +63,7 @@ public final class ClientConfigurationNetworking {
* @see ClientConfigurationNetworking#registerReceiver(CustomPacketPayload.Type, ConfigurationPayloadHandler)
*/
public static <T extends CustomPacketPayload> boolean registerGlobalReceiver(CustomPacketPayload.Type<T> type, ConfigurationPayloadHandler<T> handler) {
return ClientNetworkingImpl.CONFIGURATION.registerGlobalReceiver(type.id(), handler);
return NeoClientConfigurationNetworking.registerGlobalReceiver(type, handler);
}

/**
Expand All @@ -82,7 +80,7 @@ public static <T extends CustomPacketPayload> boolean registerGlobalReceiver(Cus
*/
@Nullable
public static ClientConfigurationNetworking.ConfigurationPayloadHandler<?> unregisterGlobalReceiver(CustomPacketPayload.Type<?> id) {
return ClientNetworkingImpl.CONFIGURATION.unregisterGlobalReceiver(id.id());
return NeoClientConfigurationNetworking.unregisterGlobalReceiver(id.id());
}

/**
Expand All @@ -92,7 +90,7 @@ public static ClientConfigurationNetworking.ConfigurationPayloadHandler<?> unreg
* @return all channel names which global receivers are registered for.
*/
public static Set<ResourceLocation> getGlobalReceivers() {
return ClientNetworkingImpl.CONFIGURATION.getChannels();
return NeoClientConfigurationNetworking.getGlobalReceivers();
}

/**
Expand All @@ -112,13 +110,7 @@ public static Set<ResourceLocation> getGlobalReceivers() {
* @see ClientPlayConnectionEvents#INIT
*/
public static <T extends CustomPacketPayload> boolean registerReceiver(CustomPacketPayload.Type<T> id, ConfigurationPayloadHandler<T> handler) {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return addon.registerChannel(id.id(), handler);
}

throw new IllegalStateException("Cannot register receiver while not configuring!");
return NeoClientConfigurationNetworking.registerReceiver(id, handler);
}

/**
Expand All @@ -133,13 +125,7 @@ public static <T extends CustomPacketPayload> boolean registerReceiver(CustomPac
*/
@Nullable
public static ClientConfigurationNetworking.ConfigurationPayloadHandler<?> unregisterReceiver(ResourceLocation id) {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return addon.unregisterChannel(id);
}

throw new IllegalStateException("Cannot unregister receiver while not configuring!");
return NeoClientConfigurationNetworking.unregisterReceiver(id);
}

/**
Expand All @@ -149,13 +135,7 @@ public static ClientConfigurationNetworking.ConfigurationPayloadHandler<?> unreg
* @throws IllegalStateException if the client is not connected to a server
*/
public static Set<ResourceLocation> getReceived() throws IllegalStateException {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

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

throw new IllegalStateException("Cannot get a list of channels the client can receive packets on while not configuring!");
return NeoClientConfigurationNetworking.getReceived();
}

/**
Expand All @@ -165,13 +145,7 @@ public static Set<ResourceLocation> getReceived() throws IllegalStateException {
* @throws IllegalStateException if the client is not connected to a server
*/
public static Set<ResourceLocation> getSendable() throws IllegalStateException {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

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

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

/**
Expand All @@ -182,13 +156,7 @@ public static Set<ResourceLocation> getSendable() throws IllegalStateException {
* False if the client is not in game.
*/
public static boolean canSend(ResourceLocation 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 NeoClientConfigurationNetworking.canSend(channelName);
}

/**
Expand All @@ -209,13 +177,7 @@ public static boolean canSend(CustomPacketPayload.Type<?> type) {
* @throws IllegalStateException if the client is not connected to a server
*/
public static PacketSender getSender() throws IllegalStateException {
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

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

throw new IllegalStateException("Cannot get PacketSender while not configuring!");
return NeoClientConfigurationNetworking.getSender();
}

/**
Expand All @@ -227,17 +189,7 @@ public static PacketSender getSender() throws IllegalStateException {
* @throws IllegalStateException if the client is not connected to a server
*/
public static void send(CustomPacketPayload payload) {
Objects.requireNonNull(payload, "Payload cannot be null");
Objects.requireNonNull(payload.type(), "CustomPayload#getId() cannot return null for payload class: " + payload.getClass());

final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

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

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

private ClientConfigurationNetworking() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
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.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.ServerCommonPacketListener;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import org.sinytra.fabric.networking_api.client.NeoClientPlayNetworking;

/**
* Offers access to play stage client-side networking functionalities.
Expand Down Expand Up @@ -67,7 +67,7 @@ public final class ClientPlayNetworking {
* @see ClientPlayNetworking#registerReceiver(CustomPacketPayload.Type, PlayPayloadHandler)
*/
public static <T extends CustomPacketPayload> boolean registerGlobalReceiver(CustomPacketPayload.Type<T> type, PlayPayloadHandler<T> handler) {
return ClientNetworkingImpl.PLAY.registerGlobalReceiver(type.id(), handler);
return NeoClientPlayNetworking.registerGlobalReceiver(type, handler);
}

/**
Expand All @@ -84,7 +84,7 @@ public static <T extends CustomPacketPayload> boolean registerGlobalReceiver(Cus
*/
@Nullable
public static ClientPlayNetworking.PlayPayloadHandler<?> unregisterGlobalReceiver(ResourceLocation id) {
return ClientNetworkingImpl.PLAY.unregisterGlobalReceiver(id);
return NeoClientPlayNetworking.unregisterGlobalReceiver(id);
}

/**
Expand All @@ -94,7 +94,7 @@ public static ClientPlayNetworking.PlayPayloadHandler<?> unregisterGlobalReceive
* @return all channel names which global receivers are registered for.
*/
public static Set<ResourceLocation> getGlobalReceivers() {
return ClientNetworkingImpl.PLAY.getChannels();
return NeoClientPlayNetworking.getGlobalReceivers();
}

/**
Expand All @@ -114,13 +114,7 @@ public static Set<ResourceLocation> getGlobalReceivers() {
* @see ClientPlayConnectionEvents#INIT
*/
public static <T extends CustomPacketPayload> boolean registerReceiver(CustomPacketPayload.Type<T> type, PlayPayloadHandler<T> handler) {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();

if (addon != null) {
return addon.registerChannel(type.id(), handler);
}

throw new IllegalStateException("Cannot register receiver while not in game!");
return NeoClientPlayNetworking.registerReceiver(type, handler);
}

/**
Expand All @@ -135,13 +129,7 @@ public static <T extends CustomPacketPayload> boolean registerReceiver(CustomPac
*/
@Nullable
public static ClientPlayNetworking.PlayPayloadHandler<?> unregisterReceiver(ResourceLocation id) {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();

if (addon != null) {
return addon.unregisterChannel(id);
}

throw new IllegalStateException("Cannot unregister receiver while not in game!");
return NeoClientPlayNetworking.unregisterReceiver(id);
}

/**
Expand All @@ -151,13 +139,7 @@ public static ClientPlayNetworking.PlayPayloadHandler<?> unregisterReceiver(Reso
* @throws IllegalStateException if the client is not connected to a server
*/
public static Set<ResourceLocation> getReceived() throws IllegalStateException {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();

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

throw new IllegalStateException("Cannot get a list of channels the client can receive packets on while not in game!");
return NeoClientPlayNetworking.getReceived();
}

/**
Expand All @@ -167,13 +149,7 @@ public static Set<ResourceLocation> getReceived() throws IllegalStateException {
* @throws IllegalStateException if the client is not connected to a server
*/
public static Set<ResourceLocation> getSendable() throws IllegalStateException {
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();

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

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

/**
Expand All @@ -186,7 +162,7 @@ public static Set<ResourceLocation> getSendable() throws IllegalStateException {
public static boolean canSend(ResourceLocation channelName) throws IllegalArgumentException {
// You cant send without a client player, so this is fine
if (Minecraft.getInstance().getConnection() != null) {
return ClientNetworkingImpl.getAddon(Minecraft.getInstance().getConnection()).getSendableChannels().contains(channelName);
return NeoClientPlayNetworking.canSend(channelName);
}

return false;
Expand Down Expand Up @@ -220,12 +196,7 @@ public static <T extends CustomPacketPayload> Packet<ServerCommonPacketListener>
* @throws IllegalStateException if the client is not connected to a server
*/
public static PacketSender getSender() throws IllegalStateException {
// You cant send without a client player, so this is fine
if (Minecraft.getInstance().getConnection() != null) {
return ClientNetworkingImpl.getAddon(Minecraft.getInstance().getConnection());
}

throw new IllegalStateException("Cannot get payload sender when not in game!");
return NeoClientPlayNetworking.getSender();
}

/**
Expand Down

This file was deleted.

Loading

0 comments on commit 6ebbd01

Please sign in to comment.