Skip to content

Commit 04f18a7

Browse files
committed
feat: assume class based off packet ID, returning a ModAPIMessage
1 parent 92aa7dd commit 04f18a7

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.mcdiamondfire.proto;
2+
3+
import com.google.protobuf.Message;
4+
5+
/**
6+
* Represents a deserialized ModAPI message.
7+
*
8+
* @param id the unique identifier of the ModAPI message type
9+
* @param message the deserialized Protocol Message instance
10+
*/
11+
public record ModAPIMessage(String id, Message message) {
12+
13+
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.protobuf.util.JsonFormat;
66

77
import java.lang.reflect.InvocationTargetException;
8+
import java.util.Optional;
89

910
/**
1011
* Utility class for serializing ModAPI messages to JSON format.
@@ -28,33 +29,38 @@ private ModAPIUtility() {
2829
public static String serializeMessage(final Message message) throws InvalidProtocolBufferException {
2930
final String json = JsonFormat.printer().print(message);
3031
final JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
31-
jsonObject.add("packet_id", new JsonPrimitive(ModAPIMessages.getMessageId(message.getClass())));
32+
final Optional<String> messageId = ModAPIMessages.getMessageId(message.getClass());
33+
if (messageId.isEmpty()) {
34+
throw new IllegalArgumentException("Message class not registered: " + message.getClass().getName());
35+
}
36+
jsonObject.add("packet_id", new JsonPrimitive(messageId.get()));
3237
return jsonObject.toString();
3338
}
3439

3540
/**
36-
* Deserializes a JSON string back into a Protocol Message of the specified class.
41+
* Deserializes a JSON string back into a {@link ModAPIMessage}.
3742
*
38-
* @param json the JSON string to deserialize
39-
* @param clazz the class of the Protocol Message to deserialize into
40-
* @param <T> the type of the Protocol Message
41-
* @return the deserialized Protocol Message
43+
* @param json the JSON string to deserialize
44+
* @return the deserialized Protocol Message and its ID wrapped in a {@link ModAPIMessage}
4245
* @throws InvalidProtocolBufferException if deserialization fails
4346
* @throws JsonSyntaxException if the JSON is not properly formatted
4447
* @throws IllegalStateException if the JSON does not contain a valid packet ID
4548
* @throws RuntimeException if reflection fails to create a new builder instance
46-
* @throws ClassCastException if the built message is not of the expected type
49+
* @throws ClassCastException if the packet ID is not of the expected type
4750
*/
48-
public static <T extends Message> T deserializeMessage(final String json, final Class<T> clazz) throws InvalidProtocolBufferException {
51+
public static ModAPIMessage deserializeMessage(final String json) throws InvalidProtocolBufferException {
4952
try {
5053
final JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
51-
jsonObject.remove("packet_id").getAsString();
54+
final String packetId = jsonObject.remove("packet_id").getAsString();
5255
final String cleanedJson = jsonObject.toString();
5356

54-
final Message.Builder builder = (Message.Builder) clazz.getMethod("newBuilder").invoke(null);
57+
final Optional<Class<? extends Message>> clazz = ModAPIMessages.getMessageClass(packetId);
58+
if (clazz.isEmpty()) {
59+
throw new IllegalStateException("No message class registered for packet ID: " + packetId);
60+
}
61+
final Message.Builder builder = (Message.Builder) clazz.get().getMethod("newBuilder").invoke(null);
5562
JSON_PARSER.merge(cleanedJson, builder);
56-
//noinspection unchecked -- on the caller to ensure the type is correct.
57-
return (T) builder.build();
63+
return new ModAPIMessage(packetId, builder.build());
5864
} catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
5965
throw new RuntimeException("Failed to deserialize message.", e);
6066
}

0 commit comments

Comments
 (0)