|
1 | 1 | package org.geysermc.mcprotocollib.network.packet; |
2 | 2 |
|
3 | 3 | import io.netty.buffer.ByteBuf; |
4 | | -import it.unimi.dsi.fastutil.ints.Int2ObjectMap; |
5 | | -import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; |
6 | 4 | import org.geysermc.mcprotocollib.network.Server; |
7 | 5 | import org.geysermc.mcprotocollib.network.Session; |
8 | 6 | import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper; |
9 | | -import org.geysermc.mcprotocollib.network.codec.PacketDefinition; |
10 | | -import org.geysermc.mcprotocollib.network.codec.PacketSerializer; |
11 | | - |
12 | | -import java.util.IdentityHashMap; |
13 | | -import java.util.Map; |
14 | 7 |
|
15 | 8 | /** |
16 | 9 | * A protocol for packet sending and receiving. |
17 | 10 | * All implementations must have a constructor that takes in a {@link ByteBuf}. |
18 | 11 | */ |
19 | 12 | public abstract class PacketProtocol { |
20 | | - private final Int2ObjectMap<PacketDefinition<? extends Packet, ?>> serverbound = new Int2ObjectOpenHashMap<>(); |
21 | | - private final Int2ObjectMap<PacketDefinition<? extends Packet, ?>> clientbound = new Int2ObjectOpenHashMap<>(); |
22 | | - |
23 | | - private final Map<Class<? extends Packet>, Integer> clientboundIds = new IdentityHashMap<>(); |
24 | | - private final Map<Class<? extends Packet>, Integer> serverboundIds = new IdentityHashMap<>(); |
25 | | - |
26 | 13 | /** |
27 | 14 | * Gets the prefix used when locating SRV records for this protocol. |
28 | 15 | * |
@@ -62,241 +49,9 @@ public abstract class PacketProtocol { |
62 | 49 | public abstract void newServerSession(Server server, Session session); |
63 | 50 |
|
64 | 51 | /** |
65 | | - * Clears all currently registered packets. |
66 | | - */ |
67 | | - public final void clearPackets() { |
68 | | - this.serverbound.clear(); |
69 | | - this.clientbound.clear(); |
70 | | - this.clientboundIds.clear(); |
71 | | - this.serverboundIds.clear(); |
72 | | - } |
73 | | - |
74 | | - /** |
75 | | - * Registers a packet to this protocol as both serverbound and clientbound. |
76 | | - * |
77 | | - * @param id Id to register the packet to. |
78 | | - * @param packet Packet to register. |
79 | | - * @param serializer The packet serializer. |
80 | | - * @throws IllegalArgumentException If the packet fails a test creation when being registered as serverbound. |
81 | | - */ |
82 | | - public final <T extends Packet, H extends PacketCodecHelper> void register(int id, Class<T> packet, PacketSerializer<T, H> serializer) { |
83 | | - this.registerServerbound(id, packet, serializer); |
84 | | - this.registerClientbound(id, packet, serializer); |
85 | | - } |
86 | | - |
87 | | - /** |
88 | | - * Registers a packet to this protocol as both serverbound and clientbound. |
89 | | - * |
90 | | - * @param definition The packet definition. |
91 | | - * @throws IllegalArgumentException If the packet fails a test creation when being registered as serverbound. |
92 | | - */ |
93 | | - public final void register(PacketDefinition<? extends Packet, ?> definition) { |
94 | | - this.registerServerbound(definition); |
95 | | - this.registerClientbound(definition); |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Registers a serverbound packet to this protocol. |
100 | | - * |
101 | | - * @param id Id to register the packet to. |
102 | | - * @param packet Packet to register. |
103 | | - * @param serializer The packet serializer. |
104 | | - * @throws IllegalArgumentException If the packet fails a test creation. |
105 | | - */ |
106 | | - public final <T extends Packet, H extends PacketCodecHelper> void registerServerbound(int id, Class<T> packet, PacketSerializer<T, H> serializer) { |
107 | | - this.registerServerbound(new PacketDefinition<>(id, packet, serializer)); |
108 | | - } |
109 | | - |
110 | | - /** |
111 | | - * Registers a serverbound packet to this protocol. |
112 | | - * |
113 | | - * @param definition The packet definition. |
114 | | - */ |
115 | | - public final void registerServerbound(PacketDefinition<? extends Packet, ?> definition) { |
116 | | - this.serverbound.put(definition.getId(), definition); |
117 | | - this.serverboundIds.put(definition.getPacketClass(), definition.getId()); |
118 | | - } |
119 | | - |
120 | | - /** |
121 | | - * Registers a clientbound packet to this protocol. |
122 | | - * |
123 | | - * @param id Id to register the packet to. |
124 | | - * @param packet Packet to register. |
125 | | - * @param serializer The packet serializer. |
126 | | - */ |
127 | | - public final <T extends Packet, H extends PacketCodecHelper> void registerClientbound(int id, Class<T> packet, PacketSerializer<T, H> serializer) { |
128 | | - this.registerClientbound(new PacketDefinition<>(id, packet, serializer)); |
129 | | - } |
130 | | - |
131 | | - /** |
132 | | - * Registers a clientbound packet to this protocol. |
133 | | - * |
134 | | - * @param definition The packet definition. |
135 | | - */ |
136 | | - public final void registerClientbound(PacketDefinition<? extends Packet, ?> definition) { |
137 | | - this.clientbound.put(definition.getId(), definition); |
138 | | - this.clientboundIds.put(definition.getPacketClass(), definition.getId()); |
139 | | - } |
140 | | - |
141 | | - /** |
142 | | - * Creates a new instance of a clientbound packet with the given id and read the clientbound input. |
143 | | - * |
144 | | - * @param id Id of the packet to create. |
145 | | - * @param buf The buffer to read the packet from. |
146 | | - * @param codecHelper The codec helper. |
147 | | - * @return The created packet. |
148 | | - * @throws IllegalArgumentException If the packet ID is not registered. |
149 | | - */ |
150 | | - @SuppressWarnings("unchecked") |
151 | | - public <H extends PacketCodecHelper> Packet createClientboundPacket(int id, ByteBuf buf, H codecHelper) { |
152 | | - PacketDefinition<?, H> definition = (PacketDefinition<?, H>) this.clientbound.get(id); |
153 | | - if (definition == null) { |
154 | | - throw new IllegalArgumentException("Invalid packet id: " + id); |
155 | | - } |
156 | | - |
157 | | - return definition.newInstance(buf, codecHelper); |
158 | | - } |
159 | | - |
160 | | - /** |
161 | | - * Gets the registered id of a clientbound packet class. |
162 | | - * |
163 | | - * @param packetClass Class of the packet to get the id for. |
164 | | - * @return The packet's registered id. |
165 | | - * @throws IllegalArgumentException If the packet is not registered. |
166 | | - */ |
167 | | - public int getClientboundId(Class<? extends Packet> packetClass) { |
168 | | - Integer packetId = this.clientboundIds.get(packetClass); |
169 | | - if (packetId == null) { |
170 | | - throw new IllegalArgumentException("Unregistered clientbound packet class: " + packetClass.getName()); |
171 | | - } |
172 | | - |
173 | | - return packetId; |
174 | | - } |
175 | | - |
176 | | - /** |
177 | | - * Gets the registered id of a clientbound {@link Packet} instance. |
178 | | - * |
179 | | - * @param packet Instance of {@link Packet} to get the id for. |
180 | | - * @return The packet's registered id. |
181 | | - * @throws IllegalArgumentException If the packet is not registered. |
182 | | - */ |
183 | | - public int getClientboundId(Packet packet) { |
184 | | - if (packet instanceof BufferedPacket bufferedPacket) { |
185 | | - return getClientboundId(bufferedPacket.getPacketClass()); |
186 | | - } |
187 | | - |
188 | | - return getClientboundId(packet.getClass()); |
189 | | - } |
190 | | - |
191 | | - /** |
192 | | - * Gets the packet class for a packet id. |
193 | | - * |
194 | | - * @param id The packet id. |
195 | | - * @return The registered packet's class |
196 | | - * @throws IllegalArgumentException If the packet ID is not registered. |
197 | | - */ |
198 | | - public Class<? extends Packet> getClientboundClass(int id) { |
199 | | - PacketDefinition<?, ?> definition = this.clientbound.get(id); |
200 | | - if (definition == null) { |
201 | | - throw new IllegalArgumentException("Invalid packet id: " + id); |
202 | | - } |
203 | | - |
204 | | - return definition.getPacketClass(); |
205 | | - } |
206 | | - |
207 | | - /** |
208 | | - * Creates a new instance of a serverbound packet with the given id and read the serverbound input. |
209 | | - * |
210 | | - * @param id Id of the packet to create. |
211 | | - * @param buf The buffer to read the packet from. |
212 | | - * @param codecHelper The codec helper. |
213 | | - * @return The created packet. |
214 | | - * @throws IllegalArgumentException If the packet ID is not registered. |
215 | | - */ |
216 | | - @SuppressWarnings("unchecked") |
217 | | - public <H extends PacketCodecHelper> Packet createServerboundPacket(int id, ByteBuf buf, H codecHelper) { |
218 | | - PacketDefinition<?, H> definition = (PacketDefinition<?, H>) this.serverbound.get(id); |
219 | | - if (definition == null) { |
220 | | - throw new IllegalArgumentException("Invalid packet id: " + id); |
221 | | - } |
222 | | - |
223 | | - return definition.newInstance(buf, codecHelper); |
224 | | - } |
225 | | - |
226 | | - /** |
227 | | - * Gets the registered id of a serverbound packet class. |
228 | | - * |
229 | | - * @param packetClass Class of the packet to get the id for. |
230 | | - * @return The packet's registered id. |
231 | | - * @throws IllegalArgumentException If the packet is not registered. |
232 | | - */ |
233 | | - public int getServerboundId(Class<? extends Packet> packetClass) { |
234 | | - Integer packetId = this.serverboundIds.get(packetClass); |
235 | | - if (packetId == null) { |
236 | | - throw new IllegalArgumentException("Unregistered serverbound packet class: " + packetClass.getName()); |
237 | | - } |
238 | | - |
239 | | - return packetId; |
240 | | - } |
241 | | - |
242 | | - /** |
243 | | - * Gets the registered id of a serverbound {@link Packet} instance. |
244 | | - * |
245 | | - * @param packet Instance of {@link Packet} to get the id for. |
246 | | - * @return The packet's registered id. |
247 | | - * @throws IllegalArgumentException If the packet is not registered. |
248 | | - */ |
249 | | - public int getServerboundId(Packet packet) { |
250 | | - if (packet instanceof BufferedPacket bufferedPacket) { |
251 | | - return getServerboundId(bufferedPacket.getPacketClass()); |
252 | | - } |
253 | | - |
254 | | - return getServerboundId(packet.getClass()); |
255 | | - } |
256 | | - |
257 | | - /** |
258 | | - * Gets the packet class for a packet id. |
| 52 | + * Gets the packet registry for this protocol. |
259 | 53 | * |
260 | | - * @param id The packet id. |
261 | | - * @return The registered packet's class |
262 | | - * @throws IllegalArgumentException If the packet ID is not registered. |
| 54 | + * @return The protocol's packet registry. |
263 | 55 | */ |
264 | | - public Class<? extends Packet> getServerboundClass(int id) { |
265 | | - PacketDefinition<?, ?> definition = this.serverbound.get(id); |
266 | | - if (definition == null) { |
267 | | - throw new IllegalArgumentException("Invalid packet id: " + id); |
268 | | - } |
269 | | - |
270 | | - return definition.getPacketClass(); |
271 | | - } |
272 | | - |
273 | | - /** |
274 | | - * Gets the serverbound packet definition for the given packet id. |
275 | | - * |
276 | | - * @param id The packet id. |
277 | | - * @return The registered packet's class |
278 | | - */ |
279 | | - public PacketDefinition<?, ?> getServerboundDefinition(int id) { |
280 | | - PacketDefinition<?, ?> definition = this.serverbound.get(id); |
281 | | - if (definition == null) { |
282 | | - throw new IllegalArgumentException("Invalid packet id: " + id); |
283 | | - } |
284 | | - |
285 | | - return definition; |
286 | | - } |
287 | | - |
288 | | - /** |
289 | | - * Gets the clientbound packet definition for the given packet id. |
290 | | - * |
291 | | - * @param id The packet id. |
292 | | - * @return The registered packet's class |
293 | | - */ |
294 | | - public PacketDefinition<?, ?> getClientboundDefinition(int id) { |
295 | | - PacketDefinition<?, ?> definition = this.clientbound.get(id); |
296 | | - if (definition == null) { |
297 | | - throw new IllegalArgumentException("Invalid packet id: " + id); |
298 | | - } |
299 | | - |
300 | | - return definition; |
301 | | - } |
| 56 | + public abstract PacketRegistry getPacketRegistry(); |
302 | 57 | } |
0 commit comments