From 3507acd42d497ea327f9a54d3718a3f3fa62aa0e Mon Sep 17 00:00:00 2001
From: ErrorNoInternet <error.nointernet@gmail.com>
Date: Fri, 13 Jan 2023 16:19:19 +0800
Subject: [PATCH] Add scripting system (part 1)

---
 Cargo.lock |  1 +
 Cargo.toml |  1 +
 src/bot.rs | 23 ++++++++++++++++++++++-
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/Cargo.lock b/Cargo.lock
index 6e3e90f..c5d5242 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -671,6 +671,7 @@ dependencies = [
  "azalea-protocol",
  "chrono",
  "colored",
+ "futures",
  "serde",
  "strum",
  "strum_macros",
diff --git a/Cargo.toml b/Cargo.toml
index 48a3453..4b87e07 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,3 +16,4 @@ colored = "2.0.0"
 chrono = "0.4.23"
 strum = "0.24.1"
 strum_macros = "0.24.1"
+futures = "0.3.25"
diff --git a/src/bot.rs b/src/bot.rs
index 24e6914..da92739 100644
--- a/src/bot.rs
+++ b/src/bot.rs
@@ -7,6 +7,7 @@ use azalea_protocol::packets::game::{
     self, serverbound_interact_packet::InteractionHand, ServerboundGamePacket,
 };
 use chrono::{Local, TimeZone};
+use futures::future::{BoxFuture, FutureExt};
 use strum::IntoEnumIterator;
 use strum_macros::EnumIter;
 
@@ -37,6 +38,7 @@ pub enum Command {
     Sprint,
     DropItem,
     DropStack,
+    Script,
     ToggleBotStatusMessages,
     ToggleAlertMessages,
     Unknown,
@@ -47,7 +49,7 @@ pub async fn process_command(
     executor: &String,
     client: &mut Client,
     state: &mut State,
-) -> String {
+) -> BoxFuture<'static, String> {
     let mut segments: Vec<String> = command
         .split(" ")
         .map(|segment| segment.to_string())
@@ -83,6 +85,7 @@ pub async fn process_command(
         "sprint" => command = Command::Sprint,
         "drop_item" => command = Command::DropItem,
         "drop_stack" => command = Command::DropStack,
+        "script" => command = Command::Script,
         "toggle_alert_messages" => command = Command::ToggleAlertMessages,
         "toggle_bot_status_messages" => command = Command::ToggleBotStatusMessages,
         _ => (),
@@ -637,6 +640,24 @@ pub async fn process_command(
             );
             return "I have successfully dropped 1 stack!".to_string();
         }
+        Command::Script => {
+            if segments.len() < 1 {
+                return "Please give me a script file!".to_string();
+            }
+
+            let script = match std::fs::read_to_string(segments[0].to_owned()) {
+                Ok(script) => script,
+                Err(error) => return format!("Unable to read script: {}", error),
+            };
+            async move {
+                for line in script.split("\n") {
+                    process_command(&line.to_string(), &executor, client, state).await;
+                }
+            }
+            .boxed();
+
+            return "Finished executing script!".to_string();
+        }
         Command::ToggleAlertMessages => {
             if state.alert_players.lock().unwrap().contains(executor) {
                 let mut players = state.alert_players.lock().unwrap().to_vec();