From c9ceba5f9606b45ea90d3dffbbefdaee188b16dd Mon Sep 17 00:00:00 2001 From: fayer3 Date: Sun, 5 May 2024 13:22:12 +0200 Subject: [PATCH] fix serverside packet handling on fabric --- .../org/vivecraft/fabric/VivecraftMod.java | 19 +++++++++++++- .../fabric/VivecraftModClientOnly.java | 25 ------------------- fabric/src/main/resources/fabric.mod.json | 3 --- gradle.properties | 4 +-- 4 files changed, 20 insertions(+), 31 deletions(-) delete mode 100644 fabric/src/main/java/org/vivecraft/fabric/VivecraftModClientOnly.java diff --git a/fabric/src/main/java/org/vivecraft/fabric/VivecraftMod.java b/fabric/src/main/java/org/vivecraft/fabric/VivecraftMod.java index 269483d1f..e8bf0c604 100644 --- a/fabric/src/main/java/org/vivecraft/fabric/VivecraftMod.java +++ b/fabric/src/main/java/org/vivecraft/fabric/VivecraftMod.java @@ -1,11 +1,15 @@ package org.vivecraft.fabric; import io.netty.buffer.Unpooled; +import net.fabricmc.api.EnvType; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.fabricmc.loader.api.FabricLoader; import net.minecraft.network.FriendlyByteBuf; +import org.vivecraft.client.network.ClientNetworking; import org.vivecraft.common.network.packets.VivecraftDataPacket; import org.vivecraft.server.ServerNetworking; import org.vivecraft.server.ServerUtil; @@ -21,11 +25,24 @@ public void onInitialize() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> ServerUtil.registerCommands(dispatcher)); + // register packets + PayloadTypeRegistry.playS2C().register(VivecraftDataPacket.TYPE, VivecraftDataPacket.STREAM_CODEC); PayloadTypeRegistry.playC2S().register(VivecraftDataPacket.TYPE, VivecraftDataPacket.STREAM_CODEC); + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { + // register client packet receiver + ClientPlayNetworking.registerGlobalReceiver(VivecraftDataPacket.TYPE, + (payload, context) -> context.client().execute(() -> { + FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer()).writeBytes(payload.buffer()); + ClientNetworking.handlePacket(payload.packetid(), buffer); + buffer.release(); + })); + } + + // register server packet receiver ServerPlayNetworking.registerGlobalReceiver(VivecraftDataPacket.TYPE, (payload, context) -> { FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer()).writeBytes(payload.buffer()); - ServerNetworking.handlePacket(payload.packetid(), buffer, context.player(), p -> context.responseSender().sendPacket(p.payload())); + ServerNetworking.handlePacket(payload.packetid(), buffer, context.player(), p -> context.responseSender().sendPacket(p)); buffer.release(); }); } diff --git a/fabric/src/main/java/org/vivecraft/fabric/VivecraftModClientOnly.java b/fabric/src/main/java/org/vivecraft/fabric/VivecraftModClientOnly.java deleted file mode 100644 index 947da91e4..000000000 --- a/fabric/src/main/java/org/vivecraft/fabric/VivecraftModClientOnly.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.vivecraft.fabric; - -import io.netty.buffer.Unpooled; -import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; -import net.minecraft.network.FriendlyByteBuf; -import org.vivecraft.client.network.ClientNetworking; -import org.vivecraft.common.network.packets.VivecraftDataPacket; - -public class VivecraftModClientOnly implements ClientModInitializer { - @Override - public void onInitializeClient() { - // init client packets - PayloadTypeRegistry.playS2C().register(VivecraftDataPacket.TYPE, VivecraftDataPacket.STREAM_CODEC); - - ClientPlayNetworking.registerGlobalReceiver(VivecraftDataPacket.TYPE, - (payload, context) -> context.client().execute(() -> { - FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer()).writeBytes(payload.buffer()); - ClientNetworking.handlePacket(payload.packetid(), buffer); - buffer.release(); - })); - - } -} diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index 876e4fe3f..236c4ad60 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -25,9 +25,6 @@ "main": [ "org.vivecraft.fabric.VivecraftMod" ], - "client": [ - "org.vivecraft.fabric.VivecraftModClientOnly" - ], "modmenu": [ "org.vivecraft.client.fabric.VivecraftModMenuApiImpl" ] diff --git a/gradle.properties b/gradle.properties index 18c51dc93..02116dbb0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,13 +4,13 @@ minecraft_version=1.20.5 enabled_platforms=fabric,neoforge archives_base_name=vivecraft -mod_version=1.1.9-a2 +mod_version=1.1.9-a3 maven_group=org.vivecraft architectury_version=12.0.26 fabric_loader_version=0.15.7 -fabric_api_version=0.97.7+1.20.5 +fabric_api_version=0.97.8+1.20.5 forge_version=1.20.4-49.0.30 neoforge_version=20.5.5-beta