From ae4e1cecd6fd712ff497da1d2aaf63df67232bdd Mon Sep 17 00:00:00 2001 From: Alwyn Mattapullut Date: Fri, 23 Jul 2021 22:53:19 +0400 Subject: [PATCH] Working Command Line Interface Fix #1 Update to v1.0.10 Add constructor of EntityBOT with ParseResult Add environment variable for prefix --- README.md | 30 +++++- build.gradle | 2 +- .../alwyn974/minecraft/bot/MinecraftBOT.java | 16 +++- .../alwyn974/minecraft/bot/cli/CLIParser.java | 34 ++++++- .../minecraft/bot/cli/ParseResult.java | 96 +++++++++---------- .../bot/cmd/utils/CommandHandler.java | 2 +- .../minecraft/bot/entity/EntityBOT.java | 15 +++ 7 files changed, 136 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 175cc8c..edcbd57 100644 --- a/README.md +++ b/README.md @@ -2,25 +2,28 @@ ## Description -Minecraft bot. Currently used for afk on a Survival Server 😅 +Minecraft bot. Currently, used for afk on a Survival Server 😅 ## Features - Graphical user interface - LogPanel to see errors directly -- Test with Spigot, Paper 1.17 +- Test with Spigot, Paper 1.17.1 - Disconnects gracefully after the end - Free - Open source +- Command Line Interface ## Todos -- Command line interface +- Multi Version +- Cracked +- Respawn the player automatically if dead ## Requirements - Java 8+ -- Minecraft Server 1.17 (It can work for 1.7.10-1.17 normally, I haven't tested it) +- Minecraft Server 1.17.1 ## Downloads @@ -37,8 +40,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 username - `MC_BOT_PASSWORD` for the password +- `MC_BOT_PREFIX` for the prefix of the commands (default=`.`) -The are some builtin commands in the bot +They are some builtin commands in the bot - `difficulty` get the difficulty of the server - `food` get the food level of the player @@ -47,6 +51,22 @@ The are some builtin commands in the bot - `list` get the players connected (Sometimes the packet is glitched, you can use the status button go get the players) - `pos` get the player position +## Command Line Interface + +

Like the GUI, the CLI can use commands and send message to the server

+

Simply type anything in the CLI and type enter

+ +``` +-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 of the user +``` + + ## Dependencies * Java 8+ diff --git a/build.gradle b/build.gradle index 0b663e2..4856f2c 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = "re.alwyn974" -version = "1.0.9" +version = "1.0.10" 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 f97ef3e..c219d44 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java +++ b/src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java @@ -45,6 +45,7 @@ public class MinecraftBOT { private static final String HOST = System.getenv("MC_BOT_HOST"); 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"); /** * The main @@ -157,14 +158,23 @@ public static String getDebug() { return DEBUG; } + /** + * Get the prefix of commands + * + * @return the prefix + */ + public static String getPrefix() { + return PREFIX != null ? PREFIX : "."; + } + /** * Retrieve the status of a server * - * @param host the host - * @param port the port + * @param host the host + * @param port the port * @param debug debug value to print some useful things */ - public static void retrieveStatus(String host, Integer port, boolean debug) { + public static void retrieveStatus(String host, int port, boolean debug) { new Thread(() -> { SessionService sessionService = new SessionService(); sessionService.setProxy(Proxy.NO_PROXY); 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 4f79837..540cdcd 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java +++ b/src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java @@ -1,7 +1,13 @@ package re.alwyn974.minecraft.bot.cli; +import com.github.steveice10.mc.auth.exception.request.RequestException; +import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket; import org.apache.commons.cli.*; import re.alwyn974.minecraft.bot.MinecraftBOT; +import re.alwyn974.minecraft.bot.cmd.utils.CommandHandler; +import re.alwyn974.minecraft.bot.entity.EntityBOT; + +import java.util.Scanner; /** * The command line parser @@ -14,11 +20,14 @@ public class CLIParser { private static final Options options = new Options(); private static CommandLine cmd; + private static EntityBOT bot; + private static Thread botThread; /** * Parse the command line arguments * * @param args the arguments + * @throws ParseException on parsing error */ public static void parse(String... args) throws ParseException { addOptions(); @@ -31,7 +40,30 @@ public static void parse(String... args) throws ParseException { MinecraftBOT.retrieveStatus(result.getHost(), result.getPort(), result.isDebug()); System.exit(0); } - + bot = new EntityBOT(result); + botThread = new Thread(() -> { + try { + MinecraftBOT.getLogger().info("Starting MinecraftBOT..."); + bot.connect(); + } catch (RequestException ex) { + MinecraftBOT.getLogger().error("Can't authenticate", ex); + botThread.interrupt(); + } + try (Scanner scanner = new Scanner(System.in)) { + while (bot != null && bot.getClient().isConnected()) { + String line = scanner.nextLine(); + if (line.equals("disconnect")) { + bot.getClient().disconnect("Disconnected"); + botThread.interrupt(); + break; + } + if (!new CommandHandler().execute(bot, line) && bot != null && bot.getClient().isConnected()) + bot.getClient().send(new ClientChatPacket(line)); + } + botThread.interrupt(); + } + }); + botThread.start(); } /** 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 f6a7f1f..937fe64 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java +++ b/src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java @@ -27,111 +27,111 @@ public String getHost() { } /** - * Set the host + * Get the port * - * @param host the host + * @return the port */ - public void setHost(String host) { - this.host = host; + public Integer getPort() { + return port; } /** - * Get the port + * Get the email * - * @return the port + * @return the email */ - public Integer getPort() { - return port; + public String getEmail() { + return email; } /** - * Set the port + * Get the password * - * @param port the port + * @return the password */ - public void setPort(Integer port) { - this.port = port; + public String getPassword() { + return password; } /** - * Get the email + * Get if debug is activate * - * @return the email + * @return debug value */ - public String getEmail() { - return email; + public Boolean isDebug() { + return debug; } /** - * Set the email + * Get if status is activate * - * @param email the email + * @return status value */ - public void setEmail(String email) { - this.email = email; + public Boolean shouldPrintStatus() { + return status; } /** - * Get the password + * Get if help is activate * - * @return the password + * @return help value */ - public String getPassword() { - return password; + public Boolean shouldPrintHelp() { + return help; } /** - * Set the password + * Set the host * - * @param password the password + * @param host the host */ - public void setPassword(String password) { - this.password = password; + public void setHost(String host) { + this.host = host; } /** - * Get if debug is activate + * Set the port * - * @return debug value + * @param port the port */ - public Boolean isDebug() { - return debug; + public void setPort(Integer port) { + this.port = port; } /** - * Set debug + * Set the email * - * @param debug debug value + * @param email the email */ - public void setDebug(Boolean debug) { - this.debug = debug; + public void setEmail(String email) { + this.email = email; } /** - * Get if status is activate + * Set the password * - * @return status value + * @param password the password */ - public Boolean shouldPrintStatus() { - return status; + public void setPassword(String password) { + this.password = password; } /** - * The status value + * Set debug * - * @param status status value + * @param debug debug value */ - public void setStatus(Boolean status) { - this.status = status; + public void setDebug(Boolean debug) { + this.debug = debug; } /** - * Get if help is activate + * The status value * - * @return help value + * @param status status value */ - public Boolean shouldPrintHelp() { - return help; + public void setStatus(Boolean status) { + this.status = status; } /** diff --git a/src/main/java/re/alwyn974/minecraft/bot/cmd/utils/CommandHandler.java b/src/main/java/re/alwyn974/minecraft/bot/cmd/utils/CommandHandler.java index 822d383..20b5ea9 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/cmd/utils/CommandHandler.java +++ b/src/main/java/re/alwyn974/minecraft/bot/cmd/utils/CommandHandler.java @@ -17,7 +17,7 @@ */ public class CommandHandler { - private static String prefix = "."; + private static String prefix = MinecraftBOT.getPrefix(); private static final HashMap COMMANDS = new HashMap<>(); private static final List COMMANDS_LIST = new ArrayList<>(); 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 068bfad..ea149e2 100644 --- a/src/main/java/re/alwyn974/minecraft/bot/entity/EntityBOT.java +++ b/src/main/java/re/alwyn974/minecraft/bot/entity/EntityBOT.java @@ -11,6 +11,7 @@ import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.tcp.TcpClientSession; import re.alwyn974.minecraft.bot.MinecraftBOT; +import re.alwyn974.minecraft.bot.cli.ParseResult; import java.net.Proxy; import java.util.List; @@ -115,6 +116,20 @@ public EntityBOT(String host, int port, Proxy proxy, String username, String pas this.debug = debug; } + /** + * Instanciate the EntityBOT from {@link ParseResult} + * + * @param result the result of parsed arguments + */ + public EntityBOT(ParseResult result) { + this.host = result.getHost(); + this.port = result.getPort(); + this.username = result.getEmail(); + this.password = result.getPassword(); + this.debug = result.isDebug(); + this.proxy = Proxy.NO_PROXY; + } + /** * Get the host *