Skip to content

Commit 6473d53

Browse files
committed
feat: command: leave - disconnect bots
1 parent 07445c0 commit 6473d53

File tree

3 files changed

+61
-22
lines changed

3 files changed

+61
-22
lines changed

src/main/java/me/creepermaxcz/mcbots/Bot.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/main/java/me/creepermaxcz/mcbots/Log.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Log {
99

1010
public static void log(String in) {
1111
System.out.println("\r[" + formatter.format(new Date()) + "] " + in);
12-
System.out.print(Main.prompt + "> ");
12+
System.out.print(Main.getPrompt() + "> ");
1313
}
1414

1515
public static void error(String in) {

src/main/java/me/creepermaxcz/mcbots/Main.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public void run() {
390390
String line = scanner.nextLine();
391391

392392
if (line.isEmpty()) {
393-
System.out.print(prompt + "> ");
393+
System.out.print(getPrompt() + "> ");
394394
continue;
395395
}
396396

@@ -430,10 +430,8 @@ public void run() {
430430
.collect(Collectors.joining(", "));
431431

432432
if (newBotCount == 1) {
433-
prompt = botNames;
434433
Log.info("Now controlling 1 bot: " + botNames);
435434
} else {
436-
prompt = newBotCount + " BOTS";
437435
Log.info("Now controlling " + newBotCount + " bots: " + botNames);
438436
}
439437

@@ -445,7 +443,6 @@ public void run() {
445443
} else {
446444
// If no bot names are supplied, remove all bots
447445
controlledBots.clear();
448-
prompt = "ALL";
449446
Log.info("No bots selected - now controlling all bots.");
450447
}
451448
}
@@ -460,6 +457,21 @@ else if (commandName.equalsIgnoreCase("list") || commandName.equalsIgnoreCase("l
460457
}
461458
}
462459

460+
else if (commandName.equalsIgnoreCase("leave") || commandName.equalsIgnoreCase("exit"))
461+
{
462+
if (!controlledBots.isEmpty()) {
463+
Log.info("Disconnecting controlled bots.");
464+
for (Bot bot : controlledBots) {
465+
bot.disconnect();
466+
}
467+
} else {
468+
Log.info("Disconnecting all bots.");
469+
for (Bot bot : bots) {
470+
bot.disconnect();
471+
}
472+
}
473+
}
474+
463475
else {
464476
Log.warn("Invalid command");
465477
}
@@ -485,6 +497,7 @@ public static synchronized void renewMainListener() {
485497

486498
public static synchronized void removeBot(Bot bot) {
487499
bots.remove(bot);
500+
controlledBots.remove(bot);
488501
if (bot.hasMainListener()) {
489502
Log.info("Bot with MainListener removed");
490503
isMainListenerMissing = true;
@@ -522,4 +535,19 @@ public static Bot findBotByName(String text) {
522535
}
523536
return null;
524537
}
538+
539+
public static String getPrompt()
540+
{
541+
int count = controlledBots.size();
542+
if (count == 0) {
543+
return "ALL";
544+
} else if (count == 1)
545+
{
546+
//If controlling only one bot, return its nickname
547+
return controlledBots.iterator().next().getNickname();
548+
}
549+
else {
550+
return count + " BOTS";
551+
}
552+
}
525553
}

0 commit comments

Comments
 (0)