Skip to content

Commit

Permalink
Bungee: correct thread safety with debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed May 7, 2022
1 parent 7880f7b commit ffe24ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Expand Up @@ -94,9 +94,18 @@ public void registerPackets() {
packets.put(61, new ProxyCommandPacketIn());
}

public static void runOnMainThread(Runnable run) {
if (Bukkit.isPrimaryThread()) {
run.run();
}
else {
Bukkit.getScheduler().scheduleSyncDelayedTask(Depenizen.instance, run);
}
}

public void sendPacket(PacketOut packet) {
if (!connected && !packet.canBeFirstPacket) {
Debug.echoError("BungeeBridge tried to send packet '" + packet.getClass().getName() + "' while not connected.");
runOnMainThread(() -> Debug.echoError("BungeeBridge tried to send packet '" + packet.getClass().getName() + "' while not connected."));
return;
}
ByteBuf buf = channel.alloc().buffer();
Expand Down
Expand Up @@ -35,9 +35,9 @@ public void fail(String reason) {
return;
}
hasClosed = true;
Debug.echoError("Depenizen-Bungee connection failed: " + reason);
channel.close();
BungeeBridge.instance.connected = false;
BungeeBridge.runOnMainThread(() -> Debug.echoError("Depenizen-Bungee connection failed: " + reason));
}

public enum Stage {
Expand Down Expand Up @@ -72,7 +72,7 @@ public void handlerAdded(ChannelHandlerContext ctx) {

@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
Debug.log("Depenizen-Bungee connection ended. If this is unexpected, check your Bungee proxy server logs.");
BungeeBridge.runOnMainThread(() -> Debug.log("Depenizen-Bungee connection ended. If this is unexpected, check your Bungee proxy server logs."));
packetBuffer.release();
packetBuffer = null;
BungeeBridge.instance.connected = false;
Expand Down
Expand Up @@ -9,14 +9,14 @@ public class NettyExceptionHandler extends ChannelDuplexHandler {

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
Debug.echoError(cause);
BungeeBridge.runOnMainThread(() -> Debug.echoError(cause));
}

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
ctx.connect(remoteAddress, localAddress, promise.addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
Debug.echoError(future.cause());
BungeeBridge.runOnMainThread(() -> Debug.echoError(future.cause()));
}
}));
}
Expand All @@ -25,7 +25,7 @@ public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, Sock
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
ctx.write(msg, promise.addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
Debug.echoError(future.cause());
BungeeBridge.runOnMainThread(() -> Debug.echoError(future.cause()));
}
}));
}
Expand Down

0 comments on commit ffe24ac

Please sign in to comment.