[CHORE] Backport from 1.20.3
This commit is contained in:
@@ -35,22 +35,16 @@ public class FabricNetworkHandler implements CraterNetworkHandler {
|
||||
this.modid = modid;
|
||||
}
|
||||
|
||||
public synchronized static CraterNetworkHandler of(String modId) {
|
||||
return NETWORK_HANDLERS.computeIfAbsent(modId, FabricNetworkHandler::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends CraterPacket<T>> void registerPacket(Class<T> clazz, Supplier<T> supplier, PacketDirection packetDirection) {
|
||||
public <T extends CraterPacket<T>> void registerPacket(Class<? extends T> clazz, Supplier<T> supplier, PacketDirection packetDirection) {
|
||||
ResourceLocation channelName = this.nextId();
|
||||
this.packets.put(clazz, new PacketData(clazz, channelName, packetDirection));
|
||||
|
||||
final Function<FriendlyByteBuf, CraterPacket<?>> decoder = buf -> Util.make(supplier.get(), message -> message.read(buf));
|
||||
|
||||
switch (packetDirection) {
|
||||
case TO_CLIENT ->
|
||||
FabricNetworkHelper.getForDist(FabricLoader.getInstance().getEnvironmentType()).registerClientReceiver(channelName, decoder);
|
||||
case TO_SERVER ->
|
||||
FabricNetworkHelper.getForDist(FabricLoader.getInstance().getEnvironmentType()).registerServerReceiver(channelName, decoder);
|
||||
case TO_CLIENT -> FabricNetworkHelper.getForDist(FabricLoader.getInstance().getEnvironmentType()).registerClientReceiver(channelName, decoder);
|
||||
case TO_SERVER -> FabricNetworkHelper.getForDist(FabricLoader.getInstance().getEnvironmentType()).registerServerReceiver(channelName, decoder);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,15 +54,13 @@ public class FabricNetworkHandler implements CraterNetworkHandler {
|
||||
|
||||
@Override
|
||||
public Packet<?> toServerBound(CraterPacket<?> packet) {
|
||||
if (this.packets.get(packet.getClass()).direction() != PacketDirection.TO_SERVER)
|
||||
throw new IllegalStateException("Attempted sending message to wrong side, expected %s, was %s".formatted(PacketDirection.TO_SERVER, PacketDirection.TO_CLIENT));
|
||||
if (this.packets.get(packet.getClass()).direction() != PacketDirection.TO_SERVER) throw new IllegalStateException("Attempted sending message to wrong side, expected %s, was %s".formatted(PacketDirection.TO_SERVER, PacketDirection.TO_CLIENT));
|
||||
return this.toPacket(ClientPlayNetworking::createC2SPacket, packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet<?> toClientBound(CraterPacket<?> packet) {
|
||||
if (this.packets.get(packet.getClass()).direction() != PacketDirection.TO_CLIENT)
|
||||
throw new IllegalStateException("Attempted sending message to wrong side, expected %s, was %s".formatted(PacketDirection.TO_CLIENT, PacketDirection.TO_SERVER));
|
||||
if (this.packets.get(packet.getClass()).direction() != PacketDirection.TO_CLIENT) throw new IllegalStateException("Attempted sending message to wrong side, expected %s, was %s".formatted(PacketDirection.TO_CLIENT, PacketDirection.TO_SERVER));
|
||||
return this.toPacket(ServerPlayNetworking::createS2CPacket, packet);
|
||||
}
|
||||
|
||||
@@ -79,8 +71,11 @@ public class FabricNetworkHandler implements CraterNetworkHandler {
|
||||
return packetFactory.apply(identifier, byteBuf);
|
||||
}
|
||||
|
||||
private record PacketData(Class<? extends CraterPacket<?>> clazz, ResourceLocation identifier,
|
||||
PacketDirection direction) {
|
||||
public synchronized static CraterNetworkHandler of(String modId) {
|
||||
return NETWORK_HANDLERS.computeIfAbsent(modId, FabricNetworkHandler::new);
|
||||
}
|
||||
|
||||
private record PacketData(Class<? extends CraterPacket<?>> clazz, ResourceLocation identifier, PacketDirection direction) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -12,6 +12,10 @@ import java.util.function.Function;
|
||||
|
||||
public interface FabricNetworkHelper {
|
||||
|
||||
/* FABRIC ONLY */
|
||||
void registerClientReceiver(@NotNull ResourceLocation channelName, @NotNull Function<FriendlyByteBuf, @NotNull CraterPacket<?>> factory);
|
||||
void registerServerReceiver(ResourceLocation channelName, Function<FriendlyByteBuf, CraterPacket<?>> factory);
|
||||
|
||||
public static FabricNetworkHelper getForDist(EnvType dist) {
|
||||
switch (dist) {
|
||||
case CLIENT -> {
|
||||
@@ -24,9 +28,4 @@ public interface FabricNetworkHelper {
|
||||
return null;
|
||||
}
|
||||
|
||||
/* FABRIC ONLY */
|
||||
void registerClientReceiver(@NotNull ResourceLocation channelName, @NotNull Function<FriendlyByteBuf, @NotNull CraterPacket<?>> factory);
|
||||
|
||||
void registerServerReceiver(ResourceLocation channelName, Function<FriendlyByteBuf, CraterPacket<?>> factory);
|
||||
|
||||
}
|
||||
|
@@ -33,7 +33,7 @@
|
||||
"depends": {
|
||||
"fabricloader": ">=0.14.21",
|
||||
"fabric-api": "*",
|
||||
"minecraft": ">=1.20.2",
|
||||
"minecraft": ">=1.20",
|
||||
"java": ">=17"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user