Skip to content

Commit a1305fa

Browse files
committed
Allow for the entire UDP pipeline to be disabled
1 parent 2facf7b commit a1305fa

12 files changed

Lines changed: 51 additions & 40 deletions

common/src/main/java/dev/ryanhcode/sable/SableConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public final class SableConfig {
1515
public static final ModConfigSpec.DoubleValue SUB_LEVEL_PUNCH_STRENGTH_MULTIPLIER;
1616
public static final ModConfigSpec.DoubleValue SUB_LEVEL_PUNCH_DOWNWARD_STRENGTH_MULTIPLIER;
1717
public static final ModConfigSpec.IntValue SUB_LEVEL_PUNCH_COOLDOWN_TICKS;
18+
public static final ModConfigSpec.BooleanValue DISABLE_UDP_PIPELINE;
1819
public static final ModConfigSpec.BooleanValue ATTEMPT_UDP_NETWORKING;
1920

2021
static {
@@ -49,6 +50,9 @@ public final class SableConfig {
4950
SUB_LEVEL_PUNCH_COOLDOWN_TICKS = builder
5051
.comment("The cooldown in ticks between sub-level punches")
5152
.defineInRange("sub_level_punch_cooldown_ticks", 3, 0, Integer.MAX_VALUE);
53+
DISABLE_UDP_PIPELINE = builder
54+
.comment("If the entire Sable UDP Networking pipeline should be disabled. This can improve compatibility with certain mods like Replay mod and certain networking setups, but will have worse performance and latency for networking sub-levels.")
55+
.define("disable_udp_pipeline", false);
5256
ATTEMPT_UDP_NETWORKING = builder
5357
.comment("If Sable should attempt to authenticate with clients and send them sub-level data over UDP")
5458
.define("attempt_udp_networking", true);

common/src/main/java/dev/ryanhcode/sable/command/SableCommand.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public static void register(final CommandDispatcher<CommandSourceStack> dispatch
5252
sableBuilder
5353
.then(debugBuilder
5454
.then(Commands.literal("udp_test").executes(ctx -> {
55-
SableUDPServer.sendPacket(ctx.getSource().getPlayerOrException(), new SableUDPEchoPacket("Skibidi Toilet"), true);
55+
final SableUDPServer server = SableUDPServer.getServer(ctx.getSource().getServer());
56+
57+
if (server != null) {
58+
server.sendUDPPacket(ctx.getSource().getPlayerOrException(), new SableUDPEchoPacket("Skibidi Toilet"), true);
59+
}
60+
5661
return 1;
5762
}))
5863
);

common/src/main/java/dev/ryanhcode/sable/mixin/udp/ClientCommonPacketListenerImplMixin.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

common/src/main/java/dev/ryanhcode/sable/mixin/udp/MinecraftServerMixin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class MinecraftServerMixin {
1919
@Inject(method = "tickServer", at = @At("TAIL"))
2020
private void sable$keepUdpSocketsAlive(final BooleanSupplier booleanSupplier, final CallbackInfo ci) {
2121
final SableUDPServer server = SableUDPServer.getServer((MinecraftServer) (Object) this);
22-
if (server == null) return;
22+
if (server == null) {
23+
return;
24+
}
2325

2426
final long time = System.currentTimeMillis();
2527

common/src/main/java/dev/ryanhcode/sable/mixin/udp/PlayerListMixin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ public class PlayerListMixin {
1919

2020
@Inject(method = "placeNewPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;send(Lnet/minecraft/network/protocol/Packet;)V", ordinal = 0, shift = At.Shift.AFTER))
2121
private void onPlayerJoin(final Connection connection, final ServerPlayer serverPlayer, final CommonListenerCookie commonListenerCookie, final CallbackInfo ci) {
22+
final SableUDPServer server = SableUDPServer.getServer(serverPlayer.server);
23+
24+
if (server == null) {
25+
return;
26+
}
27+
2228
Sable.LOGGER.info("Beginning attempted authentication with player {}", serverPlayer.getName().getString());
23-
SableUDPServer.getServer(serverPlayer.server).beginAuthentication(serverPlayer);
29+
server.beginAuthentication(serverPlayer);
2430
}
2531

2632
}

common/src/main/java/dev/ryanhcode/sable/mixin/udp/ServerConnectionListenerMixin.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.ryanhcode.sable.mixin.udp;
22

33
import dev.ryanhcode.sable.Sable;
4+
import dev.ryanhcode.sable.SableConfig;
45
import dev.ryanhcode.sable.mixinterface.udp.ServerConnectionListenerExtension;
56
import dev.ryanhcode.sable.network.udp.SableUDPPacket;
67
import dev.ryanhcode.sable.network.udp.SableUDPServer;
@@ -15,6 +16,7 @@
1516
import net.minecraft.network.protocol.PacketFlow;
1617
import net.minecraft.server.MinecraftServer;
1718
import net.minecraft.server.network.ServerConnectionListener;
19+
import org.jetbrains.annotations.Nullable;
1820
import org.spongepowered.asm.mixin.Final;
1921
import org.spongepowered.asm.mixin.Mixin;
2022
import org.spongepowered.asm.mixin.Shadow;
@@ -37,13 +39,17 @@ public class ServerConnectionListenerMixin implements ServerConnectionListenerEx
3739

3840
@Shadow
3941
@Final
40-
MinecraftServer server;
42+
private MinecraftServer server;
4143

4244
@Unique
4345
private SableUDPServer sable$server = null;
4446

4547
@Inject(method = "startTcpServerListener", at = @At("HEAD"))
4648
private void sable$startTcpServerListener(final InetAddress inetAddress, final int port, final CallbackInfo ci) {
49+
if (SableConfig.DISABLE_UDP_PIPELINE.get()) {
50+
return;
51+
}
52+
4753
synchronized (this.channels) {
4854
final Class<? extends Channel> channelClass;
4955
final EventLoopGroup eventLoopGroup;
@@ -77,6 +83,10 @@ protected void initChannel(final Channel channel) {
7783

7884
@Inject(method = "startMemoryChannel", at = @At("TAIL"))
7985
private void sable$startMemoryChannel(final CallbackInfoReturnable<SocketAddress> cir) {
86+
if (SableConfig.DISABLE_UDP_PIPELINE.get()) {
87+
return;
88+
}
89+
8090
synchronized (this.channels) {
8191
Sable.LOGGER.info("Adding local UDP server channel future");
8292

@@ -115,6 +125,7 @@ protected void initChannel(final Channel channel) {
115125
}
116126

117127
@Override
128+
@Nullable
118129
public SableUDPServer sable$getServer() {
119130
return this.sable$server;
120131
}

common/src/main/java/dev/ryanhcode/sable/mixinterface/udp/ServerConnectionListenerExtension.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import dev.ryanhcode.sable.network.udp.SableUDPServer;
44
import io.netty.channel.Channel;
5+
import org.jetbrains.annotations.Nullable;
56

67
public interface ServerConnectionListenerExtension {
78
void sable$setupUDPServer(Channel channel);
89

10+
@Nullable
911
SableUDPServer sable$getServer();
1012
}

common/src/main/java/dev/ryanhcode/sable/network/packets/udp/SableUDPAuthenticationPacket.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public SableUDPPacketType getType() {
2020

2121
@Override
2222
public void handleServer(final MinecraftServer server, final InetSocketAddress sender) {
23-
SableUDPServer.getServer(server).receiveAuthenticationPacket(UUID.fromString(this.token), sender);
23+
final SableUDPServer udpServer = SableUDPServer.getServer(server);
24+
25+
if (udpServer != null) {
26+
udpServer.receiveAuthenticationPacket(UUID.fromString(this.token), sender);
27+
}
2428
}
2529
}

common/src/main/java/dev/ryanhcode/sable/network/packets/udp/SableUDPServerboundAlivePacket.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public SableUDPPacketType getType() {
1919

2020
@Override
2121
public void handleServer(final MinecraftServer server, final InetSocketAddress sender) {
22-
SableUDPServer.getServer(server).receiveAlivePacket(sender);
22+
final SableUDPServer udpServer = SableUDPServer.getServer(server);
23+
24+
if (udpServer != null) {
25+
udpServer.receiveAlivePacket(sender);
26+
}
2327
}
2428
}

common/src/main/java/dev/ryanhcode/sable/network/udp/SableUDPServer.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import net.minecraft.server.MinecraftServer;
1919
import net.minecraft.server.level.ServerPlayer;
2020
import org.jetbrains.annotations.ApiStatus;
21+
import org.jetbrains.annotations.Nullable;
2122

2223
import java.net.InetSocketAddress;
2324
import java.util.*;
@@ -42,14 +43,11 @@ public SableUDPServer(final MinecraftServer server, final Channel channel) {
4243
this.udpAuthStates = new WeakHashMap<>();
4344
}
4445

45-
public static boolean sendPacket(final ServerPlayer player, final SableUDPPacket packet, final boolean flush) {
46-
return getServer(player.server).sendUDPPacket(player, packet, flush);
47-
}
48-
4946
/**
5047
* Retrieves the current instance of the UDP server from a {@link MinecraftServer}
5148
*/
5249
@ApiStatus.Internal
50+
@Nullable
5351
public static SableUDPServer getServer(final MinecraftServer server) {
5452
return (((ServerConnectionListenerExtension) server.getConnection())).sable$getServer();
5553
}
@@ -93,7 +91,7 @@ public boolean isConnectedTo(final ServerPlayer player) {
9391
* @param flush whether to flush the packet immediately
9492
* @return if the packet has been successfully sent
9593
*/
96-
private boolean sendUDPPacket(final ServerPlayer player, final SableUDPPacket packet, final boolean flush) {
94+
public boolean sendUDPPacket(final ServerPlayer player, final SableUDPPacket packet, final boolean flush) {
9795
if (this.channel.eventLoop().inEventLoop())
9896
throw new IllegalStateException("Cannot send packet from event loop");
9997

0 commit comments

Comments
 (0)