Skip to content

Commit d505394

Browse files
committed
simplify via connection guard
1 parent 4022c09 commit d505394

3 files changed

Lines changed: 10 additions & 28 deletions

File tree

implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/packetAdapter/modern/team/LegacyTeamDisplayPacketAdapter.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,12 @@ public void sendProperties(@NotNull PropertiesPacketType packetType, @NotNull Co
142142

143143
private static void sendRawPacket(ViaAPI<Player> via, Player player, UserConnection connection, ByteBuf packet) {
144144
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;
161-
}
145+
assert channel != null; // checked by ViaConnectionGuard, field is final and will never change
146+
channel.eventLoop().execute(() -> {
147+
if (ViaConnectionGuard.isCurrentPlayConnection(via, player, connection)) {
148+
connection.sendRawPacket(packet);
149+
}
150+
});
162151
}
163152

164153
private @NotNull Integer teamsPacketId(final Player player, final UserConnection conn) {

implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/packetAdapter/modern/util/ModernPacketSender.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ public void sendPacket(Player player, Object packet) {
8282
final UserConnection conn = this.via.getConnection(player.getUniqueId());
8383
if (conn != null) {
8484
final Channel channel = conn.getChannel();
85-
if (!ViaConnectionGuard.isCurrentPlayConnection(this.via, player, conn, channel)) {
85+
if (!ViaConnectionGuard.isCurrentPlayConnection(this.via, player, conn)) {
8686
return;
8787
}
88+
assert channel != null; // checked by ViaConnectionGuard
8889

8990
// Paper has some "network optimization" patch that makes the NMS sendPacket method
9091
// not always send the packet immediately to the player's netty channel,
@@ -96,7 +97,7 @@ public void sendPacket(Player player, Object packet) {
9697
// The player can disconnect or enter configuration while this write is queued. The
9798
// channel remains open in that state, but its encoder no longer accepts game packets.
9899
// Also guard against a reconnect replacing ViaVersion's UUID mapping in the meantime.
99-
if (ViaConnectionGuard.isCurrentPlayConnection(this.via, player, conn, channel)) {
100+
if (ViaConnectionGuard.isCurrentPlayConnection(this.via, player, conn)) {
100101
channel.writeAndFlush(packet);
101102
}
102103
});

implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/packetAdapter/modern/util/ViaConnectionGuard.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@ public static boolean isCurrentPlayConnection(
1616
Player player,
1717
UserConnection connection
1818
) {
19-
return isCurrentPlayConnection(via, player, connection, connection.getChannel());
20-
}
21-
22-
public static boolean isCurrentPlayConnection(
23-
ViaAPI<Player> via,
24-
Player player,
25-
UserConnection connection,
26-
Channel channel
27-
) {
19+
final Channel channel = connection.getChannel();
2820
final ProtocolInfo protocolInfo = connection.getProtocolInfo();
2921
return channel != null
3022
&& player.isOnline()

0 commit comments

Comments
 (0)