Skip to content

Commit ba5f278

Browse files
committed
feat(api): add basic packet processing interfaces
1 parent 8d2efb3 commit ba5f278

21 files changed

+414
-66
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
package dev.protocollib.api;
22

3+
/**
4+
* Representing a raw binary packet with a packet id and payload.
5+
*/
36
public interface BinaryPacket {
47

8+
/**
9+
* Retrieves the packet id.
10+
*
11+
* @return the packet ID
12+
*/
513
int id();
614

15+
/**
16+
* Retrieves the payload (data) of the packet.
17+
*
18+
* @return the packet payload as a byte array
19+
*/
720
byte[] payload();
821
}
22+
Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,95 @@
11
package dev.protocollib.api;
22

33
import java.net.InetSocketAddress;
4-
5-
import javax.annotation.Nullable;
4+
import java.util.Optional;
65

76
import org.bukkit.entity.Player;
87

8+
import dev.protocollib.api.listener.PacketSentListener;
9+
10+
/**
11+
* Representing a connection of a player.
12+
*/
913
public interface Connection {
1014

11-
@Nullable
12-
Player player();
15+
/**
16+
* Retrieves the player associated with the connection, if available.
17+
*
18+
* @return an optional containing the player, or empty if the player is not present
19+
*/
20+
Optional<Player> player();
1321

22+
/**
23+
* Retrieves the address of the connection.
24+
*
25+
* @return the remote address of the connection
26+
*/
1427
InetSocketAddress address();
1528

29+
/**
30+
* Retrieves the protocol version used by the connection.
31+
*
32+
* @return the protocol version
33+
*/
1634
int protocolVersion();
1735

18-
ProtocolPhase protocolPhase(PacketDirection packetDirection);
36+
/**
37+
* Retrieves the current protocol phase of the connection for a given direction.
38+
*
39+
* @param packetDirection the direction of the packet (clientbound or serverbound)
40+
* @return the protocol phase of the connection
41+
*/
42+
ProtocolPhase protocolPhase(ProtocolDirection packetDirection);
1943

44+
/**
45+
* Checks if the connection is currently open.
46+
*
47+
* @return true if the connection is open, false otherwise
48+
*/
2049
boolean isConnected();
2150

51+
/**
52+
* Sends a binary packet over the connection.
53+
*
54+
* @param packet the binary packet to send
55+
*/
2256
void sendPacket(BinaryPacket packet);
2357

58+
/**
59+
* Sends a binary packet over the connection and registers a listener for when the packet is sent.
60+
*
61+
* @param packet the binary packet to send
62+
* @param listener the listener to invoke once the packet is sent
63+
*/
64+
void sendPacket(BinaryPacket packet, PacketSentListener listener);
65+
66+
/**
67+
* Sends a packet container over the connection.
68+
*
69+
* @param packet the packet container to send
70+
*/
2471
void sendPacket(PacketContainer packet);
2572

73+
/**
74+
* Sends a packet container over the connection and registers a listener for when the packet is sent.
75+
*
76+
* @param packet the packet container to send
77+
* @param listener the listener to invoke once the packet is sent
78+
*/
79+
void sendPacket(PacketContainer packet, PacketSentListener listener);
80+
81+
/**
82+
* Receives a packet container from the connection.
83+
*
84+
* @param packet the packet container received
85+
*/
2686
void receivePacket(PacketContainer packet);
2787

88+
/**
89+
* Disconnects the connection with the specified reason.
90+
*
91+
* @param reason the reason for disconnecting
92+
*/
2893
void disconnect(String reason);
2994

3095
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
package dev.protocollib.api;
22

3+
/**
4+
* Representing a container for a packet.
5+
*/
36
public interface PacketContainer {
47

8+
/**
9+
* Retrieves the type of the packet.
10+
*
11+
* @return the packet type
12+
*/
513
PacketType packetType();
614

15+
/**
16+
* Retrieves the raw packet object.
17+
*
18+
* @return the packet object
19+
*/
720
Object packet();
821

922
}

src/main/java/dev/protocollib/api/PacketDirection.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/java/dev/protocollib/api/PacketEvent.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/dev/protocollib/api/PacketListener.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/java/dev/protocollib/api/PacketListenerBuilder.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/main/java/dev/protocollib/api/PacketListenerPriority.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/java/dev/protocollib/api/PacketListenerRegistration.java

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
package dev.protocollib.api;
22

3-
public interface PacketType {
3+
import java.util.Optional;
44

5-
PacketDirection packetDirection();
5+
import net.kyori.adventure.key.Keyed;
66

7-
Class<?> packetClass();
7+
/**
8+
* Representing the type of a network packet.
9+
*
10+
* <p>A {@code PacketType} identifies a specific type of packet in the protocol,
11+
* including information about its direction, associated class (if any), and
12+
* whether the packet is currently supported.</p>
13+
*/
14+
public interface PacketType extends Keyed {
815

9-
boolean isSupported();
16+
/**
17+
* Retrieves the direction in which the packet is being sent.
18+
*
19+
* @return the {@link ProtocolDirection} of the packet, either clientbound or serverbound
20+
*/
21+
ProtocolDirection protocolDirection();
22+
23+
/**
24+
* Retrieves the class associated with the packet type, if available.
25+
*
26+
* <p>Not all packet types have an associated class. If there is no class,
27+
* an empty {@link Optional} is returned.</p>
28+
*
29+
* @return an {@link Optional} containing the class of the packet, or empty if not applicable
30+
*/
31+
Optional<Class<?>> packetClass();
1032

11-
boolean isDeprecated();
33+
/**
34+
* Checks whether the packet type is supported by the current protocol version.
35+
*
36+
* @return {@code true} if the packet type is supported, {@code false} otherwise
37+
*/
38+
boolean isSupported();
1239

1340
}

0 commit comments

Comments
 (0)