Skip to content

Commit 271df89

Browse files
authored
Fix server disconnections and other network errors from causing a crash (#4169)
1 parent f0e9456 commit 271df89

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/main/java/com/gregtechceu/gtceu/common/network/GTNetwork.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,47 @@ public class GTNetwork {
3131
private static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(GTCEu.id("network"),
3232
() -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals);
3333

34+
private static void protectedSend(PacketDistributor.PacketTarget target, INetPacket packet) {
35+
try {
36+
INSTANCE.send(target, packet);
37+
} catch (Exception e) {
38+
GTCEu.LOGGER.warn("Failed to send packet: {}", e.getLocalizedMessage());
39+
}
40+
}
41+
3442
private static int nextPacketId = 0;
3543

3644
public static void sendToServer(INetPacket packet) {
37-
INSTANCE.sendToServer(packet);
45+
try {
46+
INSTANCE.sendToServer(packet);
47+
} catch (Exception e) {
48+
GTCEu.LOGGER.warn("Failed to send packet: {}", e.getLocalizedMessage());
49+
}
3850
}
3951

4052
public static void sendToPlayersInLevel(ResourceKey<Level> level, INetPacket packet) {
41-
INSTANCE.send(PacketDistributor.DIMENSION.with(() -> level), packet);
53+
protectedSend(PacketDistributor.DIMENSION.with(() -> level), packet);
4254
}
4355

4456
public static void sendToPlayersNearPoint(PacketDistributor.TargetPoint point, INetPacket packet) {
45-
INSTANCE.send(PacketDistributor.NEAR.with(() -> point), packet);
57+
protectedSend(PacketDistributor.NEAR.with(() -> point), packet);
4658
}
4759

4860
public static void sendToAllPlayersTrackingEntity(Entity entity, boolean includeSelf, INetPacket packet) {
49-
INSTANCE.send(includeSelf ? PacketDistributor.TRACKING_ENTITY_AND_SELF.with(() -> entity) :
61+
protectedSend(includeSelf ? PacketDistributor.TRACKING_ENTITY_AND_SELF.with(() -> entity) :
5062
PacketDistributor.TRACKING_ENTITY.with(() -> entity), packet);
5163
}
5264

5365
public static void sendToAllPlayersTrackingChunk(LevelChunk chunk, INetPacket packet) {
54-
INSTANCE.send(PacketDistributor.TRACKING_CHUNK.with(() -> chunk), packet);
66+
protectedSend(PacketDistributor.TRACKING_CHUNK.with(() -> chunk), packet);
5567
}
5668

5769
public static void sendToAll(INetPacket packet) {
58-
INSTANCE.send(PacketDistributor.ALL.noArg(), packet);
70+
protectedSend(PacketDistributor.ALL.noArg(), packet);
5971
}
6072

6173
public static void sendToPlayer(ServerPlayer player, INetPacket packet) {
62-
INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), packet);
74+
protectedSend(PacketDistributor.PLAYER.with(() -> player), packet);
6375
}
6476

6577
public static void reply(NetworkEvent.Context context, INetPacket packet) {

0 commit comments

Comments
 (0)