Skip to content

Commit

Permalink
✨ Added auto reconnect when disconnect reason is not "Disconnected"
Browse files Browse the repository at this point in the history
Fix #5
  • Loading branch information
alwyn974 committed Aug 8, 2021
1 parent 2a7e250 commit 4046d30
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 34 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -60,6 +63,7 @@ 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
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.12"
version = "1.0.13"
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 @@ -33,7 +33,7 @@
* The heart of the MinecraftBOT
*
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
* @version 1.0.7
* @version 1.0.13
* @since 1.0.0
*/
public class MinecraftBOT {
Expand All @@ -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
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* The command line parser
*
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
* @version 1.0.9
* @version 1.0.13
* @since 1.0.9
*/
public class CLIParser {
Expand Down Expand Up @@ -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");
}
Expand All @@ -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;
}
Expand Down
48 changes: 34 additions & 14 deletions src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package re.alwyn974.minecraft.bot.cli;

/**
* The resulf of parsed args
* The result of parsed args
*
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
* @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
Expand All @@ -31,7 +32,7 @@ public String getHost() {
*
* @return the port
*/
public Integer getPort() {
public int getPort() {
return port;
}

Expand All @@ -58,7 +59,7 @@ public String getPassword() {
*
* @return debug value
*/
public Boolean isDebug() {
public boolean isDebug() {
return debug;
}

Expand All @@ -67,7 +68,7 @@ public Boolean isDebug() {
*
* @return status value
*/
public Boolean shouldPrintStatus() {
public boolean shouldPrintStatus() {
return status;
}

Expand All @@ -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
*
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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{" +
Expand All @@ -153,6 +172,7 @@ public String toString() {
", debug=" + debug +
", status=" + status +
", help=" + help +
", autoReconnect=" + autoReconnect +
'}';
}
}
47 changes: 36 additions & 11 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.4
* @version 1.0.13
* @since 1.0.0
*/
public class EntityBOT {
Expand All @@ -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;
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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 <br><strong>ONLY MOJANG ACCOUNT</strong>
* @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 <br><strong>ONLY MOJANG ACCOUNT</strong>
* @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 <br><strong>ONLY MOJANG ACCOUNT</strong>
* @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;
}

/**
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +20,7 @@
* The session adapter, managing packet and more
*
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
* @version 1.0.12
* @version 1.0.13
* @since 1.0.0
*/
public class MCBOTSessionAdapter extends SessionAdapter {
Expand All @@ -36,13 +37,20 @@ public MCBOTSessionAdapter(EntityBOT bot) {
}

/**
* Handle the disconnected event
*
* <p>Handle the disconnected event</p>
* <p>If auto reconnect is enabled, it will try to connect unless the disconnection message is "Disconnected" </p>
* @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);
}
}
}

/**
Expand All @@ -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.<ServerChatPacket>getPacket().getMessage()));
if (event.getPacket() instanceof ServerPlayerPositionRotationPacket) {
Expand Down
Loading

0 comments on commit 4046d30

Please sign in to comment.