diff --git a/README.md b/README.md index 1dd292e..16b739a 100644 --- a/README.md +++ b/README.md @@ -16,6 +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`) ## Todos @@ -43,7 +44,9 @@ There are environnement variable to override the default value of host, port, us - `MC_BOT_PORT` for the port - `MC_BOT_USERNAME` for the email/username - `MC_BOT_PASSWORD` for the password +- `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 They are some builtin commands in the bot @@ -60,6 +63,7 @@ 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 diff --git a/build.gradle b/build.gradle index e1fcaa4..7aff8e1 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = "re.alwyn974" -version = "1.0.12" +version = "1.0.13" 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 c219d44..dd537fe 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java +++ b/src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java @@ -33,7 +33,7 @@ * The heart of the MinecraftBOT * * @author Alwyn974 - * @version 1.0.7 + * @version 1.0.13 * @since 1.0.0 */ public class MinecraftBOT { @@ -46,6 +46,7 @@ public class MinecraftBOT { private static final String PORT = System.getenv("MC_BOT_PORT"); 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"); /** * The main @@ -158,6 +159,15 @@ public static String getDebug() { return DEBUG; } + /** + * Get the auto reconnect value + * + * @return the auto reconnect value + */ + public static String getAutoReconnect() { + return AUTO_RECONNECT; + } + /** * 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 d591639..c7f5c55 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java +++ b/src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java @@ -13,7 +13,7 @@ * The command line parser * * @author Alwyn974 - * @version 1.0.9 + * @version 1.0.13 * @since 1.0.9 */ public class CLIParser { @@ -75,6 +75,7 @@ private static void addOptions() { options.addOption("u", "user", true, "Email of the user"); 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("s", "status", false, "Display only the status of the server"); options.addOption(null, "help", false, "Show this help page"); } @@ -87,6 +88,7 @@ private static ParseResult parseResult() { 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.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 937fe64..7ea4897 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java +++ b/src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java @@ -1,21 +1,22 @@ package re.alwyn974.minecraft.bot.cli; /** - * The resulf of parsed args + * The result of parsed args * * @author Alwyn974 - * @version 1.0.9 + * @version 1.0.13 * @since 1.0.9 */ public class ParseResult { private String host; - private Integer port; + private int port; private String email; private String password; - private Boolean debug; - private Boolean status; - private Boolean help; + private boolean debug; + private boolean status; + private boolean help; + private boolean autoReconnect; /** * Get the host @@ -31,7 +32,7 @@ public String getHost() { * * @return the port */ - public Integer getPort() { + public int getPort() { return port; } @@ -58,7 +59,7 @@ public String getPassword() { * * @return debug value */ - public Boolean isDebug() { + public boolean isDebug() { return debug; } @@ -67,7 +68,7 @@ public Boolean isDebug() { * * @return status value */ - public Boolean shouldPrintStatus() { + public boolean shouldPrintStatus() { return status; } @@ -76,10 +77,19 @@ public Boolean shouldPrintStatus() { * * @return help value */ - public Boolean shouldPrintHelp() { + public boolean shouldPrintHelp() { return help; } + /** + * Get if auto reconnect is activate + * + * @return auto reconnect value + */ + public boolean isAutoReconnect() { + return autoReconnect; + } + /** * Set the host * @@ -94,7 +104,7 @@ public void setHost(String host) { * * @param port the port */ - public void setPort(Integer port) { + public void setPort(int port) { this.port = port; } @@ -121,7 +131,7 @@ public void setPassword(String password) { * * @param debug debug value */ - public void setDebug(Boolean debug) { + public void setDebug(boolean debug) { this.debug = debug; } @@ -130,7 +140,7 @@ public void setDebug(Boolean debug) { * * @param status status value */ - public void setStatus(Boolean status) { + public void setStatus(boolean status) { this.status = status; } @@ -139,10 +149,19 @@ public void setStatus(Boolean status) { * * @param help help value */ - public void setHelp(Boolean help) { + public void setHelp(boolean help) { this.help = help; } + /** + * Set auto reconnect value + * + * @param autoReconnect auto reconnect value + */ + public void setAutoReconnect(boolean autoReconnect) { + this.autoReconnect = autoReconnect; + } + @Override public String toString() { return "ParseResult{" + @@ -153,6 +172,7 @@ public String toString() { ", debug=" + debug + ", status=" + status + ", help=" + help + + ", autoReconnect=" + autoReconnect + '}'; } } 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 3b545cb..be06387 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.4 + * @version 1.0.13 * @since 1.0.0 */ public class EntityBOT { @@ -31,6 +31,7 @@ public class EntityBOT { private final String username; private final String password; private final boolean debug; + private final boolean autoReconnect; private Session client = null; private EntityPos pos = null; private double health = -1; @@ -54,7 +55,7 @@ public EntityBOT(String username, String password) { * @param debug activate debug mode */ public EntityBOT(String username, String password, boolean debug) { - this("localhost", username, password, debug); + this("127.0.0.1", username, password, debug); } /** @@ -83,7 +84,7 @@ public EntityBOT(String host, String username, String password, boolean debug) { * @param password the password of the premium account */ public EntityBOT(String host, int port, String username, String password) { - this(host, port, Proxy.NO_PROXY, username, password, false); + this(host, port, username, password, false); } /** @@ -94,26 +95,40 @@ 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); + this(host, port, Proxy.NO_PROXY, username, password, debug, false); + } + + /** + * @param host the minecraft server address + * @param port the minecraft server port + * @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 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); } /** * 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 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 */ - public EntityBOT(String host, int port, Proxy proxy, String username, String password, Boolean debug) { + public EntityBOT(String host, int port, Proxy proxy, String username, String password, boolean debug, boolean autoReconnect) { this.host = host; this.port = port; this.proxy = proxy; this.username = username; this.password = password; this.debug = debug; + this.autoReconnect = autoReconnect; } /** @@ -128,6 +143,7 @@ public EntityBOT(ParseResult result) { this.password = result.getPassword(); this.debug = result.isDebug(); this.proxy = Proxy.NO_PROXY; + this.autoReconnect = result.isAutoReconnect(); } /** @@ -184,6 +200,15 @@ public boolean isDebug() { return debug; } + /** + * Get if auto reconnect is activate + * + * @return auto reconnect value + */ + public boolean isAutoReconnect() { + return autoReconnect; + } + /** * 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 39e2cdb..92cb17d 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/entity/MCBOTSessionAdapter.java +++ b/src/main/java/re/alwyn974/minecraft/bot/entity/MCBOTSessionAdapter.java @@ -1,5 +1,6 @@ package re.alwyn974.minecraft.bot.entity; +import com.github.steveice10.mc.auth.exception.request.RequestException; import com.github.steveice10.mc.protocol.data.game.ClientRequest; import com.github.steveice10.mc.protocol.packet.ingame.client.ClientRequestPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket; @@ -19,7 +20,7 @@ * The session adapter, managing packet and more * * @author Alwyn974 - * @version 1.0.12 + * @version 1.0.13 * @since 1.0.0 */ public class MCBOTSessionAdapter extends SessionAdapter { @@ -36,13 +37,20 @@ public MCBOTSessionAdapter(EntityBOT bot) { } /** - * Handle the disconnected event - * + *

Handle the disconnected event

+ *

If auto reconnect is enabled, it will try to connect unless the disconnection message is "Disconnected"

* @param event the disconnected event */ @Override 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 { + bot.connect(); + } catch (RequestException e) { + MinecraftBOT.getLogger().error("Can't authenticate", e); + } + } } /** @@ -52,7 +60,6 @@ public void disconnected(DisconnectedEvent event) { */ @Override public void packetReceived(PacketReceivedEvent event) { - //System.out.println(event.getPacket().getClass().getName()); if (event.getPacket() instanceof ServerChatPacket) MinecraftBOT.getLogger().info(TranslateChat.translateComponent(event.getPacket().getMessage())); if (event.getPacket() instanceof ServerPlayerPositionRotationPacket) { 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 8fb22cc..d6b5f31 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTPanel.java +++ b/src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTPanel.java @@ -19,7 +19,7 @@ * The Panel of the Gui * * @author Alwyn974 - * @version 1.0.8 + * @version 1.0.13 * @since 1.0.0 */ public class MCBOTPanel extends JPanel implements ActionListener { @@ -38,6 +38,7 @@ public class MCBOTPanel extends JPanel implements ActionListener { private final JButton statusButton = new JButton("Status"); 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 JTextArea logArea = new JTextArea(); private EntityBOT bot = null; @@ -72,6 +73,7 @@ private void addTopPanel() { addButton(statusButton); addButton(clearButton); + topPanel.add(autoReconnectBox, BorderLayout.PAGE_START); topPanel.add(debugBox, BorderLayout.PAGE_START); this.add(topPanel, BorderLayout.PAGE_START); @@ -123,7 +125,7 @@ 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()); + bot = new EntityBOT(hostField.getText(), Integer.parseInt(portField.getText()), usernameField.getText(), new String(passwordField.getPassword()), debugBox.isSelected(), autoReconnectBox.isSelected()); try { bot.connect(); } catch (RequestException ex) { @@ -152,6 +154,7 @@ private void setFieldsEnabled(boolean enabled) { passwordField.setEnabled(enabled); connectButton.setEnabled(enabled); debugBox.setEnabled(enabled); + autoReconnectBox.setEnabled(enabled); disconnectButton.setEnabled(!enabled); }