Skip to content

Commit

Permalink
Add Implementation Of Requests / Responses System
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Dec 2, 2023
1 parent cb537c4 commit 3e956db
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 6 deletions.
51 changes: 51 additions & 0 deletions src/main/java/com/mmodding/mmodding_lib/PacketReceivers.java
Original file line number Diff line number Diff line change
@@ -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 <T extends NetworkSupport> void register() {

// C2S Requests Networking
ServerPlayNetworking.registerGlobalReceiver(MModdingPackets.C2S_REQUESTS, (server, player, handler, buf, sender) -> {
Identifier requestHandler = buf.readIdentifier();
Map<UUID, NetworkList> 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<T>) 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))
);
});
}
}
Original file line number Diff line number Diff line change
@@ -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 <T extends NetworkSupport> void register() {

// S2C Requests Networking
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.S2C_REQUESTS, (server, handler, buf, sender) -> {
Identifier requestHandler = buf.readIdentifier();
Map<UUID, NetworkList> 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<T>) 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));
}
}

0 comments on commit 3e956db

Please sign in to comment.