Skip to content

Commit 192c0d9

Browse files
committed
feat: server-bound packets
1 parent e033c7c commit 192c0d9

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.mcdiamondfire.proto;
22

33
import com.google.protobuf.Message;
4+
import org.jspecify.annotations.Nullable;
45

56
/**
67
* Represents a deserialized ModAPI message.
78
*
8-
* @param id the unique identifier of the ModAPI message type
9-
* @param message the deserialized Protocol Message instance
9+
* @param id the unique identifier of the ModAPI message type
10+
* @param message the deserialized Protocol Message instance
11+
* @param requestId the ID of the original request message, or null if not applicable
1012
*/
11-
public record ModAPIMessage(String id, Message message) {
12-
13+
public record ModAPIMessage(String id, Message message, @Nullable Integer requestId) {
14+
1315
}

src/main/java/com/mcdiamondfire/proto/ModAPIMessages.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import com.google.protobuf.Message;
44
import com.mcdiamondfire.proto.messages.clientbound.player.*;
5-
import com.mcdiamondfire.proto.messages.clientbound.plot.S2CPlotInfo;
5+
import com.mcdiamondfire.proto.messages.clientbound.plot.*;
66
import com.mcdiamondfire.proto.messages.clientbound.server.*;
7+
import com.mcdiamondfire.proto.messages.serverbound.player.*;
78

89
import java.util.*;
910

@@ -16,17 +17,31 @@ public final class ModAPIMessages {
1617
private static final Map<String, Class<? extends Message>> ID_CLASS_MAP = new HashMap<>();
1718

1819
static {
20+
21+
// Client-bound.
22+
1923
// Server.
2024
registerMessage(S2CServerInfo.class, "server_info");
2125
registerMessage(S2CServerBooster.class, "server_booster");
2226

2327
// Plot.
2428
registerMessage(S2CPlotInfo.class, "plot_info");
29+
registerMessage(S2CTemplate.class, "template");
2530

2631
// Player.
2732
registerMessage(S2CPlayerCurrency.class, "player_currency");
2833
registerMessage(S2CPlayerPermissions.class, "player_permissions");
2934
registerMessage(S2CPlayerSwitchMode.class, "player_switch_mode");
35+
36+
// Server-bound.
37+
38+
// Plot.
39+
registerMessage(C2SGetTemplate.class, "get_template");
40+
41+
// Player.
42+
registerMessage(C2SPlayerTeleport.class, "player_teleport");
43+
44+
3045
}
3146

3247
private ModAPIMessages() {

src/main/java/com/mcdiamondfire/proto/ModAPIUtility.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import com.google.gson.*;
44
import com.google.protobuf.*;
55
import com.google.protobuf.util.JsonFormat;
6+
import org.jspecify.annotations.Nullable;
67

78
import java.lang.reflect.InvocationTargetException;
89
import java.util.Optional;
910

1011
/**
1112
* Utility class for serializing ModAPI messages to JSON format.
1213
*/
14+
@SuppressWarnings("unused")
1315
public final class ModAPIUtility {
1416

1517
private static final JsonFormat.Parser JSON_PARSER = JsonFormat.parser().ignoringUnknownFields();
@@ -27,13 +29,29 @@ private ModAPIUtility() {
2729
* @throws InvalidProtocolBufferException if serialization fails
2830
*/
2931
public static String serializeMessage(final Message message) throws InvalidProtocolBufferException {
30-
final String json = JsonFormat.printer().print(message);
31-
final JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
32-
final Optional<String> messageId = ModAPIMessages.getMessageId(message.getClass());
33-
if (messageId.isEmpty()) {
34-
throw new IllegalArgumentException("Message class not registered: " + message.getClass().getName());
32+
return serializeMessage(message, null);
33+
}
34+
35+
/**
36+
* Serializes a Protocol Message to a JSON string,
37+
* including the packet ID and a request ID to correlate requests and responses.
38+
*
39+
* @param message the Protocol Message to serialize
40+
* @param requestId the request ID to include in the serialized message
41+
* @return the serialized JSON string with its packet ID and request ID included
42+
* @throws InvalidProtocolBufferException if serialization fails
43+
*/
44+
public static String serializeMessage(final Message message, final @Nullable Integer requestId) throws InvalidProtocolBufferException {
45+
final JsonObject jsonObject = JsonParser.parseString(JsonFormat.printer().print(message)).getAsJsonObject();
46+
47+
final String messageId = ModAPIMessages.getMessageId(message.getClass())
48+
.orElseThrow(() -> new IllegalArgumentException("Message class not registered: " + message.getClass().getName()));
49+
50+
jsonObject.add("packet_id", new JsonPrimitive(messageId));
51+
if (requestId != null) {
52+
jsonObject.add("request_id", new JsonPrimitive(requestId));
3553
}
36-
jsonObject.add("packet_id", new JsonPrimitive(messageId.get()));
54+
3755
return jsonObject.toString();
3856
}
3957

@@ -51,16 +69,16 @@ public static String serializeMessage(final Message message) throws InvalidProto
5169
public static ModAPIMessage deserializeMessage(final String json) throws InvalidProtocolBufferException {
5270
try {
5371
final JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
54-
final String packetId = jsonObject.remove("packet_id").getAsString();
55-
final String cleanedJson = jsonObject.toString();
72+
final String packetId = jsonObject.get("packet_id").getAsString();
73+
final Integer requestId = jsonObject.has("request_id") ? jsonObject.get("request_id").getAsInt() : null;
5674

5775
final Optional<Class<? extends Message>> clazz = ModAPIMessages.getMessageClass(packetId);
5876
if (clazz.isEmpty()) {
5977
throw new IllegalStateException("No message class registered for packet ID: " + packetId);
6078
}
6179
final Message.Builder builder = (Message.Builder) clazz.get().getMethod("newBuilder").invoke(null);
62-
JSON_PARSER.merge(cleanedJson, builder);
63-
return new ModAPIMessage(packetId, builder.build());
80+
JSON_PARSER.merge(json, builder);
81+
return new ModAPIMessage(packetId, builder.build(), requestId);
6482
} catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
6583
throw new RuntimeException("Failed to deserialize message.", e);
6684
}

0 commit comments

Comments
 (0)