Compare commits

..

3 Commits

51 changed files with 2006 additions and 3528 deletions

1
.envrc
View File

@@ -1 +0,0 @@
use flake

View File

@@ -1,17 +0,0 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "monthly"
- package-ecosystem: "nix"
directory: "/"
schedule:
interval: "monthly"
- package-ecosystem: "github-actions"
directory: ".github/workflows"
schedule:
interval: "monthly"

View File

@@ -24,13 +24,13 @@ jobs:
steps: steps:
- name: Clone repository - name: Clone repository
uses: actions/checkout@v6 uses: actions/checkout@v4
- name: Install build dependencies - name: Install build dependencies
run: sudo apt install -y libluajit-5.1-dev mold run: sudo apt install -y libluajit-5.1-dev mold
- name: Set up build cache - name: Set up build cache
uses: actions/cache@v5 uses: actions/cache@v4
with: with:
path: | path: |
~/.cargo/bin/ ~/.cargo/bin/
@@ -46,7 +46,7 @@ jobs:
- run: cargo build --release ${{ matrix.feature.flags }} - run: cargo build --release ${{ matrix.feature.flags }}
- name: Upload build artifacts - name: Upload build artifacts
uses: actions/upload-artifact@v7 uses: actions/upload-artifact@v4
with: with:
name: errornowatcher_${{ matrix.feature.name }}_${{ matrix.os }} name: errornowatcher_${{ matrix.feature.name }}_${{ matrix.os }}
path: target/release/errornowatcher path: target/release/errornowatcher

View File

@@ -15,7 +15,7 @@ jobs:
steps: steps:
- name: Clone repository - name: Clone repository
uses: actions/checkout@v6 uses: actions/checkout@v4
- name: Install taplo - name: Install taplo
uses: uncenter/setup-taplo@v1 uses: uncenter/setup-taplo@v1
@@ -47,13 +47,13 @@ jobs:
steps: steps:
- name: Clone repository - name: Clone repository
uses: actions/checkout@v6 uses: actions/checkout@v4
- name: Install build dependencies - name: Install build dependencies
run: sudo apt install -y libluajit-5.1-dev mold run: sudo apt install -y libluajit-5.1-dev mold
- name: Set up build cache - name: Set up build cache
uses: actions/cache@v5 uses: actions/cache@v4
with: with:
path: | path: |
~/.cargo/bin/ ~/.cargo/bin/
@@ -69,7 +69,7 @@ jobs:
- name: Install components - name: Install components
run: rustup component add clippy rustfmt run: rustup component add clippy rustfmt
- run: cargo clippy ${{ matrix.feature.flags }} -- -D clippy::pedantic - run: cargo clippy ${{ matrix.feature.flags }} -- -D warnings -D clippy::pedantic
- if: always() - if: always()
run: cargo fmt --check run: cargo fmt --check

11
.gitignore vendored
View File

@@ -1,13 +1,2 @@
.luarc.json .luarc.json
target target
# Devenv
.devenv*
devenv.local.nix
devenv.local.yaml
# direnv
.direnv
# pre-commit
.pre-commit-config.yaml

3938
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -15,48 +15,37 @@ codegen-units = 1
lto = true lto = true
strip = true strip = true
[profile.release-no-lto]
inherits = "release"
lto = false
[profile.small]
inherits = "release"
opt-level = "z"
[build-dependencies] [build-dependencies]
built = { version = "0", features = ["git2"] } built = { git = "https://github.com/lukaslueg/built", features = ["git2"] }
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
azalea = "0" azalea = { git = "https://github.com/azalea-rs/azalea" }
bevy_app = "0" bevy_app = "0"
bevy_ecs = "0" bevy_ecs = "0"
bevy_log = "0" bevy_log = "0"
clap = { version = "4", features = ["derive", "string"] } clap = { version = "4", features = ["derive", "string"] }
console-subscriber = { version = "0", optional = true } console-subscriber = { version = "0", optional = true }
ctrlc = "3" ctrlc = { version = "3", features = ["termination"] }
dirs = { version = "6", optional = true } dirs = { version = "6", optional = true }
futures = "0" futures = "0"
futures-locks = "0" futures-locks = "0"
http-body-util = "0" http-body-util = "0"
hyper = { version = "1", features = ["server"] } hyper = { version = "1", features = ["server"] }
hyper-util = "0" hyper-util = "0"
log = "0" log = { version = "0" }
matrix-sdk = { version = "0", features = ["anyhow"], optional = true } matrix-sdk = { version = "0", features = ["anyhow"], optional = true }
mimalloc = { version = "0", optional = true } mimalloc = { version = "0", optional = true }
mlua = { version = "0", features = ["async", "luajit", "send"] } mlua = { version = "0", features = ["async", "luajit", "send"] }
ncr = { version = "0", features = ["cfb8", "ecb", "gcm"] } ncr = { version = "0", features = ["cfb8", "ecb", "gcm"] }
parking_lot = "0" parking_lot = "0"
serde = "1" serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["macros"] }
zip = { version = "8", default-features = false, features = [ zip = { version = "2", default-features = false, features = ["flate2"] }
"deflate",
], optional = true }
[features] [features]
console-subscriber = ["dep:console-subscriber"] console-subscriber = ["dep:console-subscriber"]
default = [] default = ["matrix"]
matrix = ["dep:dirs", "dep:matrix-sdk"] matrix = ["dep:dirs", "dep:matrix-sdk"]
mimalloc = ["dep:mimalloc"] mimalloc = ["dep:mimalloc"]
replay = ["dep:zip"]

View File

@@ -21,13 +21,7 @@ A Minecraft bot with Lua scripting support, written in Rust with [azalea](https:
$ git clone https://github.com/ErrorNoInternet/ErrorNoWatcher $ git clone https://github.com/ErrorNoInternet/ErrorNoWatcher
$ cd ErrorNoWatcher $ cd ErrorNoWatcher
$ cargo build --release $ cargo build --release
$ ./target/release/errornowatcher $ # ./target/release/errornowatcher
```
#### Nix
```sh
nix run github:ErrorNoInternet/ErrorNoWatcher
``` ```
Make sure the `Server` and `Username` globals are defined in `main.lua` before starting the bot. Make sure the `Server` and `Username` globals are defined in `main.lua` before starting the bot.

View File

@@ -1,24 +0,0 @@
{
lib,
pkgs,
rust,
self,
}:
pkgs.rustPlatform.buildRustPackage {
pname = "errornowatcher";
version = self.shortRev or self.dirtyShortRev;
cargoLock.lockFile = ./Cargo.lock;
src = lib.cleanSource ./.;
nativeBuildInputs = with pkgs; [
rust
mold
pkg-config
];
buildInputs = with pkgs; [
luajit
];
}

82
flake.lock generated
View File

@@ -1,82 +0,0 @@
{
"nodes": {
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1772408722,
"narHash": "sha256-rHuJtdcOjK7rAHpHphUb1iCvgkU3GpfvicLMwwnfMT0=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "f20dc5d9b8027381c474144ecabc9034d6a839a3",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1774709303,
"narHash": "sha256-D3Q07BbIA2KnTcSXIqqu9P586uWxN74zNoCH3h2ESHg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8110df5ad7abf5d4c0f6fb0f8f978390e77f9685",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1772328832,
"narHash": "sha256-e+/T/pmEkLP6BHhYjx6GmwP5ivonQQn0bJdH9YrRB+Q=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "c185c7a5e5dd8f9add5b2f8ebeff00888b070742",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"root": {
"inputs": {
"flake-parts": "flake-parts",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1775013181,
"narHash": "sha256-zPrt6oNM1r/RO5bWYaZ3hthfG9vzkr6kQdoqDd5x4Qw=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "e8046c1d9ccadd497c2344d8fa49dab62f22f7be",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View File

@@ -1,66 +0,0 @@
{
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
flake-parts,
nixpkgs,
rust-overlay,
self,
...
}@inputs:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [
"aarch64-linux"
"x86_64-linux"
];
perSystem =
{
pkgs,
self',
system,
...
}:
let
rust = pkgs.rust-bin.nightly.latest.default.override {
extensions = [
"rust-src"
"rust-analyzer-preview"
];
};
in
{
_module.args.pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
devShells.default = pkgs.mkShell {
name = "errornowatcher";
inputsFrom = [ self'.packages.default ];
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [ pkgs.taplo ];
RUST_BACKTRACE = 1;
};
packages = rec {
default = errornowatcher;
errornowatcher = pkgs.callPackage ./. { inherit rust self; };
};
};
};
description = "A Minecraft bot with Lua scripting support";
}

View File

@@ -30,7 +30,7 @@ function auto_fish()
sleep(3000) sleep(3000)
end end
hold_fishing_rod() hold_fishing_rod()
client:start_use_item() client:use_item()
end end
end, "auto-fish_watch-bobber") end, "auto-fish_watch-bobber")
@@ -41,7 +41,7 @@ function auto_fish()
end)[1] end)[1]
if distance(current_bobber.position, particle.position) <= 0.75 then if distance(current_bobber.position, particle.position) <= 0.75 then
FishLastCaught = os.time() FishLastCaught = os.time()
client:start_use_item() client:use_item()
end end
end end
end, "auto-fish") end, "auto-fish")
@@ -54,11 +54,11 @@ function auto_fish()
if os.time() - FishLastCaught >= 60 then if os.time() - FishLastCaught >= 60 then
hold_fishing_rod() hold_fishing_rod()
client:start_use_item() client:use_item()
end end
end, "auto-fish_watchdog") end, "auto-fish_watchdog")
client:start_use_item() client:use_item()
end end
function stop_auto_fish() function stop_auto_fish()
@@ -71,7 +71,7 @@ function stop_auto_fish()
return e.id == FishingBobber.id return e.id == FishingBobber.id
end)[1] then end)[1] then
FishingBobber = nil FishingBobber = nil
client:start_use_item() client:use_item()
end end
end end
@@ -111,10 +111,6 @@ function attack_entities(target_kind, minimum)
end end
function check_food(hunger) function check_food(hunger)
if not hunger then
hunger = client.hunger
end
if hunger.food >= 20 then if hunger.food >= 20 then
return return
end end
@@ -131,6 +127,6 @@ function check_food(hunger)
sleep(1000) sleep(1000)
LastEaten = current_time LastEaten = current_time
end end
client:start_use_item() client:use_item()
end end
end end

View File

@@ -63,7 +63,7 @@ function update_listeners()
end, end,
eat = function() eat = function()
sleep(5000) sleep(5000)
check_food() check_food(client.hunger)
end, end,
}, },
death = { death = {

View File

@@ -35,6 +35,9 @@ end
function steal(item_name) function steal(item_name)
for _, chest_pos in ipairs(client:find_blocks(client.position, get_block_states({ "chest" }))) do for _, chest_pos in ipairs(client:find_blocks(client.position, get_block_states({ "chest" }))) do
client:go_to({ position = chest_pos, radius = 3 }, { type = RADIUS_GOAL }) client:go_to({ position = chest_pos, radius = 3 }, { type = RADIUS_GOAL })
while client.pathfinder.is_calculating or client.pathfinder.is_executing do
sleep(500)
end
client:look_at(chest_pos) client:look_at(chest_pos)
local container = client:open_container_at(chest_pos) local container = client:open_container_at(chest_pos)

View File

@@ -93,6 +93,9 @@ function nether_travel(pos, go_to_opts)
info(string.format("currently in nether, going to %.2f %.2f", nether_pos.x, nether_pos.z)) info(string.format("currently in nether, going to %.2f %.2f", nether_pos.x, nether_pos.z))
client:go_to(nether_pos, { type = XZ_GOAL }) client:go_to(nether_pos, { type = XZ_GOAL })
while client.pathfinder.is_calculating or client.pathfinder.is_executing do
sleep(1000)
end
info("arrived, looking for nearest portal") info("arrived, looking for nearest portal")
local portals_nether = client:find_blocks(client.position, portal_block_states) local portals_nether = client:find_blocks(client.position, portal_block_states)
@@ -141,6 +144,10 @@ function interact_bed()
end end
client:go_to({ position = bed, radius = 2 }, { type = RADIUS_GOAL, options = { without_mining = true } }) client:go_to({ position = bed, radius = 2 }, { type = RADIUS_GOAL, options = { without_mining = true } })
while client.pathfinder.is_calculating or client.pathfinder.is_executing do
sleep(500)
end
client:look_at(bed) client:look_at(bed)
client:block_interact(bed) client:block_interact(bed)
end end

View File

@@ -2,7 +2,7 @@ Server = "localhost"
Username = "ErrorNoWatcher" Username = "ErrorNoWatcher"
HttpAddress = "127.0.0.1:8080" HttpAddress = "127.0.0.1:8080"
Owners = { "ErrorNoInternet" } Owners = { "ErrorNoInternet" }
MatrixOptions = { owners = { "@errornointernet:envs.net" } } Matrix = { Owners = { "@errornointernet:envs.net" } }
for _, module in ipairs({ for _, module in ipairs({
"lib", "lib",

View File

@@ -1,2 +0,0 @@
[toolchain]
channel = "nightly"

View File

@@ -1 +0,0 @@
group_imports = "StdExternalCrate"

View File

@@ -1,8 +1,6 @@
use std::path::PathBuf;
use clap::Parser;
use crate::build_info; use crate::build_info;
use clap::Parser;
use std::path::PathBuf;
/// A Minecraft bot with Lua scripting support /// A Minecraft bot with Lua scripting support
#[derive(Parser)] #[derive(Parser)]

View File

@@ -1,16 +1,11 @@
use crate::{
State,
lua::{eval, exec, reload},
};
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*}; use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
use futures::lock::Mutex; use futures::lock::Mutex;
use mlua::{Error, Result, Table, UserDataRef}; use mlua::{Function, Table};
use ncr::{ use ncr::utils::prepend_header;
encoding::{Base64Encoding, Base64rEncoding, NewBase64rEncoding},
encryption::{CaesarEncryption, Cfb8Encryption, EcbEncryption, Encryption, GcmEncryption},
utils::prepend_header,
};
use crate::{
State, crypt,
lua::{eval, exec, nochatreports::key::AesKey, reload},
};
pub type Ctx = CommandContext<Mutex<CommandSource>>; pub type Ctx = CommandContext<Mutex<CommandSource>>;
@@ -23,26 +18,21 @@ pub struct CommandSource {
impl CommandSource { impl CommandSource {
pub fn reply(&self, message: &str) { pub fn reply(&self, message: &str) {
fn encrypt(options: &Table, plaintext: &str) -> Result<String> {
Ok(crypt!(encrypt, options, &prepend_header(plaintext)))
}
for mut chunk in message for mut chunk in message
.chars() .chars()
.collect::<Vec<char>>() .collect::<Vec<char>>()
.chunks(if self.ncr_options.is_some() { 150 } else { 236 }) .chunks(if self.ncr_options.is_some() { 150 } else { 236 })
.map(|chars| chars.iter().collect::<String>()) .map(|chars| chars.iter().collect::<String>())
{ {
if let Some(ciphertext) = self if let Some(options) = &self.ncr_options
.ncr_options && let Ok(encrypt) = self.state.lua.globals().get::<Function>("ncr_encrypt")
.as_ref() && let Ok(ciphertext) = encrypt.call::<String>((options, prepend_header(&chunk)))
.and_then(|options| encrypt(options, &chunk).ok())
{ {
chunk = ciphertext; chunk = ciphertext;
} }
self.client.chat( self.client.chat(
&(if self.message.is_whisper() &(if self.message.is_whisper()
&& let Some(username) = self.message.sender() && let Some(username) = self.message.username()
{ {
format!("/w {username} {chunk}") format!("/w {username} {chunk}")
} else { } else {
@@ -59,7 +49,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
tokio::spawn(async move { tokio::spawn(async move {
let source = source.lock().await; let source = source.lock().await;
source.reply( source.reply(
&reload(&source.state.lua, source.message.sender()) &reload(&source.state.lua, source.message.username())
.map_or_else(|error| error.to_string(), |()| String::from("ok")), .map_or_else(|error| error.to_string(), |()| String::from("ok")),
); );
}); });
@@ -73,7 +63,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
tokio::spawn(async move { tokio::spawn(async move {
let source = source.lock().await; let source = source.lock().await;
source.reply( source.reply(
&eval(&source.state.lua, &code, source.message.sender()) &eval(&source.state.lua, &code, source.message.username())
.await .await
.unwrap_or_else(|error| error.to_string()), .unwrap_or_else(|error| error.to_string()),
); );
@@ -89,7 +79,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
tokio::spawn(async move { tokio::spawn(async move {
let source = source.lock().await; let source = source.lock().await;
source.reply( source.reply(
&exec(&source.state.lua, &code, source.message.sender()) &exec(&source.state.lua, &code, source.message.username())
.await .await
.map_or_else(|error| error.to_string(), |()| String::from("ok")), .map_or_else(|error| error.to_string(), |()| String::from("ok")),
); );

View File

@@ -1,29 +1,28 @@
use std::net::SocketAddr;
use anyhow::Result;
use azalea::{
brigadier::errors::BuiltInError, prelude::*, protocol::packets::game::ClientboundGamePacket,
};
use hyper::{server::conn::http1, service::service_fn};
use hyper_util::rt::TokioIo;
use log::{debug, error, info, trace};
use mlua::{Function, IntoLuaMulti, Table};
use ncr::utils::trim_header;
use tokio::net::TcpListener;
#[cfg(feature = "matrix")]
use {crate::matrix, std::time::Duration, tokio::time::sleep};
#[cfg(feature = "replay")]
use {crate::replay::recorder::Recorder, anyhow::Context, mlua::Error, std::process::exit};
use crate::{ use crate::{
State, State,
commands::CommandSource, commands::CommandSource,
http::serve, http::serve,
lua::{client, direction::Direction, player::Player, vec3::Vec3}, lua::{client, direction::Direction, player::Player, vec3::Vec3},
particle, particle,
replay::recorder::Recorder,
}; };
use anyhow::{Context, Result};
use azalea::{
brigadier::exceptions::BuiltInExceptions::DispatcherUnknownCommand, prelude::*,
protocol::packets::game::ClientboundGamePacket,
};
use hyper::{server::conn::http1, service::service_fn};
use hyper_util::rt::TokioIo;
use log::{debug, error, info, trace};
use mlua::{Error, Function, IntoLuaMulti, Table};
use ncr::utils::trim_header;
use std::{net::SocketAddr, process::exit};
use tokio::net::TcpListener;
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)] #[cfg(feature = "matrix")]
use crate::matrix;
#[allow(clippy::too_many_lines)]
pub async fn handle_event(client: Client, event: Event, state: State) -> Result<()> { pub async fn handle_event(client: Client, event: Event, state: State) -> Result<()> {
match event { match event {
Event::AddPlayer(player_info) => { Event::AddPlayer(player_info) => {
@@ -32,10 +31,9 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
Event::Chat(message) => { Event::Chat(message) => {
let globals = state.lua.globals(); let globals = state.lua.globals();
let (sender, mut content) = message.split_sender_and_content(); let (sender, mut content) = message.split_sender_and_content();
let uuid = message.sender_uuid().map(|uuid| uuid.to_string()); let uuid = message.uuid().map(|uuid| uuid.to_string());
let is_whisper = message.is_whisper(); let is_whisper = message.is_whisper();
let text = message.message(); let text = message.message();
let html_text = text.to_html();
let ansi_text = text.to_ansi(); let ansi_text = text.to_ansi();
info!("{ansi_text}"); info!("{ansi_text}");
@@ -71,7 +69,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
} }
.into(), .into(),
) )
&& *error.kind() != BuiltInError::DispatcherUnknownCommand && error.type_ != DispatcherUnknownCommand
{ {
CommandSource { CommandSource {
client, client,
@@ -87,7 +85,6 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
let table = state.lua.create_table()?; let table = state.lua.create_table()?;
table.set("text", text.to_string())?; table.set("text", text.to_string())?;
table.set("ansi_text", ansi_text)?; table.set("ansi_text", ansi_text)?;
table.set("html_text", html_text)?;
table.set("sender", sender)?; table.set("sender", sender)?;
table.set("content", content)?; table.set("content", content)?;
table.set("uuid", uuid)?; table.set("uuid", uuid)?;
@@ -97,16 +94,12 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
}) })
.await .await
} }
Event::ConnectionFailed(error) => {
call_listeners(&state, "connection_failed", || Ok(error.to_string())).await
}
Event::Death(packet) => { Event::Death(packet) => {
if let Some(packet) = packet { if let Some(packet) = packet {
call_listeners(&state, "death", || { call_listeners(&state, "death", || {
let message_table = state.lua.create_table()?; let message_table = state.lua.create_table()?;
message_table.set("text", packet.message.to_string())?; message_table.set("text", packet.message.to_string())?;
message_table.set("ansi_text", packet.message.to_ansi())?; message_table.set("ansi_text", packet.message.to_ansi())?;
message_table.set("html_text", packet.message.to_html())?;
let table = state.lua.create_table()?; let table = state.lua.create_table()?;
table.set("message", message_table)?; table.set("message", message_table)?;
table.set("player_id", packet.player_id.0)?; table.set("player_id", packet.player_id.0)?;
@@ -123,7 +116,6 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
let table = state.lua.create_table()?; let table = state.lua.create_table()?;
table.set("text", message.to_string())?; table.set("text", message.to_string())?;
table.set("ansi_text", message.to_ansi())?; table.set("ansi_text", message.to_ansi())?;
table.set("html_text", message.to_html())?;
Ok(table) Ok(table)
}) })
.await .await
@@ -132,7 +124,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
} }
} }
Event::KeepAlive(id) => call_listeners(&state, "keep_alive", || Ok(id)).await, Event::KeepAlive(id) => call_listeners(&state, "keep_alive", || Ok(id)).await,
Event::ReceiveChunk(_) => Ok(()), Event::Login => call_listeners(&state, "login", || Ok(())).await,
Event::RemovePlayer(player_info) => { Event::RemovePlayer(player_info) => {
call_listeners(&state, "remove_player", || Ok(Player::from(player_info))).await call_listeners(&state, "remove_player", || Ok(Player::from(player_info))).await
} }
@@ -190,11 +182,8 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
ClientboundGamePacket::SetPassengers(packet) => { ClientboundGamePacket::SetPassengers(packet) => {
call_listeners(&state, "set_passengers", || { call_listeners(&state, "set_passengers", || {
let table = state.lua.create_table()?; let table = state.lua.create_table()?;
table.set("vehicle", *packet.vehicle)?; table.set("vehicle", packet.vehicle)?;
table.set( table.set("passengers", &*packet.passengers)?;
"passengers",
packet.passengers.iter().map(|id| id.0).collect::<Vec<_>>(),
)?;
Ok(table) Ok(table)
}) })
.await .await
@@ -202,32 +191,28 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
ClientboundGamePacket::SetTime(packet) => { ClientboundGamePacket::SetTime(packet) => {
call_listeners(&state, "set_time", || { call_listeners(&state, "set_time", || {
let table = state.lua.create_table()?; let table = state.lua.create_table()?;
table.set("day_time", packet.day_time)?;
table.set("game_time", packet.game_time)?; table.set("game_time", packet.game_time)?;
table.set("tick_day_time", packet.tick_day_time)?;
Ok(table) Ok(table)
}) })
.await .await
} }
_ => Ok(()), _ => Ok(()),
}, },
Event::Login => {
#[cfg(feature = "matrix")]
matrix_init(&client, state.clone());
call_listeners(&state, "login", || Ok(())).await
}
Event::Init => { Event::Init => {
debug!("received init event"); debug!("received init event");
#[cfg(feature = "replay")]
{
let ecs = client.ecs.clone(); let ecs = client.ecs.clone();
ctrlc::set_handler(move || { ctrlc::set_handler(move || {
ecs.write() ecs.lock()
.remove_resource::<Recorder>() .remove_resource::<Recorder>()
.map(Recorder::finish); .map(Recorder::finish);
exit(0); exit(0);
})?; })?;
};
#[cfg(feature = "matrix")]
matrix_init(&client, state.clone());
let globals = state.lua.globals(); let globals = state.lua.globals();
lua_init(client, &state, &globals).await?; lua_init(client, &state, &globals).await?;
@@ -276,13 +261,11 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
} }
async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()> { async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()> {
#[cfg(feature = "replay")]
{
let ecs = client.ecs.clone(); let ecs = client.ecs.clone();
globals.set( globals.set(
"finish_replay_recording", "finish_replay_recording",
state.lua.create_function_mut(move |_, (): ()| { state.lua.create_function_mut(move |_, (): ()| {
ecs.write() ecs.lock()
.remove_resource::<Recorder>() .remove_resource::<Recorder>()
.context("recording not active") .context("recording not active")
.map_err(Error::external)? .map_err(Error::external)?
@@ -290,7 +273,6 @@ async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()>
.map_err(Error::external) .map_err(Error::external)
})?, })?,
)?; )?;
}
globals.set("client", client::Client(Some(client)))?; globals.set("client", client::Client(Some(client)))?;
call_listeners(state, "init", || Ok(())).await call_listeners(state, "init", || Ok(())).await
} }
@@ -298,15 +280,11 @@ async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()>
#[cfg(feature = "matrix")] #[cfg(feature = "matrix")]
fn matrix_init(client: &Client, state: State) { fn matrix_init(client: &Client, state: State) {
let globals = state.lua.globals(); let globals = state.lua.globals();
if let Ok(options) = globals.get::<Table>("MatrixOptions") { if let Ok(matrix_options) = globals.get::<Table>("MatrixOptions") {
let name = client.username(); let name = client.username();
tokio::spawn(async move { tokio::spawn(async move {
loop { if let Err(error) = matrix::login(state, matrix_options, globals, name).await {
let name = name.clone(); error!("failed to log into matrix account: {error:?}");
if let Err(error) = matrix::login(&state, &options, &globals, name).await {
error!("failed to log into matrix: {error:?}");
}
sleep(Duration::from_secs(10)).await;
} }
}); });
} }

View File

@@ -1,19 +1,20 @@
use azalea::{ use azalea::{
entity::Physics, Vec3,
movement::{KnockbackEvent, handle_knockback}, movement::{KnockbackEvent, KnockbackType},
prelude::*, prelude::Component,
}; };
use bevy_ecs::{observer::On, query::With, system::Query}; use bevy_ecs::{event::EventMutator, query::With, system::Query};
#[derive(Component)] #[derive(Component)]
pub struct AntiKnockback; pub struct AntiKnockback;
pub fn anti_knockback( pub fn anti_knockback(
knockback: On<KnockbackEvent>, mut events: EventMutator<KnockbackEvent>,
entity_query: Query<(), With<AntiKnockback>>, entity_query: Query<(), With<AntiKnockback>>,
handle_knockback_query: Query<&mut Physics>,
) { ) {
if entity_query.get(knockback.entity).is_err() { for event in events.read() {
handle_knockback(knockback, handle_knockback_query); if entity_query.get(event.entity).is_ok() {
event.knockback = KnockbackType::Add(Vec3::default());
}
} }
} }

View File

@@ -1,28 +1,21 @@
#![allow(clippy::needless_pass_by_value)]
pub mod anti_knockback; pub mod anti_knockback;
use azalea::movement::KnockbackEvent; use anti_knockback::anti_knockback;
use bevy_app::{App, Plugin, PostStartup}; use azalea::{movement::handle_knockback, packet::game::process_packet_events};
use bevy_ecs::world::World; use bevy_app::{App, Plugin, PreUpdate};
use bevy_ecs::schedule::IntoSystemConfigs;
pub struct HacksPlugin; pub struct HacksPlugin;
impl Plugin for HacksPlugin { impl Plugin for HacksPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(PostStartup, init_hacks); app.add_systems(
PreUpdate,
anti_knockback
.after(process_packet_events)
.before(handle_knockback),
);
} }
} }
fn init_hacks(ecs: &mut World) {
let observers = ecs
.observers()
.try_get_observers(ecs.event_key::<KnockbackEvent>().unwrap());
let mut to_despawn = Vec::new();
for (observer_entity, _) in observers.unwrap().global_observers() {
to_despawn.push(*observer_entity);
}
for observer_entity in to_despawn {
ecs.despawn(observer_entity);
}
ecs.add_observer(anti_knockback::anti_knockback);
}

View File

@@ -1,14 +1,13 @@
use crate::{
State,
lua::{eval, exec, reload},
};
use http_body_util::{BodyExt, Empty, Full, combinators::BoxBody}; use http_body_util::{BodyExt, Empty, Full, combinators::BoxBody};
use hyper::{ use hyper::{
Error, Method, Request, Response, StatusCode, Error, Method, Request, Response, StatusCode,
body::{Bytes, Incoming}, body::{Bytes, Incoming},
}; };
use crate::{
State,
lua::{eval, exec, reload},
};
pub async fn serve( pub async fn serve(
request: Request<Incoming>, request: Request<Incoming>,
state: State, state: State,
@@ -49,13 +48,13 @@ fn status_code_response(
response response
} }
fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, Error> { fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> {
Full::new(chunk.into()) Full::new(chunk.into())
.map_err(|never| match never {}) .map_err(|never| match never {})
.boxed() .boxed()
} }
fn empty() -> BoxBody<Bytes, Error> { fn empty() -> BoxBody<Bytes, hyper::Error> {
Empty::<Bytes>::new() Empty::<Bytes>::new()
.map_err(|never| match never {}) .map_err(|never| match never {})
.boxed() .boxed()

View File

@@ -1,6 +1,6 @@
use azalea::block::{ use azalea::blocks::{
BlockState, BlockTrait, Block as AzaleaBlock, BlockState,
properties::{ChestKind, Facing, LightLevel}, properties::{ChestType, Facing, LightLevel},
}; };
use mlua::{Function, Lua, Result, Table}; use mlua::{Function, Lua, Result, Table};
@@ -21,7 +21,7 @@ pub fn get_block_from_state(lua: &Lua, state: u32) -> Result<Option<Table>> {
let Ok(state) = BlockState::try_from(state) else { let Ok(state) = BlockState::try_from(state) else {
return Ok(None); return Ok(None);
}; };
let block: Box<dyn BlockTrait> = state.into(); let block: Box<dyn AzaleaBlock> = state.into();
let behavior = block.behavior(); let behavior = block.behavior();
let table = lua.create_table()?; let table = lua.create_table()?;
@@ -46,10 +46,10 @@ pub async fn get_block_states(
for block in for block in
(u32::MIN..u32::MAX).map_while(|possible_id| BlockState::try_from(possible_id).ok()) (u32::MIN..u32::MAX).map_while(|possible_id| BlockState::try_from(possible_id).ok())
{ {
if block_name == Into::<Box<dyn BlockTrait>>::into(block).id() if block_name == Into::<Box<dyn AzaleaBlock>>::into(block).id()
&& (if let Some(filter_fn) = &filter_fn { && (if let Some(filter_fn) = &filter_fn {
let table = lua.create_table()?; let table = lua.create_table()?;
table.set("chest_kind", block.property::<ChestKind>().map(|v| v as u8))?; table.set("chest_type", block.property::<ChestType>().map(|v| v as u8))?;
table.set("facing", block.property::<Facing>().map(|v| v as u8))?; table.set("facing", block.property::<Facing>().map(|v| v as u8))?;
table.set( table.set(
"light_level", "light_level",
@@ -60,7 +60,7 @@ pub async fn get_block_states(
true true
}) })
{ {
matched.push(block.id()); matched.push(block.id);
} }
} }
} }

View File

@@ -1,20 +1,19 @@
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
use azalea::{ use azalea::{
BlockPos, BlockPos,
entity::inventory::Inventory, inventory::{Inventory, Menu, Player, SlotList},
inventory::{Menu, Player, SlotList}, prelude::ContainerClientExt,
protocol::packets::game::ServerboundSetCarriedItem, protocol::packets::game::ServerboundSetCarriedItem,
}; };
use log::error;
use mlua::{Lua, Result, UserDataRef, Value}; use mlua::{Lua, Result, UserDataRef, Value};
use super::{Client, Container, ContainerRef, ItemStack, Vec3}; pub fn container(_lua: &Lua, client: &Client) -> Result<Option<ContainerRef>> {
use crate::unpack; Ok(client.get_open_container().map(ContainerRef))
pub fn container(_lua: &Lua, client: &Client) -> Result<ContainerRef> {
Ok(ContainerRef(client.get_inventory()))
} }
pub fn held_item(_lua: &Lua, client: &Client) -> Result<ItemStack> { pub fn held_item(_lua: &Lua, client: &Client) -> Result<ItemStack> {
Ok(ItemStack(client.get_held_item())) Ok(ItemStack(client.component::<Inventory>().held_item()))
} }
pub fn held_slot(_lua: &Lua, client: &Client) -> Result<u8> { pub fn held_slot(_lua: &Lua, client: &Client) -> Result<u8> {
@@ -96,10 +95,9 @@ pub async fn open_container_at(
client: UserDataRef<Client>, client: UserDataRef<Client>,
position: Vec3, position: Vec3,
) -> Result<Option<Container>> { ) -> Result<Option<Container>> {
let client = unpack!(client);
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
Ok(client Ok(client
.clone()
.open_container_at(BlockPos::new( .open_container_at(BlockPos::new(
position.x as i32, position.x as i32,
position.y as i32, position.y as i32,
@@ -109,7 +107,7 @@ pub async fn open_container_at(
.map(Container)) .map(Container))
} }
pub fn open_inventory(_lua: &Lua, client: &Client, (): ()) -> Result<Option<Container>> { pub fn open_inventory(_lua: &Lua, client: &mut Client, _: ()) -> Result<Option<Container>> {
Ok(client.open_inventory().map(Container)) Ok(client.open_inventory().map(Container))
} }
@@ -118,14 +116,20 @@ pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> {
return Ok(()); return Ok(());
} }
client.query_self::<&mut Inventory, _>(|mut inventory| { {
if inventory.selected_hotbar_slot != slot { let mut ecs = client.ecs.lock();
inventory.selected_hotbar_slot = slot; let mut inventory = client.query::<&mut Inventory>(&mut ecs);
if inventory.selected_hotbar_slot == slot {
return Ok(());
} }
}); inventory.selected_hotbar_slot = slot;
client.write_packet(ServerboundSetCarriedItem { };
if let Err(error) = client.write_packet(ServerboundSetCarriedItem {
slot: u16::from(slot), slot: u16::from(slot),
}); }) {
error!("failed to send SetCarriedItem packet: {error:?}");
}
Ok(()) Ok(())
} }

View File

@@ -1,21 +1,18 @@
use super::{Client, Vec3};
use azalea::{ use azalea::{
BlockPos, BlockPos, BotClientExt,
core::entity_id::MinecraftEntityId,
protocol::packets::game::{ServerboundUseItem, s_interact::InteractionHand}, protocol::packets::game::{ServerboundUseItem, s_interact::InteractionHand},
world::MinecraftEntityId,
}; };
use log::error;
use mlua::{Lua, Result, UserDataRef}; use mlua::{Lua, Result, UserDataRef};
use super::{Client, Vec3}; pub fn attack(_lua: &Lua, client: &mut Client, entity_id: i32) -> Result<()> {
use crate::unpack; client.attack(MinecraftEntityId(entity_id));
pub fn attack(_lua: &Lua, client: &Client, entity_id: i32) -> Result<()> {
if let Some(entity) = client.entity_id_by_minecraft_id(MinecraftEntityId(entity_id)) {
client.attack(entity);
}
Ok(()) Ok(())
} }
pub fn block_interact(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> { pub fn block_interact(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> {
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
client.block_interact(BlockPos::new( client.block_interact(BlockPos::new(
position.x as i32, position.x as i32,
@@ -30,10 +27,9 @@ pub fn has_attack_cooldown(_lua: &Lua, client: &Client) -> Result<bool> {
} }
pub async fn mine(_lua: Lua, client: UserDataRef<Client>, position: Vec3) -> Result<()> { pub async fn mine(_lua: Lua, client: UserDataRef<Client>, position: Vec3) -> Result<()> {
let client = unpack!(client);
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
client client
.clone()
.mine(BlockPos::new( .mine(BlockPos::new(
position.x as i32, position.x as i32,
position.y as i32, position.y as i32,
@@ -43,12 +39,12 @@ pub async fn mine(_lua: Lua, client: UserDataRef<Client>, position: Vec3) -> Res
Ok(()) Ok(())
} }
pub fn set_mining(_lua: &Lua, client: &Client, state: bool) -> Result<()> { pub fn set_mining(_lua: &Lua, client: &Client, mining: bool) -> Result<()> {
client.left_click_mine(state); client.left_click_mine(mining);
Ok(()) Ok(())
} }
pub fn start_mining(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> { pub fn start_mining(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> {
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
client.start_mining(BlockPos::new( client.start_mining(BlockPos::new(
position.x as i32, position.x as i32,
@@ -58,16 +54,18 @@ pub fn start_mining(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
Ok(()) Ok(())
} }
pub fn start_use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> { pub fn use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> {
let direction = client.direction(); let direction = client.direction();
client.write_packet(ServerboundUseItem { if let Err(error) = client.write_packet(ServerboundUseItem {
hand: match hand { hand: match hand {
Some(1) => InteractionHand::OffHand, Some(1) => InteractionHand::OffHand,
_ => InteractionHand::MainHand, _ => InteractionHand::MainHand,
}, },
seq: 0, sequence: 0,
x_rot: direction.x_rot(), yaw: direction.0,
y_rot: direction.y_rot(), pitch: direction.1,
}); }) {
error!("failed to send UseItem packet: {error:?}");
}
Ok(()) Ok(())
} }

View File

@@ -6,17 +6,15 @@ mod movement;
mod state; mod state;
mod world; mod world;
use std::ops::Deref;
use azalea::{Client as AzaleaClient, core::entity_id::MinecraftEntityId};
use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods};
use super::{ use super::{
container::{Container, ContainerRef, item_stack::ItemStack}, container::{Container, ContainerRef, item_stack::ItemStack},
direction::Direction, direction::Direction,
player::Player, player::Player,
vec3::Vec3, vec3::Vec3,
}; };
use azalea::{Client as AzaleaClient, world::MinecraftEntityId};
use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods};
use std::ops::{Deref, DerefMut};
pub struct Client(pub Option<AzaleaClient>); pub struct Client(pub Option<AzaleaClient>);
@@ -28,15 +26,19 @@ impl Deref for Client {
} }
} }
impl DerefMut for Client {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.as_mut().expect("should have received init event")
}
}
impl UserData for Client { impl UserData for Client {
fn add_fields<F: UserDataFields<Self>>(f: &mut F) { fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
f.add_field_method_get("air_supply", state::air_supply); f.add_field_method_get("air_supply", state::air_supply);
f.add_field_method_get("container", container::container); f.add_field_method_get("container", container::container);
f.add_field_method_get("dimension", world::dimension); f.add_field_method_get("dimension", world::dimension);
f.add_field_method_get("direction", movement::direction); f.add_field_method_get("direction", movement::direction);
f.add_field_method_get("experience", state::experience);
f.add_field_method_get("eye_position", movement::eye_position); f.add_field_method_get("eye_position", movement::eye_position);
f.add_field_method_get("go_to_reached", movement::go_to_reached);
f.add_field_method_get("has_attack_cooldown", interaction::has_attack_cooldown); f.add_field_method_get("has_attack_cooldown", interaction::has_attack_cooldown);
f.add_field_method_get("health", state::health); f.add_field_method_get("health", state::health);
f.add_field_method_get("held_item", container::held_item); f.add_field_method_get("held_item", container::held_item);
@@ -47,6 +49,7 @@ impl UserData for Client {
f.add_field_method_get("menu", container::menu); f.add_field_method_get("menu", container::menu);
f.add_field_method_get("pathfinder", movement::pathfinder); f.add_field_method_get("pathfinder", movement::pathfinder);
f.add_field_method_get("position", movement::position); f.add_field_method_get("position", movement::position);
f.add_field_method_get("score", state::score);
f.add_field_method_get("tab_list", tab_list); f.add_field_method_get("tab_list", tab_list);
f.add_field_method_get("username", username); f.add_field_method_get("username", username);
f.add_field_method_get("uuid", uuid); f.add_field_method_get("uuid", uuid);
@@ -61,32 +64,30 @@ impl UserData for Client {
m.add_async_method("mine", interaction::mine); m.add_async_method("mine", interaction::mine);
m.add_async_method("open_container_at", container::open_container_at); m.add_async_method("open_container_at", container::open_container_at);
m.add_async_method("set_client_information", state::set_client_information); m.add_async_method("set_client_information", state::set_client_information);
m.add_async_method("start_go_to", movement::start_go_to);
m.add_async_method("wait_until_goal_reached", movement::wait_until_goal_reached);
m.add_method("attack", interaction::attack);
m.add_method("best_tool_for_block", world::best_tool_for_block); m.add_method("best_tool_for_block", world::best_tool_for_block);
m.add_method("block_interact", interaction::block_interact);
m.add_method("chat", chat); m.add_method("chat", chat);
m.add_method("disconnect", disconnect); m.add_method("disconnect", disconnect);
m.add_method("find_blocks", world::find::blocks); m.add_method("find_blocks", world::find::blocks);
m.add_method("get_block_state", world::get_block_state); m.add_method("get_block_state", world::get_block_state);
m.add_method("get_fluid_state", world::get_fluid_state); m.add_method("get_fluid_state", world::get_fluid_state);
m.add_method("jump", movement::jump);
m.add_method("look_at", movement::look_at);
m.add_method("open_inventory", container::open_inventory);
m.add_method("set_component", state::set_component); m.add_method("set_component", state::set_component);
m.add_method("set_direction", movement::set_direction);
m.add_method("set_held_slot", container::set_held_slot); m.add_method("set_held_slot", container::set_held_slot);
m.add_method("set_jumping", movement::set_jumping);
m.add_method("set_mining", interaction::set_mining); m.add_method("set_mining", interaction::set_mining);
m.add_method("set_position", movement::set_position); m.add_method("set_position", movement::set_position);
m.add_method("set_sneaking", movement::set_sneaking); m.add_method("set_sneaking", movement::set_sneaking);
m.add_method("sprint", movement::sprint);
m.add_method("start_mining", interaction::start_mining);
m.add_method("start_use_item", interaction::start_use_item);
m.add_method("stop_pathfinding", movement::stop_pathfinding); m.add_method("stop_pathfinding", movement::stop_pathfinding);
m.add_method("stop_sleeping", movement::stop_sleeping); m.add_method("stop_sleeping", movement::stop_sleeping);
m.add_method("walk", movement::walk); m.add_method("use_item", interaction::use_item);
m.add_method_mut("attack", interaction::attack);
m.add_method_mut("block_interact", interaction::block_interact);
m.add_method_mut("jump", movement::jump);
m.add_method_mut("look_at", movement::look_at);
m.add_method_mut("open_inventory", container::open_inventory);
m.add_method_mut("set_direction", movement::set_direction);
m.add_method_mut("set_jumping", movement::set_jumping);
m.add_method_mut("sprint", movement::sprint);
m.add_method_mut("start_mining", interaction::start_mining);
m.add_method_mut("walk", movement::walk);
} }
} }
@@ -95,7 +96,7 @@ fn chat(_lua: &Lua, client: &Client, message: String) -> Result<()> {
Ok(()) Ok(())
} }
fn disconnect(_lua: &Lua, client: &Client, (): ()) -> Result<()> { fn disconnect(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
client.disconnect(); client.disconnect();
Ok(()) Ok(())
} }
@@ -115,12 +116,3 @@ fn username(_lua: &Lua, client: &Client) -> Result<String> {
fn uuid(_lua: &Lua, client: &Client) -> Result<String> { fn uuid(_lua: &Lua, client: &Client) -> Result<String> {
Ok(client.uuid().to_string()) Ok(client.uuid().to_string())
} }
#[macro_export]
macro_rules! unpack {
($client:ident) => {{
let inner = (**$client).clone();
drop($client);
inner
}};
}

View File

@@ -1,135 +1,23 @@
use super::{Client, Direction, Vec3};
use azalea::{ use azalea::{
BlockPos, Client as AzaleaClient, SprintDirection, WalkDirection, BlockPos, BotClientExt, SprintDirection, WalkDirection,
core::{entity_id::MinecraftEntityId, hit_result::HitResult},
entity::Position, entity::Position,
interact::pick::HitResultComponent, interact::HitResultComponent,
pathfinder::{ pathfinder::{
ExecutingPath, Pathfinder, PathfinderClientExt, PathfinderOpts, ExecutingPath, GotoEvent, Pathfinder, PathfinderClientExt,
goals::{BlockPosGoal, Goal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal}, goals::{BlockPosGoal, Goal, InverseGoal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal},
}, },
protocol::packets::game::{ServerboundPlayerCommand, s_player_command::Action}, protocol::packets::game::{ServerboundPlayerCommand, s_player_command::Action},
world::MinecraftEntityId,
}; };
use log::error;
use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value}; use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value};
use super::{Client, Direction, Vec3};
use crate::unpack;
#[derive(Debug)]
struct AnyGoal(Box<dyn Goal>);
impl Goal for AnyGoal {
fn success(&self, n: BlockPos) -> bool {
self.0.success(n)
}
fn heuristic(&self, n: BlockPos) -> f32 {
self.0.heuristic(n)
}
}
#[allow(clippy::cast_possible_truncation)]
fn to_goal(lua: &Lua, client: &AzaleaClient, data: Table, kind: u8) -> Result<AnyGoal> {
let goal: Box<dyn Goal> = match kind {
1 => {
let pos = Vec3::from_lua(data.get("position")?, lua)?;
Box::new(RadiusGoal {
pos: azalea::Vec3::new(pos.x, pos.y, pos.z),
radius: data.get("radius")?,
})
}
2 => {
let distance = data.get("distance").unwrap_or(4.5);
let pos = Vec3::from_lua(Value::Table(data), lua)?;
Box::new(ReachBlockPosGoal::new_with_distance(
BlockPos::new(pos.x as i32, pos.y as i32, pos.z as i32),
distance,
client.world().read().chunks.clone(),
))
}
3 => Box::new(XZGoal {
x: data.get("x")?,
z: data.get("z")?,
}),
4 => Box::new(YGoal { y: data.get("y")? }),
_ => {
let pos = Vec3::from_lua(Value::Table(data), lua)?;
Box::new(BlockPosGoal(BlockPos::new(
pos.x as i32,
pos.y as i32,
pos.z as i32,
)))
}
};
Ok(AnyGoal(goal))
}
pub fn go_to_reached(_lua: &Lua, client: &Client) -> Result<bool> {
Ok(client.is_goto_target_reached())
}
pub async fn wait_until_goal_reached(_lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<()> {
let client = unpack!(client);
client.wait_until_goto_target_reached().await;
Ok(())
}
pub async fn go_to(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Table, Option<Table>),
) -> Result<()> {
let client = unpack!(client);
let metadata = metadata.unwrap_or(lua.create_table()?);
let options = metadata.get("options").unwrap_or(lua.create_table()?);
let goal = to_goal(
&lua,
&client,
data,
metadata.get("type").unwrap_or_default(),
)?;
client
.goto_with_opts(
goal,
PathfinderOpts::new()
.allow_mining(!options.get::<bool>("without_mining").unwrap_or_default()),
)
.await;
Ok(())
}
pub async fn start_go_to(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Table, Option<Table>),
) -> Result<()> {
let client = unpack!(client);
let metadata = metadata.unwrap_or(lua.create_table()?);
let options = metadata.get("options").unwrap_or(lua.create_table()?);
let goal = to_goal(
&lua,
&client,
data,
metadata.get("type").unwrap_or_default(),
)?;
client.start_goto_with_opts(
goal,
PathfinderOpts::new()
.allow_mining(!options.get::<bool>("without_mining").unwrap_or_default()),
);
let _ = client.get_tick_broadcaster().recv().await;
Ok(())
}
pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> { pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
let direction = client.direction(); let direction = client.direction();
Ok(Direction { Ok(Direction {
y: direction.y_rot(), y: direction.0,
x: direction.x_rot(), x: direction.1,
}) })
} }
@@ -137,28 +25,96 @@ pub fn eye_position(_lua: &Lua, client: &Client) -> Result<Vec3> {
Ok(Vec3::from(client.eye_position())) Ok(Vec3::from(client.eye_position()))
} }
pub fn jump(_lua: &Lua, client: &Client, (): ()) -> Result<()> { pub async fn go_to(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Table, Option<Table>),
) -> Result<()> {
fn goto_with_options<G: Goal + Send + Sync + 'static>(
client: &Client,
options: &Table,
goal: G,
) {
if options.get("without_mining").unwrap_or_default() {
client.goto_without_mining(goal);
} else {
client.goto(goal);
}
}
let table = metadata.unwrap_or(lua.create_table()?);
let goal_type = table.get("type").unwrap_or_default();
let options = table.get("options").unwrap_or(lua.create_table()?);
macro_rules! goto {
($goal:expr) => {
if options.get("inverse").unwrap_or_default() {
goto_with_options(&client, &options, InverseGoal($goal));
} else {
goto_with_options(&client, &options, $goal);
}
};
}
#[allow(clippy::cast_possible_truncation)]
match goal_type {
1 => {
let p = Vec3::from_lua(data.get("position")?, &lua)?;
goto!(RadiusGoal {
pos: azalea::Vec3::new(p.x, p.y, p.z),
radius: data.get("radius")?,
});
}
2 => {
let p = Vec3::from_lua(Value::Table(data), &lua)?;
goto!(ReachBlockPosGoal {
pos: BlockPos::new(p.x as i32, p.y as i32, p.z as i32),
chunk_storage: client.world().read().chunks.clone(),
});
}
3 => {
goto!(XZGoal {
x: data.get("x")?,
z: data.get("z")?,
});
}
4 => goto!(YGoal { y: data.get("y")? }),
_ => {
let p = Vec3::from_lua(Value::Table(data), &lua)?;
goto!(BlockPosGoal(BlockPos::new(
p.x as i32, p.y as i32, p.z as i32
)));
}
}
while client.get_tick_broadcaster().recv().await.is_ok() {
if client.ecs.lock().get::<GotoEvent>(client.entity).is_none() {
break;
}
}
Ok(())
}
pub fn jump(_lua: &Lua, client: &mut Client, _: ()) -> Result<()> {
client.jump(); client.jump();
Ok(()) Ok(())
} }
pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> { pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
Ok( let result = client.component::<HitResultComponent>();
if let HitResult::Block(ref result) = **client.component::<HitResultComponent>() { Ok(if result.miss {
None
} else {
let table = lua.create_table()?; let table = lua.create_table()?;
table.set("direction", Vec3::from(result.direction.normal()))?;
table.set("inside", result.inside)?;
table.set("location", Vec3::from(result.location))?;
table.set("position", Vec3::from(result.block_pos))?; table.set("position", Vec3::from(result.block_pos))?;
table.set("inside", result.inside)?;
table.set("world_border", result.world_border)?; table.set("world_border", result.world_border)?;
Some(table) Some(table)
} else { })
None
},
)
} }
pub fn look_at(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> { pub fn look_at(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> {
client.look_at(azalea::Vec3::new(position.x, position.y, position.z)); client.look_at(azalea::Vec3::new(position.x, position.y, position.z));
Ok(()) Ok(())
} }
@@ -177,8 +133,8 @@ pub fn pathfinder(lua: &Lua, client: &Client) -> Result<Table> {
Vec3::from(pathfinder.last_reached_node), Vec3::from(pathfinder.last_reached_node),
)?; )?;
table.set( table.set(
"ticks_since_last_node_reached", "last_node_reach_elapsed",
pathfinder.ticks_since_last_node_reached, pathfinder.last_node_reached_at.elapsed().as_millis(),
)?; )?;
table.set("is_path_partial", pathfinder.is_path_partial)?; table.set("is_path_partial", pathfinder.is_path_partial)?;
true true
@@ -190,34 +146,44 @@ pub fn pathfinder(lua: &Lua, client: &Client) -> Result<Table> {
} }
pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> { pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {
Ok(Vec3::from(*client.component::<Position>())) Ok(Vec3::from(&client.component::<Position>()))
} }
pub fn set_direction(_lua: &Lua, client: &Client, direction: Direction) -> Result<()> { pub fn set_direction(_lua: &Lua, client: &mut Client, direction: Direction) -> Result<()> {
client.set_direction(direction.y, direction.x); client.set_direction(direction.y, direction.x);
Ok(()) Ok(())
} }
pub fn set_jumping(_lua: &Lua, client: &Client, jumping: bool) -> Result<()> { pub fn set_jumping(_lua: &Lua, client: &mut Client, jumping: bool) -> Result<()> {
client.set_jumping(jumping); client.set_jumping(jumping);
Ok(()) Ok(())
} }
pub fn set_position(_lua: &Lua, client: &Client, new_pos: Vec3) -> Result<()> { pub fn set_position(_lua: &Lua, client: &Client, new_position: Vec3) -> Result<()> {
client.query_self::<&mut Position, _>(|mut pos| { let mut ecs = client.ecs.lock();
pos.x = new_pos.x; let mut position = client.query::<&mut Position>(&mut ecs);
pos.y = new_pos.y; position.x = new_position.x;
pos.z = new_pos.z; position.y = new_position.y;
}); position.z = new_position.z;
Ok(()) Ok(())
} }
pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> { pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
client.set_crouching(sneaking); if let Err(error) = client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
action: if sneaking {
Action::PressShiftKey
} else {
Action::ReleaseShiftKey
},
data: 0,
}) {
error!("failed to send PlayerCommand packet: {error:?}");
}
Ok(()) Ok(())
} }
pub fn sprint(_lua: &Lua, client: &Client, direction: u8) -> Result<()> { pub fn sprint(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> {
client.sprint(match direction { client.sprint(match direction {
5 => SprintDirection::ForwardRight, 5 => SprintDirection::ForwardRight,
6 => SprintDirection::ForwardLeft, 6 => SprintDirection::ForwardLeft,
@@ -226,21 +192,23 @@ pub fn sprint(_lua: &Lua, client: &Client, direction: u8) -> Result<()> {
Ok(()) Ok(())
} }
pub fn stop_pathfinding(_lua: &Lua, client: &Client, (): ()) -> Result<()> { pub fn stop_pathfinding(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
client.stop_pathfinding(); client.stop_pathfinding();
Ok(()) Ok(())
} }
pub fn stop_sleeping(_lua: &Lua, client: &Client, (): ()) -> Result<()> { pub fn stop_sleeping(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
client.write_packet(ServerboundPlayerCommand { if let Err(error) = client.write_packet(ServerboundPlayerCommand {
id: *client.component::<MinecraftEntityId>(), id: client.component::<MinecraftEntityId>(),
action: Action::StopSleeping, action: Action::StopSleeping,
data: 0, data: 0,
}); }) {
error!("failed to send PlayerCommand packet: {error:?}");
}
Ok(()) Ok(())
} }
pub fn walk(_lua: &Lua, client: &Client, direction: u8) -> Result<()> { pub fn walk(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> {
client.walk(match direction { client.walk(match direction {
1 => WalkDirection::Forward, 1 => WalkDirection::Forward,
2 => WalkDirection::Backward, 2 => WalkDirection::Backward,

View File

@@ -1,25 +1,18 @@
use crate::hacks::anti_knockback::AntiKnockback;
use super::Client;
use azalea::{ use azalea::{
ClientInformation, entity::metadata::AirSupply, pathfinder::debug::PathfinderDebugParticles, ClientInformation,
entity::metadata::{AirSupply, Score},
pathfinder::PathfinderDebugParticles,
protocol::common::client_information::ModelCustomization, protocol::common::client_information::ModelCustomization,
}; };
use mlua::{Error, Lua, Result, Table, UserDataRef}; use mlua::{Error, Lua, Result, Table, UserDataRef};
use super::Client;
use crate::{hacks::anti_knockback::AntiKnockback, unpack};
pub fn air_supply(_lua: &Lua, client: &Client) -> Result<i32> { pub fn air_supply(_lua: &Lua, client: &Client) -> Result<i32> {
Ok(client.component::<AirSupply>().0) Ok(client.component::<AirSupply>().0)
} }
pub fn experience(lua: &Lua, client: &Client) -> Result<Table> {
let experience = client.experience();
let table = lua.create_table()?;
table.set("progress", experience.progress)?;
table.set("total", experience.total)?;
table.set("level", experience.level)?;
Ok(table)
}
pub fn health(_lua: &Lua, client: &Client) -> Result<f32> { pub fn health(_lua: &Lua, client: &Client) -> Result<f32> {
Ok(client.health()) Ok(client.health())
} }
@@ -32,33 +25,36 @@ pub fn hunger(lua: &Lua, client: &Client) -> Result<Table> {
Ok(table) Ok(table)
} }
pub fn score(_lua: &Lua, client: &Client) -> Result<i32> {
Ok(client.component::<Score>().0)
}
pub async fn set_client_information( pub async fn set_client_information(
_lua: Lua, _lua: Lua,
client: UserDataRef<Client>, client: UserDataRef<Client>,
info: Table, info: Table,
) -> Result<()> { ) -> Result<()> {
let client = unpack!(client);
let get_bool = |table: &Table, name| table.get(name).unwrap_or(true); let get_bool = |table: &Table, name| table.get(name).unwrap_or(true);
client.set_client_information(ClientInformation { client
.set_client_information(ClientInformation {
allows_listing: info.get("allows_listing")?, allows_listing: info.get("allows_listing")?,
model_customization: info model_customization: info
.get::<Table>("model_customization") .get::<Table>("model_customization")
.as_ref()
.map(|t| ModelCustomization { .map(|t| ModelCustomization {
cape: get_bool(t, "cape"), cape: get_bool(&t, "cape"),
jacket: get_bool(t, "jacket"), jacket: get_bool(&t, "jacket"),
left_sleeve: get_bool(t, "left_sleeve"), left_sleeve: get_bool(&t, "left_sleeve"),
right_sleeve: get_bool(t, "right_sleeve"), right_sleeve: get_bool(&t, "right_sleeve"),
left_pants: get_bool(t, "left_pants"), left_pants: get_bool(&t, "left_pants"),
right_pants: get_bool(t, "right_pants"), right_pants: get_bool(&t, "right_pants"),
hat: get_bool(t, "hat"), hat: get_bool(&t, "hat"),
}) })
.unwrap_or_default(), .unwrap_or_default(),
view_distance: info.get("view_distance").unwrap_or(8), view_distance: info.get("view_distance").unwrap_or(8),
..ClientInformation::default() ..ClientInformation::default()
}); })
Ok(()) .await
.map_err(Error::external)
} }
pub fn set_component( pub fn set_component(
@@ -68,7 +64,7 @@ pub fn set_component(
) -> Result<()> { ) -> Result<()> {
macro_rules! set { macro_rules! set {
($name:ident) => {{ ($name:ident) => {{
let mut ecs = client.ecs.write(); let mut ecs = client.ecs.lock();
let mut entity = ecs.entity_mut(client.entity); let mut entity = ecs.entity_mut(client.entity);
if enabled.unwrap_or(true) { if enabled.unwrap_or(true) {
entity.insert($name) entity.insert($name)

View File

@@ -1,17 +1,16 @@
use super::{Client, Direction, Vec3};
use azalea::{ use azalea::{
BlockPos, BlockPos,
block::{BlockState, BlockStates}, blocks::{BlockState, BlockStates},
ecs::query::{With, Without}, ecs::query::{With, Without},
entity::{ entity::{
Dead, EntityKindComponent, EntityUuid, LookDirection, Pose, Position as AzaleaPosition, Dead, EntityKind, EntityUuid, LookDirection, Pose, Position as AzaleaPosition,
metadata::{CustomName, Owneruuid, Player}, metadata::{CustomName, Owneruuid, Player},
}, },
world::MinecraftEntityId,
}; };
use mlua::{Function, Lua, Result, Table, UserDataRef}; use mlua::{Function, Lua, Result, Table, UserDataRef};
use super::{Client, Direction, Vec3};
use crate::{lua::client::MinecraftEntityId, unpack};
pub fn blocks( pub fn blocks(
_lua: &Lua, _lua: &Lua,
client: &Client, client: &Client,
@@ -28,10 +27,7 @@ pub fn blocks(
nearest_to.z as i32, nearest_to.z as i32,
), ),
&BlockStates { &BlockStates {
set: block_states set: block_states.iter().map(|&id| BlockState { id }).collect(),
.into_iter()
.flat_map(BlockState::try_from)
.collect(),
}, },
) )
.map(Vec3::from) .map(Vec3::from)
@@ -39,8 +35,6 @@ pub fn blocks(
} }
pub async fn all_entities(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> { pub async fn all_entities(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
let client = unpack!(client);
let mut matched = Vec::with_capacity(256); let mut matched = Vec::with_capacity(256);
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
get_entities!(client) get_entities!(client)
@@ -67,8 +61,6 @@ pub async fn entities(
client: UserDataRef<Client>, client: UserDataRef<Client>,
filter_fn: Function, filter_fn: Function,
) -> Result<Vec<Table>> { ) -> Result<Vec<Table>> {
let client = unpack!(client);
let mut matched = Vec::new(); let mut matched = Vec::new();
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
get_entities!(client) get_entities!(client)
@@ -93,8 +85,6 @@ pub async fn entities(
} }
pub async fn all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> { pub async fn all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
let client = unpack!(client);
let mut matched = Vec::new(); let mut matched = Vec::new();
for (id, uuid, kind, position, direction, pose) in get_players!(client) { for (id, uuid, kind, position, direction, pose) in get_players!(client) {
let table = lua.create_table()?; let table = lua.create_table()?;
@@ -114,8 +104,6 @@ pub async fn players(
client: UserDataRef<Client>, client: UserDataRef<Client>,
filter_fn: Function, filter_fn: Function,
) -> Result<Vec<Table>> { ) -> Result<Vec<Table>> {
let client = unpack!(client);
let mut matched = Vec::new(); let mut matched = Vec::new();
for (id, uuid, kind, position, direction, pose) in get_players!(client) { for (id, uuid, kind, position, direction, pose) in get_players!(client) {
let table = lua.create_table()?; let table = lua.create_table()?;

View File

@@ -2,24 +2,20 @@
mod queries; mod queries;
pub mod find; pub mod find;
use azalea::{BlockPos, block::BlockState, world::WorldName};
use mlua::{Lua, Result, Table, Value};
use super::{Client, Direction, Vec3}; use super::{Client, Direction, Vec3};
use azalea::{BlockPos, auto_tool::AutoToolClientExt, blocks::BlockState, world::InstanceName};
use mlua::{Lua, Result, Table};
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Value> { pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Table> {
let Ok(block) = BlockState::try_from(block_state) else { let result = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
return Ok(Value::Nil);
};
let result = client.best_tool_in_hotbar_for_block(block);
let table = lua.create_table()?; let table = lua.create_table()?;
table.set("index", result.index)?; table.set("index", result.index)?;
table.set("percentage_per_tick", result.percentage_per_tick)?; table.set("percentage_per_tick", result.percentage_per_tick)?;
Ok(Value::Table(table)) Ok(table)
} }
pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> { pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> {
Ok(client.component::<WorldName>().to_string()) Ok(client.component::<InstanceName>().to_string())
} }
pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> { pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> {
@@ -27,22 +23,22 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
Ok(client Ok(client
.world() .world()
.read() .read()
.get_block_state(BlockPos::new( .get_block_state(&BlockPos::new(
position.x as i32, position.x as i32,
position.y as i32, position.y as i32,
position.z as i32, position.z as i32,
)) ))
.map(|block| block.id())) .map(|block| block.id))
} }
#[allow(clippy::cast_possible_truncation)]
pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Option<Table>> { pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Option<Table>> {
let fluid_state = client.world().read().get_fluid_state(BlockPos::new( #[allow(clippy::cast_possible_truncation)]
Ok(
if let Some(state) = client.world().read().get_fluid_state(&BlockPos::new(
position.x as i32, position.x as i32,
position.y as i32, position.y as i32,
position.z as i32, position.z as i32,
)); )) {
Ok(if let Some(state) = fluid_state {
let table = lua.create_table()?; let table = lua.create_table()?;
table.set("kind", state.kind as u8)?; table.set("kind", state.kind as u8)?;
table.set("amount", state.amount)?; table.set("amount", state.amount)?;
@@ -50,5 +46,6 @@ pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Opt
Some(table) Some(table)
} else { } else {
None None
}) },
)
} }

View File

@@ -1,24 +1,22 @@
#[macro_export] #[macro_export]
macro_rules! get_entities { macro_rules! get_entities {
($client:ident) => {{ ($client:ident) => {{
let ecs = $client.ecs.read(); let mut ecs = $client.ecs.lock();
ecs.try_query::<( ecs.query::<(
&AzaleaPosition, &AzaleaPosition,
&CustomName, &CustomName,
&EntityKindComponent, &EntityKind,
&EntityUuid, &EntityUuid,
&LookDirection, &LookDirection,
&MinecraftEntityId, &MinecraftEntityId,
Option<&Owneruuid>, Option<&Owneruuid>,
&Pose, &Pose,
)>() )>()
.map(|mut query| {
query
.iter(&ecs) .iter(&ecs)
.map( .map(
|(position, custom_name, kind, uuid, direction, id, owner_uuid, pose)| { |(position, custom_name, kind, uuid, direction, id, owner_uuid, pose)| {
( (
Vec3::from(*position), Vec3::from(position),
custom_name.as_ref().map(ToString::to_string), custom_name.as_ref().map(ToString::to_string),
kind.to_string(), kind.to_string(),
uuid.to_string(), uuid.to_string(),
@@ -30,38 +28,32 @@ macro_rules! get_entities {
}, },
) )
.collect::<Vec<_>>() .collect::<Vec<_>>()
})
.unwrap_or_default()
}}; }};
} }
#[macro_export] #[macro_export]
macro_rules! get_players { macro_rules! get_players {
($client:ident) => {{ ($client:ident) => {{
let ecs = $client.ecs.read(); let mut ecs = $client.ecs.lock();
ecs.try_query_filtered::<( ecs.query_filtered::<(
&MinecraftEntityId, &MinecraftEntityId,
&EntityUuid, &EntityUuid,
&EntityKindComponent, &EntityKind,
&AzaleaPosition, &AzaleaPosition,
&LookDirection, &LookDirection,
&Pose, &Pose,
), (With<Player>, Without<Dead>)>() ), (With<Player>, Without<Dead>)>()
.map(|mut query| {
query
.iter(&ecs) .iter(&ecs)
.map(|(id, uuid, kind, position, direction, pose)| { .map(|(id, uuid, kind, position, direction, pose)| {
( (
id.0, id.0,
uuid.to_string(), uuid.to_string(),
kind.to_string(), kind.to_string(),
Vec3::from(*position), Vec3::from(position),
Direction::from(direction), Direction::from(direction),
*pose as u8, *pose as u8,
) )
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
})
.unwrap_or_default()
}}; }};
} }

View File

@@ -1,5 +1,5 @@
use azalea::inventory::{ use azalea::inventory::{
self, ItemStackData, self,
components::{Consumable, CustomName, Damage, Food, MaxDamage}, components::{Consumable, CustomName, Damage, Food, MaxDamage},
}; };
use mlua::{UserData, UserDataFields, UserDataMethods}; use mlua::{UserData, UserDataFields, UserDataMethods};
@@ -14,7 +14,8 @@ impl UserData for ItemStack {
f.add_field_method_get("kind", |_, this| Ok(this.0.kind().to_string())); f.add_field_method_get("kind", |_, this| Ok(this.0.kind().to_string()));
f.add_field_method_get("custom_name", |_, this| { f.add_field_method_get("custom_name", |_, this| {
Ok(this.0.as_present().map(|data| { Ok(this.0.as_present().map(|data| {
data.get_component::<CustomName>() data.components
.get::<CustomName>()
.map(|c| c.name.to_string()) .map(|c| c.name.to_string())
})) }))
}); });
@@ -22,13 +23,13 @@ impl UserData for ItemStack {
Ok(this Ok(this
.0 .0
.as_present() .as_present()
.map(|data| data.get_component::<Damage>().map(|d| d.amount))) .map(|data| data.components.get::<Damage>().map(|d| d.amount)))
}); });
f.add_field_method_get("max_damage", |_, this| { f.add_field_method_get("max_damage", |_, this| {
Ok(this Ok(this
.0 .0
.as_present() .as_present()
.map(|data| data.get_component::<MaxDamage>().map(|d| d.amount))) .map(|data| data.components.get::<MaxDamage>().map(|d| d.amount)))
}); });
f.add_field_method_get("consumable", |lua, this| { f.add_field_method_get("consumable", |lua, this| {
@@ -36,7 +37,7 @@ impl UserData for ItemStack {
if let Some(consumable) = this if let Some(consumable) = this
.0 .0
.as_present() .as_present()
.and_then(ItemStackData::get_component::<Consumable>) .and_then(|data| data.components.get::<Consumable>())
{ {
let table = lua.create_table()?; let table = lua.create_table()?;
table.set("animation", consumable.animation as u8)?; table.set("animation", consumable.animation as u8)?;
@@ -54,12 +55,13 @@ impl UserData for ItemStack {
if let Some(food) = this if let Some(food) = this
.0 .0
.as_present() .as_present()
.and_then(ItemStackData::get_component::<Food>) .and_then(|data| data.components.get::<Food>())
{ {
let table = lua.create_table()?; let table = lua.create_table()?;
table.set("nutrition", food.nutrition)?; table.set("nutrition", food.nutrition)?;
table.set("saturation", food.saturation)?; table.set("saturation", food.saturation)?;
table.set("can_always_eat", food.can_always_eat)?; table.set("can_always_eat", food.can_always_eat)?;
table.set("eat_seconds", food.eat_seconds)?;
Some(table) Some(table)
} else { } else {
None None
@@ -69,7 +71,9 @@ impl UserData for ItemStack {
} }
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) { fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_method_mut("split", |_, this, count: u32| Ok(Self(this.0.split(count)))); m.add_method_mut("split", |_, this, count: u32| {
Ok(ItemStack(this.0.split(count)))
});
m.add_method_mut("update_empty", |_, this, (): ()| { m.add_method_mut("update_empty", |_, this, (): ()| {
this.0.update_empty(); this.0.update_empty();
Ok(()) Ok(())

View File

@@ -10,8 +10,8 @@ pub struct Direction {
impl From<&LookDirection> for Direction { impl From<&LookDirection> for Direction {
fn from(d: &LookDirection) -> Self { fn from(d: &LookDirection) -> Self {
Self { Self {
y: d.y_rot(), y: d.y_rot,
x: d.x_rot(), x: d.x_rot,
} }
} }
} }

View File

@@ -1,9 +1,7 @@
use std::time::{SystemTime, UNIX_EPOCH}; use crate::ListenerMap;
use futures::executor::block_on; use futures::executor::block_on;
use mlua::{Function, Lua, Result, Table}; use mlua::{Function, Lua, Result, Table};
use std::time::{SystemTime, UNIX_EPOCH};
use crate::ListenerMap;
pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap) -> Result<()> { pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap) -> Result<()> {
let m = event_listeners.clone(); let m = event_listeners.clone();
@@ -13,15 +11,13 @@ pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap
move |_, (event_type, callback, optional_id): (String, Function, Option<String>)| { move |_, (event_type, callback, optional_id): (String, Function, Option<String>)| {
let m = m.clone(); let m = m.clone();
let id = optional_id.unwrap_or_else(|| { let id = optional_id.unwrap_or_else(|| {
callback.info().name.unwrap_or_else(|| { callback.info().name.unwrap_or(format!(
format!(
"anonymous @ {}", "anonymous @ {}",
SystemTime::now() SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.unwrap_or_default() .unwrap_or_default()
.as_millis() .as_millis()
) ))
})
}); });
tokio::spawn(async move { tokio::spawn(async move {
m.write() m.write()
@@ -42,10 +38,12 @@ pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap
let m = m.clone(); let m = m.clone();
tokio::spawn(async move { tokio::spawn(async move {
let mut m = m.write().await; let mut m = m.write().await;
let empty = m.get_mut(&event_type).is_some_and(|listeners| { let empty = if let Some(listeners) = m.get_mut(&event_type) {
listeners.retain(|(id, _)| target_id != *id); listeners.retain(|(id, _)| target_id != *id);
listeners.is_empty() listeners.is_empty()
}); } else {
false
};
if empty { if empty {
m.remove(&event_type); m.remove(&event_type);
} }

View File

@@ -1,12 +1,10 @@
use std::sync::Arc; use super::room::Room;
use matrix_sdk::{ use matrix_sdk::{
Client as MatrixClient, Client as MatrixClient,
ruma::{RoomId, UserId}, ruma::{RoomId, UserId},
}; };
use mlua::{Error, UserData, UserDataFields, UserDataMethods}; use mlua::{Error, UserData, UserDataFields, UserDataMethods};
use std::sync::Arc;
use super::room::Room;
pub struct Client(pub Arc<MatrixClient>); pub struct Client(pub Arc<MatrixClient>);

View File

@@ -1,3 +1,4 @@
use super::member::Member;
use matrix_sdk::{ use matrix_sdk::{
RoomMemberships, RoomMemberships,
room::Room as MatrixRoom, room::Room as MatrixRoom,
@@ -5,8 +6,6 @@ use matrix_sdk::{
}; };
use mlua::{Error, UserData, UserDataFields, UserDataMethods}; use mlua::{Error, UserData, UserDataFields, UserDataMethods};
use super::member::Member;
pub struct Room(pub MatrixRoom); pub struct Room(pub MatrixRoom);
impl UserData for Room { impl UserData for Room {
@@ -37,7 +36,12 @@ impl UserData for Room {
.members(RoomMemberships::all()) .members(RoomMemberships::all())
.await .await
.map_err(Error::external) .map_err(Error::external)
.map(|members| members.into_iter().map(Member).collect::<Vec<_>>()) .map(|members| {
members
.into_iter()
.map(|member| Member(member.clone()))
.collect::<Vec<_>>()
})
}); });
m.add_async_method( m.add_async_method(
"kick_user", "kick_user",
@@ -75,15 +79,5 @@ impl UserData for Room {
.map_err(Error::external) .map_err(Error::external)
.map(|response| response.event_id.to_string()) .map(|response| response.event_id.to_string())
}); });
m.add_async_method(
"send_html",
async |_, this, (body, html_body): (String, String)| {
this.0
.send(RoomMessageEventContent::text_html(body, html_body))
.await
.map_err(Error::external)
.map(|response| response.event_id.to_string())
},
);
} }
} }

View File

@@ -13,15 +13,13 @@ pub mod vec3;
#[cfg(feature = "matrix")] #[cfg(feature = "matrix")]
pub mod matrix; pub mod matrix;
use crate::{ListenerMap, build_info::built};
use mlua::{Lua, Table};
use std::{ use std::{
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
io, io,
}; };
use mlua::{Lua, Table};
use crate::{ListenerMap, build_info::built};
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
CreateEnv(mlua::Error), CreateEnv(mlua::Error),
@@ -38,12 +36,12 @@ impl Display for Error {
formatter, formatter,
"failed to {}", "failed to {}",
match self { match self {
Self::CreateEnv(error) => format!("create environment: {error}"), Error::CreateEnv(error) => format!("create environment: {error}"),
Self::EvalChunk(error) => format!("evaluate chunk: {error}"), Error::EvalChunk(error) => format!("evaluate chunk: {error}"),
Self::ExecChunk(error) => format!("execute chunk: {error}"), Error::ExecChunk(error) => format!("execute chunk: {error}"),
Self::LoadChunk(error) => format!("load chunk: {error}"), Error::LoadChunk(error) => format!("load chunk: {error}"),
Self::MissingPath(error) => format!("get SCRIPT_PATH global: {error}"), Error::MissingPath(error) => format!("get SCRIPT_PATH global: {error}"),
Self::ReadFile(error) => format!("read script file: {error}"), Error::ReadFile(error) => format!("read script file: {error}"),
} }
) )
} }

View File

@@ -3,8 +3,9 @@ macro_rules! crypt {
($op:ident, $options:expr, $text:expr) => {{ ($op:ident, $options:expr, $text:expr) => {{
macro_rules! crypt_with { macro_rules! crypt_with {
($algo:ident) => {{ ($algo:ident) => {{
let encoding = $options.get("encoding").unwrap_or_default();
let key = &$options.get::<UserDataRef<AesKey>>("key")?.0; let key = &$options.get::<UserDataRef<AesKey>>("key")?.0;
match $options.get("encoding").unwrap_or_default() { match encoding {
1 => $algo::<Base64Encoding>::$op($text, &key), 1 => $algo::<Base64Encoding>::$op($text, &key),
2 => $algo::<Base64rEncoding>::$op($text, &key), 2 => $algo::<Base64rEncoding>::$op($text, &key),
_ => $algo::<NewBase64rEncoding>::$op($text, &key), _ => $algo::<NewBase64rEncoding>::$op($text, &key),

View File

@@ -1,4 +1,4 @@
use azalea::player::PlayerInfo; use azalea::PlayerInfo;
use mlua::{IntoLua, Lua, Result, Value}; use mlua::{IntoLua, Lua, Result, Value};
#[derive(Clone)] #[derive(Clone)]

View File

@@ -1,12 +1,11 @@
use log::error;
use mlua::{Lua, Result, Table};
use std::{ use std::{
ffi::OsString, ffi::OsString,
process::{Command, Stdio}, process::{Command, Stdio},
thread, thread,
}; };
use log::error;
use mlua::{Lua, Result, Table};
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> { pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set( globals.set(
"system", "system",

View File

@@ -1,6 +1,5 @@
use std::time::Duration;
use mlua::{Error, Function, Lua, Result, Table}; use mlua::{Error, Function, Lua, Result, Table};
use std::time::Duration;
use tokio::time::{sleep, timeout}; use tokio::time::{sleep, timeout};
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> { pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {

View File

@@ -28,8 +28,8 @@ impl From<azalea::Vec3> for Vec3 {
} }
} }
impl From<Position> for Vec3 { impl From<&Position> for Vec3 {
fn from(p: Position) -> Self { fn from(p: &Position) -> Self {
Self { Self {
x: p.x, x: p.x,
y: p.y, y: p.y,
@@ -40,7 +40,7 @@ impl From<Position> for Vec3 {
impl From<BlockPos> for Vec3 { impl From<BlockPos> for Vec3 {
fn from(p: BlockPos) -> Self { fn from(p: BlockPos) -> Self {
Self { Vec3 {
x: f64::from(p.x), x: f64::from(p.x),
y: f64::from(p.y), y: f64::from(p.y),
z: f64::from(p.z), z: f64::from(p.z),

View File

@@ -1,5 +1,4 @@
#![warn(clippy::pedantic, clippy::nursery)] #![feature(if_let_guard, let_chains)]
#![allow(clippy::significant_drop_tightening)]
mod arguments; mod arguments;
mod build_info; mod build_info;
@@ -9,24 +8,15 @@ mod hacks;
mod http; mod http;
mod lua; mod lua;
mod particle; mod particle;
mod replay;
#[cfg(feature = "matrix")] #[cfg(feature = "matrix")]
mod matrix; mod matrix;
#[cfg(feature = "replay")] use anyhow::Context;
mod replay;
use std::{
collections::HashMap,
env,
fs::{OpenOptions, read_to_string},
sync::Arc,
};
use anyhow::{Context, Result, bail};
use arguments::Arguments; use arguments::Arguments;
use azalea::{ use azalea::{
DefaultPlugins, bot::DefaultBotPlugins, brigadier::prelude::CommandDispatcher, prelude::*, DefaultBotPlugins, DefaultPlugins, brigadier::prelude::CommandDispatcher, prelude::*,
}; };
use bevy_app::PluginGroup; use bevy_app::PluginGroup;
use bevy_log::{ use bevy_log::{
@@ -37,16 +27,17 @@ use clap::Parser;
use commands::{CommandSource, register}; use commands::{CommandSource, register};
use futures::lock::Mutex; use futures::lock::Mutex;
use futures_locks::RwLock; use futures_locks::RwLock;
use hacks::HacksPlugin;
use log::debug; use log::debug;
use mlua::{Function, Lua}; use mlua::{Function, Lua, Table};
#[cfg(feature = "replay")] use replay::{plugin::RecordPlugin, recorder::Recorder};
use { use std::{
mlua::Table, collections::HashMap,
replay::{plugin::RecordPlugin, recorder::Recorder}, env,
fs::{OpenOptions, read_to_string},
sync::Arc,
}; };
use crate::hacks::HacksPlugin;
#[cfg(feature = "mimalloc")] #[cfg(feature = "mimalloc")]
#[global_allocator] #[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
@@ -54,14 +45,14 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
type ListenerMap = Arc<RwLock<HashMap<String, Vec<(String, Function)>>>>; type ListenerMap = Arc<RwLock<HashMap<String, Vec<(String, Function)>>>>;
#[derive(Default, Clone, Component)] #[derive(Default, Clone, Component)]
struct State { pub struct State {
lua: Arc<Lua>, lua: Arc<Lua>,
event_listeners: ListenerMap, event_listeners: ListenerMap,
commands: Arc<CommandDispatcher<Mutex<CommandSource>>>, commands: Arc<CommandDispatcher<Mutex<CommandSource>>>,
} }
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> anyhow::Result<()> {
#[cfg(feature = "console-subscriber")] #[cfg(feature = "console-subscriber")]
console_subscriber::init(); console_subscriber::init();
@@ -101,25 +92,21 @@ async fn main() -> Result<()> {
DefaultPlugins.set(LogPlugin { DefaultPlugins.set(LogPlugin {
custom_layer: |_| { custom_layer: |_| {
env::var("LOG_FILE").ok().map(|path| { env::var("LOG_FILE").ok().map(|path| {
let file = OpenOptions::new() layer()
.with_writer(
OpenOptions::new()
.append(true) .append(true)
.create(true) .create(true)
.open(&path) .open(&path)
.expect(&(path + " should be accessible")); .expect(&(path + " should be accessible")),
layer().with_writer(file).boxed() )
.boxed()
}) })
}, },
..Default::default() ..Default::default()
}) })
}; };
let record_plugin = RecordPlugin {
let builder = ClientBuilder::new_without_plugins()
.add_plugins(default_plugins)
.add_plugins(DefaultBotPlugins)
.add_plugins(HacksPlugin);
#[cfg(feature = "replay")]
let builder = builder.add_plugins(RecordPlugin {
recorder: Arc::new(parking_lot::Mutex::new( recorder: Arc::new(parking_lot::Mutex::new(
if let Ok(options) = globals.get::<Table>("ReplayRecordingOptions") if let Ok(options) = globals.get::<Table>("ReplayRecordingOptions")
&& let Ok(path) = options.get::<String>("path") && let Ok(path) = options.get::<String>("path")
@@ -135,15 +122,18 @@ async fn main() -> Result<()> {
None None
}, },
)), )),
}); };
let account = if username.contains('@') { let account = if username.contains('@') {
Account::microsoft(&username).await? Account::microsoft(&username).await?
} else { } else {
Account::offline(&username) Account::offline(&username)
}; };
if let AppExit::Error(code) = builder let Err(error) = ClientBuilder::new_without_plugins()
.add_plugins(DefaultBotPlugins)
.add_plugins(HacksPlugin)
.add_plugins(default_plugins)
.add_plugins(record_plugin)
.set_handler(events::handle_event) .set_handler(events::handle_event)
.set_state(State { .set_state(State {
lua: Arc::new(lua), lua: Arc::new(lua),
@@ -151,10 +141,8 @@ async fn main() -> Result<()> {
commands: Arc::new(commands), commands: Arc::new(commands),
}) })
.start(account, server) .start(account, server)
.await .await;
{ eprintln!("{error}");
bail!("azalea exited with code {code}")
}
Ok(()) Ok(())
} }

View File

@@ -1,5 +1,8 @@
use std::time::Duration; use super::Context;
use crate::{
events::call_listeners,
lua::{eval, exec, matrix::room::Room as LuaRoom, reload},
};
use anyhow::Result; use anyhow::Result;
use log::{debug, error}; use log::{debug, error};
use matrix_sdk::{ use matrix_sdk::{
@@ -10,14 +13,9 @@ use matrix_sdk::{
message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent}, message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent},
}, },
}; };
use std::time::Duration;
use tokio::time::sleep; use tokio::time::sleep;
use super::Context;
use crate::{
events::call_listeners,
lua::{eval, exec, matrix::room::Room as LuaRoom, reload},
};
pub async fn on_regular_room_message( pub async fn on_regular_room_message(
event: OriginalSyncRoomMessageEvent, event: OriginalSyncRoomMessageEvent,
room: Room, room: Room,
@@ -30,7 +28,7 @@ pub async fn on_regular_room_message(
return Ok(()); return Ok(());
}; };
if text_content.body.starts_with(&ctx.name) && ctx.is_owner(&event.sender.to_string()) { if ctx.is_owner(&event.sender.to_string()) && text_content.body.starts_with(&ctx.name) {
let body = text_content.body[ctx.name.len()..] let body = text_content.body[ctx.name.len()..]
.trim_start_matches(':') .trim_start_matches(':')
.trim(); .trim();
@@ -99,7 +97,10 @@ pub async fn on_stripped_state_member(
{ {
debug!("joining room {}", room.room_id()); debug!("joining room {}", room.room_id());
while let Err(error) = room.join().await { while let Err(error) = room.join().await {
error!("failed to join room {}: {error:?}", room.room_id()); error!(
"failed to join room {}: {error:?}, retrying...",
room.room_id()
);
sleep(Duration::from_secs(10)).await; sleep(Duration::from_secs(10)).await;
} }
debug!("successfully joined room {}", room.room_id()); debug!("successfully joined room {}", room.room_id());

View File

@@ -1,8 +1,7 @@
mod bot; mod bot;
mod verification; mod verification;
use std::{path::Path, sync::Arc, time::Duration}; use crate::{State, lua::matrix::client::Client as LuaClient};
use anyhow::{Context as _, Result}; use anyhow::{Context as _, Result};
use bot::{on_regular_room_message, on_stripped_state_member}; use bot::{on_regular_room_message, on_stripped_state_member};
use log::{error, warn}; use log::{error, warn};
@@ -11,13 +10,12 @@ use matrix_sdk::{
}; };
use mlua::Table; use mlua::Table;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{path::Path, sync::Arc};
use tokio::fs; use tokio::fs;
use verification::{on_device_key_verification_request, on_room_message_verification_request}; use verification::{on_device_key_verification_request, on_room_message_verification_request};
use crate::{State, events::call_listeners, lua::matrix::client::Client as LuaClient};
#[derive(Clone)] #[derive(Clone)]
struct Context { pub struct Context {
state: State, state: State,
name: String, name: String,
} }
@@ -31,7 +29,7 @@ impl Context {
.ok() .ok()
.and_then(|options| { .and_then(|options| {
options options
.get::<Vec<String>>("owners") .get::<Vec<String>>("Owners")
.ok() .ok()
.and_then(|owners| owners.contains(name).then_some(())) .and_then(|owners| owners.contains(name).then_some(()))
}) })
@@ -56,13 +54,13 @@ async fn persist_sync_token(
Ok(()) Ok(())
} }
pub async fn login(state: &State, options: &Table, globals: &Table, name: String) -> Result<()> { pub async fn login(state: State, options: Table, globals: Table, name: String) -> Result<()> {
let (homeserver_url, username, password, sync_timeout) = ( let (homeserver_url, username, password) = (
options.get::<String>("homeserver_url")?, options.get::<String>("HomeserverUrl")?,
options.get::<String>("username")?, options.get::<String>("Username")?,
&options.get::<String>("password")?, &options.get::<String>("Password")?,
options.get::<u64>("sync_timeout"),
); );
let root_dir = dirs::data_dir() let root_dir = dirs::data_dir()
.context("no data directory")? .context("no data directory")?
.join("errornowatcher") .join("errornowatcher")
@@ -79,12 +77,8 @@ pub async fn login(state: &State, options: &Table, globals: &Table, name: String
} }
let client = builder.build().await?; let client = builder.build().await?;
let mut sync_settings = SyncSettings::new();
if let Ok(seconds) = sync_timeout {
sync_settings = sync_settings.timeout(Duration::from_secs(seconds));
}
let mut new_session; let mut new_session;
let mut sync_settings = SyncSettings::default();
let session_file = root_dir.join("session.json"); let session_file = root_dir.join("session.json");
if let Some(session) = fs::read_to_string(&session_file) if let Some(session) = fs::read_to_string(&session_file)
.await .await
@@ -110,10 +104,7 @@ pub async fn login(state: &State, options: &Table, globals: &Table, name: String
fs::write(&session_file, serde_json::to_string(&new_session)?).await?; fs::write(&session_file, serde_json::to_string(&new_session)?).await?;
} }
client.add_event_handler_context(Context { client.add_event_handler_context(Context { state, name });
state: state.to_owned(),
name,
});
client.add_event_handler(on_stripped_state_member); client.add_event_handler(on_stripped_state_member);
loop { loop {
match client.sync_once(sync_settings.clone()).await { match client.sync_once(sync_settings.clone()).await {
@@ -134,7 +125,6 @@ pub async fn login(state: &State, options: &Table, globals: &Table, name: String
let client = Arc::new(client); let client = Arc::new(client);
globals.set("matrix", LuaClient(client.clone()))?; globals.set("matrix", LuaClient(client.clone()))?;
call_listeners(state, "matrix_init", || Ok(())).await?;
client client
.sync_with_result_callback(sync_settings, |sync_result| async { .sync_with_result_callback(sync_settings, |sync_result| async {

View File

@@ -1,5 +1,3 @@
use std::time::Duration;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use futures::StreamExt; use futures::StreamExt;
use log::{error, info, warn}; use log::{error, info, warn};
@@ -17,6 +15,7 @@ use matrix_sdk::{
}, },
}, },
}; };
use std::time::Duration;
use tokio::time::sleep; use tokio::time::sleep;
async fn confirm_emojis(sas: SasVerification, emoji: [Emoji; 7]) { async fn confirm_emojis(sas: SasVerification, emoji: [Emoji; 7]) {

View File

@@ -1,124 +1,119 @@
use azalea::{entity::particle::Particle, registry::builtin::ParticleKind}; use azalea::{entity::particle::Particle, registry::ParticleKind};
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
pub const fn to_kind(particle: &Particle) -> ParticleKind { pub fn to_kind(particle: &Particle) -> ParticleKind {
match particle { match particle {
Particle::AngryVillager => ParticleKind::AngryVillager, Particle::AngryVillager => ParticleKind::AngryVillager,
Particle::Ash => ParticleKind::Ash,
Particle::Block(_) => ParticleKind::Block, Particle::Block(_) => ParticleKind::Block,
Particle::BlockCrumble => ParticleKind::BlockCrumble,
Particle::BlockMarker(_) => ParticleKind::BlockMarker, Particle::BlockMarker(_) => ParticleKind::BlockMarker,
Particle::Bubble => ParticleKind::Bubble, Particle::Bubble => ParticleKind::Bubble,
Particle::BubbleColumnUp => ParticleKind::BubbleColumnUp,
Particle::BubblePop => ParticleKind::BubblePop,
Particle::CampfireCosySmoke => ParticleKind::CampfireCosySmoke,
Particle::CampfireSignalSmoke => ParticleKind::CampfireSignalSmoke,
Particle::CherryLeaves => ParticleKind::CherryLeaves,
Particle::Cloud => ParticleKind::Cloud, Particle::Cloud => ParticleKind::Cloud,
Particle::Composter => ParticleKind::Composter,
Particle::CopperFireFlame => ParticleKind::CopperFireFlame,
Particle::CrimsonSpore => ParticleKind::CrimsonSpore,
Particle::Crit => ParticleKind::Crit, Particle::Crit => ParticleKind::Crit,
Particle::CurrentDown => ParticleKind::CurrentDown,
Particle::DamageIndicator => ParticleKind::DamageIndicator, Particle::DamageIndicator => ParticleKind::DamageIndicator,
Particle::Dolphin => ParticleKind::Dolphin,
Particle::DragonBreath => ParticleKind::DragonBreath, Particle::DragonBreath => ParticleKind::DragonBreath,
Particle::DrippingDripstoneLava => ParticleKind::DrippingDripstoneLava,
Particle::DrippingDripstoneWater => ParticleKind::DrippingDripstoneWater,
Particle::DrippingHoney => ParticleKind::DrippingHoney,
Particle::DrippingLava => ParticleKind::DrippingLava, Particle::DrippingLava => ParticleKind::DrippingLava,
Particle::DrippingObsidianTear => ParticleKind::DrippingObsidianTear, Particle::FallingLava => ParticleKind::FallingLava,
Particle::LandingLava => ParticleKind::LandingLava,
Particle::DrippingWater => ParticleKind::DrippingWater, Particle::DrippingWater => ParticleKind::DrippingWater,
Particle::FallingWater => ParticleKind::FallingWater,
Particle::Dust(_) => ParticleKind::Dust, Particle::Dust(_) => ParticleKind::Dust,
Particle::DustColorTransition(_) => ParticleKind::DustColorTransition, Particle::DustColorTransition(_) => ParticleKind::DustColorTransition,
Particle::DustPillar => ParticleKind::DustPillar,
Particle::DustPlume => ParticleKind::DustPlume,
Particle::Effect => ParticleKind::Effect, Particle::Effect => ParticleKind::Effect,
Particle::EggCrack => ParticleKind::EggCrack,
Particle::ElderGuardian => ParticleKind::ElderGuardian, Particle::ElderGuardian => ParticleKind::ElderGuardian,
Particle::ElectricSpark => ParticleKind::ElectricSpark,
Particle::Enchant => ParticleKind::Enchant,
Particle::EnchantedHit => ParticleKind::EnchantedHit, Particle::EnchantedHit => ParticleKind::EnchantedHit,
Particle::Enchant => ParticleKind::Enchant,
Particle::EndRod => ParticleKind::EndRod, Particle::EndRod => ParticleKind::EndRod,
Particle::EntityEffect(_) => ParticleKind::EntityEffect, Particle::EntityEffect(_) => ParticleKind::EntityEffect,
Particle::Explosion => ParticleKind::Explosion,
Particle::ExplosionEmitter => ParticleKind::ExplosionEmitter, Particle::ExplosionEmitter => ParticleKind::ExplosionEmitter,
Particle::FallingDripstoneLava => ParticleKind::FallingDripstoneLava, Particle::Explosion => ParticleKind::Explosion,
Particle::FallingDripstoneWater => ParticleKind::FallingDripstoneWater, Particle::Gust => ParticleKind::Gust,
Particle::SonicBoom => ParticleKind::SonicBoom,
Particle::FallingDust(_) => ParticleKind::FallingDust, Particle::FallingDust(_) => ParticleKind::FallingDust,
Particle::FallingHoney => ParticleKind::FallingHoney,
Particle::FallingLava => ParticleKind::FallingLava,
Particle::FallingNectar => ParticleKind::FallingNectar,
Particle::FallingObsidianTear => ParticleKind::FallingObsidianTear,
Particle::FallingSporeBlossom => ParticleKind::FallingSporeBlossom,
Particle::FallingWater => ParticleKind::FallingWater,
Particle::Firefly => ParticleKind::Firefly,
Particle::Firework => ParticleKind::Firework, Particle::Firework => ParticleKind::Firework,
Particle::Fishing => ParticleKind::Fishing, Particle::Fishing => ParticleKind::Fishing,
Particle::Flame => ParticleKind::Flame, Particle::Flame => ParticleKind::Flame,
Particle::CherryLeaves => ParticleKind::CherryLeaves,
Particle::PaleOakLeaves => ParticleKind::PaleOakLeaves,
Particle::SculkSoul => ParticleKind::SculkSoul,
Particle::SculkCharge(_) => ParticleKind::SculkCharge,
Particle::SculkChargePop => ParticleKind::SculkChargePop,
Particle::SoulFireFlame => ParticleKind::SoulFireFlame,
Particle::Soul => ParticleKind::Soul,
Particle::Flash => ParticleKind::Flash, Particle::Flash => ParticleKind::Flash,
Particle::Glow => ParticleKind::Glow,
Particle::GlowSquidInk => ParticleKind::GlowSquidInk,
Particle::Gust => ParticleKind::Gust,
Particle::GustEmitterLarge => ParticleKind::GustEmitterLarge,
Particle::GustEmitterSmall => ParticleKind::GustEmitterSmall,
Particle::HappyVillager => ParticleKind::HappyVillager, Particle::HappyVillager => ParticleKind::HappyVillager,
Particle::Composter => ParticleKind::Composter,
Particle::Heart => ParticleKind::Heart, Particle::Heart => ParticleKind::Heart,
Particle::Infested => ParticleKind::Infested,
Particle::InstantEffect => ParticleKind::InstantEffect, Particle::InstantEffect => ParticleKind::InstantEffect,
Particle::Item(_) => ParticleKind::Item, Particle::Item(_) => ParticleKind::Item,
Particle::ItemCobweb => ParticleKind::ItemCobweb, Particle::Vibration(_) => ParticleKind::Vibration,
Particle::ItemSlime => ParticleKind::ItemSlime, Particle::ItemSlime => ParticleKind::ItemSlime,
Particle::ItemSnowball => ParticleKind::ItemSnowball, Particle::ItemSnowball => ParticleKind::ItemSnowball,
Particle::LandingHoney => ParticleKind::LandingHoney,
Particle::LandingLava => ParticleKind::LandingLava,
Particle::LandingObsidianTear => ParticleKind::LandingObsidianTear,
Particle::LargeSmoke => ParticleKind::LargeSmoke, Particle::LargeSmoke => ParticleKind::LargeSmoke,
Particle::Lava => ParticleKind::Lava, Particle::Lava => ParticleKind::Lava,
Particle::Mycelium => ParticleKind::Mycelium, Particle::Mycelium => ParticleKind::Mycelium,
Particle::Nautilus => ParticleKind::Nautilus,
Particle::Note => ParticleKind::Note, Particle::Note => ParticleKind::Note,
Particle::OminousSpawning => ParticleKind::OminousSpawning,
Particle::PaleOakLeaves => ParticleKind::PaleOakLeaves,
Particle::PauseMobGrowth => ParticleKind::PauseMobGrowth,
Particle::Poof => ParticleKind::Poof, Particle::Poof => ParticleKind::Poof,
Particle::Portal => ParticleKind::Portal, Particle::Portal => ParticleKind::Portal,
Particle::RaidOmen => ParticleKind::RaidOmen,
Particle::Rain => ParticleKind::Rain, Particle::Rain => ParticleKind::Rain,
Particle::ResetMobGrowth => ParticleKind::ResetMobGrowth,
Particle::ReversePortal => ParticleKind::ReversePortal,
Particle::Scrape => ParticleKind::Scrape,
Particle::SculkCharge(_) => ParticleKind::SculkCharge,
Particle::SculkChargePop => ParticleKind::SculkChargePop,
Particle::SculkSoul => ParticleKind::SculkSoul,
Particle::Shriek(_) => ParticleKind::Shriek,
Particle::SmallFlame => ParticleKind::SmallFlame,
Particle::SmallGust => ParticleKind::SmallGust,
Particle::Smoke => ParticleKind::Smoke, Particle::Smoke => ParticleKind::Smoke,
Particle::WhiteSmoke => ParticleKind::WhiteSmoke,
Particle::Sneeze => ParticleKind::Sneeze, Particle::Sneeze => ParticleKind::Sneeze,
Particle::Snowflake => ParticleKind::Snowflake,
Particle::SonicBoom => ParticleKind::SonicBoom,
Particle::Soul => ParticleKind::Soul,
Particle::SoulFireFlame => ParticleKind::SoulFireFlame,
Particle::Spit => ParticleKind::Spit, Particle::Spit => ParticleKind::Spit,
Particle::Splash => ParticleKind::Splash,
Particle::SporeBlossomAir => ParticleKind::SporeBlossomAir,
Particle::SquidInk => ParticleKind::SquidInk, Particle::SquidInk => ParticleKind::SquidInk,
Particle::SweepAttack => ParticleKind::SweepAttack, Particle::SweepAttack => ParticleKind::SweepAttack,
Particle::TintedLeaves => ParticleKind::TintedLeaves,
Particle::TotemOfUndying => ParticleKind::TotemOfUndying, Particle::TotemOfUndying => ParticleKind::TotemOfUndying,
Particle::Trail => ParticleKind::Trail, Particle::Underwater => ParticleKind::Underwater,
Particle::TrialOmen => ParticleKind::TrialOmen, Particle::Splash => ParticleKind::Splash,
Particle::Witch => ParticleKind::Witch,
Particle::BubblePop => ParticleKind::BubblePop,
Particle::CurrentDown => ParticleKind::CurrentDown,
Particle::BubbleColumnUp => ParticleKind::BubbleColumnUp,
Particle::Nautilus => ParticleKind::Nautilus,
Particle::Dolphin => ParticleKind::Dolphin,
Particle::CampfireCosySmoke => ParticleKind::CampfireCosySmoke,
Particle::CampfireSignalSmoke => ParticleKind::CampfireSignalSmoke,
Particle::DrippingHoney => ParticleKind::DrippingHoney,
Particle::FallingHoney => ParticleKind::FallingHoney,
Particle::LandingHoney => ParticleKind::LandingHoney,
Particle::FallingNectar => ParticleKind::FallingNectar,
Particle::FallingSporeBlossom => ParticleKind::FallingSporeBlossom,
Particle::Ash => ParticleKind::Ash,
Particle::CrimsonSpore => ParticleKind::CrimsonSpore,
Particle::WarpedSpore => ParticleKind::WarpedSpore,
Particle::SporeBlossomAir => ParticleKind::SporeBlossomAir,
Particle::DrippingObsidianTear => ParticleKind::DrippingObsidianTear,
Particle::FallingObsidianTear => ParticleKind::FallingObsidianTear,
Particle::LandingObsidianTear => ParticleKind::LandingObsidianTear,
Particle::ReversePortal => ParticleKind::ReversePortal,
Particle::WhiteAsh => ParticleKind::WhiteAsh,
Particle::SmallFlame => ParticleKind::SmallFlame,
Particle::Snowflake => ParticleKind::Snowflake,
Particle::DrippingDripstoneLava => ParticleKind::DrippingDripstoneLava,
Particle::FallingDripstoneLava => ParticleKind::FallingDripstoneLava,
Particle::DrippingDripstoneWater => ParticleKind::DrippingDripstoneWater,
Particle::FallingDripstoneWater => ParticleKind::FallingDripstoneWater,
Particle::GlowSquidInk => ParticleKind::GlowSquidInk,
Particle::Glow => ParticleKind::Glow,
Particle::WaxOn => ParticleKind::WaxOn,
Particle::WaxOff => ParticleKind::WaxOff,
Particle::ElectricSpark => ParticleKind::ElectricSpark,
Particle::Scrape => ParticleKind::Scrape,
Particle::Shriek(_) => ParticleKind::Shriek,
Particle::EggCrack => ParticleKind::EggCrack,
Particle::DustPlume => ParticleKind::DustPlume,
Particle::SmallGust => ParticleKind::SmallGust,
Particle::GustEmitterLarge => ParticleKind::GustEmitterLarge,
Particle::GustEmitterSmall => ParticleKind::GustEmitterSmall,
Particle::Infested => ParticleKind::Infested,
Particle::ItemCobweb => ParticleKind::ItemCobweb,
Particle::TrialSpawnerDetection => ParticleKind::TrialSpawnerDetection, Particle::TrialSpawnerDetection => ParticleKind::TrialSpawnerDetection,
Particle::TrialSpawnerDetectionOminous => ParticleKind::TrialSpawnerDetectionOminous, Particle::TrialSpawnerDetectionOminous => ParticleKind::TrialSpawnerDetectionOminous,
Particle::Underwater => ParticleKind::Underwater,
Particle::VaultConnection => ParticleKind::VaultConnection, Particle::VaultConnection => ParticleKind::VaultConnection,
Particle::Vibration(_) => ParticleKind::Vibration, Particle::DustPillar => ParticleKind::DustPillar,
Particle::WarpedSpore => ParticleKind::WarpedSpore, Particle::OminousSpawning => ParticleKind::OminousSpawning,
Particle::WaxOff => ParticleKind::WaxOff, Particle::RaidOmen => ParticleKind::RaidOmen,
Particle::WaxOn => ParticleKind::WaxOn, Particle::TrialOmen => ParticleKind::TrialOmen,
Particle::WhiteAsh => ParticleKind::WhiteAsh, Particle::Trail => ParticleKind::Trail,
Particle::WhiteSmoke => ParticleKind::WhiteSmoke, Particle::BlockCrumble => ParticleKind::BlockCrumble,
Particle::Witch => ParticleKind::Witch,
} }
} }

View File

@@ -1,40 +1,43 @@
#![allow(clippy::needless_pass_by_value)] #![allow(clippy::needless_pass_by_value)]
use std::sync::Arc; use super::recorder::Recorder;
use azalea::{ use azalea::{
ecs::{event::EventReader, system::Query},
packet::{ packet::{
config::ReceiveConfigPacketEvent, game::ReceiveGamePacketEvent, config::ReceiveConfigPacketEvent,
login::ReceiveLoginPacketEvent, game::emit_receive_packet_events,
login::{LoginPacketEvent, process_packet_events},
}, },
protocol::packets::login::ClientboundLoginPacket, protocol::packets::login::ClientboundLoginPacket,
raw_connection::RawConnection,
}; };
use bevy_app::{App, First, Plugin}; use bevy_app::{First, Plugin};
use bevy_ecs::{message::MessageReader, system::ResMut}; use bevy_ecs::{schedule::IntoSystemConfigs, system::ResMut};
use log::error; use log::error;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::sync::Arc;
use super::recorder::Recorder;
pub struct RecordPlugin { pub struct RecordPlugin {
pub recorder: Arc<Mutex<Option<Recorder>>>, pub recorder: Arc<Mutex<Option<Recorder>>>,
} }
impl Plugin for RecordPlugin { impl Plugin for RecordPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut bevy_app::App) {
let recorder = self.recorder.lock().take(); if let Some(recorder) = self.recorder.lock().take() {
if let Some(recorder) = recorder {
app.insert_resource(recorder) app.insert_resource(recorder)
.add_systems(First, record_login_packets) .add_systems(First, record_login_packets.before(process_packet_events))
.add_systems(First, record_configuration_packets) .add_systems(First, record_configuration_packets)
.add_systems(First, record_game_packets); .add_systems(
First,
record_game_packets.before(emit_receive_packet_events),
);
} }
} }
} }
fn record_login_packets( fn record_login_packets(
recorder: Option<ResMut<Recorder>>, recorder: Option<ResMut<Recorder>>,
mut events: MessageReader<ReceiveLoginPacketEvent>, mut events: EventReader<LoginPacketEvent>,
) { ) {
if let Some(mut recorder) = recorder { if let Some(mut recorder) = recorder {
for event in events.read() { for event in events.read() {
@@ -53,24 +56,24 @@ fn record_login_packets(
fn record_configuration_packets( fn record_configuration_packets(
recorder: Option<ResMut<Recorder>>, recorder: Option<ResMut<Recorder>>,
mut events: MessageReader<ReceiveConfigPacketEvent>, mut events: EventReader<ReceiveConfigPacketEvent>,
) { ) {
if let Some(mut recorder) = recorder { if let Some(mut recorder) = recorder {
for event in events.read() { for event in events.read() {
if let Err(error) = recorder.save_packet(event.packet.as_ref()) { if let Err(error) = recorder.save_packet(&event.packet) {
error!("failed to record configuration packet: {error:?}"); error!("failed to record configuration packet: {error:?}");
} }
} }
} }
} }
fn record_game_packets( fn record_game_packets(recorder: Option<ResMut<Recorder>>, query: Query<&RawConnection>) {
recorder: Option<ResMut<Recorder>>, if let Some(mut recorder) = recorder
mut events: MessageReader<ReceiveGamePacketEvent>, && let Ok(raw_conn) = query.get_single()
) { {
if let Some(mut recorder) = recorder { let queue = raw_conn.incoming_packet_queue();
for event in events.read() { for raw_packet in queue.lock().iter() {
if let Err(error) = recorder.save_packet(event.packet.as_ref()) { if let Err(error) = recorder.save_raw_packet(raw_packet) {
error!("failed to record game packet: {error:?}"); error!("failed to record game packet: {error:?}");
} }
} }

View File

@@ -1,21 +1,19 @@
use std::{ use crate::build_info;
fs::File,
io::{BufWriter, Write},
time::{Instant, SystemTime, UNIX_EPOCH},
};
use anyhow::Result; use anyhow::Result;
use azalea::{ use azalea::{
buf::AzBufVar, buf::AzaleaWriteVar,
prelude::Resource, prelude::Resource,
protocol::packets::{PROTOCOL_VERSION, ProtocolPacket, VERSION_NAME}, protocol::packets::{PROTOCOL_VERSION, ProtocolPacket, VERSION_NAME},
}; };
use log::debug; use log::debug;
use serde_json::json; use serde_json::json;
use std::{
fs::File,
io::{BufWriter, Write},
time::{Instant, SystemTime, UNIX_EPOCH},
};
use zip::{ZipWriter, write::SimpleFileOptions}; use zip::{ZipWriter, write::SimpleFileOptions};
use crate::build_info;
#[derive(Resource)] #[derive(Resource)]
pub struct Recorder { pub struct Recorder {
zip_writer: BufWriter<ZipWriter<File>>, zip_writer: BufWriter<ZipWriter<File>>,
@@ -58,7 +56,7 @@ impl Recorder {
"fileFormat": "MCPR", "fileFormat": "MCPR",
"fileFormatVersion": 14, "fileFormatVersion": 14,
"protocol": PROTOCOL_VERSION, "protocol": PROTOCOL_VERSION,
"generator": format!("ErrorNoWatcher {}", build_info::version_formatted()), "generator": format!("errornowatcher {}", build_info::version_formatted()),
}) })
.to_string() .to_string()
.as_bytes(), .as_bytes(),