ViaLoader/src/main/java/net/raphimc/vialoader/netty/viabedrock/DisconnectHandler.java

39 lines
1.5 KiB
Java

/*
* This file is part of ViaLoader - https://github.com/RaphiMC/ViaLoader
* Copyright (C) 2020-2024 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.vialoader.netty.viabedrock;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
public class DisconnectHandler extends ChannelOutboundHandlerAdapter {
private boolean calledDisconnect = false;
@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
if (ctx.channel().isActive() && !this.calledDisconnect) {
this.calledDisconnect = true;
ctx.disconnect(promise); // Send disconnect notification to the server and close the channel
} else {
super.close(ctx, promise);
}
}
}