1010import com .viaversion .viaversion .api .type .Types ;
1111import io .netty .buffer .ByteBuf ;
1212import io .netty .buffer .Unpooled ;
13+ import io .netty .channel .Channel ;
1314import net .megavex .scoreboardlibrary .implementation .commons .LegacyFormatUtil ;
1415import net .megavex .scoreboardlibrary .implementation .packetAdapter .ImmutableTeamProperties ;
1516import net .megavex .scoreboardlibrary .implementation .packetAdapter .PropertiesPacketType ;
1617import net .megavex .scoreboardlibrary .implementation .packetAdapter .modern .PacketAdapterProviderImpl ;
18+ import net .megavex .scoreboardlibrary .implementation .packetAdapter .modern .util .ViaConnectionGuard ;
1719import net .megavex .scoreboardlibrary .implementation .packetAdapter .team .EntriesPacketType ;
1820import net .megavex .scoreboardlibrary .implementation .packetAdapter .team .TeamConstants ;
1921import net .megavex .scoreboardlibrary .implementation .packetAdapter .team .TeamDisplayPacketAdapter ;
@@ -45,14 +47,14 @@ public void removeTeam(@NotNull Iterable<Player> players) {
4547 assert via != null ;
4648 for (final Player player : players ) {
4749 final UserConnection conn = via .getConnection (player .getUniqueId ());
48- if (conn == null ) continue ;
50+ if (conn == null || ! ViaConnectionGuard . isCurrentPlayConnection ( via , player , conn ) ) continue ;
4951
5052 final ByteBuf buf = Unpooled .buffer (128 );
5153 Types .VAR_INT .writePrimitive (buf , teamsPacketId (player , conn ));
5254 Types .STRING .write (buf , teamName );
5355 Types .BYTE .writePrimitive (buf , (byte ) TeamConstants .MODE_REMOVE );
5456
55- via . sendRawPacket (player . getUniqueId () , buf );
57+ sendRawPacket (via , player , conn , buf );
5658 }
5759 }
5860
@@ -62,7 +64,7 @@ public void sendEntries(@NotNull EntriesPacketType packetType, @NotNull Collecti
6264 assert via != null ;
6365 for (final Player player : players ) {
6466 final UserConnection conn = via .getConnection (player .getUniqueId ());
65- if (conn == null ) continue ;
67+ if (conn == null || ! ViaConnectionGuard . isCurrentPlayConnection ( via , player , conn ) ) continue ;
6668
6769 final ByteBuf buf = Unpooled .buffer (128 );
6870 Types .VAR_INT .writePrimitive (buf , teamsPacketId (player , conn ));
@@ -81,7 +83,7 @@ public void sendEntries(@NotNull EntriesPacketType packetType, @NotNull Collecti
8183 Types .STRING .write (buf , entry );
8284 }
8385
84- via . sendRawPacket (player . getUniqueId () , buf );
86+ sendRawPacket (via , player , conn , buf );
8587 }
8688 }
8789
@@ -91,7 +93,7 @@ public void sendProperties(@NotNull PropertiesPacketType packetType, @NotNull Co
9193 assert via != null ;
9294 for (final Player player : players ) {
9395 final UserConnection conn = via .getConnection (player .getUniqueId ());
94- if (conn == null ) continue ;
96+ if (conn == null || ! ViaConnectionGuard . isCurrentPlayConnection ( via , player , conn ) ) continue ;
9597
9698 final ByteBuf buf = Unpooled .buffer (128 );
9799 Types .VAR_INT .writePrimitive (buf , teamsPacketId (player , conn ));
@@ -134,7 +136,28 @@ public void sendProperties(@NotNull PropertiesPacketType packetType, @NotNull Co
134136 }
135137 }
136138
137- via .sendRawPacket (player .getUniqueId (), buf );
139+ sendRawPacket (via , player , conn , buf );
140+ }
141+ }
142+
143+ private static void sendRawPacket (ViaAPI <Player > via , Player player , UserConnection connection , ByteBuf packet ) {
144+ final Channel channel = connection .getChannel ();
145+ if (!ViaConnectionGuard .isCurrentPlayConnection (via , player , connection , channel )) {
146+ packet .release ();
147+ return ;
148+ }
149+
150+ try {
151+ channel .eventLoop ().execute (() -> {
152+ if (ViaConnectionGuard .isCurrentPlayConnection (via , player , connection , channel )) {
153+ connection .sendRawPacket (packet );
154+ } else {
155+ packet .release ();
156+ }
157+ });
158+ } catch (RuntimeException | Error exception ) {
159+ packet .release ();
160+ throw exception ;
138161 }
139162 }
140163
0 commit comments