diff --git a/bot/CommandClient.cpp b/bot/CommandClient.cpp index c1c5f37..50dedba 100644 --- a/bot/CommandClient.cpp +++ b/bot/CommandClient.cpp @@ -17,8 +17,70 @@ using namespace Botcraft; using namespace ProtocolCraft; CommandClient::CommandClient(const bool use_renderer_) : TemplatedBehaviourClient(use_renderer_){ - + std::cout << "Bot started!" << std::endl; } +CommandClient::~CommandClient(){} -CommandClient::~CommandClient(){} \ No newline at end of file +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("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; + } +} \ No newline at end of file diff --git a/bot/CommandClient.h b/bot/CommandClient.h index 0cdee26..231e5c2 100644 --- a/bot/CommandClient.h +++ b/bot/CommandClient.h @@ -13,20 +13,10 @@ public: CommandClient(const bool use_renderer_); ~CommandClient(); -protected: -#if PROTOCOL_VERSION < 759 /* < 1.19 */ -// virtual void Handle(ProtocolCraft::ClientboundChatPacket& msg) override; -#else -// virtual void Handle(ProtocolCraft::ClientboundPlayerChatPacket& msg) override; -// virtual void Handle(ProtocolCraft::ClientboundSystemChatPacket& msg) override; -#endif + void gotopos(int x, int y, int z); + void gotocmd(int x, int y, int z); -// void ProcessChatMsg(const std::vector& splitted_msg); + void startGotoSteamMode(); - // Check for any spawnable blocks in a sphere from pos and prints - // 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); + void stop(); }; \ No newline at end of file diff --git a/bot/external/Botcraft/Examples/2_ChatCommandExample/src/ChatCommandClient.cpp b/bot/external/Botcraft/Examples/2_ChatCommandExample/src/ChatCommandClient.cpp index c4ad7b2..6634e7a 100644 --- a/bot/external/Botcraft/Examples/2_ChatCommandExample/src/ChatCommandClient.cpp +++ b/bot/external/Botcraft/Examples/2_ChatCommandExample/src/ChatCommandClient.cpp @@ -128,8 +128,8 @@ void ChatCommandClient::ProcessChatMsg(const std::vector& splitted_ // possibilities to create a leaf. Note that // only the lambda solution can use default // parameters values - .leaf("go to lambda", [=](ChatCommandClient& 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 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, true, 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 :(") diff --git a/bot/main.cpp b/bot/main.cpp index fa9571a..f96f16b 100644 --- a/bot/main.cpp +++ b/bot/main.cpp @@ -20,10 +20,52 @@ struct Args Args ParseCommandLine(int argc, char* argv[]); -int main(int argc, char* argv[]) -{ - try - { +std::vector split(std::string text, char delim) { + std::string line; + std::vector 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 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 Botcraft::Logger::GetInstance().SetLogLevel(Botcraft::LogLevel::Info); Botcraft::Logger::GetInstance().SetFilename(""); @@ -48,42 +90,17 @@ int main(int argc, char* argv[]) } } -// Botcraft::ConnectionClient client; CommandClient client(false); + client.SetAutoRespawn(true); -// CommandClient client(true); -// + LOG_INFO("Starting connection process"); -// -// client.SetAutoRespawn(true); -// -// LOG_INFO("Starting connection process"); + client.Connect(args.address, args.login); -// -// client.RunBehaviourUntilClosed(); -// client.Disconnect(); + std::thread thread(runSTDINThing, std::ref(client)); -// return 0; - -// 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.RunBehaviourUntilClosed(); //// client.SetAutoRespawn(true); // diff --git a/bot/run.sh b/bot/run.sh index 05cf916..432d996 100755 --- a/bot/run.sh +++ b/bot/run.sh @@ -1,2 +1,3 @@ cd /home/astatin3/Documents/GitHub/meteorbot/bot/bin -./McConnectBot \ No newline at end of file +#./2_ChatCommandExample_d +./McConnectBot diff --git a/meteoraddon/src/main/java/com/astatin3/meteorbot/commands/CommandExample.java b/meteoraddon/src/main/java/com/astatin3/meteorbot/commands/CommandExample.java index df25e0f..040d06e 100644 --- a/meteoraddon/src/main/java/com/astatin3/meteorbot/commands/CommandExample.java +++ b/meteoraddon/src/main/java/com/astatin3/meteorbot/commands/CommandExample.java @@ -1,6 +1,7 @@ package com.astatin3.meteorbot.commands; import com.astatin3.meteorbot.modules.ModuleExample; +import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import meteordevelopment.meteorclient.commands.Command; @@ -23,8 +24,11 @@ public class CommandExample extends Command { ".mb help - Print this help info\n" + ".mb chat - Send chat message\n" + ".mb cmd - Send command (No '/')\n" + - ".mb stop - Stop bot\n" + - ".mb rawsend - Send raw message to bot\n"); + ".mb goto - Go to xyz choord\n" + + ".mb follow - Follow player\n" + + ".mb stop - Stop current action\n" + + ".mb rawsend - Send raw message to bot\n" + + ".mb quit - Make bot leave the game"); } @Override @@ -59,11 +63,39 @@ public class CommandExample extends Command { builder.then(literal("stop").executes(context -> { - module.stopBot(); + module.writeSTDIN("Stop"); + if(module.followMode){ + module.followMode = false; + } 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") .then( argument( @@ -76,5 +108,11 @@ public class CommandExample extends Command { return SINGLE_SUCCESS; })))); + builder.then(literal("follow") + .executes(context -> { + module.startFollowMode(); + return SINGLE_SUCCESS; + })); + } } diff --git a/meteoraddon/src/main/java/com/astatin3/meteorbot/modules/ModuleExample.java b/meteoraddon/src/main/java/com/astatin3/meteorbot/modules/ModuleExample.java index c7984f8..f534bbe 100644 --- a/meteoraddon/src/main/java/com/astatin3/meteorbot/modules/ModuleExample.java +++ b/meteoraddon/src/main/java/com/astatin3/meteorbot/modules/ModuleExample.java @@ -9,6 +9,7 @@ import meteordevelopment.meteorclient.systems.modules.Module; import net.minecraft.util.math.Vec3d; import java.io.*; +import java.util.concurrent.TimeUnit; public class ModuleExample extends Module { @@ -58,7 +59,7 @@ public class ModuleExample extends Module { cmdThread = new Thread(new Runnable() { public void run() { readCMD(); - stopBot(); + quitBot(); } }); cmdThread.start(); @@ -75,8 +76,7 @@ public class ModuleExample extends Module { String line; while ((line = stdout.readLine()) != null) { - if(verboseMode.get()) - info(line); + parseSTDOUT(line); } 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; public void onActivate() { - stopBot(); + quitBot(); startCMD(); } @Override public void onDeactivate() { - stopBot(); + quitBot(); } - public void stopBot(){ + public void quitBot(){ if (cmdThread != null) { writeSTDIN("Disconnect"); cmdThread.interrupt(); @@ -134,17 +150,22 @@ public class ModuleExample extends Module { } } - private void parseResponse(String response){ - if(response.equals("Get Player Pos")){ - Vec3d vel = mc.player.getVelocity(); - writeSTDIN("PlayerPos:" + - mc.player.getX() + "," + - mc.player.getY() + "," + - mc.player.getZ() + "," + - vel.getX() + "," + - vel.getZ() + "," + - vel.getZ() + ","); - } + public boolean followMode = false; + public void startFollowMode(){ + writeSTDIN("startGotoSteamMode"); + followMode = true; + new Thread(new Runnable() { + public void run() { + while(followMode){ + writeSTDIN("goto:"+mc.player.getX()+","+mc.player.getY()+","+mc.player.getZ()); + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + } + } + }).start(); } }