Skip to content

Commit

Permalink
➕ Reconnect Delay
Browse files Browse the repository at this point in the history
🔃 Update the whole code to integrate reconnect delay
Fix #7
  • Loading branch information
alwyn974 committed Aug 30, 2021
1 parent feb4b75 commit 6a2601d
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 53 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Minecraft bot. Currently, used for afk on a Survival Server 😅

- Graphical User Interface
- LogPanel to see errors directly
- Test with Spigot, Paper 1.17.1
- Tested with Spigot, Paper 1.17.1
- Disconnects gracefully after the end
- Free
- Open source
- Command Line Interface
- Online (Mojang)
- Cracked
- Automatic Respawn
- Auto Reconnect (Only if `DisconnectEvent` is throw, and the reason is not `Disconnected`)
- Auto Reconnect with Delay (Only if `DisconnectEvent` is throw, and the reason is not `Disconnected`)

## Todos

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

## Images

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

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

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

They are some builtin commands in the bot

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

```
-a,--autoReconnect Activate auto reconnect
-d,--debug Activate debug
-h,--host <arg> Setup the host value (Default=127.0.0.1)
--help Show this help page
-p,--port <arg> Setup the port value (Default=25565)
--password <arg> Password of the user
-s,--status Display only the status of the server
-u,--user <arg> Email/Username of the user
-a,--autoReconnect Activate auto reconnect
-d,--debug Activate debug
-h,--host <arg> Setup the host value (Default=127.0.0.1)
--help Show this help page
-p,--port <arg> Setup the port value (Default=25565)
--password <arg> Password of the user
--reconnectDelay <arg> Delay before reconnection
-s,--status Display only the status of the server
-u,--user <arg> Email/Username of the user
```


Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "re.alwyn974"
version = "1.0.14"
version = "1.0.15"
archivesBaseName = "MinecraftBOT"

compileJava {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class MinecraftBOT {
private static final String DEBUG = System.getenv("MC_BOT_DEBUG");
private static final String PREFIX = System.getenv("MC_BOT_PREFIX");
private static final String AUTO_RECONNECT = System.getenv("MC_BOT_AUTO_RECONNECT");
private static final String RECONNECT_DELAY = System.getenv("MC_BOT_RECONNECT_DELAY");

/**
* The main
Expand Down Expand Up @@ -100,7 +101,7 @@ private static void runHeadless(String... args) {
}

/**
* Get the logger of MinecraftBOT
* Get logger of MinecraftBOT
*
* @return the logger {@link BasicLogger}
*/
Expand Down Expand Up @@ -172,6 +173,15 @@ public static String getAutoReconnect() {
return AUTO_RECONNECT;
}

/**
* Get reconnect delay
*
* @return the delay
*/
public static String getReconnectDelay() {
return RECONNECT_DELAY != null ? RECONNECT_DELAY : "1000";
}

/**
* Get the prefix of commands
*
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private static void addOptions() {
options.addOption(null, "password", true, "Password of the user");
options.addOption("d", "debug", false, "Activate debug");
options.addOption("a", "autoReconnect", false, "Activate auto reconnect");
options.addOption(null, "reconnectDelay", true, "Delay before reconnection");
options.addOption("s", "status", false, "Display only the status of the server");
options.addOption(null, "help", false, "Show this help page");
}
Expand All @@ -87,8 +88,9 @@ private static ParseResult parseResult() {
result.setEmail(cmd.hasOption("u") ? cmd.getOptionValue("u") : MinecraftBOT.getUsername());
result.setPassword(cmd.hasOption("password") ? cmd.getOptionValue("password") : MinecraftBOT.getPassword());
result.setStatus(cmd.hasOption("s"));
result.setDebug(cmd.hasOption("d"));
result.setAutoReconnect(cmd.hasOption("a"));
result.setDebug(cmd.hasOption("d") ? cmd.hasOption("d") : Boolean.parseBoolean(MinecraftBOT.getDebug()));
result.setAutoReconnect(cmd.hasOption("a") ? cmd.hasOption("a") : Boolean.parseBoolean(MinecraftBOT.getAutoReconnect()));
result.setReconnectDelay(Long.parseLong(cmd.hasOption("reconnectDelay") ? cmd.getOptionValue("reconnectDelat") : MinecraftBOT.getReconnectDelay()));
result.setHelp(cmd.hasOption("help"));
return result;
}
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* The result of parsed args
*
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
* @version 1.0.13
* @version 1.0.15
* @since 1.0.9
*/
public class ParseResult {
Expand All @@ -17,6 +17,7 @@ public class ParseResult {
private boolean status;
private boolean help;
private boolean autoReconnect;
private long reconnectDelay;

/**
* Get the host
Expand Down Expand Up @@ -90,6 +91,15 @@ public boolean isAutoReconnect() {
return autoReconnect;
}

/**
* Get reconnect delay
*
* @return the delay
*/
public long getReconnectDelay() {
return reconnectDelay;
}

/**
* Set the host
*
Expand Down Expand Up @@ -162,6 +172,15 @@ public void setAutoReconnect(boolean autoReconnect) {
this.autoReconnect = autoReconnect;
}

/**
* Set reconnect delay
*
* @param reconnectDelay the delay
*/
public void setReconnectDelay(long reconnectDelay) {
this.reconnectDelay = reconnectDelay;
}

@Override
public String toString() {
return "ParseResult{" +
Expand Down
39 changes: 26 additions & 13 deletions src/main/java/re/alwyn974/minecraft/bot/entity/EntityBOT.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* The EntityBOT used to store all information about the user
*
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
* @version 1.0.13
* @version 1.0.15
* @since 1.0.0
*/
public class EntityBOT {
Expand All @@ -32,6 +32,7 @@ public class EntityBOT {
private final String password;
private final boolean debug;
private final boolean autoReconnect;
private final long reconnectDelay;
private Session client = null;
private EntityPos pos = null;
private double health = -1;
Expand All @@ -46,7 +47,7 @@ public class EntityBOT {
* @param password the password of the account
*/
public EntityBOT(String username, String password) {
this("localhost", username, password, false);
this("127.0.0.1", username, password, false);
}

/**
Expand Down Expand Up @@ -95,7 +96,7 @@ public EntityBOT(String host, int port, String username, String password) {
* @param debug activate debug mode
*/
public EntityBOT(String host, int port, String username, String password, boolean debug) {
this(host, port, Proxy.NO_PROXY, username, password, debug, false);
this(host, port, username, password, debug, false, 1000);
}

/**
Expand All @@ -106,29 +107,31 @@ public EntityBOT(String host, int port, String username, String password, boolea
* @param debug activate debug mode
* @param autoReconnect activate auto reconnect mode
*/
public EntityBOT(String host, int port, String username, String password, boolean debug, boolean autoReconnect) {
this(host, port, Proxy.NO_PROXY, username, password, debug, autoReconnect);
public EntityBOT(String host, int port, String username, String password, boolean debug, boolean autoReconnect, long reconnectDelay) {
this(host, port, Proxy.NO_PROXY, username, password, debug, autoReconnect, reconnectDelay);
}

/**
* Instanciate the EntityBOT
*
* @param host the minecraft server address
* @param port the minecraft server port
* @param proxy the proxy
* @param username the email of the premium account <br><strong>ONLY MOJANG ACCOUNT</strong>
* @param password the password of the premium account
* @param debug activate debug mode
* @param autoReconnect activate auto reconnect
* @param host the minecraft server address
* @param port the minecraft server port
* @param proxy the proxy
* @param username the email of the premium account <br><strong>ONLY MOJANG ACCOUNT</strong>
* @param password the password of the premium account
* @param debug activate debug mode
* @param autoReconnect activate auto reconnect
* @param reconnectDelay delay before reconnect
*/
public EntityBOT(String host, int port, Proxy proxy, String username, String password, boolean debug, boolean autoReconnect) {
public EntityBOT(String host, int port, Proxy proxy, String username, String password, boolean debug, boolean autoReconnect, long reconnectDelay) {
this.host = host;
this.port = port;
this.proxy = proxy;
this.username = username;
this.password = password;
this.debug = debug;
this.autoReconnect = autoReconnect;
this.reconnectDelay = reconnectDelay;
}

/**
Expand All @@ -144,6 +147,7 @@ public EntityBOT(ParseResult result) {
this.debug = result.isDebug();
this.proxy = Proxy.NO_PROXY;
this.autoReconnect = result.isAutoReconnect();
this.reconnectDelay = result.getReconnectDelay();
}

/**
Expand Down Expand Up @@ -209,6 +213,15 @@ public boolean isAutoReconnect() {
return autoReconnect;
}

/**
* Get reconnect delay
*
* @return the delay
*/
public long getReconnectDelay() {
return reconnectDelay;
}

/**
* Get the client
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import re.alwyn974.minecraft.bot.chat.TranslateChat;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
* The session adapter, managing packet and more
Expand Down Expand Up @@ -46,9 +47,10 @@ public void disconnected(DisconnectedEvent event) {
MinecraftBOT.getLogger().info("Disconnected: %s\n%s", event.getReason(), event.getCause() != null ? event.getCause() : "");
if (bot.isAutoReconnect() && !event.getReason().equals("Disconnected")) {
try {
TimeUnit.MILLISECONDS.sleep(bot.getReconnectDelay());
bot.connect();
} catch (RequestException e) {
MinecraftBOT.getLogger().error("Can't authenticate", e);
} catch (RequestException | InterruptedException e) {
MinecraftBOT.getLogger().error(e instanceof InterruptedException ? "Can't delay the reconnection" : "Can't authenticate", e);
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
import com.formdev.flatlaf.FlatDarculaLaf;
import re.alwyn974.minecraft.bot.MinecraftBOT;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.*;

/**
* The Frame for the Gui
*
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
* @version 1.0.8
* @version 1.0.15
* @since 1.0.0
*/
public class MCBOTFrame extends JFrame {
Expand All @@ -22,7 +19,7 @@ public MCBOTFrame() {
setSystemLookAndFeel();

this.setTitle("MinecraftBOT - Dev by Alwyn974");
this.setMinimumSize(new Dimension(800, 300));
this.setMinimumSize(new Dimension(800, 350));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setContentPane(new MCBOTPanel());
Expand Down
Loading

0 comments on commit 6a2601d

Please sign in to comment.