22
33import com .google .gson .JsonObject ;
44import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayNetworking ;
5+ import net .fabricmc .fabric .api .networking .v1 .PacketByteBufs ;
6+ import net .fabricmc .fabric .api .networking .v1 .PacketSender ;
57import net .minecraft .client .MinecraftClient ;
8+ import net .minecraft .client .network .ClientPlayNetworkHandler ;
69import net .minecraft .client .network .ClientPlayerEntity ;
710import net .minecraft .entity .player .PlayerEntity ;
11+ import net .minecraft .network .PacketByteBuf ;
812import net .minecraft .server .MinecraftServer ;
913import net .minecraft .text .Text ;
1014import net .minecraft .util .Formatting ;
2832public class ClientMusicNetworkHandler {
2933
3034 public static void register () {
31- ClientPlayNetworking .registerGlobalReceiver (ConcertoPayload .ID , ClientMusicNetworkHandler ::generalReceiver );
35+ ClientPlayNetworking .registerGlobalReceiver (ConcertoNetworking .MUSIC_DATA , ClientMusicNetworkHandler ::musicDataReceiver );
36+ ClientPlayNetworking .registerGlobalReceiver (ConcertoNetworking .HANDSHAKE , ClientMusicNetworkHandler ::playerJoinHandshake );
37+ ClientPlayNetworking .registerGlobalReceiver (ConcertoNetworking .AUDITION_SYNC , ClientMusicNetworkHandler ::auditionDataSyncReceiver );
38+ ClientPlayNetworking .registerGlobalReceiver (ConcertoNetworking .MUSIC_ROOM , MusicRoom ::clientReceiver );
39+ ClientPlayNetworking .registerGlobalReceiver (ConcertoNetworking .PRESET_RADIOS , ClientMusicNetworkHandler ::presetRadiosReceiver );
40+ ClientPlayNetworking .registerGlobalReceiver (ConcertoNetworking .MUSIC_AGENT , ClientMusicNetworkHandler ::musicAgentMusicReceiver );
3241 }
3342
3443 public static final Map <UUID , MusicDataPacket > WAIT_CONFIRMATION = new HashMap <>();
@@ -39,17 +48,6 @@ public static void removeFirst() {
3948 iterator .remove ();
4049 }
4150
42- public static void generalReceiver (ConcertoPayload payload , ClientPlayNetworking .Context context ) {
43- switch (payload .channel ) {
44- case MUSIC_DATA -> musicDataReceiver (payload , context );
45- case HANDSHAKE -> playerJoinHandshake (payload , context );
46- case AUDITION_SYNC -> auditionDataSyncReceiver (payload , context );
47- case MUSIC_ROOM -> MusicRoom .clientReceiver (payload , context );
48- case PRESET_RADIOS -> presetRadiosReceiver (payload , context );
49- case MUSIC_AGENT -> musicAgentMusicReceiver (payload , context );
50- }
51- }
52-
5351 public static void sendC2SMusicData (MusicDataPacket packet ) {
5452 if (!ConcertoClient .isServerAvailable ()) {
5553 ClientPlayerEntity player = MinecraftClient .getInstance ().player ;
@@ -73,8 +71,8 @@ public static void sendC2SMusicData(MusicDataPacket packet) {
7371 throw new RuntimeException ("You are NULL, bro :)" );
7472 }
7573 packet .music .load ();
76- ConcertoPayload buf = packet .toPacket (player .getName ().getString ());
77- ClientPlayNetworking .send (buf );
74+ PacketByteBuf buf = packet .toPacket (player .getName ().getString ());
75+ ClientPlayNetworking .send (ConcertoNetworking . MUSIC_DATA , buf );
7876 }
7977
8078 public static void accept (PlayerEntity player , UUID uuid , MinecraftClient client ) {
@@ -137,12 +135,13 @@ public static void addToWaitList(MinecraftClient client, MusicDataPacket packet,
137135 });
138136 }
139137
140- public static void musicDataReceiver (ConcertoPayload payload , ClientPlayNetworking .Context context ) {
138+ public static void musicDataReceiver (MinecraftClient client , ClientPlayNetworkHandler handler ,
139+ PacketByteBuf buf , PacketSender packetSender ) {
141140 try {
142- MusicDataPacket packet = MusicDataPacket .fromPacket (payload , true );
143- PlayerEntity self = context .player () ;
141+ MusicDataPacket packet = MusicDataPacket .fromPacket (buf , true );
142+ PlayerEntity self = client .player ;
144143 if (packet != null && packet .music != null && self != null ) {
145- addToWaitList (context . client () , packet , self );
144+ addToWaitList (client , packet , self );
146145 } else {
147146 ConcertoClient .LOGGER .warn ("Received an unknown music data packet" );
148147 }
@@ -152,18 +151,19 @@ public static void musicDataReceiver(ConcertoPayload payload, ClientPlayNetworki
152151 }
153152 }
154153
155- public static void playerJoinHandshake (ConcertoPayload payload , ClientPlayNetworking .Context context ) {
156- String str = payload .string ;
154+ public static void playerJoinHandshake (MinecraftClient client , ClientPlayNetworkHandler handler ,
155+ PacketByteBuf buf , PacketSender packetSender ) {
156+ String str = buf .readString (Short .MAX_VALUE << 4 );
157157 if (!str .startsWith (ConcertoNetworking .HANDSHAKE_STRING )) return ;
158158 String [] args = str .split (":" );
159159 if (args .length < 3 ) return ;
160160 if (args [1 ].equals ("CallJoin" )) {
161161 String playerName = args [2 ];
162- ClientPlayerEntity player = context .player () ;
162+ ClientPlayerEntity player = client .player ;
163163 if (player != null && playerName .equals (player .getName ().getString ())) {
164164 ConcertoClient .serverAvailable = true ;
165165 ConcertoClient .LOGGER .info ("Concerto has been installed in this server" );
166- if (args .length > 3 && !MinecraftClient . getInstance () .isInSingleplayer () && args [3 ].equals ("Invite" )) {
166+ if (args .length > 3 && !client .isInSingleplayer () && args [3 ].equals ("Invite" )) {
167167 if (ClientConfig .INSTANCE .options .joinAgentWhenInvited ) {
168168 player .networkHandler .sendChatCommand ("/musicroom agent join" );
169169 } else {
@@ -180,8 +180,9 @@ public static void playerJoinHandshake(ConcertoPayload payload, ClientPlayNetwor
180180 }
181181 }
182182
183- public static void auditionDataSyncReceiver (ConcertoPayload payload , ClientPlayNetworking .Context context ) {
184- String str = payload .string ;
183+ public static void auditionDataSyncReceiver (MinecraftClient client , ClientPlayNetworkHandler handler ,
184+ PacketByteBuf buf , PacketSender packetSender ) {
185+ String str = buf .readString (Short .MAX_VALUE << 4 );
185186 String [] args = str .split (";" );
186187 if (args .length != 3 ) return ;
187188 try {
@@ -196,37 +197,44 @@ public static void auditionDataSyncReceiver(ConcertoPayload payload, ClientPlayN
196197 }
197198 }
198199
199- public static void presetRadiosReceiver (ConcertoPayload payload , ClientPlayNetworking .Context context ) {
200- MusicPlayer .run (() -> ConcertoClient .presetRadios = PresetRadioConfig .fromJson (payload .string ).stream ().filter (playlist ->
200+ public static void presetRadiosReceiver (MinecraftClient client , ClientPlayNetworkHandler handler ,
201+ PacketByteBuf buf , PacketSender packetSender ) {
202+ String str = buf .readString (Short .MAX_VALUE << 4 );
203+ MusicPlayer .run (() -> ConcertoClient .presetRadios = PresetRadioConfig .fromJson (str ).stream ().filter (playlist ->
201204 playlist .getList ().stream ().allMatch (MusicDataPacket ::isMusicSafe ))
202205 .peek (playlist -> MusicPlayerHandler .loadInThreadPool (playlist .getList ())).toList (), () -> {
203- MinecraftClient client = context .client ();
204206 if (client != null && client .currentScreen instanceof PresetRadiosScreen screen ) {
205207 screen .reset ();
206208 }
207209 });
208210 }
209211
212+ public static void musicAgentSender (String command ) {
213+ PacketByteBuf buf = PacketByteBufs .create ();
214+ buf .writeString (command );
215+ ClientPlayNetworking .send (ConcertoNetworking .MUSIC_AGENT , buf );
216+ }
217+
210218 public static void musicAgentJoin () {
211- ClientPlayNetworking . send ( new ConcertoPayload ( ConcertoPayload . Channel . MUSIC_AGENT , "Join" ) );
219+ musicAgentSender ( "Join" );
212220 ConcertoClient .clientState = ConcertoClient .ClientState .MUSIC_AGENT ;
213221 }
214222
215223 public static void musicAgentQuit () {
216- ClientPlayNetworking . send ( new ConcertoPayload ( ConcertoPayload . Channel . MUSIC_AGENT , "Quit" ) );
224+ musicAgentSender ( "Quit" );
217225 ConcertoClient .clientState = ConcertoClient .ClientState .LOCAL ;
218226 }
219227
220228 public static void musicAgentNewVote () {
221- ClientPlayNetworking . send ( new ConcertoPayload ( ConcertoPayload . Channel . MUSIC_AGENT , "Vote:New" ) );
229+ musicAgentSender ( "Vote:New" );
222230 }
223231
224232 public static void musicAgentQuery () {
225- ClientPlayNetworking . send ( new ConcertoPayload ( ConcertoPayload . Channel . MUSIC_AGENT , "Query" ) );
233+ musicAgentSender ( "Query" );
226234 }
227235
228236 public static void musicAgentVote (boolean vote ) {
229- ClientPlayNetworking . send ( new ConcertoPayload ( ConcertoPayload . Channel . MUSIC_AGENT , "Vote:" + (vote ? "1" : "0" ) ));
237+ musicAgentSender ( "Vote:" + (vote ? "1" : "0" ));
230238 }
231239
232240 public static boolean musicAgentAddCurrentMusic () {
@@ -237,15 +245,16 @@ public static boolean musicAgentAddCurrentMusic() {
237245 public static boolean musicAgentAddMusic (Music music ) {
238246 JsonObject object = MusicJsonParsers .to (music );
239247 if (object == null ) return false ;
240- ClientPlayNetworking .send (new ConcertoPayload (ConcertoPayload .Channel .MUSIC_AGENT ,
241- "Add:" + TextUtil .toBase64 (object .toString ())));
248+ musicAgentSender ("Add:" + TextUtil .toBase64 (object .toString ()));
242249 return true ;
243250 }
244251
245- public static void musicAgentMusicReceiver (ConcertoPayload payload , ClientPlayNetworking .Context context ) {
252+ public static void musicAgentMusicReceiver (MinecraftClient client , ClientPlayNetworkHandler handler ,
253+ PacketByteBuf buf , PacketSender packetSender ) {
254+ String str = buf .readString (Short .MAX_VALUE << 4 );
246255 if (ConcertoClient .clientState != ConcertoClient .ClientState .MUSIC_AGENT ) return ;
247256 MusicPlayer .run (() -> {
248- Music music = MusicJsonParsers .from (TextUtil .fromBase64 (payload . string ));
257+ Music music = MusicJsonParsers .from (TextUtil .fromBase64 (str ));
249258 if (music != null ) {
250259 MusicPlayer .INSTANCE .playTempMusic (music );
251260 }
0 commit comments