Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 838cd0d

Browse files
committed
Avoid unnecessary indirection through a BiConsumer in PacketDispatcher
1 parent fb87b7f commit 838cd0d

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

patchwork-networking/src/main/java/net/minecraftforge/fml/network/NetworkEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public static class Context {
216216
private boolean packetHandled;
217217

218218
Context(ClientConnection connection, NetworkDirection networkDirection, int index) {
219-
this(connection, networkDirection, new PacketDispatcher.ClientConnectionDispatcher(connection, index, networkDirection.reply()::buildPacket));
219+
this(connection, networkDirection, new PacketDispatcher.ClientConnectionDispatcher(connection, index, networkDirection.reply()));
220220
}
221221

222222
Context(ClientConnection clientConnection, NetworkDirection networkDirection, PacketDispatcher dispatcher) {

patchwork-networking/src/main/java/net/minecraftforge/fml/network/PacketDispatcher.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package net.minecraftforge.fml.network;
2121

2222
import java.util.function.BiConsumer;
23-
import java.util.function.BiFunction;
2423

2524
import org.apache.commons.lang3.tuple.Pair;
2625

@@ -32,35 +31,42 @@
3231
* Dispatcher for sending packets in response to a received packet. Abstracts out the difference between wrapped packets
3332
* and unwrapped packets.
3433
*/
35-
public class PacketDispatcher {
36-
BiConsumer<Identifier, PacketByteBuf> packetSink;
37-
38-
PacketDispatcher(final BiConsumer<Identifier, PacketByteBuf> packetSink) {
39-
this.packetSink = packetSink;
40-
}
41-
34+
public abstract class PacketDispatcher {
4235
private PacketDispatcher() {
43-
// NO-OP; cannot call package-private constructor due to Java limitations
36+
// No-op
4437
}
4538

46-
public void sendPacket(Identifier identifier, PacketByteBuf buffer) {
47-
packetSink.accept(identifier, buffer);
39+
public abstract void sendPacket(Identifier identifier, PacketByteBuf buffer);
40+
41+
static class FunctionalDispatcher extends PacketDispatcher {
42+
private final BiConsumer<Identifier, PacketByteBuf> packetSink;
43+
44+
FunctionalDispatcher(final BiConsumer<Identifier, PacketByteBuf> packetSink) {
45+
this.packetSink = packetSink;
46+
}
47+
48+
@Override
49+
public void sendPacket(final Identifier identifier, final PacketByteBuf buffer) {
50+
packetSink.accept(identifier, buffer);
51+
}
4852
}
4953

5054
static class ClientConnectionDispatcher extends PacketDispatcher {
5155
private final ClientConnection connection;
5256
private final int packetIndex;
53-
private final BiFunction<Pair<PacketByteBuf, Integer>, Identifier, ICustomPacket<?>> customPacketSupplier;
57+
private final NetworkDirection direction;
5458

55-
ClientConnectionDispatcher(ClientConnection connection, int packetIndex, BiFunction<Pair<PacketByteBuf, Integer>, Identifier, ICustomPacket<?>> customPacketSupplier) {
59+
ClientConnectionDispatcher(ClientConnection connection, int packetIndex, NetworkDirection direction) {
5660
super();
5761
this.connection = connection;
5862
this.packetIndex = packetIndex;
59-
this.customPacketSupplier = customPacketSupplier;
63+
this.direction = direction;
6064
}
6165

62-
private void dispatchPacket(final Identifier connection, final PacketByteBuf buffer) {
63-
final ICustomPacket<?> packet = this.customPacketSupplier.apply(Pair.of(buffer, packetIndex), connection);
66+
@Override
67+
public void sendPacket(final Identifier identifier, final PacketByteBuf buffer) {
68+
final ICustomPacket<?> packet = this.direction.buildPacket(Pair.of(buffer, packetIndex), identifier);
69+
6470
this.connection.send(packet.getThis());
6571
}
6672
}

0 commit comments

Comments
 (0)