Skip to content

Commit

Permalink
Working Command Line Interface
Browse files Browse the repository at this point in the history
Fix #1
Update to v1.0.10
Add constructor of EntityBOT with ParseResult
Add environment variable for prefix
  • Loading branch information
alwyn974 committed Jul 23, 2021
1 parent 214ddce commit ae4e1ce
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 59 deletions.
30 changes: 25 additions & 5 deletions README.md
Expand Up @@ -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

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

<p> Like the GUI, the CLI can use commands and send message to the server </p>
<p> Simply type anything in the CLI and type enter</p>

```
-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 of the user
```


## Dependencies

* Java 8+
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "re.alwyn974"
version = "1.0.9"
version = "1.0.10"
archivesBaseName = "MinecraftBOT"

compileJava {
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 33 additions & 1 deletion 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
Expand All @@ -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();
Expand All @@ -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();
}

/**
Expand Down
96 changes: 48 additions & 48 deletions src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java
Expand Up @@ -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;
}

/**
Expand Down
Expand Up @@ -17,7 +17,7 @@
*/
public class CommandHandler {

private static String prefix = ".";
private static String prefix = MinecraftBOT.getPrefix();
private static final HashMap<String, ICommand> COMMANDS = new HashMap<>();
private static final List<ICommand> COMMANDS_LIST = new ArrayList<>();

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/re/alwyn974/minecraft/bot/entity/EntityBOT.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit ae4e1ce

Please sign in to comment.