Skip to content

Commit

Permalink
Register server-side codecs for packets sent to the client. Should fi…
Browse files Browse the repository at this point in the history
…x the only issues with #3
  • Loading branch information
Sollace committed Sep 4, 2024
1 parent 007721d commit a0984cf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;

/**
Expand Down Expand Up @@ -43,7 +41,7 @@ public interface SimpleNetworking {
* @return A registered PacketType
*/
static <T extends Packet> C2SPacketType<T> clientToServer(Identifier id, Function<PacketByteBuf, T> factory) {
return ServerSimpleNetworkingImpl.register(id, factory);
return ServerSimpleNetworkingImpl.registerC2S(id, factory);
}
/**
* Registers a packet type for transmission to the client.
Expand All @@ -60,7 +58,6 @@ static <T extends Packet> S2CPacketType<T> serverToClient(Identifier id, Functio
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
return ClientSimpleNetworkingImpl.register(id, factory);
}
var packetId = new CustomPayload.Id<Payload<T>>(id);
return new S2CPacketType<>(packetId, Payload.createCodec(packetId, PacketCodec.of(Packet::toBuffer, factory::apply)), Receivers.empty(id));
return ServerSimpleNetworkingImpl.registerS2C(id, factory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class ServerSimpleNetworkingImpl {
private ServerSimpleNetworkingImpl() { throw new RuntimeException("new ServerSimpleNetworkingImpl()"); }

public static <T extends Packet> C2SPacketType<T> register(Identifier id, Function<PacketByteBuf, T> factory) {
public static <T extends Packet> C2SPacketType<T> registerC2S(Identifier id, Function<PacketByteBuf, T> factory) {
var packetId = new CustomPayload.Id<Payload<T>>(id);
var type = new C2SPacketType<>(packetId, Payload.createCodec(packetId, PacketCodec.of(Packet::toBuffer, factory::apply)), new ReceiverImpl<>(id));
PayloadTypeRegistry.playC2S().register(type.id(), type.codec());
Expand All @@ -31,6 +31,13 @@ public static <T extends Packet> C2SPacketType<T> register(Identifier id, Functi
return type;
}

public static <T extends Packet> S2CPacketType<T> registerS2C(Identifier id, Function<PacketByteBuf, T> factory) {
var packetId = new CustomPayload.Id<Payload<T>>(id);
var type = new S2CPacketType<>(packetId, Payload.createCodec(packetId, PacketCodec.of(Packet::toBuffer, factory::apply)), Receivers.empty(id));
PayloadTypeRegistry.playS2C().register(type.id(), type.codec());
return type;
}

@SuppressWarnings("unchecked")
public static <T extends Packet> Future<T> waitForReponse(C2SPacketType<T> packetType, ClientConnection connection) {
Objects.requireNonNull(connection, "Client Connection cannot be null");
Expand Down

0 comments on commit a0984cf

Please sign in to comment.