diff --git a/README.md b/README.md index 33a7386..98b64d4 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 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 @@ -16,7 +16,7 @@ Minecraft bot. Currently, used for afk on a Survival Server 😅 - 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 @@ -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 @@ -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 @@ -63,14 +64,15 @@ They are some builtin commands in the bot

Simply type anything in the CLI and type enter

``` --a,--autoReconnect Activate auto reconnect --d,--debug Activate debug --h,--host Setup the host value (Default=127.0.0.1) - --help Show this help page --p,--port Setup the port value (Default=25565) - --password Password of the user --s,--status Display only the status of the server --u,--user Email/Username of the user + -a,--autoReconnect Activate auto reconnect + -d,--debug Activate debug + -h,--host Setup the host value (Default=127.0.0.1) + --help Show this help page + -p,--port Setup the port value (Default=25565) + --password Password of the user + --reconnectDelay Delay before reconnection + -s,--status Display only the status of the server + -u,--user Email/Username of the user ``` diff --git a/build.gradle b/build.gradle index 695900f..7316c5f 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = "re.alwyn974" -version = "1.0.14" +version = "1.0.15" archivesBaseName = "MinecraftBOT" compileJava { diff --git a/src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java b/src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java index 015488e..0faeaee 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java +++ b/src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java @@ -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 @@ -100,7 +101,7 @@ private static void runHeadless(String... args) { } /** - * Get the logger of MinecraftBOT + * Get logger of MinecraftBOT * * @return the logger {@link BasicLogger} */ @@ -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 * diff --git a/src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java b/src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java index 9cb39fb..d194957 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java +++ b/src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java @@ -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"); } @@ -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; } diff --git a/src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java b/src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java index 7ea4897..ff6540a 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java +++ b/src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java @@ -4,7 +4,7 @@ * The result of parsed args * * @author Alwyn974 - * @version 1.0.13 + * @version 1.0.15 * @since 1.0.9 */ public class ParseResult { @@ -17,6 +17,7 @@ public class ParseResult { private boolean status; private boolean help; private boolean autoReconnect; + private long reconnectDelay; /** * Get the host @@ -90,6 +91,15 @@ public boolean isAutoReconnect() { return autoReconnect; } + /** + * Get reconnect delay + * + * @return the delay + */ + public long getReconnectDelay() { + return reconnectDelay; + } + /** * Set the host * @@ -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{" + diff --git a/src/main/java/re/alwyn974/minecraft/bot/entity/EntityBOT.java b/src/main/java/re/alwyn974/minecraft/bot/entity/EntityBOT.java index be06387..805e19f 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/entity/EntityBOT.java +++ b/src/main/java/re/alwyn974/minecraft/bot/entity/EntityBOT.java @@ -20,7 +20,7 @@ * The EntityBOT used to store all information about the user * * @author Alwyn974 - * @version 1.0.13 + * @version 1.0.15 * @since 1.0.0 */ public class EntityBOT { @@ -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; @@ -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); } /** @@ -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); } /** @@ -106,22 +107,23 @@ 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
ONLY MOJANG ACCOUNT - * @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
ONLY MOJANG ACCOUNT + * @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; @@ -129,6 +131,7 @@ public EntityBOT(String host, int port, Proxy proxy, String username, String pas this.password = password; this.debug = debug; this.autoReconnect = autoReconnect; + this.reconnectDelay = reconnectDelay; } /** @@ -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(); } /** @@ -209,6 +213,15 @@ public boolean isAutoReconnect() { return autoReconnect; } + /** + * Get reconnect delay + * + * @return the delay + */ + public long getReconnectDelay() { + return reconnectDelay; + } + /** * Get the client * diff --git a/src/main/java/re/alwyn974/minecraft/bot/entity/MCBOTSessionAdapter.java b/src/main/java/re/alwyn974/minecraft/bot/entity/MCBOTSessionAdapter.java index 92cb17d..278d94f 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/entity/MCBOTSessionAdapter.java +++ b/src/main/java/re/alwyn974/minecraft/bot/entity/MCBOTSessionAdapter.java @@ -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 @@ -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); } } } diff --git a/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTFrame.java b/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTFrame.java index a4c7fe2..f20cee5 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTFrame.java +++ b/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTFrame.java @@ -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 Alwyn974 - * @version 1.0.8 + * @version 1.0.15 * @since 1.0.0 */ public class MCBOTFrame extends JFrame { @@ -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()); diff --git a/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTPanel.java b/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTPanel.java index 47a5b01..a8cccfd 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTPanel.java +++ b/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTPanel.java @@ -18,12 +18,14 @@ * The Panel of the Gui * * @author Alwyn974 - * @version 1.0.14 + * @version 1.0.15 * @since 1.0.0 */ public class MCBOTPanel extends JPanel implements ActionListener { private final JPanel topPanel = new JPanel(); + private final JPanel topTopPanel = new JPanel(); + private final JPanel topBottomPanel = new JPanel(); private final JPanel bottomPanel = new JPanel(); private final JTextField usernameField = new JTextField(MinecraftBOT.getUsername()); @@ -38,6 +40,7 @@ public class MCBOTPanel extends JPanel implements ActionListener { private final JButton clearButton = new JButton("Clear"); private final JCheckBox debugBox = new JCheckBox("Debug", Boolean.parseBoolean(MinecraftBOT.getDebug())); private final JCheckBox autoReconnectBox = new JCheckBox("Auto Reconnect", Boolean.parseBoolean(MinecraftBOT.getAutoReconnect())); + private final JSpinner reconnectDelay = new JSpinner(); private final JTextArea logArea = new JTextArea(); private EntityBOT bot = null; @@ -52,19 +55,27 @@ public MCBOTPanel() { } private void addTopPanel() { - topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS)); + topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS)); + topTopPanel.setLayout(new BoxLayout(topTopPanel, BoxLayout.X_AXIS)); + topBottomPanel.setLayout(new BoxLayout(topBottomPanel, BoxLayout.X_AXIS)); - topPanel.add(new JLabel("Host: ")); - topPanel.add(hostField, BorderLayout.PAGE_START); + topTopPanel.add(new JLabel("Host: ")); + topTopPanel.add(hostField, BorderLayout.PAGE_START); - topPanel.add(new JLabel("Port: ")); - topPanel.add(portField, BorderLayout.PAGE_START); + topTopPanel.add(new JLabel("Port: ")); + topTopPanel.add(portField, BorderLayout.PAGE_START); - topPanel.add(new JLabel("Email: ")); - topPanel.add(usernameField, BorderLayout.PAGE_START); + topTopPanel.add(new JLabel("Email: ")); + topTopPanel.add(usernameField, BorderLayout.PAGE_START); - topPanel.add(new JLabel("Password: ")); - topPanel.add(passwordField, BorderLayout.PAGE_START); + topTopPanel.add(new JLabel("Password: ")); + topTopPanel.add(passwordField, BorderLayout.PAGE_START); + + topBottomPanel.add(autoReconnectBox, BorderLayout.PAGE_START); + topBottomPanel.add(new JLabel("| (ms): ")); + reconnectDelay.setValue(Long.parseLong(MinecraftBOT.getReconnectDelay())); + topBottomPanel.add(reconnectDelay); + topBottomPanel.add(debugBox, BorderLayout.PAGE_START); addButton(connectButton); disconnectButton.setEnabled(false); @@ -72,16 +83,15 @@ private void addTopPanel() { addButton(statusButton); addButton(clearButton); - topPanel.add(autoReconnectBox, BorderLayout.PAGE_START); - topPanel.add(debugBox, BorderLayout.PAGE_START); - + topPanel.add(topTopPanel); + topPanel.add(topBottomPanel); this.add(topPanel, BorderLayout.PAGE_START); } private void addButton(JButton button) { button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); button.addActionListener(this); - topPanel.add(button, BorderLayout.PAGE_START); + topBottomPanel.add(button, BorderLayout.PAGE_START); } private void addCenterPanel() { @@ -124,8 +134,9 @@ public void actionPerformed(ActionEvent e) { if (e.getSource() == connectButton) { setFieldsEnabled(false); botThread = new Thread(() -> { - bot = new EntityBOT(hostField.getText(), Integer.parseInt(portField.getText()), usernameField.getText(), new String(passwordField.getPassword()), debugBox.isSelected(), autoReconnectBox.isSelected()); try { + long delay = Long.parseLong(reconnectDelay.getValue().toString()); + bot = new EntityBOT(hostField.getText(), Integer.parseInt(portField.getText()), usernameField.getText(), new String(passwordField.getPassword()), debugBox.isSelected(), autoReconnectBox.isSelected(), delay); bot.connect(); } catch (Exception ex) { MinecraftBOT.getLogger().error("Error: %s", ex.getMessage()); @@ -155,6 +166,7 @@ private void setFieldsEnabled(boolean enabled) { connectButton.setEnabled(enabled); debugBox.setEnabled(enabled); autoReconnectBox.setEnabled(enabled); + reconnectDelay.setEnabled(enabled); disconnectButton.setEnabled(!enabled); }