|
20 | 20 | package net.minecraftforge.fml.network; |
21 | 21 |
|
22 | 22 | import java.util.function.BiConsumer; |
23 | | -import java.util.function.BiFunction; |
24 | 23 |
|
25 | 24 | import org.apache.commons.lang3.tuple.Pair; |
26 | 25 |
|
|
32 | 31 | * Dispatcher for sending packets in response to a received packet. Abstracts out the difference between wrapped packets |
33 | 32 | * and unwrapped packets. |
34 | 33 | */ |
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 { |
42 | 35 | private PacketDispatcher() { |
43 | | - // NO-OP; cannot call package-private constructor due to Java limitations |
| 36 | + // No-op |
44 | 37 | } |
45 | 38 |
|
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 | + } |
48 | 52 | } |
49 | 53 |
|
50 | 54 | static class ClientConnectionDispatcher extends PacketDispatcher { |
51 | 55 | private final ClientConnection connection; |
52 | 56 | private final int packetIndex; |
53 | | - private final BiFunction<Pair<PacketByteBuf, Integer>, Identifier, ICustomPacket<?>> customPacketSupplier; |
| 57 | + private final NetworkDirection direction; |
54 | 58 |
|
55 | | - ClientConnectionDispatcher(ClientConnection connection, int packetIndex, BiFunction<Pair<PacketByteBuf, Integer>, Identifier, ICustomPacket<?>> customPacketSupplier) { |
| 59 | + ClientConnectionDispatcher(ClientConnection connection, int packetIndex, NetworkDirection direction) { |
56 | 60 | super(); |
57 | 61 | this.connection = connection; |
58 | 62 | this.packetIndex = packetIndex; |
59 | | - this.customPacketSupplier = customPacketSupplier; |
| 63 | + this.direction = direction; |
60 | 64 | } |
61 | 65 |
|
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 | + |
64 | 70 | this.connection.send(packet.getThis()); |
65 | 71 | } |
66 | 72 | } |
|
0 commit comments