From 3e956db254927e076fc523eef1011c06a46b1355 Mon Sep 17 00:00:00 2001 From: FirstMegaGame4 <84094287+FirstMegaGame4@users.noreply.github.com> Date: Sat, 2 Dec 2023 19:14:24 +0100 Subject: [PATCH] Add Implementation Of Requests / Responses System --- .../mmodding_lib/PacketReceivers.java | 51 +++++++++++++++++++ .../client/ClientPacketReceivers.java | 51 ++++++++++++++++--- 2 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/mmodding/mmodding_lib/PacketReceivers.java diff --git a/src/main/java/com/mmodding/mmodding_lib/PacketReceivers.java b/src/main/java/com/mmodding/mmodding_lib/PacketReceivers.java new file mode 100644 index 0000000..54a348c --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/PacketReceivers.java @@ -0,0 +1,51 @@ +package com.mmodding.mmodding_lib; + +import com.mmodding.mmodding_lib.library.network.request.s2c.ServerPendingRequestManager; +import com.mmodding.mmodding_lib.library.network.support.NetworkSupport; +import com.mmodding.mmodding_lib.library.network.request.c2s.ServerRequestHandler; +import com.mmodding.mmodding_lib.library.network.support.type.NetworkList; +import com.mmodding.mmodding_lib.library.network.support.type.NetworkMap; +import com.mmodding.mmodding_lib.library.utils.MModdingIdentifier; +import com.mmodding.mmodding_lib.networking.MModdingPackets; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.ApiStatus; +import org.quiltmc.qsl.networking.api.PacketByteBufs; +import org.quiltmc.qsl.networking.api.ServerPlayNetworking; + +import java.util.Map; +import java.util.UUID; + +@ApiStatus.Internal +public class PacketReceivers { + + @SuppressWarnings("unchecked") + public static void register() { + + // C2S Requests Networking + ServerPlayNetworking.registerGlobalReceiver(MModdingPackets.C2S_REQUESTS, (server, player, handler, buf, sender) -> { + Identifier requestHandler = buf.readIdentifier(); + Map requestMap = buf.readMap(PacketByteBuf::readUuid, NetworkSupport::readComplete); + PacketByteBuf packet = PacketByteBufs.create(); + NetworkMap responseMap = new NetworkMap(); + requestMap.forEach((uuid, args) -> { + T value = (T) ServerRequestHandler.HANDLERS.get(requestHandler).respond(args); + responseMap.put(new MModdingIdentifier(uuid.toString()), (Class) value.getClass(), value); + }); + packet.writeIdentifier(requestHandler); + responseMap.writeComplete(packet); + ServerPlayNetworking.send(player, MModdingPackets.C2S_RESPONSES, packet); + }); + + // S2C Responses Networking + ServerPlayNetworking.registerGlobalReceiver(MModdingPackets.S2C_RESPONSES, (server, player, handler, buf, sender) -> { + Identifier manager = buf.readIdentifier(); + NetworkMap map = NetworkSupport.readComplete(buf); + map.forEach( + (identifier, type, value) -> ServerPendingRequestManager.getManager(manager) + .consume(UUID.fromString(identifier.getPath())) + .ifPresent(action -> action.cast(value)) + ); + }); + } +} diff --git a/src/main/java/com/mmodding/mmodding_lib/client/ClientPacketReceivers.java b/src/main/java/com/mmodding/mmodding_lib/client/ClientPacketReceivers.java index 4219d1b..1aaf84b 100644 --- a/src/main/java/com/mmodding/mmodding_lib/client/ClientPacketReceivers.java +++ b/src/main/java/com/mmodding/mmodding_lib/client/ClientPacketReceivers.java @@ -1,30 +1,69 @@ package com.mmodding.mmodding_lib.client; +import com.mmodding.mmodding_lib.library.network.request.c2s.ClientPendingRequestManager; +import com.mmodding.mmodding_lib.library.network.request.s2c.ClientRequestHandler; +import com.mmodding.mmodding_lib.library.network.support.NetworkSupport; +import com.mmodding.mmodding_lib.library.network.support.type.NetworkList; +import com.mmodding.mmodding_lib.library.network.support.type.NetworkMap; +import com.mmodding.mmodding_lib.library.utils.MModdingIdentifier; import com.mmodding.mmodding_lib.networking.MModdingPackets; import com.mmodding.mmodding_lib.networking.client.ClientOperations; +import net.minecraft.client.MinecraftClient; import net.minecraft.entity.Entity; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.util.Identifier; import org.jetbrains.annotations.ApiStatus; import org.quiltmc.loader.api.minecraft.ClientOnly; +import org.quiltmc.qsl.networking.api.PacketByteBufs; import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking; +import java.util.Map; +import java.util.UUID; + @ClientOnly @ApiStatus.Internal public class ClientPacketReceivers { - public static void register() { + @SuppressWarnings("unchecked") + public static void register() { + + // S2C Requests Networking + ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.S2C_REQUESTS, (server, handler, buf, sender) -> { + Identifier requestHandler = buf.readIdentifier(); + Map requestMap = buf.readMap(PacketByteBuf::readUuid, NetworkSupport::readComplete); + PacketByteBuf packet = PacketByteBufs.create(); + NetworkMap responseMap = new NetworkMap(); + requestMap.forEach((uuid, args) -> { + T value = (T) ClientRequestHandler.HANDLERS.get(requestHandler).respond(MinecraftClient.getInstance().player, args); + responseMap.put(new MModdingIdentifier(uuid.toString()), (Class) value.getClass(), value); + }); + packet.writeIdentifier(requestHandler); + responseMap.writeComplete(packet); + ClientPlayNetworking.send(MModdingPackets.S2C_RESPONSES, packet); + }); + + // C2S Responses Networking + ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.C2S_RESPONSES, (client, handler, buf, response) -> { + Identifier manager = buf.readIdentifier(); + NetworkMap map = NetworkSupport.readComplete(buf); + map.forEach( + (identifier, type, value) -> ClientPendingRequestManager.getManager(manager) + .consume(UUID.fromString(identifier.getPath())) + .ifPresent(action -> action.cast(value)) + ); + }); // Syncable Data Elements Networking - ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.SYNCABLE_DATA, (((client, handler, buf, sender) -> { + ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.SYNCABLE_DATA, (client, handler, buf, sender) -> { int entityId = buf.readVarInt(); assert client.world != null; Entity entity = client.world.getEntityById(entityId); assert entity != null; entity.getSyncableDataRegistry().accept(buf); - }))); + }); // Client Operations Networking - ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.CONFIGS, ((client, handler, buf, sender) -> ClientOperations.receiveConfigOnClient(buf))); - ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.GLINT_PACKS, ((client, handler, buf, sender) -> ClientOperations.receiveGlintPackOnClient(buf))); - ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.STELLAR_STATUS, (((client, handler, buf, sender) -> ClientOperations.receiveStellarStatusOnClient(handler, buf)))); + ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.CONFIGS, (client, handler, buf, sender) -> ClientOperations.receiveConfigOnClient(buf)); + ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.STELLAR_STATUS, (client, handler, buf, sender) -> ClientOperations.receiveStellarStatusOnClient(handler, buf)); } }