Skip to content

Commit 92aa7dd

Browse files
committed
feat: add id to class registry
1 parent 4b1f8b1 commit 92aa7dd

File tree

1 file changed

+34
-11
lines changed

1 file changed

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

33
import com.google.protobuf.Message;
4+
import com.mcdiamondfire.proto.messages.*;
45

56
import java.util.*;
67

@@ -9,34 +10,56 @@
910
*/
1011
public final class ModAPIMessages {
1112

12-
private static final Map<Class<? extends Message>, String> messages = new HashMap<>();
13+
private static final Map<Class<? extends Message>, String> CLASS_ID_MAP = new HashMap<>();
14+
private static final Map<String, Class<? extends Message>> ID_CLASS_MAP = new HashMap<>();
1315

1416
static {
1517
// Server.
16-
messages.put(ServerInfo.class, "server_info");
17-
messages.put(ServerBooster.class, "server_booster");
18+
registerMessage(ServerInfo.class, "server_info");
19+
registerMessage(ServerBooster.class, "server_booster");
1820

1921
// Plot.
20-
messages.put(PlotInfo.class, "plot_info");
22+
registerMessage(PlotInfo.class, "plot_info");
2123

2224
// Player.
23-
messages.put(PlayerCurrency.class, "player_currency");
24-
messages.put(PlayerPermissions.class, "player_permissions");
25-
messages.put(PlayerSwitchMode.class, "player_switch_mode");
25+
registerMessage(PlayerCurrency.class, "player_currency");
26+
registerMessage(PlayerPermissions.class, "player_permissions");
27+
registerMessage(PlayerSwitchMode.class, "player_switch_mode");
2628
}
2729

2830
private ModAPIMessages() {
2931
// Prevent instantiation.
3032
}
3133

34+
private static void registerMessage(final Class<? extends Message> clazz, final String id) {
35+
if (CLASS_ID_MAP.containsKey(clazz)) {
36+
throw new IllegalStateException("Message class already registered: " + clazz.getName());
37+
}
38+
if (ID_CLASS_MAP.containsKey(id)) {
39+
throw new IllegalStateException("Message ID already registered: " + id);
40+
}
41+
CLASS_ID_MAP.put(clazz, id);
42+
ID_CLASS_MAP.put(id, clazz);
43+
}
44+
3245
/**
3346
* Gets the identifier for a given ModAPI message class.
3447
*
3548
* @param clazz the ModAPI message class
36-
* @return the identifier for the message class, or null if not registered
49+
* @return an Optional containing the identifier or empty if not registered
50+
*/
51+
public static Optional<String> getMessageId(final Class<? extends Message> clazz) {
52+
return Optional.ofNullable(CLASS_ID_MAP.get(clazz));
53+
}
54+
55+
/**
56+
* Gets the ModAPI message class for a given identifier.
57+
*
58+
* @param id the identifier of the ModAPI message
59+
* @return an Optional containing the ModAPI message class or empty if not registered
3760
*/
38-
public static String getMessageId(final Class<? extends Message> clazz) {
39-
return messages.get(clazz);
61+
public static Optional<Class<? extends Message>> getMessageClass(final String id) {
62+
return Optional.ofNullable(ID_CLASS_MAP.get(id));
4063
}
4164

4265
/**
@@ -45,7 +68,7 @@ public static String getMessageId(final Class<? extends Message> clazz) {
4568
* @return a mapping of message classes to their identifiers
4669
*/
4770
public static Map<Class<? extends Message>, String> getMessages() {
48-
return Collections.unmodifiableMap(messages);
71+
return Collections.unmodifiableMap(CLASS_ID_MAP);
4972
}
5073

5174
}

0 commit comments

Comments
 (0)