package me.hypherionmc.craterlib.common; import me.hypherionmc.craterlib.network.CraterNetworkHandler; import me.hypherionmc.craterlib.network.CraterPacket; import me.hypherionmc.craterlib.network.FabricNetworkHandler; import me.hypherionmc.craterlib.platform.services.LibCommonHelper; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.network.Connection; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import java.util.function.Function; /** * @author HypherionSA * @date 24/09/2022 */ public class FabricCommonHelper implements LibCommonHelper { public static MinecraftServer server; @Override public CraterNetworkHandler createPacketHandler(String modid) { return FabricNetworkHandler.of(modid); } @Override public Minecraft getClientInstance() { return Minecraft.getInstance(); } @Override public Player getClientPlayer() { return Minecraft.getInstance().player; } @Override public Level getClientLevel() { return Minecraft.getInstance().level; } @Override public Connection getClientConnection() { return Minecraft.getInstance().getConnection().getConnection(); } @Override public MinecraftServer getMCServer() { return server; } @Override public void registerClientReceiver(ResourceLocation channelName, Function> factory) { ClientPlayNetworking.registerGlobalReceiver(channelName, (Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender) -> { CraterPacket packet = factory.apply(buf); client.execute(() -> packet.handle(client.player, client)); }); } @Override public void registerServerReceiver(ResourceLocation channelName, Function> factory) { ServerPlayNetworking.registerGlobalReceiver(channelName, (MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, FriendlyByteBuf buf, PacketSender responseSender) -> { CraterPacket packet = factory.apply(buf); server.execute(() -> packet.handle(player, server)); }); } }