|
|
|
@@ -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) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|