Skip to content

Commit fbc24f6

Browse files
committed
feat: add bundle behavior, terminal packets
1 parent 34ed976 commit fbc24f6

14 files changed

+237
-30
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package dev.protocollib;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.plugin.Plugin;
5+
import dev.protocollib.api.ProtocolLib;
6+
import dev.protocollib.api.listener.*;
7+
import dev.protocollib.api.packet.*;
8+
import java.util.concurrent.*;
9+
10+
public class Example {
11+
12+
private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
13+
private static ProtocolLib protocolLib;
14+
private static Plugin plugin;
15+
16+
// ========================
17+
// Packet Listeners
18+
// ========================
19+
20+
static void registerListeners() {
21+
// sync read-only listener
22+
protocolLib
23+
.createListener(plugin)
24+
.types(PacketTypes.Game.LEVEL_CHUNK)
25+
.priority(PacketListenerPriority.LOW)
26+
.includeCanceledPackets()
27+
.bundleBehavior(PacketListenerBundleBehavior.SKIP_OUTSIDE_BUNDLE)
28+
.registerSync((packet, context) -> {
29+
Chunk chunk = Chunk.from(packet);
30+
// Sync processing
31+
});
32+
33+
// async modify packet on netty thread
34+
protocolLib
35+
.createListener(plugin)
36+
.types(PacketTypes.Game.LEVEL_CHUNK)
37+
.mutable()
38+
.registerAsync((packet, context) -> {
39+
Chunk chunk = Chunk.from(packet);
40+
41+
// do processing here ...
42+
// write changes to packet ...
43+
44+
context.addTransmissionListener(chunk::markSent);
45+
context.resumeProcessing();
46+
});
47+
48+
// async modify packet on app thread-pool
49+
protocolLib
50+
.createListener(plugin)
51+
.types(PacketTypes.Game.LEVEL_CHUNK)
52+
.mutable()
53+
.registerAsync((packet, context) -> {
54+
Chunk chunk = Chunk.from(packet);
55+
56+
EXECUTOR.execute(() -> {
57+
58+
// do heavy processing here ...
59+
// write changes to packet ...
60+
61+
context.addTransmissionListener(chunk::markSent);
62+
context.resumeProcessing();
63+
});
64+
});
65+
}
66+
67+
// ========================
68+
// Packet Sending
69+
// ========================
70+
71+
static void sendPackets(Player player, Chunk chunk) {
72+
// full packet operation
73+
protocolLib
74+
.connection(player)
75+
.packetOperation()
76+
.skipProcessing()
77+
.postTransmission(chunk::markSent)
78+
.send(chunk.packet());
79+
80+
// connection shortcut
81+
protocolLib
82+
.connection(player)
83+
.sendPacket(chunk.packet());
84+
85+
// direct API shortcut
86+
protocolLib
87+
.sendPacket(player, chunk.packet());
88+
}
89+
90+
// ========================
91+
// Chunk Implementation
92+
// ========================
93+
94+
static class Chunk {
95+
static Chunk from(PacketContainer packet) {
96+
return new Chunk();
97+
}
98+
99+
PacketContainer packet() { return null; }
100+
void markSent() {}
101+
}
102+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
*/
66
public enum ProtocolPhase {
77

8-
HANDSHAKE, PLAY, STATUS, LOGIN, CONFIGURATION, UNKNOWN;
8+
HANDSHAKE, GAME, STATUS, LOGIN, CONFIGURATION, UNKNOWN;
99

1010
}

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

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

3+
import org.jetbrains.annotations.NotNull;
4+
35
import dev.protocollib.api.packet.PacketContainer;
46

57
/**
@@ -27,6 +29,6 @@ public interface AsyncPacketListener {
2729
* @param packet the packet to handle
2830
* @param context the context providing additional information about the packet and connection
2931
*/
30-
void handlePacket(PacketContainer packet, AsyncPacketListenerContext context);
32+
void handlePacket(@NotNull PacketContainer packet, @NotNull AsyncPacketListenerContext context);
3133

3234
}

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

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

3+
import org.jetbrains.annotations.NotNull;
4+
35
/**
46
* Representing the context of an asynchronous packet listener.
57
*/
@@ -16,6 +18,6 @@ public interface AsyncPacketListenerContext extends SyncPacketListenerContext {
1618
*
1719
* @param throwable the processing exception
1820
*/
19-
void resumeProcessingWithException(Throwable throwable);
21+
void resumeProcessingWithException(@NotNull Throwable throwable);
2022

2123
}

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.util.Collection;
44

5+
import org.jetbrains.annotations.Contract;
6+
import org.jetbrains.annotations.NotNull;
7+
58
import dev.protocollib.api.packet.PacketType;
69

710
/**
@@ -15,15 +18,17 @@ public interface PacketListenerBuilder {
1518
* @param packetTypes the packet types to listen for
1619
* @return the same builder for further configuration
1720
*/
18-
PacketListenerBuilder.WithType types(PacketType... packetTypes);
21+
@NotNull
22+
PacketListenerBuilder.WithType types(@NotNull PacketType... packetTypes);
1923

2024
/**
2125
* Specifies the types of packets to listen for.
2226
*
2327
* @param packetTypes the collection of packet types to listen for
2428
* @return the same builder for further configuration
2529
*/
26-
PacketListenerBuilder.WithType types(Collection<PacketType> packetTypes);
30+
@NotNull
31+
PacketListenerBuilder.WithType types(@NotNull Collection<PacketType> packetTypes);
2732

2833
/**
2934
* Interface for building a packet listener with specific packet types.
@@ -36,22 +41,36 @@ public interface WithType {
3641
* @param priority the priority to assign to the listener
3742
* @return the same builder for further configuration
3843
*/
39-
PacketListenerBuilder.WithType priority(PacketListenerPriority priority);
44+
@Contract("_ -> this")
45+
PacketListenerBuilder.WithType priority(@NotNull PacketListenerPriority priority);
4046

4147
/**
42-
* Marks the listener as immutable, preventing modifications of packets
43-
* inside the listener.
48+
* Allows the listener to modify packets. By default, listeners are read-only and
49+
* cannot modify packets.
4450
*
4551
* @return the same builder for further configuration
4652
*/
47-
PacketListenerBuilder.WithType immutable();
53+
@Contract("_ -> this")
54+
PacketListenerBuilder.WithType mutable();
4855

4956
/**
50-
* Configures the listener to ignore packets that have been cancelled.
57+
* Configures the bundle behavior for the listener, determining how packets in bundles are handled.
5158
*
59+
* @param bundleBehavior the bundle behavior to apply
5260
* @return the same builder for further configuration
61+
* @see PacketListenerBundleBehavior
5362
*/
54-
PacketListenerBuilder.WithType ignoreCancelledPackets();
63+
@Contract("_ -> this")
64+
PacketListenerBuilder.WithType bundleBehavior(@NotNull PacketListenerBundleBehavior bundleBehavior);
65+
66+
/**
67+
* Configures the listener to include packets that have been canceled. By default,
68+
* canceled packets are skipped.
69+
*
70+
* @return the same builder for further configuration
71+
*/
72+
@Contract("_ -> this")
73+
PacketListenerBuilder.WithType includeCanceledPackets();
5574

5675
/**
5776
* Registers the packet listener to operate synchronously. The listener
@@ -60,15 +79,16 @@ public interface WithType {
6079
* @param listener the synchronous packet listener to register
6180
* @return the same builder for further configuration
6281
*/
63-
PacketListenerRegistration registerSync(SyncPacketListener listener);
82+
@NotNull
83+
PacketListenerRegistration registerSync(@NotNull SyncPacketListener listener);
6484

6585
/**
6686
* Registers the packet listener to operate asynchronously.
6787
*
6888
* @param listener the asynchronous packet listener to register
6989
* @return the same builder for further configuration
7090
*/
71-
PacketListenerRegistration registerAsync(AsyncPacketListener listener);
91+
@NotNull
92+
PacketListenerRegistration registerAsync(@NotNull AsyncPacketListener listener);
7293
}
7394
}
74-
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package dev.protocollib.api.listener;
2+
3+
/**
4+
* Controls packet processing behavior relative to packet bundles (groups of
5+
* packets processed atomically).
6+
* <p>
7+
* Determines whether individual packets should be processed or ignored based on
8+
* their membership in a packet bundle. Packet bundles typically represent
9+
* groups of packets that are be processed together as an atomic unit.
10+
* </p>
11+
*/
12+
public enum PacketListenerBundleBehavior {
13+
14+
/**
15+
* Processes all packets unconditionally, regardless of whether they are part of
16+
* a packet bundle.
17+
*/
18+
ALWAYS,
19+
20+
/**
21+
* Processes only packets that exist outside of packet bundles, ignoring those
22+
* inside bundles.
23+
*/
24+
SKIP_INSIDE_BUNDLE,
25+
26+
/**
27+
* Processes only packets that are part of packet bundles, ignoring standalone
28+
* packets.
29+
*/
30+
SKIP_OUTSIDE_BUNDLE;
31+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ public interface PacketListenerRegistration {
1111
*/
1212
void unregister();
1313
}
14-

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ public interface PacketTransmissionListener {
1515
*/
1616
void invoke();
1717
}
18-

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

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

3+
import org.jetbrains.annotations.NotNull;
4+
35
import dev.protocollib.api.packet.PacketContainer;
46

57
/**
@@ -14,6 +16,6 @@ public interface SyncPacketListener {
1416
* @param packet the packet to handle
1517
* @param context the context providing additional information about the packet and functions
1618
*/
17-
void handlePacket(PacketContainer packet, SyncPacketListenerContext context);
19+
void handlePacket(@NotNull PacketContainer packet, @NotNull SyncPacketListenerContext context);
1820

1921
}

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

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

3+
import org.jetbrains.annotations.NotNull;
4+
35
import dev.protocollib.api.Connection;
46

57
/**
@@ -12,6 +14,7 @@ public interface SyncPacketListenerContext {
1214
*
1315
* @return the connection handling the packet
1416
*/
17+
@NotNull
1518
Connection connection();
1619

1720
/**
@@ -22,15 +25,17 @@ public interface SyncPacketListenerContext {
2225
boolean isCancelled();
2326

2427
/**
25-
* Cancels the packet, preventing it from being processed further.
28+
* Sets whether the packet handling is cancelled. If cancelled, the packet
29+
* will not be processed further.
30+
*
31+
* @param cancelled true to cancel the packet, false to allow processing
2632
*/
27-
void cancel();
33+
void setCancelled(boolean cancelled);
2834

2935
/**
3036
* Adds a listener to be invoked after the packet is sent or received.
3137
*
3238
* @param listener the transmission listener to invoke
3339
*/
34-
void addTransmissionListener(PacketTransmissionListener listener);
40+
void addTransmissionListener(@NotNull PacketTransmissionListener listener);
3541
}
36-

0 commit comments

Comments
 (0)