Skip to content

Commit 34ed976

Browse files
committed
feat(api): add send/receive packet methods
1 parent ba5f278 commit 34ed976

14 files changed

+119
-48
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ dependencies {
5252
}
5353

5454
java {
55-
sourceCompatibility = JavaVersion.VERSION_1_8
56-
targetCompatibility = JavaVersion.VERSION_1_8
55+
sourceCompatibility = JavaVersion.VERSION_17
56+
targetCompatibility = JavaVersion.VERSION_17
5757

5858
withJavadocJar()
5959
withSourcesJar()

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

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,31 @@
55

66
import org.bukkit.entity.Player;
77

8-
import dev.protocollib.api.listener.PacketSentListener;
8+
import dev.protocollib.api.packet.PacketLike;
9+
import dev.protocollib.api.packet.PacketOperationBuilder;
910

1011
/**
11-
* Representing a connection of a player.
12+
* Represents a connection associated with a player.
13+
*
14+
* <p>This interface provides methods to interact with the player's network connection,
15+
* including retrieving the player's information, connection address, protocol version,
16+
* and current connection state. It also allows for sending and receiving packets
17+
* through the connection.</p>
1218
*/
1319
public interface Connection {
1420

21+
1522
/**
1623
* Retrieves the player associated with the connection, if available.
1724
*
18-
* @return an optional containing the player, or empty if the player is not present
25+
* @return an {@link Optional} containing the player, or empty if the player is not present
1926
*/
2027
Optional<Player> player();
2128

2229
/**
2330
* Retrieves the address of the connection.
2431
*
25-
* @return the remote address of the connection
32+
* @return the remote {@link InetSocketAddress} of the connection
2633
*/
2734
InetSocketAddress address();
2835

@@ -37,53 +44,37 @@ public interface Connection {
3744
* Retrieves the current protocol phase of the connection for a given direction.
3845
*
3946
* @param packetDirection the direction of the packet (clientbound or serverbound)
40-
* @return the protocol phase of the connection
47+
* @return the {@link ProtocolPhase} representing the current phase of the connection
4148
*/
4249
ProtocolPhase protocolPhase(ProtocolDirection packetDirection);
4350

4451
/**
4552
* Checks if the connection is currently open.
4653
*
47-
* @return true if the connection is open, false otherwise
54+
* @return {@code true} if the connection is open, {@code false} otherwise
4855
*/
4956
boolean isConnected();
5057

5158
/**
52-
* Sends a binary packet over the connection.
53-
*
54-
* @param packet the binary packet to send
55-
*/
56-
void sendPacket(BinaryPacket packet);
57-
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.
59+
* Initiates a packet operation, which can involve sending or receiving a packet.
6860
*
69-
* @param packet the packet container to send
61+
* @return a {@link PacketOperationBuilder} to configure the packet operation
7062
*/
71-
void sendPacket(PacketContainer packet);
63+
PacketOperationBuilder packetOperation();
7264

7365
/**
74-
* Sends a packet container over the connection and registers a listener for when the packet is sent.
66+
* Sends a packet to the client.
7567
*
76-
* @param packet the packet container to send
77-
* @param listener the listener to invoke once the packet is sent
68+
* @param packet the packet to send
7869
*/
79-
void sendPacket(PacketContainer packet, PacketSentListener listener);
70+
void sendPacket(PacketLike packet);
8071

8172
/**
82-
* Receives a packet container from the connection.
73+
* Receives a packet as if the client had sent it.
8374
*
84-
* @param packet the packet container received
75+
* @param packet the received packet
8576
*/
86-
void receivePacket(PacketContainer packet);
77+
void receivePacket(PacketLike packet);
8778

8879
/**
8980
* Disconnects the connection with the specified reason.

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import org.bukkit.plugin.Plugin;
55

66
import dev.protocollib.api.listener.PacketListenerBuilder;
7+
import dev.protocollib.api.packet.BinaryPacket;
8+
import dev.protocollib.api.packet.PacketContainer;
9+
import dev.protocollib.api.packet.PacketLike;
10+
import dev.protocollib.api.packet.PacketType;
711

812
/**
913
* Representing the main entry point for the ProtocolLib API.
@@ -26,6 +30,24 @@ public interface ProtocolLib {
2630
*/
2731
Connection connection(Player player);
2832

33+
/**
34+
* Sends a packet to the player.
35+
*
36+
* @param packet the packet to send
37+
*/
38+
default void sendPacket(Player player, PacketLike packet) {
39+
connection(player).sendPacket(packet);
40+
}
41+
42+
/**
43+
* Receives a packet as if the player had sent it.
44+
*
45+
* @param packet the received packet
46+
*/
47+
default void receivePacket(Player player, PacketLike packet) {
48+
connection(player).receivePacket(packet);
49+
}
50+
2951
/**
3052
* Creates a new binary packet with the given type and payload.
3153
*

src/main/java/dev/protocollib/api/listener/AsyncPacketListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.protocollib.api.listener;
22

3-
import dev.protocollib.api.PacketContainer;
3+
import dev.protocollib.api.packet.PacketContainer;
44

55
/**
66
* Functional interface for handling packets asynchronously.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.Collection;
44

5-
import dev.protocollib.api.PacketType;
5+
import dev.protocollib.api.packet.PacketType;
66

77
/**
88
* Builder for creating and registering packet listeners.

src/main/java/dev/protocollib/api/listener/PacketSentListener.java renamed to src/main/java/dev/protocollib/api/listener/PacketTransmissionListener.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package dev.protocollib.api.listener;
22

33
/**
4-
* Functional interface for a listener that is invoked when a packet has been sent.
4+
* Functional interface for a listener that is invoked when a packet has been sent
5+
* (according to the underlying write's ChannelFuture) or received (just before the
6+
* packet gets processed by mojangs packet handlers).
57
*
68
* This method will get invoked on the underlying channel's event-loop.
79
*/
810
@FunctionalInterface
9-
public interface PacketSentListener {
11+
public interface PacketTransmissionListener {
1012

1113
/**
1214
* Invoked when a packet has been successfully sent.

src/main/java/dev/protocollib/api/listener/SyncPacketListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.protocollib.api.listener;
22

3-
import dev.protocollib.api.PacketContainer;
3+
import dev.protocollib.api.packet.PacketContainer;
44

55
/**
66
* Functional interface for handling packets synchronously.

src/main/java/dev/protocollib/api/listener/SyncPacketListenerContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public interface SyncPacketListenerContext {
2727
void cancel();
2828

2929
/**
30-
* Adds a listener to be invoked after the packet is sent.
30+
* Adds a listener to be invoked after the packet is sent or received.
3131
*
32-
* @param listener the post-sent listener
32+
* @param listener the transmission listener to invoke
3333
*/
34-
void addPostSentListener(PacketSentListener listener);
34+
void addTransmissionListener(PacketTransmissionListener listener);
3535
}
3636

src/main/java/dev/protocollib/api/BinaryPacket.java renamed to src/main/java/dev/protocollib/api/packet/BinaryPacket.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package dev.protocollib.api;
1+
package dev.protocollib.api.packet;
22

33
/**
44
* Representing a raw binary packet with a packet id and payload.
55
*/
6-
public interface BinaryPacket {
6+
public non-sealed interface BinaryPacket extends PacketLike {
77

88
/**
99
* Retrieves the packet id.
@@ -19,4 +19,3 @@ public interface BinaryPacket {
1919
*/
2020
byte[] payload();
2121
}
22-

src/main/java/dev/protocollib/api/PacketContainer.java renamed to src/main/java/dev/protocollib/api/packet/PacketContainer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package dev.protocollib.api;
1+
package dev.protocollib.api.packet;
22

33
/**
44
* Representing a container for a packet.
55
*/
6-
public interface PacketContainer {
6+
public non-sealed interface PacketContainer extends PacketLike {
77

88
/**
99
* Retrieves the type of the packet.
@@ -19,4 +19,11 @@ public interface PacketContainer {
1919
*/
2020
Object packet();
2121

22+
/**
23+
* Creates and returns a mutable copy of this packet.
24+
*
25+
* @return a clone of this instance.
26+
*/
27+
PacketContainer clone();
28+
2229
}

0 commit comments

Comments
 (0)