Skip to content

Commit 6a2601d

Browse files
committed
➕ Reconnect Delay
🔃 Update the whole code to integrate reconnect delay Fix #7
1 parent feb4b75 commit 6a2601d

File tree

9 files changed

+110
-53
lines changed

9 files changed

+110
-53
lines changed

README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Minecraft bot. Currently, used for afk on a Survival Server 😅
88

99
- Graphical User Interface
1010
- LogPanel to see errors directly
11-
- Test with Spigot, Paper 1.17.1
11+
- Tested with Spigot, Paper 1.17.1
1212
- Disconnects gracefully after the end
1313
- Free
1414
- Open source
1515
- Command Line Interface
1616
- Online (Mojang)
1717
- Cracked
1818
- Automatic Respawn
19-
- Auto Reconnect (Only if `DisconnectEvent` is throw, and the reason is not `Disconnected`)
19+
- Auto Reconnect with Delay (Only if `DisconnectEvent` is throw, and the reason is not `Disconnected`)
2020

2121
## Todos
2222

@@ -33,7 +33,7 @@ https://github.com/alwyn974/MinecraftBOT/releases
3333

3434
## Images
3535

36-
![Gui](https://i.imgur.com/pfapJRo.png)
36+
![Gui](https://i.imgur.com/g7isV6F.png)
3737

3838
For cracked account, just type the username in `Email` field
3939

@@ -47,6 +47,7 @@ There are environnement variable to override the default value of host, port, us
4747
- `MC_BOT_DEBUG` for the debug mode
4848
- `MC_BOT_PREFIX` for the prefix of the commands (default=`.`)
4949
- `MC_BOT_AUTO_RECONNECT` for the auto reconnect mode
50+
- `MC_BOT_RECONNECT_DELAY` for the delay before reconnect
5051

5152
They are some builtin commands in the bot
5253

@@ -63,14 +64,15 @@ They are some builtin commands in the bot
6364
<p> Simply type anything in the CLI and type enter</p>
6465

6566
```
66-
-a,--autoReconnect Activate auto reconnect
67-
-d,--debug Activate debug
68-
-h,--host <arg> Setup the host value (Default=127.0.0.1)
69-
--help Show this help page
70-
-p,--port <arg> Setup the port value (Default=25565)
71-
--password <arg> Password of the user
72-
-s,--status Display only the status of the server
73-
-u,--user <arg> Email/Username of the user
67+
-a,--autoReconnect Activate auto reconnect
68+
-d,--debug Activate debug
69+
-h,--host <arg> Setup the host value (Default=127.0.0.1)
70+
--help Show this help page
71+
-p,--port <arg> Setup the port value (Default=25565)
72+
--password <arg> Password of the user
73+
--reconnectDelay <arg> Delay before reconnection
74+
-s,--status Display only the status of the server
75+
-u,--user <arg> Email/Username of the user
7476
```
7577

7678

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "re.alwyn974"
9-
version = "1.0.14"
9+
version = "1.0.15"
1010
archivesBaseName = "MinecraftBOT"
1111

1212
compileJava {

src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class MinecraftBOT {
4747
private static final String DEBUG = System.getenv("MC_BOT_DEBUG");
4848
private static final String PREFIX = System.getenv("MC_BOT_PREFIX");
4949
private static final String AUTO_RECONNECT = System.getenv("MC_BOT_AUTO_RECONNECT");
50+
private static final String RECONNECT_DELAY = System.getenv("MC_BOT_RECONNECT_DELAY");
5051

5152
/**
5253
* The main
@@ -100,7 +101,7 @@ private static void runHeadless(String... args) {
100101
}
101102

102103
/**
103-
* Get the logger of MinecraftBOT
104+
* Get logger of MinecraftBOT
104105
*
105106
* @return the logger {@link BasicLogger}
106107
*/
@@ -172,6 +173,15 @@ public static String getAutoReconnect() {
172173
return AUTO_RECONNECT;
173174
}
174175

176+
/**
177+
* Get reconnect delay
178+
*
179+
* @return the delay
180+
*/
181+
public static String getReconnectDelay() {
182+
return RECONNECT_DELAY != null ? RECONNECT_DELAY : "1000";
183+
}
184+
175185
/**
176186
* Get the prefix of commands
177187
*

src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ private static void addOptions() {
7676
options.addOption(null, "password", true, "Password of the user");
7777
options.addOption("d", "debug", false, "Activate debug");
7878
options.addOption("a", "autoReconnect", false, "Activate auto reconnect");
79+
options.addOption(null, "reconnectDelay", true, "Delay before reconnection");
7980
options.addOption("s", "status", false, "Display only the status of the server");
8081
options.addOption(null, "help", false, "Show this help page");
8182
}
@@ -87,8 +88,9 @@ private static ParseResult parseResult() {
8788
result.setEmail(cmd.hasOption("u") ? cmd.getOptionValue("u") : MinecraftBOT.getUsername());
8889
result.setPassword(cmd.hasOption("password") ? cmd.getOptionValue("password") : MinecraftBOT.getPassword());
8990
result.setStatus(cmd.hasOption("s"));
90-
result.setDebug(cmd.hasOption("d"));
91-
result.setAutoReconnect(cmd.hasOption("a"));
91+
result.setDebug(cmd.hasOption("d") ? cmd.hasOption("d") : Boolean.parseBoolean(MinecraftBOT.getDebug()));
92+
result.setAutoReconnect(cmd.hasOption("a") ? cmd.hasOption("a") : Boolean.parseBoolean(MinecraftBOT.getAutoReconnect()));
93+
result.setReconnectDelay(Long.parseLong(cmd.hasOption("reconnectDelay") ? cmd.getOptionValue("reconnectDelat") : MinecraftBOT.getReconnectDelay()));
9294
result.setHelp(cmd.hasOption("help"));
9395
return result;
9496
}

src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* The result of parsed args
55
*
66
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
7-
* @version 1.0.13
7+
* @version 1.0.15
88
* @since 1.0.9
99
*/
1010
public class ParseResult {
@@ -17,6 +17,7 @@ public class ParseResult {
1717
private boolean status;
1818
private boolean help;
1919
private boolean autoReconnect;
20+
private long reconnectDelay;
2021

2122
/**
2223
* Get the host
@@ -90,6 +91,15 @@ public boolean isAutoReconnect() {
9091
return autoReconnect;
9192
}
9293

94+
/**
95+
* Get reconnect delay
96+
*
97+
* @return the delay
98+
*/
99+
public long getReconnectDelay() {
100+
return reconnectDelay;
101+
}
102+
93103
/**
94104
* Set the host
95105
*
@@ -162,6 +172,15 @@ public void setAutoReconnect(boolean autoReconnect) {
162172
this.autoReconnect = autoReconnect;
163173
}
164174

175+
/**
176+
* Set reconnect delay
177+
*
178+
* @param reconnectDelay the delay
179+
*/
180+
public void setReconnectDelay(long reconnectDelay) {
181+
this.reconnectDelay = reconnectDelay;
182+
}
183+
165184
@Override
166185
public String toString() {
167186
return "ParseResult{" +

src/main/java/re/alwyn974/minecraft/bot/entity/EntityBOT.java

+26-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* The EntityBOT used to store all information about the user
2121
*
2222
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
23-
* @version 1.0.13
23+
* @version 1.0.15
2424
* @since 1.0.0
2525
*/
2626
public class EntityBOT {
@@ -32,6 +32,7 @@ public class EntityBOT {
3232
private final String password;
3333
private final boolean debug;
3434
private final boolean autoReconnect;
35+
private final long reconnectDelay;
3536
private Session client = null;
3637
private EntityPos pos = null;
3738
private double health = -1;
@@ -46,7 +47,7 @@ public class EntityBOT {
4647
* @param password the password of the account
4748
*/
4849
public EntityBOT(String username, String password) {
49-
this("localhost", username, password, false);
50+
this("127.0.0.1", username, password, false);
5051
}
5152

5253
/**
@@ -95,7 +96,7 @@ public EntityBOT(String host, int port, String username, String password) {
9596
* @param debug activate debug mode
9697
*/
9798
public EntityBOT(String host, int port, String username, String password, boolean debug) {
98-
this(host, port, Proxy.NO_PROXY, username, password, debug, false);
99+
this(host, port, username, password, debug, false, 1000);
99100
}
100101

101102
/**
@@ -106,29 +107,31 @@ public EntityBOT(String host, int port, String username, String password, boolea
106107
* @param debug activate debug mode
107108
* @param autoReconnect activate auto reconnect mode
108109
*/
109-
public EntityBOT(String host, int port, String username, String password, boolean debug, boolean autoReconnect) {
110-
this(host, port, Proxy.NO_PROXY, username, password, debug, autoReconnect);
110+
public EntityBOT(String host, int port, String username, String password, boolean debug, boolean autoReconnect, long reconnectDelay) {
111+
this(host, port, Proxy.NO_PROXY, username, password, debug, autoReconnect, reconnectDelay);
111112
}
112113

113114
/**
114115
* Instanciate the EntityBOT
115116
*
116-
* @param host the minecraft server address
117-
* @param port the minecraft server port
118-
* @param proxy the proxy
119-
* @param username the email of the premium account <br><strong>ONLY MOJANG ACCOUNT</strong>
120-
* @param password the password of the premium account
121-
* @param debug activate debug mode
122-
* @param autoReconnect activate auto reconnect
117+
* @param host the minecraft server address
118+
* @param port the minecraft server port
119+
* @param proxy the proxy
120+
* @param username the email of the premium account <br><strong>ONLY MOJANG ACCOUNT</strong>
121+
* @param password the password of the premium account
122+
* @param debug activate debug mode
123+
* @param autoReconnect activate auto reconnect
124+
* @param reconnectDelay delay before reconnect
123125
*/
124-
public EntityBOT(String host, int port, Proxy proxy, String username, String password, boolean debug, boolean autoReconnect) {
126+
public EntityBOT(String host, int port, Proxy proxy, String username, String password, boolean debug, boolean autoReconnect, long reconnectDelay) {
125127
this.host = host;
126128
this.port = port;
127129
this.proxy = proxy;
128130
this.username = username;
129131
this.password = password;
130132
this.debug = debug;
131133
this.autoReconnect = autoReconnect;
134+
this.reconnectDelay = reconnectDelay;
132135
}
133136

134137
/**
@@ -144,6 +147,7 @@ public EntityBOT(ParseResult result) {
144147
this.debug = result.isDebug();
145148
this.proxy = Proxy.NO_PROXY;
146149
this.autoReconnect = result.isAutoReconnect();
150+
this.reconnectDelay = result.getReconnectDelay();
147151
}
148152

149153
/**
@@ -209,6 +213,15 @@ public boolean isAutoReconnect() {
209213
return autoReconnect;
210214
}
211215

216+
/**
217+
* Get reconnect delay
218+
*
219+
* @return the delay
220+
*/
221+
public long getReconnectDelay() {
222+
return reconnectDelay;
223+
}
224+
212225
/**
213226
* Get the client
214227
*

src/main/java/re/alwyn974/minecraft/bot/entity/MCBOTSessionAdapter.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import re.alwyn974.minecraft.bot.chat.TranslateChat;
1616

1717
import java.util.Arrays;
18+
import java.util.concurrent.TimeUnit;
1819

1920
/**
2021
* The session adapter, managing packet and more
@@ -46,9 +47,10 @@ public void disconnected(DisconnectedEvent event) {
4647
MinecraftBOT.getLogger().info("Disconnected: %s\n%s", event.getReason(), event.getCause() != null ? event.getCause() : "");
4748
if (bot.isAutoReconnect() && !event.getReason().equals("Disconnected")) {
4849
try {
50+
TimeUnit.MILLISECONDS.sleep(bot.getReconnectDelay());
4951
bot.connect();
50-
} catch (RequestException e) {
51-
MinecraftBOT.getLogger().error("Can't authenticate", e);
52+
} catch (RequestException | InterruptedException e) {
53+
MinecraftBOT.getLogger().error(e instanceof InterruptedException ? "Can't delay the reconnection" : "Can't authenticate", e);
5254
}
5355
}
5456
}

src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTFrame.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
import com.formdev.flatlaf.FlatDarculaLaf;
44
import re.alwyn974.minecraft.bot.MinecraftBOT;
55

6-
import javax.swing.JFrame;
7-
import javax.swing.UIManager;
8-
import javax.swing.UnsupportedLookAndFeelException;
9-
import javax.swing.WindowConstants;
10-
import java.awt.Dimension;
6+
import javax.swing.*;
7+
import java.awt.*;
118

129
/**
1310
* The Frame for the Gui
1411
*
1512
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
16-
* @version 1.0.8
13+
* @version 1.0.15
1714
* @since 1.0.0
1815
*/
1916
public class MCBOTFrame extends JFrame {
@@ -22,7 +19,7 @@ public MCBOTFrame() {
2219
setSystemLookAndFeel();
2320

2421
this.setTitle("MinecraftBOT - Dev by Alwyn974");
25-
this.setMinimumSize(new Dimension(800, 300));
22+
this.setMinimumSize(new Dimension(800, 350));
2623
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
2724
this.setLocationRelativeTo(null);
2825
this.setContentPane(new MCBOTPanel());

0 commit comments

Comments
 (0)