6
6
import commonnetwork .networking .data .PacketContext ;
7
7
import commonnetwork .networking .data .Side ;
8
8
import commonnetwork .networking .exceptions .RegistrationException ;
9
+ import io .netty .buffer .Unpooled ;
9
10
import net .minecraft .client .Minecraft ;
10
11
import net .minecraft .network .Connection ;
12
+ import net .minecraft .network .FriendlyByteBuf ;
13
+ import net .minecraft .network .RegistryFriendlyByteBuf ;
11
14
import net .minecraft .server .level .ServerPlayer ;
12
15
import net .minecraftforge .event .network .CustomPayloadEvent ;
13
- import net .minecraftforge .network .Channel ;
14
16
import net .minecraftforge .network .ChannelBuilder ;
15
17
import net .minecraftforge .network .EventNetworkChannel ;
16
18
21
23
22
24
public class ForgeNetworkHandler extends PacketRegistrationHandler
23
25
{
24
- private final Map <Class <?>, EventNetworkChannel > CHANNELS = new HashMap <>();
26
+ // private final Map<Class<?>, EventNetworkChannel> CHANNELS = new HashMap<>();
27
+ private final Map <Class <?>, Message <?>> CHANNELS = new HashMap <>();
25
28
26
29
public ForgeNetworkHandler (Side side )
27
30
{
@@ -37,20 +40,23 @@ protected <T> void registerPacket(PacketContainer<T> container)
37
40
CommonPacketWrapper <T > msg = container .getCodec ().decode (event .getPayload ());
38
41
buildHandler (container .handler ()).accept (msg .packet (), event .getSource ());
39
42
});
40
- CHANNELS .put (container .classType (), channel );
43
+ CHANNELS .put (container .classType (), new Message <>( channel , container ) );
41
44
}
42
45
}
43
46
44
47
public <T > void sendToServer (T packet , boolean ignoreCheck )
45
48
{
46
49
47
- Channel < T > channel = (Channel <T >) CHANNELS .get (packet .getClass ());
48
- if (channel != null )
50
+ var message = (Message <T >) CHANNELS .get (packet .getClass ());
51
+ if (message != null )
49
52
{
53
+ var channel = message .channel ();
50
54
Connection connection = Minecraft .getInstance ().getConnection ().getConnection ();
51
55
if (ignoreCheck || channel .isRemotePresent (connection ))
52
56
{
53
- channel .send (packet , connection );
57
+ FriendlyByteBuf buf = new RegistryFriendlyByteBuf (Unpooled .buffer (), Minecraft .getInstance ().player .registryAccess ());
58
+ message .container .codec ().encode (buf , packet );
59
+ channel .send (buf , connection );
54
60
}
55
61
}
56
62
else
@@ -63,14 +69,16 @@ public <T> void sendToServer(T packet, boolean ignoreCheck)
63
69
public <T > void sendToClient (T packet , ServerPlayer player , boolean ignoreCheck )
64
70
{
65
71
66
- Channel <T > channel = (Channel <T >) CHANNELS .get (packet .getClass ());
67
-
68
- Connection connection = player .connection .getConnection ();
69
- if (channel != null )
72
+ var message = (Message <T >) CHANNELS .get (packet .getClass ());
73
+ if (message != null )
70
74
{
75
+ var channel = message .channel ();
76
+ Connection connection = player .connection .getConnection ();
71
77
if (ignoreCheck || channel .isRemotePresent (connection ))
72
78
{
73
- channel .send (packet , connection );
79
+ FriendlyByteBuf buf = new RegistryFriendlyByteBuf (Unpooled .buffer (), player .server .registryAccess ());
80
+ message .container .codec ().encode (buf , packet );
81
+ channel .send (buf , connection );
74
82
}
75
83
76
84
}
@@ -100,7 +108,7 @@ private <T> BiConsumer<T, CustomPayloadEvent.Context> buildHandler(Consumer<Pack
100
108
};
101
109
}
102
110
103
- // public record Message<T>(EventNetworkChannel channel, BiConsumer<T, ? extends FriendlyByteBuf> encoder )
104
- // {
105
- // }
111
+ public record Message <T >(EventNetworkChannel channel , PacketContainer < T > container )
112
+ {
113
+ }
106
114
}
0 commit comments