Skip to content

Commit

Permalink
Networking part 4 of many
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Jan 19, 2024
1 parent f109c73 commit b709b71
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public final class ClientConfigurationNetworking {
* @see ClientConfigurationNetworking#unregisterGlobalReceiver(PacketType)
* @see ClientConfigurationNetworking#registerReceiver(PacketType, ConfigurationPayloadHandler)
*/
public static <T extends CustomPayload> boolean registerGlobalReceiver(CustomPayload.Type<PacketByteBuf, T> type, ConfigurationPayloadHandler<T> handler) {
return ClientNetworkingImpl.CONFIGURATION.registerGlobalReceiver(type.getId(), wrapTyped(type, handler));
public static <T extends CustomPayload> boolean registerGlobalReceiver(CustomPayload.Id<T> type, ConfigurationPayloadHandler<T> handler) {
return ClientNetworkingImpl.CONFIGURATION.registerGlobalReceiver(type.id(), handler);
}

/**
Expand All @@ -75,7 +75,7 @@ public static <T extends CustomPayload> boolean registerGlobalReceiver(CustomPay
*/
@Nullable
public static <T extends CustomPayload> ClientConfigurationNetworking.ConfigurationPayloadHandler<T> unregisterGlobalReceiver(CustomPayload.Type<PacketByteBuf, T> type) {
return unwrapTyped(ClientNetworkingImpl.CONFIGURATION.unregisterGlobalReceiver(type.id().id()));
return (ConfigurationPayloadHandler<T>) ClientNetworkingImpl.CONFIGURATION.unregisterGlobalReceiver(type.id().id());
}

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ public static <T extends CustomPayload> boolean registerReceiver(CustomPayload.T
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

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

throw new IllegalStateException("Cannot register receiver while not configuring!");
Expand All @@ -128,7 +128,7 @@ public static <T extends CustomPayload> ClientConfigurationNetworking.Configurat
final ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (addon != null) {
return unwrapTyped(addon.unregisterChannel(type.getId()));
return (ConfigurationPayloadHandler<T>) addon.unregisterChannel(type.id());
}

throw new IllegalStateException("Cannot unregister receiver while not configuring!");
Expand Down Expand Up @@ -253,10 +253,10 @@ public interface ConfigurationPayloadHandler<T extends CustomPayload> {
* }</pre>
*
*
* @param packet the packet
* @param payload the packet payload
* @param responseSender the packet sender
* @see CustomPayload
*/
void receive(T packet, PacketSender responseSender);
void receive(T payload, PacketSender responseSender);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
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;

/**
* Offers access to play stage client-side networking functionalities.
Expand Down Expand Up @@ -65,7 +64,7 @@ public final class ClientPlayNetworking {
* @see ClientPlayNetworking#registerReceiver(PacketType, PlayPacketHandler)
*/
public static <T extends CustomPayload> boolean registerGlobalReceiver(CustomPayload.Id<T> type, PlayPacketHandler<T> handler) {
return ClientNetworkingImpl.PLAY.registerGlobalReceiver(type.id(), wrapTyped(type, handler));
return ClientNetworkingImpl.PLAY.registerGlobalReceiver(type.id(), handler);
}

/**
Expand All @@ -82,7 +81,7 @@ public static <T extends CustomPayload> boolean registerGlobalReceiver(CustomPay
*/
@Nullable
public static <T extends CustomPayload> ClientPlayNetworking.PlayPacketHandler<T> unregisterGlobalReceiver(CustomPayload.Id<T> type) {
return ClientNetworkingImpl.PLAY.unregisterGlobalReceiver(type.id());
return (PlayPacketHandler<T>) ClientNetworkingImpl.PLAY.unregisterGlobalReceiver(type.id());
}

/**
Expand Down Expand Up @@ -114,7 +113,7 @@ public static <T extends CustomPayload> boolean registerReceiver(CustomPayload.I
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();

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

throw new IllegalStateException("Cannot register receiver while not in game!");
Expand All @@ -135,7 +134,7 @@ public static <T extends CustomPayload> ClientPlayNetworking.PlayPacketHandler<T
final ClientPlayNetworkAddon addon = ClientNetworkingImpl.getClientPlayAddon();

if (addon != null) {
return unwrapTyped(addon.unregisterChannel(type.id()));
return (PlayPacketHandler<T>) addon.unregisterChannel(type.id());
}

throw new IllegalStateException("Cannot unregister receiver while not in game!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@
import java.util.Collections;
import java.util.List;

import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;

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

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientConfigurationNetworkHandler;
import net.minecraft.network.NetworkState;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.Packet;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.client.networking.v1.C2SConfigurationChannelEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon;
import net.fabricmc.fabric.impl.networking.ChannelInfoHolder;
import net.fabricmc.fabric.impl.networking.NetworkingImpl;
import net.fabricmc.fabric.impl.networking.payload.ResolvablePayload;
import net.fabricmc.fabric.impl.networking.payload.ResolvedPayload;
import net.fabricmc.fabric.mixin.networking.client.accessor.ClientCommonNetworkHandlerAccessor;
import net.fabricmc.fabric.mixin.networking.client.accessor.ClientConfigurationNetworkHandlerAccessor;

public final class ClientConfigurationNetworkAddon extends AbstractChanneledNetworkAddon<ClientConfigurationNetworkAddon.Handler> {
public final class ClientConfigurationNetworkAddon extends AbstractChanneledNetworkAddon<ClientConfigurationNetworking.ConfigurationPayloadHandler<?>> {
private final ClientConfigurationNetworkHandler handler;
private final MinecraftClient client;
private boolean sentInitialRegisterPacket;
Expand All @@ -63,8 +65,8 @@ public void onServerReady() {
}

@Override
protected void receiveRegistration(boolean register, ResolvablePayload resolvable) {
super.receiveRegistration(register, resolvable);
protected void receiveRegistration(boolean register, RegistrationPayload payload) {
super.receiveRegistration(register, payload);

if (register && !this.sentInitialRegisterPacket) {
this.sendInitialChannelRegistrationPacket();
Expand All @@ -73,8 +75,8 @@ protected void receiveRegistration(boolean register, ResolvablePayload resolvabl
}

@Override
protected void receive(Handler handler, ResolvedPayload payload) {
handler.receive(this.client, this.handler, payload, this);
protected void receive(ClientConfigurationNetworking.ConfigurationPayloadHandler<?> handler, CustomPayload payload) {
((ClientConfigurationNetworking.ConfigurationPayloadHandler) handler).receive(payload, this);
}

// impl details
Expand All @@ -85,12 +87,7 @@ protected void schedule(Runnable task) {
}

@Override
public Packet<?> createPacket(Identifier channelName, PacketByteBuf buf) {
return ClientPlayNetworking.createC2SPacket(channelName, buf);
}

@Override
public Packet<?> createPacket(FabricPacket packet) {
public Packet<?> createPacket(CustomPayload packet) {
return ClientPlayNetworking.createC2SPacket(packet);
}

Expand All @@ -108,10 +105,10 @@ protected void invokeUnregisterEvent(List<Identifier> ids) {
protected void handleRegistration(Identifier channelName) {
// If we can already send packets, immediately send the register packet for this channel
if (this.sentInitialRegisterPacket) {
final PacketByteBuf buf = this.createRegistrationPacket(Collections.singleton(channelName));
final RegistrationPayload payload = this.createRegistrationPayload(RegistrationPayload.REGISTER, Collections.singleton(channelName));

if (buf != null) {
this.sendPacket(NetworkingImpl.REGISTER_CHANNEL, buf);
if (payload != null) {
this.sendPacket(payload);
}
}
}
Expand All @@ -120,10 +117,10 @@ protected void handleRegistration(Identifier channelName) {
protected void handleUnregistration(Identifier channelName) {
// If we can already send packets, immediately send the unregister packet for this channel
if (this.sentInitialRegisterPacket) {
final PacketByteBuf buf = this.createRegistrationPacket(Collections.singleton(channelName));
final RegistrationPayload payload = this.createRegistrationPayload(RegistrationPayload.UNREGISTER, Collections.singleton(channelName));

if (buf != null) {
this.sendPacket(NetworkingImpl.UNREGISTER_CHANNEL, buf);
if (payload != null) {
this.sendPacket(payload);
}
}
}
Expand All @@ -146,8 +143,4 @@ protected boolean isReservedChannel(Identifier channelName) {
public ChannelInfoHolder getChannelInfoHolder() {
return (ChannelInfoHolder) ((ClientCommonNetworkHandlerAccessor) handler).getConnection();
}

public interface Handler {
void receive(MinecraftClient client, ClientConfigurationNetworkHandler handler, ResolvedPayload payload, PacketSender responseSender);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import java.util.Objects;

import net.minecraft.network.packet.CustomPayload;

import org.jetbrains.annotations.Nullable;

import net.minecraft.client.MinecraftClient;
Expand All @@ -29,29 +27,23 @@
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.NetworkState;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.listener.ServerCommonPacketListener;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.impl.networking.CommonPacketsImpl;
import net.fabricmc.fabric.impl.networking.CommonRegisterPayload;
import net.fabricmc.fabric.impl.networking.CommonVersionPayload;
import net.fabricmc.fabric.impl.networking.GlobalReceiverRegistry;
import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions;
import net.fabricmc.fabric.impl.networking.NetworkingImpl;
import net.fabricmc.fabric.impl.networking.payload.ResolvablePayload;
import net.fabricmc.fabric.impl.networking.payload.ResolvedPayload;
import net.fabricmc.fabric.impl.networking.payload.TypedPayload;
import net.fabricmc.fabric.impl.networking.payload.UntypedPayload;
import net.fabricmc.fabric.mixin.networking.client.accessor.ConnectScreenAccessor;
import net.fabricmc.fabric.mixin.networking.client.accessor.MinecraftClientAccessor;

Expand Down Expand Up @@ -148,16 +140,14 @@ public static void clientInit() {
});

// Version packet
ClientConfigurationNetworking.registerGlobalReceiver(CommonVersionPayload.PACKET_ID, (client, handler, buf, responseSender) -> {
var payload = new CommonVersionPayload(buf);
ClientConfigurationNetworking.registerGlobalReceiver(CommonVersionPayload.ID, (payload, responseSender) -> {
int negotiatedVersion = handleVersionPacket(payload, responseSender);
ClientNetworkingImpl.getAddon(handler).onCommonVersionPacket(negotiatedVersion);
ClientNetworkingImpl.getClientConfigurationAddon().onCommonVersionPacket(negotiatedVersion);
});

// Register packet
ClientConfigurationNetworking.registerGlobalReceiver(CommonRegisterPayload.PACKET_ID, (client, handler, buf, responseSender) -> {
var payload = new CommonRegisterPayload(buf);
ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getAddon(handler);
ClientConfigurationNetworking.registerGlobalReceiver(CommonRegisterPayload.ID, (payload, responseSender) -> {
ClientConfigurationNetworkAddon addon = ClientNetworkingImpl.getClientConfigurationAddon();

if (CommonRegisterPayload.PLAY_PHASE.equals(payload.phase())) {
if (payload.version() != addon.getNegotiatedVersion()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import java.util.List;

import com.mojang.logging.LogUtils;

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

import net.minecraft.network.packet.CustomPayload;

import org.slf4j.Logger;

import net.minecraft.client.MinecraftClient;
Expand All @@ -32,14 +37,11 @@
import net.fabricmc.fabric.api.client.networking.v1.C2SPlayChannelEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon;
import net.fabricmc.fabric.impl.networking.ChannelInfoHolder;
import net.fabricmc.fabric.impl.networking.NetworkingImpl;
import net.fabricmc.fabric.impl.networking.payload.ResolvedPayload;

public final class ClientPlayNetworkAddon extends AbstractChanneledNetworkAddon<ClientPlayNetworkAddon.Handler> {
public final class ClientPlayNetworkAddon extends AbstractChanneledNetworkAddon<ClientPlayNetworking.PlayPacketHandler<?>> {
private final ClientPlayNetworkHandler handler;
private final MinecraftClient client;
private boolean sentInitialRegisterPacket;
Expand Down Expand Up @@ -73,8 +75,8 @@ public void onServerReady() {
}

@Override
protected void receive(Handler handler, ResolvedPayload payload) {
handler.receive(this.client, this.handler, payload, this);
protected void receive(ClientPlayNetworking.PlayPacketHandler<?> handler, CustomPayload payload) {
((ClientPlayNetworking.PlayPacketHandler) handler).receive(payload, this.client.player, this);
}

// impl details
Expand All @@ -85,12 +87,7 @@ protected void schedule(Runnable task) {
}

@Override
public Packet<?> createPacket(Identifier channelName, PacketByteBuf buf) {
return ClientPlayNetworking.createC2SPacket(channelName, buf);
}

@Override
public Packet<?> createPacket(FabricPacket packet) {
public Packet<?> createPacket(CustomPayload packet) {
return ClientPlayNetworking.createC2SPacket(packet);
}

Expand All @@ -108,10 +105,10 @@ protected void invokeUnregisterEvent(List<Identifier> ids) {
protected void handleRegistration(Identifier channelName) {
// If we can already send packets, immediately send the register packet for this channel
if (this.sentInitialRegisterPacket) {
final PacketByteBuf buf = this.createRegistrationPacket(Collections.singleton(channelName));
final RegistrationPayload payload = this.createRegistrationPayload(RegistrationPayload.REGISTER, Collections.singleton(channelName));

if (buf != null) {
this.sendPacket(NetworkingImpl.REGISTER_CHANNEL, buf);
if (payload != null) {
this.sendPacket(payload);
}
}
}
Expand All @@ -120,10 +117,10 @@ protected void handleRegistration(Identifier channelName) {
protected void handleUnregistration(Identifier channelName) {
// If we can already send packets, immediately send the unregister packet for this channel
if (this.sentInitialRegisterPacket) {
final PacketByteBuf buf = this.createRegistrationPacket(Collections.singleton(channelName));
final RegistrationPayload payload = this.createRegistrationPayload(RegistrationPayload.UNREGISTER, Collections.singleton(channelName));

if (buf != null) {
this.sendPacket(NetworkingImpl.UNREGISTER_CHANNEL, buf);
if (payload != null) {
this.sendPacket(payload);
}
}
}
Expand All @@ -137,8 +134,4 @@ protected void invokeDisconnectEvent() {
protected boolean isReservedChannel(Identifier channelName) {
return NetworkingImpl.isReservedCommonChannel(channelName);
}

public interface Handler {
void receive(MinecraftClient client, ClientPlayNetworkHandler handler, ResolvedPayload payload, PacketSender responseSender);
}
}
Loading

0 comments on commit b709b71

Please sign in to comment.