Centralize disconnect handling

This commit is contained in:
Brady
2020-03-05 23:48:42 -06:00
parent 51840df97d
commit cffe907c76
4 changed files with 14 additions and 17 deletions

View File

@@ -22,6 +22,7 @@ import baritone.api.bot.connect.IConnectionResult;
import baritone.api.event.events.TickEvent;
import com.mojang.authlib.GameProfile;
import net.minecraft.util.Session;
import net.minecraft.util.text.ITextComponent;
import java.util.List;
import java.util.Optional;
@@ -46,10 +47,12 @@ public interface IUserManager {
/**
* Disconnects the specified {@link IBaritoneUser} from its current server. All valid users
* are automatically disconnected when the current game state becomes {@link TickEvent.Type#OUT}.
* A reason may be specified, but is more widely used in server-initiated disconnects.
*
* @param user The user to disconnect
* @param reason The reason for the disconnect, may be {@code null}
*/
void disconnect(IBaritoneUser user);
void disconnect(IBaritoneUser user, ITextComponent reason);
/**
* Finds the {@link IBaritoneUser} associated with the specified {@link GameProfile}

View File

@@ -36,6 +36,8 @@ import net.minecraft.network.NetworkManager;
import net.minecraft.network.handshake.client.C00Handshake;
import net.minecraft.network.login.client.CPacketLoginStart;
import net.minecraft.util.Session;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import java.net.InetAddress;
import java.net.UnknownHostException;
@@ -65,12 +67,12 @@ public final class UserManager implements IUserManager, Helper {
public final void onTick(TickEvent event) {
if (event.getState() == EventState.PRE) {
if (event.getType() == TickEvent.Type.OUT) {
UserManager.this.users.forEach(UserManager.this::disconnect);
UserManager.this.users.forEach(user -> UserManager.this.disconnect(user, null));
}
UserManager.this.users.forEach(user -> {
if (!user.getNetworkManager().isChannelOpen()) {
UserManager.this.disconnect(user);
UserManager.this.disconnect(user, new TextComponentString("Channel Closed"));
}
});
@@ -154,17 +156,6 @@ public final class UserManager implements IUserManager, Helper {
}
}
/**
* Notifies the manager of an {@link IBaritoneUser} disconnect, and
* removes the {@link IBaritoneUser} from the list of users.
*
* @param user The user that disconnected
* @param state The connection state at the time of disconnect
*/
public final void notifyDisconnect(IBaritoneUser user, EnumConnectionState state) {
this.users.remove(user);
}
/**
* @return The bot world provider
*/
@@ -173,7 +164,7 @@ public final class UserManager implements IUserManager, Helper {
}
@Override
public final void disconnect(IBaritoneUser user) {
public final void disconnect(IBaritoneUser user, ITextComponent reason) {
if (this.users.contains(user)) {
if (user.getNetworkManager().isChannelOpen()) {
// It's probably fine to pass null to this, because the handlers aren't doing anything with it
@@ -181,6 +172,9 @@ public final class UserManager implements IUserManager, Helper {
user.getNetworkManager().closeChannel(null);
}
this.users.remove(user);
if (reason != null) {
logDirect(user.getSession().getUsername() + " Disconnected: " + reason.getUnformattedText());
}
}
}

View File

@@ -66,6 +66,6 @@ public class BotNetHandlerLoginClient extends NetHandlerLoginClient {
@Override
public void onDisconnect(@Nonnull ITextComponent reason) {
// It's important that we don't call the superclass method because that would mess up GUIs and make us upset
this.user.getManager().notifyDisconnect(this.user, EnumConnectionState.LOGIN);
this.user.getManager().disconnect(this.user, reason);
}
}

View File

@@ -687,7 +687,7 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
public void onDisconnect(@Nonnull ITextComponent reason) {
// TODO Maybe more world unloadinde
this.world.removeEntity(this.player);
this.user.getManager().notifyDisconnect(this.user, EnumConnectionState.PLAY);
this.user.getManager().disconnect(this.user, reason);
}
@Nonnull