@@ -44,6 +44,8 @@ public class Bot extends Thread {
4444
4545 private boolean connected ;
4646
47+ private boolean manualDisconnecting = false ;
48+
4749 public Bot (String nickname , InetSocketAddress address , ProxyInfo proxy ) {
4850 this .nickname = nickname ;
4951 this .address = address ;
@@ -112,32 +114,35 @@ public void run() {
112114 @ Override
113115 public void disconnected (DisconnectedEvent event ) {
114116 connected = false ;
115- Log . info ( );
117+ Main . removeBot ( Bot . this );
116118 Log .info (nickname + " disconnected" );
117119
118- //Log.info(" -> " + event.getReason());
120+ // Do not write disconnect reason if disconnected by command
121+ if (!manualDisconnecting ) {
122+ //Log.info(" -> " + event.getReason());
119123
120- //fix broken reason string by finding the content with regex
121- Pattern pattern = Pattern .compile ("content=\" (.*?)\" " );
122- Matcher matcher = pattern .matcher (event .getReason ());
124+ //fix broken reason string by finding the content with regex
125+ Pattern pattern = Pattern .compile ("content=\" (.*?)\" " );
126+ Matcher matcher = pattern .matcher (event .getReason ());
123127
124- StringBuilder reason = new StringBuilder ();
125- while (matcher .find ()) {
126- reason .append (matcher .group (1 ));
127- }
128+ StringBuilder reason = new StringBuilder ();
129+ while (matcher .find ()) {
130+ reason .append (matcher .group (1 ));
131+ }
128132
129- Log .info (" -> " + reason .toString ());
133+ Log .info (" -> " + reason .toString ());
130134
131- if (event .getCause () != null ) {
132- event .getCause ().printStackTrace ();
135+ if (event .getCause () != null ) {
136+ event .getCause ().printStackTrace ();
133137
134- if (event .getCause () instanceof UnexpectedEncryptionException ) {
135- Log .warn ("Server is running in online (premium) mode. Please use the -o option to use online mode bot." );
136- System .exit (1 );
138+ if (event .getCause () instanceof UnexpectedEncryptionException ) {
139+ Log .warn ("Server is running in online (premium) mode. Please use the -o option to use online mode bot." );
140+ System .exit (1 );
141+ }
137142 }
143+ Log .info ();
138144 }
139- Log .info ();
140- Main .removeBot (Bot .this );
145+
141146 Thread .currentThread ().interrupt ();
142147 }
143148 });
@@ -214,4 +219,10 @@ public void moveTo(double x, double y, double z)
214219 public boolean isConnected () {
215220 return connected ;
216221 }
222+
223+ public void disconnect ()
224+ {
225+ manualDisconnecting = true ;
226+ client .disconnect ("Leaving" );
227+ }
217228}
0 commit comments