Make the bot work

This commit is contained in:
Astatin3
2024-05-02 16:12:00 -06:00
parent 7b5e763327
commit f64d8dbcde
7 changed files with 202 additions and 73 deletions
+64 -2
View File
@@ -17,8 +17,70 @@ using namespace Botcraft;
using namespace ProtocolCraft; using namespace ProtocolCraft;
CommandClient::CommandClient(const bool use_renderer_) : TemplatedBehaviourClient<CommandClient>(use_renderer_){ CommandClient::CommandClient(const bool use_renderer_) : TemplatedBehaviourClient<CommandClient>(use_renderer_){
std::cout << "Bot started!" << std::endl;
} }
CommandClient::~CommandClient(){}
CommandClient::~CommandClient(){} bool running = false;
bool gotoCmdStream = false;
void CommandClient::gotopos(int x, int y, int z){
Position target_position = Position(x, y, z);
float speed_multiplier = 2.0f;;
auto tree = Builder<CommandClient>("goto tree")
.sequence()
// Perform the pathfinding in a Selector,
// so it exits as soon as one leaf
// returns success
.selector()
// The next three lines do exactly the same,
// they're only here to show the different
// possibilities to create a leaf. Note that
// only the lambda solution can use default
// parameters values
.leaf("go to lambda", [=](CommandClient& c) { return GoTo(c, target_position, 0, 0, 0, true, false, speed_multiplier); })
.leaf("go to function", GoTo, target_position, 0, 0, 0, true, false, speed_multiplier)
.leaf("go to std::bind", std::bind(GoTo, std::placeholders::_1, target_position, 0, 0, 0, true, false, speed_multiplier))
// If goto fails, say something in chat
.leaf(Say, "Pathfinding failed :(")
.end()
// Switch back to empty behaviour
.leaf([](CommandClient& c) { c.SetBehaviourTree(nullptr); return Status::Success; })
.end();
SetBehaviourTree(tree);
}
void CommandClient::gotocmd(int x, int y, int z){
if(running && !gotoCmdStream){
std::cout << "Already running a task." << std::endl;
running = true;
return;
}
gotopos(x,y,z);
}
void CommandClient::startGotoSteamMode(){
if(running){
std::cout << "Already running a task." << std::endl;
running = true;
return;
}
running = true;
gotoCmdStream = true;
}
void CommandClient::stop(){
if(running){
std::cout << "Stopped..." << std::endl;
SetBehaviourTree(nullptr);
gotoCmdStream = false;
running = false;
}else{
std::cout << "No task is running..." << std::endl;
}
}
+4 -14
View File
@@ -13,20 +13,10 @@ public:
CommandClient(const bool use_renderer_); CommandClient(const bool use_renderer_);
~CommandClient(); ~CommandClient();
protected: void gotopos(int x, int y, int z);
#if PROTOCOL_VERSION < 759 /* < 1.19 */ void gotocmd(int x, int y, int z);
// virtual void Handle(ProtocolCraft::ClientboundChatPacket& msg) override;
#else
// virtual void Handle(ProtocolCraft::ClientboundPlayerChatPacket& msg) override;
// virtual void Handle(ProtocolCraft::ClientboundSystemChatPacket& msg) override;
#endif
// void ProcessChatMsg(const std::vector<std::string>& splitted_msg); void startGotoSteamMode();
// Check for any spawnable blocks in a sphere from pos and prints void stop();
// all the positions into a file
// Use check_lighting to add a check on light block value (> 7)
// (warning: ignore top slabs and upside-down stairs,
// you should check for such blocks manually)
// void CheckPerimeter(const Botcraft::Position& pos, const float radius, const bool check_lighting);
}; };
@@ -128,8 +128,8 @@ void ChatCommandClient::ProcessChatMsg(const std::vector<std::string>& splitted_
// possibilities to create a leaf. Note that // possibilities to create a leaf. Note that
// only the lambda solution can use default // only the lambda solution can use default
// parameters values // parameters values
.leaf("go to lambda", [=](ChatCommandClient& c) { return GoTo(c, target_position, 0, 0, 0, true, false, speed_multiplier); }) .leaf("go to lambda", [=](ChatCommandClient& c) { return GoTo(c, target_position, 0, 0, 0, true, true, speed_multiplier); })
.leaf("go to function", GoTo, target_position, 0, 0, 0, true, false, speed_multiplier) .leaf("go to function", GoTo, target_position, 0, 0, 0, true, true, speed_multiplier)
.leaf("go to std::bind", std::bind(GoTo, std::placeholders::_1, target_position, 0, 0, 0, true, false, speed_multiplier)) .leaf("go to std::bind", std::bind(GoTo, std::placeholders::_1, target_position, 0, 0, 0, true, false, speed_multiplier))
// If goto fails, say something in chat // If goto fails, say something in chat
.leaf(Say, "Pathfinding failed :(") .leaf(Say, "Pathfinding failed :(")
+51 -34
View File
@@ -20,10 +20,52 @@ struct Args
Args ParseCommandLine(int argc, char* argv[]); Args ParseCommandLine(int argc, char* argv[]);
int main(int argc, char* argv[]) std::vector<std::string> split(std::string text, char delim) {
{ std::string line;
try std::vector<std::string> vec;
{ std::stringstream ss(text);
while(std::getline(ss, line, delim)) {
vec.push_back(line);
}
return vec;
}
void runSTDINThing(CommandClient &client){
for (std::string line; std::getline(std::cin, line);) {
std::cout << "Got message: " + line << std::endl;
if (line.starts_with("chat:")) {
std::string msg = line.substr(5, line.length());
std::cout << "Sending chat message: " + msg << std::endl;
client.SendChatMessage(msg);
} else if (line.starts_with("cmd:")) {
std::string cmd = line.substr(4, line.length());
std::cout << "Sending commnad: /" + cmd << std::endl;
client.SendChatCommand(cmd);
} else if (line.starts_with("Stop")){
std::cout << "Stopped current action." << std::endl;
client.stop();
} else if (line.starts_with("Disconnect")) {
std::cout << "Disconnected." << std::endl;
client.Disconnect();
} else if (line.starts_with("goto:")) {
std::vector<std::string> xyz = split(line.substr(5, line.length()), ',');
std::cout << "Moving toward: (" + xyz.at(0) + ", "
+ xyz.at(1) + ", "
+ xyz.at(2) + ")" << std::endl;
client.gotocmd(std::stoi(xyz.at(0)),
std::stoi(xyz.at(1)),
std::stoi(xyz.at(2)));
} else if (line.starts_with("startGotoSteamMode")){
std::cout << "Started GOTO stream mode..." << std::endl;
client.startGotoSteamMode();
}
}
}
int main(int argc, char* argv[]){
try {
// Init logging, log everything >= Info, only to console, no file // Init logging, log everything >= Info, only to console, no file
Botcraft::Logger::GetInstance().SetLogLevel(Botcraft::LogLevel::Info); Botcraft::Logger::GetInstance().SetLogLevel(Botcraft::LogLevel::Info);
Botcraft::Logger::GetInstance().SetFilename(""); Botcraft::Logger::GetInstance().SetFilename("");
@@ -48,42 +90,17 @@ int main(int argc, char* argv[])
} }
} }
// Botcraft::ConnectionClient client;
CommandClient client(false); CommandClient client(false);
client.SetAutoRespawn(true); client.SetAutoRespawn(true);
// CommandClient client(true);
//
LOG_INFO("Starting connection process"); LOG_INFO("Starting connection process");
//
// client.SetAutoRespawn(true);
//
// LOG_INFO("Starting connection process");
client.Connect(args.address, args.login); client.Connect(args.address, args.login);
//
// client.RunBehaviourUntilClosed();
// client.Disconnect(); std::thread thread(runSTDINThing, std::ref(client));
// return 0; client.RunBehaviourUntilClosed();
// client.Connect(args.address, args.login);
for (std::string line; std::getline(std::cin, line);) {
std::cout << "Got message: "+line << std::endl;
if(line.starts_with("chat:")){
client.SendChatMessage(line.substr(5,line.length()));
}else if(line.starts_with("cmd:")){
client.SendChatCommand(line.substr(4,line.length()));
}else if(line.starts_with("Disconnect")){
std::cout << "Disconnected." << std::endl;
client.Disconnect();
return 0;
}
}
//// client.SetAutoRespawn(true); //// client.SetAutoRespawn(true);
// //
+2 -1
View File
@@ -1,2 +1,3 @@
cd /home/astatin3/Documents/GitHub/meteorbot/bot/bin cd /home/astatin3/Documents/GitHub/meteorbot/bot/bin
./McConnectBot #./2_ChatCommandExample_d
./McConnectBot
@@ -1,6 +1,7 @@
package com.astatin3.meteorbot.commands; package com.astatin3.meteorbot.commands;
import com.astatin3.meteorbot.modules.ModuleExample; import com.astatin3.meteorbot.modules.ModuleExample;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import meteordevelopment.meteorclient.commands.Command; import meteordevelopment.meteorclient.commands.Command;
@@ -23,8 +24,11 @@ public class CommandExample extends Command {
".mb help - Print this help info\n" + ".mb help - Print this help info\n" +
".mb chat <msg> - Send chat message\n" + ".mb chat <msg> - Send chat message\n" +
".mb cmd <cmd> - Send command (No '/')\n" + ".mb cmd <cmd> - Send command (No '/')\n" +
".mb stop - Stop bot\n" + ".mb goto <x> <y> <z> - Go to xyz choord\n" +
".mb rawsend <type> <content> - Send raw message to bot\n"); ".mb follow - Follow player\n" +
".mb stop - Stop current action\n" +
".mb rawsend <type> <content> - Send raw message to bot\n" +
".mb quit - Make bot leave the game");
} }
@Override @Override
@@ -59,11 +63,39 @@ public class CommandExample extends Command {
builder.then(literal("stop").executes(context -> { builder.then(literal("stop").executes(context -> {
module.stopBot(); module.writeSTDIN("Stop");
if(module.followMode){
module.followMode = false;
}
return SINGLE_SUCCESS; return SINGLE_SUCCESS;
})); }));
builder.then(literal("quit").executes(context -> {
module.quitBot();
return SINGLE_SUCCESS;
}));
builder.then(literal("goto")
.then(
argument(
"x", IntegerArgumentType.integer())
.then(
argument(
"y", IntegerArgumentType.integer())
.then(
argument(
"z", IntegerArgumentType.integer())
.executes(context -> {
module.writeSTDIN(
"goto:" + context.getArgument("x", Integer.class) + "," +
context.getArgument("y", Integer.class) + "," +
context.getArgument("z", Integer.class));
return SINGLE_SUCCESS;
})))));
builder.then(literal("rawsend") builder.then(literal("rawsend")
.then( .then(
argument( argument(
@@ -76,5 +108,11 @@ public class CommandExample extends Command {
return SINGLE_SUCCESS; return SINGLE_SUCCESS;
})))); }))));
builder.then(literal("follow")
.executes(context -> {
module.startFollowMode();
return SINGLE_SUCCESS;
}));
} }
} }
@@ -9,6 +9,7 @@ import meteordevelopment.meteorclient.systems.modules.Module;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import java.io.*; import java.io.*;
import java.util.concurrent.TimeUnit;
public class ModuleExample extends Module { public class ModuleExample extends Module {
@@ -58,7 +59,7 @@ public class ModuleExample extends Module {
cmdThread = new Thread(new Runnable() { cmdThread = new Thread(new Runnable() {
public void run() { public void run() {
readCMD(); readCMD();
stopBot(); quitBot();
} }
}); });
cmdThread.start(); cmdThread.start();
@@ -75,8 +76,7 @@ public class ModuleExample extends Module {
String line; String line;
while ((line = stdout.readLine()) != null) { while ((line = stdout.readLine()) != null) {
if(verboseMode.get()) parseSTDOUT(line);
info(line);
} }
int exitCode = process.waitFor(); int exitCode = process.waitFor();
@@ -89,20 +89,36 @@ public class ModuleExample extends Module {
} }
} }
private void parseSTDOUT(String str){
if(verboseMode.get())
info(str);
if(str.equals("Get Player Pos")){
assert mc.player != null;
Vec3d vel = mc.player.getVelocity();
writeSTDIN("PlayerPos:" +
mc.player.getX() + "," +
mc.player.getY() + "," +
mc.player.getZ() + "," +
vel.getX() + "," +
vel.getZ() + "," +
vel.getZ() + ",");
}
}
private Thread cmdThread = null; private Thread cmdThread = null;
public void onActivate() { public void onActivate() {
stopBot(); quitBot();
startCMD(); startCMD();
} }
@Override @Override
public void onDeactivate() { public void onDeactivate() {
stopBot(); quitBot();
} }
public void stopBot(){ public void quitBot(){
if (cmdThread != null) { if (cmdThread != null) {
writeSTDIN("Disconnect"); writeSTDIN("Disconnect");
cmdThread.interrupt(); cmdThread.interrupt();
@@ -134,17 +150,22 @@ public class ModuleExample extends Module {
} }
} }
private void parseResponse(String response){ public boolean followMode = false;
if(response.equals("Get Player Pos")){ public void startFollowMode(){
Vec3d vel = mc.player.getVelocity(); writeSTDIN("startGotoSteamMode");
writeSTDIN("PlayerPos:" + followMode = true;
mc.player.getX() + "," + new Thread(new Runnable() {
mc.player.getY() + "," + public void run() {
mc.player.getZ() + "," + while(followMode){
vel.getX() + "," + writeSTDIN("goto:"+mc.player.getX()+","+mc.player.getY()+","+mc.player.getZ());
vel.getZ() + "," + try {
vel.getZ() + ","); TimeUnit.SECONDS.sleep(1);
} } catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
}
}).start();
} }
} }