Add scripting system (part 1)

This commit is contained in:
ErrorNoInternet 2023-01-13 16:19:19 +08:00
parent 5c1b59c078
commit 3507acd42d
Signed by untrusted user who does not match committer: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 24 additions and 1 deletions

1
Cargo.lock generated
View File

@ -671,6 +671,7 @@ dependencies = [
"azalea-protocol", "azalea-protocol",
"chrono", "chrono",
"colored", "colored",
"futures",
"serde", "serde",
"strum", "strum",
"strum_macros", "strum_macros",

View File

@ -16,3 +16,4 @@ colored = "2.0.0"
chrono = "0.4.23" chrono = "0.4.23"
strum = "0.24.1" strum = "0.24.1"
strum_macros = "0.24.1" strum_macros = "0.24.1"
futures = "0.3.25"

View File

@ -7,6 +7,7 @@ use azalea_protocol::packets::game::{
self, serverbound_interact_packet::InteractionHand, ServerboundGamePacket, self, serverbound_interact_packet::InteractionHand, ServerboundGamePacket,
}; };
use chrono::{Local, TimeZone}; use chrono::{Local, TimeZone};
use futures::future::{BoxFuture, FutureExt};
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use strum_macros::EnumIter; use strum_macros::EnumIter;
@ -37,6 +38,7 @@ pub enum Command {
Sprint, Sprint,
DropItem, DropItem,
DropStack, DropStack,
Script,
ToggleBotStatusMessages, ToggleBotStatusMessages,
ToggleAlertMessages, ToggleAlertMessages,
Unknown, Unknown,
@ -47,7 +49,7 @@ pub async fn process_command(
executor: &String, executor: &String,
client: &mut Client, client: &mut Client,
state: &mut State, state: &mut State,
) -> String { ) -> BoxFuture<'static, String> {
let mut segments: Vec<String> = command let mut segments: Vec<String> = command
.split(" ") .split(" ")
.map(|segment| segment.to_string()) .map(|segment| segment.to_string())
@ -83,6 +85,7 @@ pub async fn process_command(
"sprint" => command = Command::Sprint, "sprint" => command = Command::Sprint,
"drop_item" => command = Command::DropItem, "drop_item" => command = Command::DropItem,
"drop_stack" => command = Command::DropStack, "drop_stack" => command = Command::DropStack,
"script" => command = Command::Script,
"toggle_alert_messages" => command = Command::ToggleAlertMessages, "toggle_alert_messages" => command = Command::ToggleAlertMessages,
"toggle_bot_status_messages" => command = Command::ToggleBotStatusMessages, "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(); 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 => { Command::ToggleAlertMessages => {
if state.alert_players.lock().unwrap().contains(executor) { if state.alert_players.lock().unwrap().contains(executor) {
let mut players = state.alert_players.lock().unwrap().to_vec(); let mut players = state.alert_players.lock().unwrap().to_vec();