-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Implementation Of Requests / Responses System
- Loading branch information
1 parent
cb537c4
commit 3e956db
Showing
2 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/main/java/com/mmodding/mmodding_lib/PacketReceivers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
); | ||
}); | ||
} | ||
} |
51 changes: 45 additions & 6 deletions
51
src/main/java/com/mmodding/mmodding_lib/client/ClientPacketReceivers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |