Skip to content

Commit 41ecced

Browse files
Streamline api (#832)
This basically makes MinecraftCodec stateless and removes unused APIs. The MinecraftCodecHelper has a state for an unused registry API, which is not needed at all. Developers can just send their own registry as seen in ServerListener. These only exist because there was an attempt at allowing multi-version support inside MinecraftCodecHelper, but that's not gonna be necessary if ViaVersion support gets added to MCPL. The option to add back logic for multi-version support is fully open for the future. I didn't remove MinecraftCodecHelper being instance-based, so it's fully possible to readd the multi-version logic back in the future. Just level events and sounds being dynamic is not enough for multi-version support and thus should be removed to streamline the API. This also removes many workarounds from the code that cause possible inconsistency. Often the MinecraftCodecHelper gets initialized in the tests with empty parameters. This removes them as parameters and removes the need to provide them by moving them to the object itself. PacketCodecs were also improved to decrease the inheritance issues and streamline the code flow and remove the UnsupportedOperationExceptions being needed. BufferedPacket was also removed since it's useless for Minecraft.
1 parent 05a699d commit 41ecced

File tree

17 files changed

+352
-566
lines changed

17 files changed

+352
-566
lines changed

example/src/main/java/org/geysermc/mcprotocollib/network/example/TestProtocol.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.geysermc.mcprotocollib.network.packet.DefaultPacketHeader;
1313
import org.geysermc.mcprotocollib.network.packet.PacketHeader;
1414
import org.geysermc.mcprotocollib.network.packet.PacketProtocol;
15+
import org.geysermc.mcprotocollib.network.packet.PacketRegistry;
1516
import org.slf4j.Logger;
1617
import org.slf4j.LoggerFactory;
1718

@@ -21,6 +22,7 @@
2122
public class TestProtocol extends PacketProtocol {
2223
private static final Logger log = LoggerFactory.getLogger(TestProtocol.class);
2324
private final PacketHeader header = new DefaultPacketHeader();
25+
private final PacketRegistry registry = new PacketRegistry();
2426
private AESEncryption encrypt;
2527

2628
@SuppressWarnings("unused")
@@ -36,7 +38,7 @@ public PacketCodecHelper createHelper() {
3638
}
3739

3840
public void setSecretKey(SecretKey key) {
39-
this.register(0, PingPacket.class, new PacketSerializer<>() {
41+
registry.register(0, PingPacket.class, new PacketSerializer<>() {
4042
@Override
4143
public void serialize(ByteBuf buf, PacketCodecHelper helper, PingPacket packet) {
4244
helper.writeString(buf, packet.getPingId());
@@ -78,4 +80,9 @@ public void newClientSession(Session session, boolean transferring) {
7880
public void newServerSession(Server server, Session session) {
7981
session.addListener(new ServerSessionListener());
8082
}
83+
84+
@Override
85+
public PacketRegistry getPacketRegistry() {
86+
return registry;
87+
}
8188
}

protocol/src/main/java/org/geysermc/mcprotocollib/network/packet/BufferedPacket.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 3 additions & 248 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
package org.geysermc.mcprotocollib.network.packet;
22

33
import io.netty.buffer.ByteBuf;
4-
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
5-
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
64
import org.geysermc.mcprotocollib.network.Server;
75
import org.geysermc.mcprotocollib.network.Session;
86
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;
147

158
/**
169
* A protocol for packet sending and receiving.
1710
* All implementations must have a constructor that takes in a {@link ByteBuf}.
1811
*/
1912
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-
2613
/**
2714
* Gets the prefix used when locating SRV records for this protocol.
2815
*
@@ -62,241 +49,9 @@ public abstract class PacketProtocol {
6249
public abstract void newServerSession(Server server, Session session);
6350

6451
/**
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.
25953
*
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.
26355
*/
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();
30257
}

0 commit comments

Comments
 (0)