Compare commits
10 Commits
update
...
35ef5133e8
Author | SHA1 | Date | |
---|---|---|---|
35ef5133e8 | |||
21798fa2a9 | |||
54c09b3dc5 | |||
71f0c557c7 | |||
28fac5468f | |||
54b32a22a8
|
|||
2bc1e85f94 | |||
f121addc40 | |||
2932c12468 | |||
a98b6814b1 |
@@ -1,7 +1,3 @@
|
|||||||
[target.x86_64-unknown-linux-gnu]
|
[target.x86_64-unknown-linux-gnu]
|
||||||
linker = "clang"
|
linker = "clang"
|
||||||
rustflags = ["-Clink-arg=-fuse-ld=mold", "-Zshare-generics=y"]
|
rustflags = ["-Clink-arg=-fuse-ld=mold", "-Zshare-generics=y"]
|
||||||
|
|
||||||
[target.aarch64-unknown-linux-gnu]
|
|
||||||
linker = "clang"
|
|
||||||
rustflags = ["-Clink-arg=-fuse-ld=mold", "-Zshare-generics=y"]
|
|
||||||
|
52
.github/workflows/build.yaml
vendored
52
.github/workflows/build.yaml
vendored
@@ -1,52 +0,0 @@
|
|||||||
name: Build
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
paths:
|
|
||||||
- "**.rs"
|
|
||||||
- "**.toml"
|
|
||||||
- "Cargo.*"
|
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
errornowatcher:
|
|
||||||
name: errornowatcher (${{ matrix.os }}, ${{ matrix.feature.name }})
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ubuntu-24.04, ubuntu-24.04-arm]
|
|
||||||
feature:
|
|
||||||
- name: default
|
|
||||||
|
|
||||||
- name: mimalloc
|
|
||||||
flags: "-F mimalloc"
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Clone repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Install build dependencies
|
|
||||||
run: sudo apt install -y libluajit-5.1-dev mold
|
|
||||||
|
|
||||||
- name: Set up build cache
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/.cargo/bin/
|
|
||||||
~/.cargo/registry/index/
|
|
||||||
~/.cargo/registry/cache/
|
|
||||||
~/.cargo/git/db/
|
|
||||||
target/
|
|
||||||
key: build_${{ matrix.os }}_${{ matrix.feature.name }}_${{ hashFiles('**.toml', 'Cargo.*') }}
|
|
||||||
|
|
||||||
- name: Switch to nightly toolchain
|
|
||||||
run: rustup default nightly
|
|
||||||
|
|
||||||
- run: cargo build --release ${{ matrix.feature.flags }}
|
|
||||||
|
|
||||||
- name: Upload build artifacts
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: errornowatcher_${{ matrix.feature.name }}_${{ matrix.os }}
|
|
||||||
path: target/release/errornowatcher
|
|
75
.github/workflows/lint.yaml
vendored
75
.github/workflows/lint.yaml
vendored
@@ -1,75 +0,0 @@
|
|||||||
name: Lint
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
paths:
|
|
||||||
- "**.rs"
|
|
||||||
- "**.toml"
|
|
||||||
- "Cargo.*"
|
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
cargo-toml:
|
|
||||||
name: Cargo.toml
|
|
||||||
runs-on: ubuntu-24.04
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Clone repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Install taplo
|
|
||||||
uses: uncenter/setup-taplo@v1
|
|
||||||
|
|
||||||
- name: Run taplo lint
|
|
||||||
run: taplo lint Cargo.toml
|
|
||||||
|
|
||||||
- name: Run taplo fmt
|
|
||||||
if: always()
|
|
||||||
run: taplo fmt --check Cargo.toml
|
|
||||||
|
|
||||||
rust:
|
|
||||||
name: Rust (${{ matrix.feature.name }})
|
|
||||||
runs-on: ubuntu-24.04
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
feature:
|
|
||||||
- name: default
|
|
||||||
|
|
||||||
- name: minimal+mimalloc
|
|
||||||
flags: "--no-default-features -F mimalloc"
|
|
||||||
|
|
||||||
- name: minimal
|
|
||||||
flags: "--no-default-features"
|
|
||||||
|
|
||||||
- name: mimalloc
|
|
||||||
flags: "-F mimalloc"
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Clone repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Install build dependencies
|
|
||||||
run: sudo apt install -y libluajit-5.1-dev mold
|
|
||||||
|
|
||||||
- name: Set up build cache
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/.cargo/bin/
|
|
||||||
~/.cargo/registry/index/
|
|
||||||
~/.cargo/registry/cache/
|
|
||||||
~/.cargo/git/db/
|
|
||||||
target/
|
|
||||||
key: lint_${{ matrix.feature.name }}_${{ hashFiles('**.toml', 'Cargo.*') }}
|
|
||||||
|
|
||||||
- name: Switch to nightly toolchain
|
|
||||||
run: rustup default nightly
|
|
||||||
|
|
||||||
- name: Install components
|
|
||||||
run: rustup component add clippy rustfmt
|
|
||||||
|
|
||||||
- run: cargo clippy ${{ matrix.feature.flags }} -- -D warnings -D clippy::pedantic
|
|
||||||
|
|
||||||
- if: always()
|
|
||||||
run: cargo fmt --check
|
|
3183
Cargo.lock
generated
3183
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
17
Cargo.toml
17
Cargo.toml
@@ -16,36 +16,31 @@ lto = true
|
|||||||
strip = true
|
strip = true
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
built = { git = "https://github.com/lukaslueg/built", features = ["git2"] }
|
built = { version = "0", features = ["git2"] }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
azalea = { git = "https://github.com/azalea-rs/azalea" }
|
azalea = { git = "https://github.com/azalea-rs/azalea.git" }
|
||||||
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"
|
|
||||||
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 }
|
|
||||||
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_json = "1"
|
serde_json = "1"
|
||||||
tokio = { version = "1", features = ["full"] }
|
smallvec = { version = "1", features = ["write"] }
|
||||||
zip = { version = "2", default-features = false, features = ["flate2"] }
|
tokio = { version = "1", features = ["macros"] }
|
||||||
|
zip = "2"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
console-subscriber = ["dep:console-subscriber"]
|
console-subscriber = ["dep:console-subscriber"]
|
||||||
default = ["matrix"]
|
|
||||||
matrix = ["dep:dirs", "dep:matrix-sdk"]
|
|
||||||
mimalloc = ["dep:mimalloc"]
|
mimalloc = ["dep:mimalloc"]
|
||||||
|
@@ -5,15 +5,14 @@ A Minecraft bot with Lua scripting support, written in Rust with [azalea](https:
|
|||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Running Lua from
|
- Running Lua from
|
||||||
|
- `errornowatcher.lua`
|
||||||
- in-game chat messages
|
- in-game chat messages
|
||||||
- Matrix chat messages
|
|
||||||
- POST requests to HTTP server
|
- POST requests to HTTP server
|
||||||
- Listening to in-game events
|
- Listening to in-game events
|
||||||
- Pathfinding (from azalea)
|
- Pathfinding (from azalea)
|
||||||
- Entity and chest interaction
|
- Entity and chest interaction
|
||||||
- NoChatReports encryption
|
- NoChatReports encryption
|
||||||
- Saving ReplayMod recordings
|
- Saving ReplayMod recordings
|
||||||
- Matrix integration (w/ E2EE)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@@ -24,4 +23,4 @@ $ cargo build --release
|
|||||||
$ # ./target/release/errornowatcher
|
$ # ./target/release/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 `errornowatcher.lua` before starting the bot.
|
||||||
|
@@ -2,7 +2,6 @@ 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" } }
|
|
||||||
|
|
||||||
for _, module in ipairs({
|
for _, module in ipairs({
|
||||||
"lib",
|
"lib",
|
@@ -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
|
||||||
|
@@ -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 = {
|
||||||
|
@@ -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)
|
||||||
|
@@ -45,28 +45,30 @@ function entity_speed(uuid, seconds)
|
|||||||
return speed
|
return speed
|
||||||
end
|
end
|
||||||
|
|
||||||
function tps(ms)
|
function tps(seconds)
|
||||||
if not ms then
|
if not seconds then
|
||||||
ms = 1000
|
seconds = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
add_listener("tick", function()
|
add_listener("tick", function()
|
||||||
if not TpsTracking.ticks then
|
if not TpsTracking.ticks then
|
||||||
TpsTracking.ticks = 0
|
TpsTracking.ticks = 0
|
||||||
sleep(ms)
|
TpsTracking.start = clock_gettime(0)
|
||||||
TpsTracking.result = TpsTracking.ticks
|
|
||||||
remove_listeners("tick", "tps_tracking")
|
|
||||||
else
|
else
|
||||||
TpsTracking.ticks = TpsTracking.ticks + 1
|
TpsTracking.ticks = TpsTracking.ticks + 1
|
||||||
|
if TpsTracking.ticks >= seconds * 20 then
|
||||||
|
TpsTracking.stop = clock_gettime(0)
|
||||||
|
remove_listeners("tick", "tps_tracking")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end, "tps_tracking")
|
end, "tps_tracking")
|
||||||
|
|
||||||
sleep(ms)
|
sleep(seconds * 1000)
|
||||||
repeat
|
repeat
|
||||||
sleep(20)
|
sleep(20)
|
||||||
until TpsTracking.result
|
until TpsTracking.stop
|
||||||
|
|
||||||
local tps = TpsTracking.result / (ms / 1000)
|
local tps = seconds * 20 / (TpsTracking.stop - TpsTracking.start)
|
||||||
TpsTracking = {}
|
TpsTracking = {}
|
||||||
return tps
|
return tps
|
||||||
end
|
end
|
||||||
@@ -93,6 +95,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 +146,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
|
||||||
|
@@ -1,2 +0,0 @@
|
|||||||
[toolchain]
|
|
||||||
channel = "nightly"
|
|
@@ -1 +0,0 @@
|
|||||||
group_imports = "StdExternalCrate"
|
|
@@ -1,18 +1,16 @@
|
|||||||
|
use crate::build_info;
|
||||||
|
use clap::Parser;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::Parser;
|
/// A Minecraft utility bot
|
||||||
|
|
||||||
use crate::build_info;
|
|
||||||
|
|
||||||
/// A Minecraft bot with Lua scripting support
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(version = build_info::version_formatted())]
|
#[command(version = build_info::version_formatted())]
|
||||||
pub struct Arguments {
|
pub struct Arguments {
|
||||||
/// Path to main Lua file
|
/// Path to Lua entrypoint
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
pub script: Option<PathBuf>,
|
pub script: Option<PathBuf>,
|
||||||
|
|
||||||
/// Code to execute (after script)
|
/// Code to execute after loading script
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
pub exec: Option<String>,
|
pub exec: Option<String>,
|
||||||
}
|
}
|
||||||
|
@@ -1,12 +1,11 @@
|
|||||||
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
|
|
||||||
use futures::lock::Mutex;
|
|
||||||
use mlua::{Function, Table};
|
|
||||||
use ncr::utils::prepend_header;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
State,
|
State,
|
||||||
lua::{eval, exec, reload},
|
lua::{eval, exec, reload},
|
||||||
};
|
};
|
||||||
|
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
|
||||||
|
use futures::lock::Mutex;
|
||||||
|
use mlua::{Function, Table};
|
||||||
|
use ncr::utils::prepend_header;
|
||||||
|
|
||||||
pub type Ctx = CommandContext<Mutex<CommandSource>>;
|
pub type Ctx = CommandContext<Mutex<CommandSource>>;
|
||||||
|
|
||||||
@@ -33,7 +32,7 @@ impl CommandSource {
|
|||||||
}
|
}
|
||||||
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 {
|
||||||
@@ -49,10 +48,10 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
|
|||||||
let source = ctx.source.clone();
|
let source = ctx.source.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let source = source.lock().await;
|
let source = source.lock().await;
|
||||||
source.reply(
|
source.reply(&format!(
|
||||||
&reload(&source.state.lua, source.message.sender())
|
"{:?}",
|
||||||
.map_or_else(|error| error.to_string(), |()| String::from("ok")),
|
reload(&source.state.lua, source.message.username())
|
||||||
);
|
));
|
||||||
});
|
});
|
||||||
1
|
1
|
||||||
}));
|
}));
|
||||||
@@ -63,11 +62,10 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
|
|||||||
let code = get_string(ctx, "code").expect("argument should exist");
|
let code = get_string(ctx, "code").expect("argument should exist");
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let source = source.lock().await;
|
let source = source.lock().await;
|
||||||
source.reply(
|
source.reply(&format!(
|
||||||
&eval(&source.state.lua, &code, source.message.sender())
|
"{:?}",
|
||||||
.await
|
eval(&source.state.lua, &code, source.message.username()).await
|
||||||
.unwrap_or_else(|error| error.to_string()),
|
));
|
||||||
);
|
|
||||||
});
|
});
|
||||||
1
|
1
|
||||||
})),
|
})),
|
||||||
@@ -79,11 +77,10 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
|
|||||||
let code = get_string(ctx, "code").expect("argument should exist");
|
let code = get_string(ctx, "code").expect("argument should exist");
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let source = source.lock().await;
|
let source = source.lock().await;
|
||||||
source.reply(
|
source.reply(&format!(
|
||||||
&exec(&source.state.lua, &code, source.message.sender())
|
"{:?}",
|
||||||
.await
|
exec(&source.state.lua, &code, source.message.username()).await
|
||||||
.map_or_else(|error| error.to_string(), |()| String::from("ok")),
|
));
|
||||||
);
|
|
||||||
});
|
});
|
||||||
1
|
1
|
||||||
})),
|
})),
|
||||||
|
203
src/events.rs
203
src/events.rs
@@ -1,6 +1,12 @@
|
|||||||
use std::{net::SocketAddr, process::exit};
|
use crate::{
|
||||||
|
State,
|
||||||
use anyhow::{Context, Result};
|
commands::CommandSource,
|
||||||
|
http::serve,
|
||||||
|
lua::{client, direction::Direction, player::Player, vec3::Vec3},
|
||||||
|
particle,
|
||||||
|
replay::Recorder,
|
||||||
|
};
|
||||||
|
use anyhow::Context;
|
||||||
use azalea::{
|
use azalea::{
|
||||||
brigadier::exceptions::BuiltInExceptions::DispatcherUnknownCommand, prelude::*,
|
brigadier::exceptions::BuiltInExceptions::DispatcherUnknownCommand, prelude::*,
|
||||||
protocol::packets::game::ClientboundGamePacket,
|
protocol::packets::game::ClientboundGamePacket,
|
||||||
@@ -10,32 +16,21 @@ use hyper_util::rt::TokioIo;
|
|||||||
use log::{debug, error, info, trace};
|
use log::{debug, error, info, trace};
|
||||||
use mlua::{Error, Function, IntoLuaMulti, Table};
|
use mlua::{Error, Function, IntoLuaMulti, Table};
|
||||||
use ncr::utils::trim_header;
|
use ncr::utils::trim_header;
|
||||||
|
use std::net::SocketAddr;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
#[cfg(feature = "matrix")]
|
|
||||||
use {crate::matrix, std::time::Duration, tokio::time::sleep};
|
|
||||||
|
|
||||||
use crate::{
|
#[allow(clippy::too_many_lines)]
|
||||||
State,
|
pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow::Result<()> {
|
||||||
commands::CommandSource,
|
|
||||||
http::serve,
|
|
||||||
lua::{client, direction::Direction, player::Player, vec3::Vec3},
|
|
||||||
particle,
|
|
||||||
replay::recorder::Recorder,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]
|
|
||||||
pub async fn handle_event(client: Client, event: Event, state: State) -> Result<()> {
|
|
||||||
match event {
|
match event {
|
||||||
Event::AddPlayer(player_info) => {
|
Event::AddPlayer(player_info) => {
|
||||||
call_listeners(&state, "add_player", || Ok(Player::from(player_info))).await
|
call_listeners(&state, "add_player", Player::from(player_info)).await;
|
||||||
}
|
}
|
||||||
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}");
|
||||||
|
|
||||||
@@ -48,7 +43,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
|
|||||||
.call::<String>((options.clone(), content.clone()))
|
.call::<String>((options.clone(), content.clone()))
|
||||||
.ok()
|
.ok()
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.and_then(|string| trim_header(string).ok())
|
.and_then(|s| trim_header(s).ok())
|
||||||
{
|
{
|
||||||
is_encrypted = true;
|
is_encrypted = true;
|
||||||
ncr_options = Some(options);
|
ncr_options = Some(options);
|
||||||
@@ -83,63 +78,50 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
call_listeners(&state, "chat", || {
|
|
||||||
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)?;
|
||||||
table.set("is_whisper", is_whisper)?;
|
table.set("is_whisper", is_whisper)?;
|
||||||
table.set("is_encrypted", is_encrypted)?;
|
table.set("is_encrypted", is_encrypted)?;
|
||||||
Ok(table)
|
call_listeners(&state, "chat", table).await;
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
Event::Death(packet) => {
|
Event::Death(packet) => {
|
||||||
if let Some(packet) = packet {
|
if let Some(packet) = packet {
|
||||||
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)?;
|
||||||
Ok(table)
|
call_listeners(&state, "death", table).await;
|
||||||
})
|
|
||||||
.await
|
|
||||||
} else {
|
} else {
|
||||||
call_listeners(&state, "death", || Ok(())).await
|
call_listeners(&state, "death", ()).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Disconnect(message) => {
|
Event::Disconnect(message) => {
|
||||||
if let Some(message) = message {
|
if let Some(message) = message {
|
||||||
call_listeners(&state, "disconnect", || {
|
|
||||||
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())?;
|
call_listeners(&state, "disconnect", table).await;
|
||||||
Ok(table)
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
} else {
|
} else {
|
||||||
call_listeners(&state, "disconnect", || Ok(())).await
|
call_listeners(&state, "disconnect", ()).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::KeepAlive(id) => call_listeners(&state, "keep_alive", || Ok(id)).await,
|
Event::KeepAlive(id) => call_listeners(&state, "keep_alive", id).await,
|
||||||
|
Event::Login => call_listeners(&state, "login", ()).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", Player::from(player_info)).await;
|
||||||
}
|
}
|
||||||
Event::Spawn => call_listeners(&state, "spawn", || Ok(())).await,
|
Event::Tick => call_listeners(&state, "tick", ()).await,
|
||||||
Event::Tick => call_listeners(&state, "tick", || Ok(())).await,
|
|
||||||
Event::UpdatePlayer(player_info) => {
|
Event::UpdatePlayer(player_info) => {
|
||||||
call_listeners(&state, "update_player", || Ok(Player::from(player_info))).await
|
call_listeners(&state, "update_player", Player::from(player_info)).await;
|
||||||
}
|
}
|
||||||
Event::Packet(packet) => match packet.as_ref() {
|
Event::Packet(packet) => match packet.as_ref() {
|
||||||
ClientboundGamePacket::AddEntity(packet) => {
|
ClientboundGamePacket::AddEntity(packet) => {
|
||||||
call_listeners(&state, "add_entity", || {
|
|
||||||
let table = state.lua.create_table()?;
|
let table = state.lua.create_table()?;
|
||||||
table.set("id", packet.id.0)?;
|
table.set("id", packet.id.0)?;
|
||||||
table.set("uuid", packet.uuid.to_string())?;
|
table.set("uuid", packet.uuid.to_string())?;
|
||||||
@@ -153,76 +135,69 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
|
|||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
table.set("data", packet.data)?;
|
table.set("data", packet.data)?;
|
||||||
Ok(table)
|
call_listeners(&state, "add_entity", table).await;
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
ClientboundGamePacket::LevelParticles(packet) => {
|
ClientboundGamePacket::LevelParticles(packet) => {
|
||||||
call_listeners(&state, "level_particles", || {
|
|
||||||
let table = state.lua.create_table()?;
|
let table = state.lua.create_table()?;
|
||||||
table.set("position", Vec3::from(packet.pos))?;
|
table.set("position", Vec3::from(packet.pos))?;
|
||||||
table.set("count", packet.count)?;
|
table.set("count", packet.count)?;
|
||||||
table.set("kind", particle::to_kind(&packet.particle) as u8)?;
|
table.set("kind", particle::to_kind(&packet.particle) as u8)?;
|
||||||
Ok(table)
|
call_listeners(&state, "level_particles", table).await;
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
ClientboundGamePacket::RemoveEntities(packet) => {
|
ClientboundGamePacket::RemoveEntities(packet) => {
|
||||||
call_listeners(&state, "remove_entities", || {
|
call_listeners(
|
||||||
Ok(packet.entity_ids.iter().map(|id| id.0).collect::<Vec<_>>())
|
&state,
|
||||||
})
|
"remove_entities",
|
||||||
.await
|
packet.entity_ids.iter().map(|id| id.0).collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
ClientboundGamePacket::SetHealth(packet) => {
|
ClientboundGamePacket::SetHealth(packet) => {
|
||||||
call_listeners(&state, "set_health", || {
|
|
||||||
let table = state.lua.create_table()?;
|
let table = state.lua.create_table()?;
|
||||||
table.set("food", packet.food)?;
|
table.set("food", packet.food)?;
|
||||||
table.set("health", packet.health)?;
|
table.set("health", packet.health)?;
|
||||||
table.set("saturation", packet.saturation)?;
|
table.set("saturation", packet.saturation)?;
|
||||||
Ok(table)
|
call_listeners(&state, "set_health", table).await;
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
ClientboundGamePacket::SetPassengers(packet) => {
|
ClientboundGamePacket::SetPassengers(packet) => {
|
||||||
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("passengers", &*packet.passengers)?;
|
table.set("passengers", &*packet.passengers)?;
|
||||||
Ok(table)
|
call_listeners(&state, "set_passengers", table).await;
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
ClientboundGamePacket::SetTime(packet) => {
|
ClientboundGamePacket::SetTime(packet) => {
|
||||||
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("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)?;
|
table.set("tick_day_time", packet.tick_day_time)?;
|
||||||
Ok(table)
|
call_listeners(&state, "set_time", table).await;
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
_ => 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 initialize event");
|
||||||
|
|
||||||
let ecs = client.ecs.clone();
|
|
||||||
ctrlc::set_handler(move || {
|
|
||||||
ecs.lock()
|
|
||||||
.remove_resource::<Recorder>()
|
|
||||||
.map(Recorder::finish);
|
|
||||||
exit(0);
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let globals = state.lua.globals();
|
let globals = state.lua.globals();
|
||||||
lua_init(client, &state, &globals).await?;
|
let lua_ecs = client.ecs.clone();
|
||||||
|
globals.set(
|
||||||
|
"finish_replay_recording",
|
||||||
|
state.lua.create_function_mut(move |_, (): ()| {
|
||||||
|
lua_ecs
|
||||||
|
.lock()
|
||||||
|
.remove_resource::<Recorder>()
|
||||||
|
.context("recording not active")
|
||||||
|
.map_err(Error::external)?
|
||||||
|
.finish()
|
||||||
|
.map_err(Error::external)
|
||||||
|
})?,
|
||||||
|
)?;
|
||||||
|
globals.set(
|
||||||
|
"client",
|
||||||
|
client::Client {
|
||||||
|
inner: Some(client),
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
call_listeners(&state, "init", ()).await;
|
||||||
|
|
||||||
let Some(address): Option<SocketAddr> = globals
|
let Some(address): Option<SocketAddr> = globals
|
||||||
.get::<String>("HttpAddress")
|
.get::<String>("HttpAddress")
|
||||||
@@ -232,19 +207,14 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let listener = TcpListener::bind(address).await.inspect_err(|error| {
|
let listener = TcpListener::bind(address).await.map_err(|error| {
|
||||||
error!("failed to listen on {address}: {error:?}");
|
error!("failed to listen on {address}: {error:?}");
|
||||||
|
error
|
||||||
})?;
|
})?;
|
||||||
debug!("http server listening on {address}");
|
debug!("http server listening on {address}");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let (stream, peer) = match listener.accept().await {
|
let (stream, peer) = listener.accept().await?;
|
||||||
Ok(pair) => pair,
|
|
||||||
Err(error) => {
|
|
||||||
error!("failed to accept connection: {error:?}");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
trace!("http server got connection from {peer}");
|
trace!("http server got connection from {peer}");
|
||||||
|
|
||||||
let conn_state = state.clone();
|
let conn_state = state.clone();
|
||||||
@@ -263,59 +233,24 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => todo!(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()> {
|
async fn call_listeners<T: Clone + IntoLuaMulti + Send + 'static>(
|
||||||
let ecs = client.ecs.clone();
|
state: &State,
|
||||||
globals.set(
|
event_type: &'static str,
|
||||||
"finish_replay_recording",
|
data: T,
|
||||||
state.lua.create_function_mut(move |_, (): ()| {
|
) {
|
||||||
ecs.lock()
|
|
||||||
.remove_resource::<Recorder>()
|
|
||||||
.context("recording not active")
|
|
||||||
.map_err(Error::external)?
|
|
||||||
.finish()
|
|
||||||
.map_err(Error::external)
|
|
||||||
})?,
|
|
||||||
)?;
|
|
||||||
globals.set("client", client::Client(Some(client)))?;
|
|
||||||
call_listeners(state, "init", || Ok(())).await
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "matrix")]
|
|
||||||
fn matrix_init(client: &Client, state: State) {
|
|
||||||
let globals = state.lua.globals();
|
|
||||||
if let Ok(options) = globals.get::<Table>("MatrixOptions") {
|
|
||||||
let name = client.username();
|
|
||||||
tokio::spawn(async move {
|
|
||||||
loop {
|
|
||||||
let name = name.clone();
|
|
||||||
if let Err(error) = matrix::login(&state, &options, &globals, name).await {
|
|
||||||
error!("failed to log into matrix: {error:?}");
|
|
||||||
}
|
|
||||||
sleep(Duration::from_secs(10)).await;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn call_listeners<T, F>(state: &State, event_type: &'static str, getter: F) -> Result<()>
|
|
||||||
where
|
|
||||||
T: Clone + IntoLuaMulti + Send + 'static,
|
|
||||||
F: FnOnce() -> Result<T>,
|
|
||||||
{
|
|
||||||
if let Some(listeners) = state.event_listeners.read().await.get(event_type).cloned() {
|
if let Some(listeners) = state.event_listeners.read().await.get(event_type).cloned() {
|
||||||
let data = getter()?;
|
|
||||||
for (id, callback) in listeners {
|
for (id, callback) in listeners {
|
||||||
let data = data.clone();
|
let data = data.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Err(error) = callback.call_async::<()>(data).await {
|
if let Err(error) = callback.call_async::<()>(data).await {
|
||||||
error!("failed to call lua event listener {id} for {event_type}: {error}");
|
error!("failed to call lua event listener {id} for {event_type}: {error:?}");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
@@ -1,20 +0,0 @@
|
|||||||
use azalea::{
|
|
||||||
Vec3,
|
|
||||||
movement::{KnockbackEvent, KnockbackType},
|
|
||||||
prelude::Component,
|
|
||||||
};
|
|
||||||
use bevy_ecs::{event::EventMutator, query::With, system::Query};
|
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct AntiKnockback;
|
|
||||||
|
|
||||||
pub fn anti_knockback(
|
|
||||||
mut events: EventMutator<KnockbackEvent>,
|
|
||||||
entity_query: Query<(), With<AntiKnockback>>,
|
|
||||||
) {
|
|
||||||
for event in events.read() {
|
|
||||||
if entity_query.get(event.entity).is_ok() {
|
|
||||||
event.knockback = KnockbackType::Add(Vec3::default());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,19 +0,0 @@
|
|||||||
#![allow(clippy::needless_pass_by_value)]
|
|
||||||
|
|
||||||
pub mod anti_knockback;
|
|
||||||
|
|
||||||
use anti_knockback::anti_knockback;
|
|
||||||
use azalea::{connection::read_packets, movement::handle_knockback};
|
|
||||||
use bevy_app::{App, Plugin, PreUpdate};
|
|
||||||
use bevy_ecs::schedule::IntoScheduleConfigs;
|
|
||||||
|
|
||||||
pub struct HacksPlugin;
|
|
||||||
|
|
||||||
impl Plugin for HacksPlugin {
|
|
||||||
fn build(&self, app: &mut App) {
|
|
||||||
app.add_systems(
|
|
||||||
PreUpdate,
|
|
||||||
anti_knockback.after(read_packets).before(handle_knockback),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
63
src/http.rs
63
src/http.rs
@@ -1,61 +1,58 @@
|
|||||||
use http_body_util::{BodyExt, Empty, Full, combinators::BoxBody};
|
|
||||||
use hyper::{
|
|
||||||
Error, Method, Request, Response, StatusCode,
|
|
||||||
body::{Bytes, Incoming},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
State,
|
State,
|
||||||
lua::{eval, exec, reload},
|
lua::{eval, exec, reload},
|
||||||
};
|
};
|
||||||
|
use http_body_util::{BodyExt, Empty, Full, combinators::BoxBody};
|
||||||
|
use hyper::{Method, Request, Response, StatusCode, body::Bytes};
|
||||||
|
|
||||||
pub async fn serve(
|
pub async fn serve(
|
||||||
request: Request<Incoming>,
|
request: Request<hyper::body::Incoming>,
|
||||||
state: State,
|
state: State,
|
||||||
) -> Result<Response<BoxBody<Bytes, Error>>, Error> {
|
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
|
||||||
Ok(match (request.method(), request.uri().path()) {
|
let path = request.uri().path().to_owned();
|
||||||
(&Method::POST, "/reload") => Response::new(
|
|
||||||
reload(&state.lua, None).map_or_else(|error| full(error.to_string()), |()| empty()),
|
Ok(match (request.method(), path.as_str()) {
|
||||||
),
|
(&Method::POST, "/reload") => {
|
||||||
(&Method::POST, "/eval") => Response::new(full(
|
Response::new(full(format!("{:#?}", reload(&state.lua, None))))
|
||||||
eval(
|
}
|
||||||
&state.lua,
|
|
||||||
&String::from_utf8_lossy(&request.into_body().collect().await?.to_bytes()),
|
(&Method::POST, "/eval" | "/exec") => {
|
||||||
None,
|
let bytes = request.into_body().collect().await?.to_bytes();
|
||||||
)
|
match std::str::from_utf8(&bytes) {
|
||||||
.await
|
Ok(code) => Response::new(full(match path.as_str() {
|
||||||
.unwrap_or_else(|error| error.to_string()),
|
"/eval" => format!("{:#?}", eval(&state.lua, code, None).await),
|
||||||
)),
|
"/exec" => format!("{:#?}", exec(&state.lua, code, None).await),
|
||||||
(&Method::POST, "/exec") => Response::new(
|
_ => unreachable!(),
|
||||||
exec(
|
})),
|
||||||
&state.lua,
|
Err(error) => status_code_response(
|
||||||
&String::from_utf8_lossy(&request.into_body().collect().await?.to_bytes()),
|
StatusCode::BAD_REQUEST,
|
||||||
None,
|
full(format!("invalid utf-8 data received: {error:?}")),
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_or_else(|error| full(error.to_string()), |()| empty()),
|
|
||||||
),
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
(&Method::GET, "/ping") => Response::new(full("pong!")),
|
(&Method::GET, "/ping") => Response::new(full("pong!")),
|
||||||
|
|
||||||
_ => status_code_response(StatusCode::NOT_FOUND, empty()),
|
_ => status_code_response(StatusCode::NOT_FOUND, empty()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn status_code_response(
|
fn status_code_response(
|
||||||
status_code: StatusCode,
|
status_code: StatusCode,
|
||||||
bytes: BoxBody<Bytes, Error>,
|
bytes: BoxBody<Bytes, hyper::Error>,
|
||||||
) -> Response<BoxBody<Bytes, Error>> {
|
) -> Response<BoxBody<Bytes, hyper::Error>> {
|
||||||
let mut response = Response::new(bytes);
|
let mut response = Response::new(bytes);
|
||||||
*response.status_mut() = status_code;
|
*response.status_mut() = status_code;
|
||||||
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()
|
||||||
|
@@ -60,7 +60,7 @@ pub async fn get_block_states(
|
|||||||
true
|
true
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
matched.push(block.id());
|
matched.push(block.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,19 +1,21 @@
|
|||||||
|
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
|
||||||
use azalea::{
|
use azalea::{
|
||||||
BlockPos,
|
BlockPos,
|
||||||
inventory::{Inventory, Menu, Player, SlotList},
|
inventory::{Inventory, Menu, Player, SlotList},
|
||||||
prelude::ContainerClientExt,
|
prelude::ContainerClientExt,
|
||||||
protocol::packets::game::ServerboundSetCarriedItem,
|
protocol::packets::game::ServerboundSetCarriedItem,
|
||||||
};
|
};
|
||||||
use mlua::{Lua, Result, UserDataRef, Value};
|
use log::error;
|
||||||
|
use mlua::{Lua, Result, Table, UserDataRef};
|
||||||
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
|
|
||||||
|
|
||||||
pub fn container(_lua: &Lua, client: &Client) -> Result<Option<ContainerRef>> {
|
pub fn container(_lua: &Lua, client: &Client) -> Result<Option<ContainerRef>> {
|
||||||
Ok(client.get_open_container().map(ContainerRef))
|
Ok(client
|
||||||
|
.get_open_container()
|
||||||
|
.map(|c| ContainerRef { inner: c }))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn held_item(_lua: &Lua, client: &Client) -> Result<ItemStack> {
|
pub fn held_item(_lua: &Lua, client: &Client) -> Result<ItemStack> {
|
||||||
Ok(ItemStack(client.component::<Inventory>().held_item()))
|
Ok(ItemStack::from(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> {
|
||||||
@@ -21,11 +23,10 @@ pub fn held_slot(_lua: &Lua, client: &Client) -> Result<u8> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_lines)]
|
#[allow(clippy::too_many_lines)]
|
||||||
pub fn menu(lua: &Lua, client: &Client) -> Result<Value> {
|
pub fn menu(lua: &Lua, client: &Client) -> Result<Table> {
|
||||||
fn from_slot_list<const N: usize>(slot_list: SlotList<N>) -> Vec<ItemStack> {
|
fn from_slot_list<const N: usize>(s: SlotList<N>) -> Vec<ItemStack> {
|
||||||
slot_list
|
s.iter()
|
||||||
.iter()
|
.map(|i| ItemStack::from(i.to_owned()))
|
||||||
.map(|item_stack| ItemStack(item_stack.to_owned()))
|
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,11 +40,11 @@ pub fn menu(lua: &Lua, client: &Client) -> Result<Value> {
|
|||||||
offhand,
|
offhand,
|
||||||
}) => {
|
}) => {
|
||||||
table.set("type", 0)?;
|
table.set("type", 0)?;
|
||||||
table.set("craft_result", ItemStack(craft_result))?;
|
table.set("craft_result", ItemStack::from(craft_result))?;
|
||||||
table.set("craft", from_slot_list(craft))?;
|
table.set("craft", from_slot_list(craft))?;
|
||||||
table.set("armor", from_slot_list(armor))?;
|
table.set("armor", from_slot_list(armor))?;
|
||||||
table.set("inventory", from_slot_list(inventory))?;
|
table.set("inventory", from_slot_list(inventory))?;
|
||||||
table.set("offhand", ItemStack(offhand))?;
|
table.set("offhand", ItemStack::from(offhand))?;
|
||||||
}
|
}
|
||||||
Menu::Generic9x3 { contents, player } => {
|
Menu::Generic9x3 { contents, player } => {
|
||||||
table.set("type", 3)?;
|
table.set("type", 3)?;
|
||||||
@@ -61,7 +62,7 @@ pub fn menu(lua: &Lua, client: &Client) -> Result<Value> {
|
|||||||
player,
|
player,
|
||||||
} => {
|
} => {
|
||||||
table.set("type", 13)?;
|
table.set("type", 13)?;
|
||||||
table.set("result", ItemStack(result))?;
|
table.set("result", ItemStack::from(result))?;
|
||||||
table.set("grid", from_slot_list(grid))?;
|
table.set("grid", from_slot_list(grid))?;
|
||||||
table.set("player", from_slot_list(player))?;
|
table.set("player", from_slot_list(player))?;
|
||||||
}
|
}
|
||||||
@@ -77,7 +78,7 @@ pub fn menu(lua: &Lua, client: &Client) -> Result<Value> {
|
|||||||
} => {
|
} => {
|
||||||
table.set("type", 20)?;
|
table.set("type", 20)?;
|
||||||
table.set("payments", from_slot_list(payments))?;
|
table.set("payments", from_slot_list(payments))?;
|
||||||
table.set("result", ItemStack(result))?;
|
table.set("result", ItemStack::from(result))?;
|
||||||
table.set("player", from_slot_list(player))?;
|
table.set("player", from_slot_list(player))?;
|
||||||
}
|
}
|
||||||
Menu::ShulkerBox { contents, player } => {
|
Menu::ShulkerBox { contents, player } => {
|
||||||
@@ -85,9 +86,9 @@ pub fn menu(lua: &Lua, client: &Client) -> Result<Value> {
|
|||||||
table.set("contents", from_slot_list(contents))?;
|
table.set("contents", from_slot_list(contents))?;
|
||||||
table.set("player", from_slot_list(player))?;
|
table.set("player", from_slot_list(player))?;
|
||||||
}
|
}
|
||||||
_ => return Ok(Value::Nil),
|
_ => (),
|
||||||
}
|
}
|
||||||
Ok(Value::Table(table))
|
Ok(table)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn open_container_at(
|
pub async fn open_container_at(
|
||||||
@@ -104,11 +105,11 @@ pub async fn open_container_at(
|
|||||||
position.z as i32,
|
position.z as i32,
|
||||||
))
|
))
|
||||||
.await
|
.await
|
||||||
.map(Container))
|
.map(|c| Container { inner: c }))
|
||||||
}
|
}
|
||||||
|
|
||||||
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(|c| Container { inner: c }))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> {
|
pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> {
|
||||||
@@ -125,8 +126,11 @@ pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> {
|
|||||||
inventory.selected_hotbar_slot = slot;
|
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(())
|
||||||
}
|
}
|
||||||
|
@@ -1,17 +1,18 @@
|
|||||||
|
use super::{Client, Vec3};
|
||||||
use azalea::{
|
use azalea::{
|
||||||
BlockPos, BotClientExt, interact::StartUseItemEvent,
|
BlockPos, BotClientExt,
|
||||||
protocol::packets::game::s_interact::InteractionHand, world::MinecraftEntityId,
|
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<()> {
|
||||||
|
|
||||||
pub fn attack(_lua: &Lua, client: &Client, entity_id: i32) -> Result<()> {
|
|
||||||
client.attack(MinecraftEntityId(entity_id));
|
client.attack(MinecraftEntityId(entity_id));
|
||||||
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,
|
||||||
@@ -38,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,
|
||||||
@@ -53,14 +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<()> {
|
||||||
client.ecs.lock().send_event(StartUseItemEvent {
|
let d = client.direction();
|
||||||
entity: client.entity,
|
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,
|
||||||
},
|
},
|
||||||
force_block: None,
|
sequence: 0,
|
||||||
});
|
yaw: d.0,
|
||||||
|
pitch: d.1,
|
||||||
|
}) {
|
||||||
|
error!("failed to send UseItem packet: {error:?}");
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@@ -1,36 +1,38 @@
|
|||||||
#![allow(clippy::needless_pass_by_value, clippy::unnecessary_wraps)]
|
|
||||||
|
|
||||||
mod container;
|
mod container;
|
||||||
mod interaction;
|
mod interaction;
|
||||||
mod movement;
|
mod movement;
|
||||||
mod state;
|
mod state;
|
||||||
mod world;
|
mod world;
|
||||||
|
|
||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
|
|
||||||
use azalea::{Client as AzaleaClient, world::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 inner: Option<AzaleaClient>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Deref for Client {
|
impl Deref for Client {
|
||||||
type Target = AzaleaClient;
|
type Target = AzaleaClient;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
self.0.as_ref().expect("should have received init event")
|
self.inner
|
||||||
|
.as_ref()
|
||||||
|
.expect("should have received init event")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DerefMut for Client {
|
impl DerefMut for Client {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
self.0.as_mut().expect("should have received init event")
|
self.inner
|
||||||
|
.as_mut()
|
||||||
|
.expect("should have received init event")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +43,6 @@ impl UserData for Client {
|
|||||||
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("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);
|
||||||
@@ -59,40 +60,37 @@ impl UserData for Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
|
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
|
||||||
m.add_async_method("find_all_entities", world::find::all_entities);
|
m.add_async_method("find_all_entities", world::find_all_entities);
|
||||||
m.add_async_method("find_all_players", world::find::all_players);
|
m.add_async_method("find_all_players", world::find_all_players);
|
||||||
m.add_async_method("find_entities", world::find::entities);
|
m.add_async_method("find_entities", world::find_entities);
|
||||||
m.add_async_method("find_players", world::find::players);
|
m.add_async_method("find_players", world::find_players);
|
||||||
m.add_async_method("go_to", movement::go_to);
|
m.add_async_method("go_to", movement::go_to);
|
||||||
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_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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +99,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(())
|
||||||
}
|
}
|
||||||
|
@@ -1,80 +1,25 @@
|
|||||||
|
use super::{Client, Direction, Vec3};
|
||||||
use azalea::{
|
use azalea::{
|
||||||
BlockPos, BotClientExt, SprintDirection, WalkDirection,
|
BlockPos, BotClientExt, SprintDirection, WalkDirection,
|
||||||
core::hit_result::HitResult,
|
|
||||||
entity::Position,
|
entity::Position,
|
||||||
interact::HitResultComponent,
|
interact::HitResultComponent,
|
||||||
pathfinder::{
|
pathfinder::{
|
||||||
ExecutingPath, Pathfinder, PathfinderClientExt,
|
ExecutingPath, GotoEvent, Pathfinder, PathfinderClientExt,
|
||||||
goals::{BlockPosGoal, Goal, InverseGoal, 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,
|
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};
|
pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
|
||||||
|
let d = client.direction();
|
||||||
#[derive(Debug)]
|
Ok(Direction { y: d.0, x: d.1 })
|
||||||
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)]
|
pub fn eye_position(_lua: &Lua, client: &Client) -> Result<Vec3> {
|
||||||
fn to_goal(lua: &Lua, client: &Client, data: Table, options: &Table, kind: u8) -> Result<AnyGoal> {
|
Ok(Vec3::from(client.eye_position()))
|
||||||
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(if options.get("inverse").unwrap_or_default() {
|
|
||||||
Box::new(InverseGoal(AnyGoal(goal)))
|
|
||||||
} else {
|
|
||||||
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<()> {
|
|
||||||
client.wait_until_goto_target_reached().await;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn go_to(
|
pub async fn go_to(
|
||||||
@@ -82,82 +27,91 @@ pub async fn go_to(
|
|||||||
client: UserDataRef<Client>,
|
client: UserDataRef<Client>,
|
||||||
(data, metadata): (Table, Option<Table>),
|
(data, metadata): (Table, Option<Table>),
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let metadata = metadata.unwrap_or(lua.create_table()?);
|
fn goto_with_options<G: Goal + Send + Sync + 'static>(
|
||||||
let options = metadata.get("options").unwrap_or(lua.create_table()?);
|
client: &Client,
|
||||||
let goal = to_goal(
|
options: &Table,
|
||||||
&lua,
|
goal: G,
|
||||||
&client,
|
) {
|
||||||
data,
|
|
||||||
&options,
|
|
||||||
metadata.get("type").unwrap_or_default(),
|
|
||||||
)?;
|
|
||||||
if options.get("without_mining").unwrap_or_default() {
|
if options.get("without_mining").unwrap_or_default() {
|
||||||
client.start_goto_without_mining(goal);
|
client.goto_without_mining(goal);
|
||||||
client.wait_until_goto_target_reached().await;
|
|
||||||
} else {
|
} else {
|
||||||
client.goto(goal).await;
|
client.goto(goal);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn start_go_to(
|
let table = metadata.unwrap_or(lua.create_table()?);
|
||||||
lua: Lua,
|
let goal_type = table.get("type").unwrap_or_default();
|
||||||
client: UserDataRef<Client>,
|
let options = table.get("options").unwrap_or(lua.create_table()?);
|
||||||
(data, metadata): (Table, Option<Table>),
|
|
||||||
) -> Result<()> {
|
macro_rules! goto {
|
||||||
let metadata = metadata.unwrap_or(lua.create_table()?);
|
($goal:expr) => {
|
||||||
let options = metadata.get("options").unwrap_or(lua.create_table()?);
|
if options.get("inverse").unwrap_or_default() {
|
||||||
let goal = to_goal(
|
goto_with_options(&client, &options, InverseGoal($goal));
|
||||||
&lua,
|
|
||||||
&client,
|
|
||||||
data,
|
|
||||||
&options,
|
|
||||||
metadata.get("type").unwrap_or_default(),
|
|
||||||
)?;
|
|
||||||
if options.get("without_mining").unwrap_or_default() {
|
|
||||||
client.start_goto_without_mining(goal);
|
|
||||||
} else {
|
} else {
|
||||||
client.start_goto(goal);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let _ = client.get_tick_broadcaster().recv().await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
|
pub fn jump(_lua: &Lua, client: &mut Client, _: ()) -> Result<()> {
|
||||||
let direction = client.direction();
|
|
||||||
Ok(Direction {
|
|
||||||
y: direction.0,
|
|
||||||
x: direction.1,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn eye_position(_lua: &Lua, client: &Client) -> Result<Vec3> {
|
|
||||||
Ok(Vec3::from(client.eye_position()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn jump(_lua: &Lua, client: &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(())
|
||||||
}
|
}
|
||||||
@@ -192,12 +146,12 @@ 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(())
|
||||||
}
|
}
|
||||||
@@ -212,7 +166,7 @@ pub fn set_position(_lua: &Lua, client: &Client, new_position: Vec3) -> Result<(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
|
pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
|
||||||
client.write_packet(ServerboundPlayerCommand {
|
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
|
||||||
id: client.component::<MinecraftEntityId>(),
|
id: client.component::<MinecraftEntityId>(),
|
||||||
action: if sneaking {
|
action: if sneaking {
|
||||||
Action::PressShiftKey
|
Action::PressShiftKey
|
||||||
@@ -220,11 +174,13 @@ pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
|
|||||||
Action::ReleaseShiftKey
|
Action::ReleaseShiftKey
|
||||||
},
|
},
|
||||||
data: 0,
|
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,
|
||||||
@@ -233,21 +189,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,
|
||||||
|
@@ -1,13 +1,11 @@
|
|||||||
|
use super::Client;
|
||||||
use azalea::{
|
use azalea::{
|
||||||
ClientInformation,
|
ClientInformation,
|
||||||
entity::metadata::{AirSupply, Score},
|
entity::metadata::{AirSupply, Score},
|
||||||
pathfinder::debug::PathfinderDebugParticles,
|
|
||||||
protocol::common::client_information::ModelCustomization,
|
protocol::common::client_information::ModelCustomization,
|
||||||
};
|
};
|
||||||
use mlua::{Error, Lua, Result, Table, UserDataRef};
|
use log::error;
|
||||||
|
use mlua::{Lua, Result, Table, UserDataRef};
|
||||||
use super::Client;
|
|
||||||
use crate::hacks::anti_knockback::AntiKnockback;
|
|
||||||
|
|
||||||
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)
|
||||||
@@ -19,6 +17,7 @@ pub fn health(_lua: &Lua, client: &Client) -> Result<f32> {
|
|||||||
|
|
||||||
pub fn hunger(lua: &Lua, client: &Client) -> Result<Table> {
|
pub fn hunger(lua: &Lua, client: &Client) -> Result<Table> {
|
||||||
let hunger = client.hunger();
|
let hunger = client.hunger();
|
||||||
|
|
||||||
let table = lua.create_table()?;
|
let table = lua.create_table()?;
|
||||||
table.set("food", hunger.food)?;
|
table.set("food", hunger.food)?;
|
||||||
table.set("saturation", hunger.saturation)?;
|
table.set("saturation", hunger.saturation)?;
|
||||||
@@ -35,50 +34,28 @@ pub async fn set_client_information(
|
|||||||
info: Table,
|
info: Table,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
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
|
|
||||||
|
if let Err(error) = client
|
||||||
.set_client_information(ClientInformation {
|
.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()
|
||||||
})
|
})
|
||||||
.await;
|
.await
|
||||||
|
{
|
||||||
|
error!("failed to set client client information: {error:?}");
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_component(
|
|
||||||
_lua: &Lua,
|
|
||||||
client: &Client,
|
|
||||||
(name, enabled): (String, Option<bool>),
|
|
||||||
) -> Result<()> {
|
|
||||||
macro_rules! set {
|
|
||||||
($name:ident) => {{
|
|
||||||
let mut ecs = client.ecs.lock();
|
|
||||||
let mut entity = ecs.entity_mut(client.entity);
|
|
||||||
if enabled.unwrap_or(true) {
|
|
||||||
entity.insert($name)
|
|
||||||
} else {
|
|
||||||
entity.remove::<$name>()
|
|
||||||
};
|
|
||||||
Ok(())
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
match name.as_str() {
|
|
||||||
"AntiKnockback" => set!(AntiKnockback),
|
|
||||||
"PathfinderDebugParticles" => set!(PathfinderDebugParticles),
|
|
||||||
_ => Err(Error::external("invalid component")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@@ -1,125 +0,0 @@
|
|||||||
use azalea::{
|
|
||||||
BlockPos,
|
|
||||||
blocks::{BlockState, BlockStates},
|
|
||||||
ecs::query::{With, Without},
|
|
||||||
entity::{
|
|
||||||
Dead, EntityKind, EntityUuid, LookDirection, Pose, Position as AzaleaPosition,
|
|
||||||
metadata::{CustomName, Owneruuid, Player},
|
|
||||||
},
|
|
||||||
world::MinecraftEntityId,
|
|
||||||
};
|
|
||||||
use mlua::{Function, Lua, Result, Table, UserDataRef};
|
|
||||||
|
|
||||||
use super::{Client, Direction, Vec3};
|
|
||||||
|
|
||||||
pub fn blocks(
|
|
||||||
_lua: &Lua,
|
|
||||||
client: &Client,
|
|
||||||
(nearest_to, block_states): (Vec3, Vec<u16>),
|
|
||||||
) -> Result<Vec<Vec3>> {
|
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
|
||||||
Ok(client
|
|
||||||
.world()
|
|
||||||
.read()
|
|
||||||
.find_blocks(
|
|
||||||
BlockPos::new(
|
|
||||||
nearest_to.x as i32,
|
|
||||||
nearest_to.y as i32,
|
|
||||||
nearest_to.z as i32,
|
|
||||||
),
|
|
||||||
&BlockStates {
|
|
||||||
set: block_states
|
|
||||||
.into_iter()
|
|
||||||
.flat_map(BlockState::try_from)
|
|
||||||
.collect(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.map(Vec3::from)
|
|
||||||
.collect())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn all_entities(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
|
|
||||||
let mut matched = Vec::with_capacity(256);
|
|
||||||
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
|
|
||||||
get_entities!(client)
|
|
||||||
{
|
|
||||||
let table = lua.create_table()?;
|
|
||||||
table.set("position", position)?;
|
|
||||||
table.set("custom_name", custom_name)?;
|
|
||||||
table.set("kind", kind)?;
|
|
||||||
table.set("uuid", uuid)?;
|
|
||||||
table.set("direction", direction)?;
|
|
||||||
table.set("id", id)?;
|
|
||||||
table.set(
|
|
||||||
"owner_uuid",
|
|
||||||
owner_uuid.and_then(|v| *v).map(|v| v.to_string()),
|
|
||||||
)?;
|
|
||||||
table.set("pose", pose)?;
|
|
||||||
matched.push(table);
|
|
||||||
}
|
|
||||||
Ok(matched)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn entities(
|
|
||||||
lua: Lua,
|
|
||||||
client: UserDataRef<Client>,
|
|
||||||
filter_fn: Function,
|
|
||||||
) -> Result<Vec<Table>> {
|
|
||||||
let mut matched = Vec::new();
|
|
||||||
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
|
|
||||||
get_entities!(client)
|
|
||||||
{
|
|
||||||
let table = lua.create_table()?;
|
|
||||||
table.set("position", position)?;
|
|
||||||
table.set("custom_name", custom_name)?;
|
|
||||||
table.set("kind", kind)?;
|
|
||||||
table.set("uuid", uuid)?;
|
|
||||||
table.set("direction", direction)?;
|
|
||||||
table.set("id", id)?;
|
|
||||||
table.set(
|
|
||||||
"owner_uuid",
|
|
||||||
owner_uuid.and_then(|v| *v).map(|v| v.to_string()),
|
|
||||||
)?;
|
|
||||||
table.set("pose", pose)?;
|
|
||||||
if filter_fn.call_async::<bool>(&table).await? {
|
|
||||||
matched.push(table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(matched)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
|
|
||||||
let mut matched = Vec::new();
|
|
||||||
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
|
|
||||||
let table = lua.create_table()?;
|
|
||||||
table.set("id", id)?;
|
|
||||||
table.set("uuid", uuid)?;
|
|
||||||
table.set("kind", kind)?;
|
|
||||||
table.set("position", position)?;
|
|
||||||
table.set("direction", direction)?;
|
|
||||||
table.set("pose", pose)?;
|
|
||||||
matched.push(table);
|
|
||||||
}
|
|
||||||
Ok(matched)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn players(
|
|
||||||
lua: Lua,
|
|
||||||
client: UserDataRef<Client>,
|
|
||||||
filter_fn: Function,
|
|
||||||
) -> Result<Vec<Table>> {
|
|
||||||
let mut matched = Vec::new();
|
|
||||||
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
|
|
||||||
let table = lua.create_table()?;
|
|
||||||
table.set("id", id)?;
|
|
||||||
table.set("uuid", uuid)?;
|
|
||||||
table.set("kind", kind)?;
|
|
||||||
table.set("position", position)?;
|
|
||||||
table.set("direction", direction)?;
|
|
||||||
table.set("pose", pose)?;
|
|
||||||
if filter_fn.call_async::<bool>(&table).await? {
|
|
||||||
matched.push(table);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(matched)
|
|
||||||
}
|
|
@@ -1,27 +1,146 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod queries;
|
mod queries;
|
||||||
pub mod find;
|
|
||||||
|
|
||||||
use azalea::{BlockPos, auto_tool::AutoToolClientExt, blocks::BlockState, world::InstanceName};
|
|
||||||
use mlua::{Lua, Result, Table, Value};
|
|
||||||
|
|
||||||
use super::{Client, Direction, Vec3};
|
use super::{Client, Direction, Vec3};
|
||||||
|
use azalea::{
|
||||||
|
BlockPos,
|
||||||
|
auto_tool::AutoToolClientExt,
|
||||||
|
blocks::{BlockState, BlockStates},
|
||||||
|
ecs::query::{With, Without},
|
||||||
|
entity::{
|
||||||
|
Dead, EntityKind, EntityUuid, LookDirection, Pose, Position as AzaleaPosition,
|
||||||
|
metadata::{CustomName, Owneruuid, Player},
|
||||||
|
},
|
||||||
|
world::{InstanceName, MinecraftEntityId},
|
||||||
|
};
|
||||||
|
use mlua::{Function, Lua, Result, Table, UserDataRef};
|
||||||
|
|
||||||
|
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Table> {
|
||||||
|
let result = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
|
||||||
|
|
||||||
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Value> {
|
|
||||||
let Ok(block) = BlockState::try_from(block_state) else {
|
|
||||||
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::<InstanceName>().to_string())
|
Ok(client.component::<InstanceName>().to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_blocks(
|
||||||
|
_lua: &Lua,
|
||||||
|
client: &Client,
|
||||||
|
(nearest_to, block_states): (Vec3, Vec<u16>),
|
||||||
|
) -> Result<Vec<Vec3>> {
|
||||||
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
|
Ok(client
|
||||||
|
.world()
|
||||||
|
.read()
|
||||||
|
.find_blocks(
|
||||||
|
BlockPos::new(
|
||||||
|
nearest_to.x as i32,
|
||||||
|
nearest_to.y as i32,
|
||||||
|
nearest_to.z as i32,
|
||||||
|
),
|
||||||
|
&BlockStates {
|
||||||
|
set: block_states.iter().map(|&id| BlockState { id }).collect(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.map(Vec3::from)
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn find_all_entities(
|
||||||
|
lua: Lua,
|
||||||
|
client: UserDataRef<Client>,
|
||||||
|
(): (),
|
||||||
|
) -> Result<Vec<Table>> {
|
||||||
|
let mut matched = Vec::with_capacity(256);
|
||||||
|
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
|
||||||
|
get_entities!(client)
|
||||||
|
{
|
||||||
|
let table = lua.create_table()?;
|
||||||
|
table.set("position", position)?;
|
||||||
|
table.set("custom_name", custom_name)?;
|
||||||
|
table.set("kind", kind)?;
|
||||||
|
table.set("uuid", uuid)?;
|
||||||
|
table.set("direction", direction)?;
|
||||||
|
table.set("id", id)?;
|
||||||
|
table.set(
|
||||||
|
"owner_uuid",
|
||||||
|
owner_uuid.and_then(|v| *v).map(|v| v.to_string()),
|
||||||
|
)?;
|
||||||
|
table.set("pose", pose)?;
|
||||||
|
matched.push(table);
|
||||||
|
}
|
||||||
|
Ok(matched)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn find_entities(
|
||||||
|
lua: Lua,
|
||||||
|
client: UserDataRef<Client>,
|
||||||
|
filter_fn: Function,
|
||||||
|
) -> Result<Vec<Table>> {
|
||||||
|
let mut matched = Vec::new();
|
||||||
|
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
|
||||||
|
get_entities!(client)
|
||||||
|
{
|
||||||
|
let table = lua.create_table()?;
|
||||||
|
table.set("position", position)?;
|
||||||
|
table.set("custom_name", custom_name)?;
|
||||||
|
table.set("kind", kind)?;
|
||||||
|
table.set("uuid", uuid)?;
|
||||||
|
table.set("direction", direction)?;
|
||||||
|
table.set("id", id)?;
|
||||||
|
table.set(
|
||||||
|
"owner_uuid",
|
||||||
|
owner_uuid.and_then(|v| *v).map(|v| v.to_string()),
|
||||||
|
)?;
|
||||||
|
table.set("pose", pose)?;
|
||||||
|
if filter_fn.call_async::<bool>(&table).await? {
|
||||||
|
matched.push(table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(matched)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn find_all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
|
||||||
|
let mut matched = Vec::new();
|
||||||
|
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
|
||||||
|
let table = lua.create_table()?;
|
||||||
|
table.set("id", id)?;
|
||||||
|
table.set("uuid", uuid)?;
|
||||||
|
table.set("kind", kind)?;
|
||||||
|
table.set("position", position)?;
|
||||||
|
table.set("direction", direction)?;
|
||||||
|
table.set("pose", pose)?;
|
||||||
|
matched.push(table);
|
||||||
|
}
|
||||||
|
Ok(matched)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn find_players(
|
||||||
|
lua: Lua,
|
||||||
|
client: UserDataRef<Client>,
|
||||||
|
filter_fn: Function,
|
||||||
|
) -> Result<Vec<Table>> {
|
||||||
|
let mut matched = Vec::new();
|
||||||
|
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
|
||||||
|
let table = lua.create_table()?;
|
||||||
|
table.set("id", id)?;
|
||||||
|
table.set("uuid", uuid)?;
|
||||||
|
table.set("kind", kind)?;
|
||||||
|
table.set("position", position)?;
|
||||||
|
table.set("direction", direction)?;
|
||||||
|
table.set("pose", pose)?;
|
||||||
|
if filter_fn.call_async::<bool>(&table).await? {
|
||||||
|
matched.push(table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(matched)
|
||||||
|
}
|
||||||
|
|
||||||
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>> {
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
Ok(client
|
Ok(client
|
||||||
@@ -32,17 +151,17 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
|
|||||||
position.y as i32,
|
position.y as i32,
|
||||||
position.z as i32,
|
position.z as i32,
|
||||||
))
|
))
|
||||||
.map(|block| block.id()))
|
.map(|b| b.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 +169,6 @@ pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Opt
|
|||||||
Some(table)
|
Some(table)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,7 @@ use azalea::inventory::operations::{
|
|||||||
};
|
};
|
||||||
use mlua::{Result, Table};
|
use mlua::{Result, Table};
|
||||||
|
|
||||||
pub fn operation_from_table(op: &Table, op_type: Option<u8>) -> Result<ClickOperation> {
|
pub fn operation_from_table(op: Table, op_type: Option<u8>) -> Result<ClickOperation> {
|
||||||
Ok(match op_type.unwrap_or_default() {
|
Ok(match op_type.unwrap_or_default() {
|
||||||
0 => ClickOperation::Pickup(PickupClick::Left {
|
0 => ClickOperation::Pickup(PickupClick::Left {
|
||||||
slot: op.get("slot")?,
|
slot: op.get("slot")?,
|
||||||
|
@@ -1,19 +1,24 @@
|
|||||||
use azalea::inventory::{
|
use azalea::inventory::components::{CustomName, Damage, Food, MaxDamage};
|
||||||
self,
|
|
||||||
components::{Consumable, CustomName, Damage, Food, MaxDamage},
|
|
||||||
};
|
|
||||||
use mlua::{UserData, UserDataFields, UserDataMethods};
|
use mlua::{UserData, UserDataFields, UserDataMethods};
|
||||||
|
|
||||||
pub struct ItemStack(pub inventory::ItemStack);
|
pub struct ItemStack {
|
||||||
|
pub inner: azalea::inventory::ItemStack,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<azalea::inventory::ItemStack> for ItemStack {
|
||||||
|
fn from(inner: azalea::inventory::ItemStack) -> Self {
|
||||||
|
Self { inner }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl UserData for ItemStack {
|
impl UserData for ItemStack {
|
||||||
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
||||||
f.add_field_method_get("is_empty", |_, this| Ok(this.0.is_empty()));
|
f.add_field_method_get("is_empty", |_, this| Ok(this.inner.is_empty()));
|
||||||
f.add_field_method_get("is_present", |_, this| Ok(this.0.is_present()));
|
f.add_field_method_get("is_present", |_, this| Ok(this.inner.is_present()));
|
||||||
f.add_field_method_get("count", |_, this| Ok(this.0.count()));
|
f.add_field_method_get("count", |_, this| Ok(this.inner.count()));
|
||||||
f.add_field_method_get("kind", |_, this| Ok(this.0.kind().to_string()));
|
f.add_field_method_get("kind", |_, this| Ok(this.inner.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.inner.as_present().map(|data| {
|
||||||
data.components
|
data.components
|
||||||
.get::<CustomName>()
|
.get::<CustomName>()
|
||||||
.map(|c| c.name.to_string())
|
.map(|c| c.name.to_string())
|
||||||
@@ -21,39 +26,21 @@ impl UserData for ItemStack {
|
|||||||
});
|
});
|
||||||
f.add_field_method_get("damage", |_, this| {
|
f.add_field_method_get("damage", |_, this| {
|
||||||
Ok(this
|
Ok(this
|
||||||
.0
|
.inner
|
||||||
.as_present()
|
.as_present()
|
||||||
.map(|data| data.components.get::<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
|
.inner
|
||||||
.as_present()
|
.as_present()
|
||||||
.map(|data| data.components.get::<MaxDamage>().map(|d| d.amount)))
|
.map(|data| data.components.get::<MaxDamage>().map(|d| d.amount)))
|
||||||
});
|
});
|
||||||
|
|
||||||
f.add_field_method_get("consumable", |lua, this| {
|
|
||||||
Ok(
|
|
||||||
if let Some(consumable) = this
|
|
||||||
.0
|
|
||||||
.as_present()
|
|
||||||
.and_then(|data| data.components.get::<Consumable>())
|
|
||||||
{
|
|
||||||
let table = lua.create_table()?;
|
|
||||||
table.set("animation", consumable.animation as u8)?;
|
|
||||||
table.set("consume_seconds", consumable.consume_seconds)?;
|
|
||||||
table.set("has_consume_particles", consumable.has_consume_particles)?;
|
|
||||||
Some(table)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
f.add_field_method_get("food", |lua, this| {
|
f.add_field_method_get("food", |lua, this| {
|
||||||
Ok(
|
Ok(
|
||||||
if let Some(food) = this
|
if let Some(food) = this
|
||||||
.0
|
.inner
|
||||||
.as_present()
|
.as_present()
|
||||||
.and_then(|data| data.components.get::<Food>())
|
.and_then(|data| data.components.get::<Food>())
|
||||||
{
|
{
|
||||||
@@ -71,9 +58,11 @@ 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::from(this.inner.split(count)))
|
||||||
|
});
|
||||||
m.add_method_mut("update_empty", |_, this, (): ()| {
|
m.add_method_mut("update_empty", |_, this, (): ()| {
|
||||||
this.0.update_empty();
|
this.inner.update_empty();
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -6,20 +6,22 @@ use click::operation_from_table;
|
|||||||
use item_stack::ItemStack;
|
use item_stack::ItemStack;
|
||||||
use mlua::{Table, UserData, UserDataFields, UserDataMethods};
|
use mlua::{Table, UserData, UserDataFields, UserDataMethods};
|
||||||
|
|
||||||
pub struct Container(pub ContainerHandle);
|
pub struct Container {
|
||||||
|
pub inner: ContainerHandle,
|
||||||
|
}
|
||||||
|
|
||||||
impl UserData for Container {
|
impl UserData for Container {
|
||||||
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
||||||
f.add_field_method_get("id", |_, this| Ok(this.0.id()));
|
f.add_field_method_get("id", |_, this| Ok(this.inner.id()));
|
||||||
|
|
||||||
f.add_field_method_get("menu", |_, this| {
|
f.add_field_method_get("menu", |_, this| {
|
||||||
Ok(this.0.menu().map(|m| format!("{m:?}")))
|
Ok(this.inner.menu().map(|m| format!("{m:?}")))
|
||||||
});
|
});
|
||||||
|
|
||||||
f.add_field_method_get("contents", |_, this| {
|
f.add_field_method_get("contents", |_, this| {
|
||||||
Ok(this.0.contents().map(|v| {
|
Ok(this.inner.contents().map(|v| {
|
||||||
v.iter()
|
v.iter()
|
||||||
.map(|i| ItemStack(i.to_owned()))
|
.map(|i| ItemStack::from(i.to_owned()))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
}))
|
}))
|
||||||
});
|
});
|
||||||
@@ -29,28 +31,30 @@ impl UserData for Container {
|
|||||||
m.add_method(
|
m.add_method(
|
||||||
"click",
|
"click",
|
||||||
|_, this, (operation, operation_type): (Table, Option<u8>)| {
|
|_, this, (operation, operation_type): (Table, Option<u8>)| {
|
||||||
this.0
|
this.inner
|
||||||
.click(operation_from_table(&operation, operation_type)?);
|
.click(operation_from_table(operation, operation_type)?);
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ContainerRef(pub ContainerHandleRef);
|
pub struct ContainerRef {
|
||||||
|
pub inner: ContainerHandleRef,
|
||||||
|
}
|
||||||
|
|
||||||
impl UserData for ContainerRef {
|
impl UserData for ContainerRef {
|
||||||
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
||||||
f.add_field_method_get("id", |_, this| Ok(this.0.id()));
|
f.add_field_method_get("id", |_, this| Ok(this.inner.id()));
|
||||||
|
|
||||||
f.add_field_method_get("menu", |_, this| {
|
f.add_field_method_get("menu", |_, this| {
|
||||||
Ok(this.0.menu().map(|m| format!("{m:?}")))
|
Ok(this.inner.menu().map(|m| format!("{m:?}")))
|
||||||
});
|
});
|
||||||
|
|
||||||
f.add_field_method_get("contents", |_, this| {
|
f.add_field_method_get("contents", |_, this| {
|
||||||
Ok(this.0.contents().map(|v| {
|
Ok(this.inner.contents().map(|v| {
|
||||||
v.iter()
|
v.iter()
|
||||||
.map(|i| ItemStack(i.to_owned()))
|
.map(|i| ItemStack::from(i.to_owned()))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
}))
|
}))
|
||||||
});
|
});
|
||||||
@@ -58,15 +62,15 @@ impl UserData for ContainerRef {
|
|||||||
|
|
||||||
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
|
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
|
||||||
m.add_method("close", |_, this, (): ()| {
|
m.add_method("close", |_, this, (): ()| {
|
||||||
this.0.close();
|
this.inner.close();
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|
||||||
m.add_method(
|
m.add_method(
|
||||||
"click",
|
"click",
|
||||||
|_, this, (operation, operation_type): (Table, Option<u8>)| {
|
|_, this, (operation, operation_type): (Table, Option<u8>)| {
|
||||||
this.0
|
this.inner
|
||||||
.click(operation_from_table(&operation, operation_type)?);
|
.click(operation_from_table(operation, operation_type)?);
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
use azalea::entity::LookDirection;
|
use azalea::entity::LookDirection;
|
||||||
use mlua::{Error, FromLua, IntoLua, Lua, Result, Value};
|
use mlua::{FromLua, IntoLua, Lua, Result, Value};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Direction {
|
pub struct Direction {
|
||||||
@@ -37,7 +37,7 @@ impl FromLua for Direction {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(Error::FromLuaConversionError {
|
Err(mlua::Error::FromLuaConversionError {
|
||||||
from: value.type_name(),
|
from: value.type_name(),
|
||||||
to: "Direction".to_string(),
|
to: "Direction".to_string(),
|
||||||
message: None,
|
message: None,
|
||||||
|
@@ -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);
|
||||||
}
|
}
|
||||||
|
@@ -1,63 +0,0 @@
|
|||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use matrix_sdk::{
|
|
||||||
Client as MatrixClient,
|
|
||||||
ruma::{RoomId, UserId},
|
|
||||||
};
|
|
||||||
use mlua::{Error, UserData, UserDataFields, UserDataMethods};
|
|
||||||
|
|
||||||
use super::room::Room;
|
|
||||||
|
|
||||||
pub struct Client(pub Arc<MatrixClient>);
|
|
||||||
|
|
||||||
impl UserData for Client {
|
|
||||||
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
|
||||||
f.add_field_method_get("invited_rooms", |_, this| {
|
|
||||||
Ok(this
|
|
||||||
.0
|
|
||||||
.invited_rooms()
|
|
||||||
.into_iter()
|
|
||||||
.map(Room)
|
|
||||||
.collect::<Vec<_>>())
|
|
||||||
});
|
|
||||||
f.add_field_method_get("joined_rooms", |_, this| {
|
|
||||||
Ok(this
|
|
||||||
.0
|
|
||||||
.joined_rooms()
|
|
||||||
.into_iter()
|
|
||||||
.map(Room)
|
|
||||||
.collect::<Vec<_>>())
|
|
||||||
});
|
|
||||||
f.add_field_method_get("left_rooms", |_, this| {
|
|
||||||
Ok(this
|
|
||||||
.0
|
|
||||||
.left_rooms()
|
|
||||||
.into_iter()
|
|
||||||
.map(Room)
|
|
||||||
.collect::<Vec<_>>())
|
|
||||||
});
|
|
||||||
f.add_field_method_get("rooms", |_, this| {
|
|
||||||
Ok(this.0.rooms().into_iter().map(Room).collect::<Vec<_>>())
|
|
||||||
});
|
|
||||||
f.add_field_method_get("user_id", |_, this| {
|
|
||||||
Ok(this.0.user_id().map(ToString::to_string))
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
|
|
||||||
m.add_async_method("create_dm", async |_, this, user_id: String| {
|
|
||||||
this.0
|
|
||||||
.create_dm(&UserId::parse(user_id).map_err(Error::external)?)
|
|
||||||
.await
|
|
||||||
.map_err(Error::external)
|
|
||||||
.map(Room)
|
|
||||||
});
|
|
||||||
m.add_async_method("join_room_by_id", async |_, this, room_id: String| {
|
|
||||||
this.0
|
|
||||||
.join_room_by_id(&RoomId::parse(room_id).map_err(Error::external)?)
|
|
||||||
.await
|
|
||||||
.map_err(Error::external)
|
|
||||||
.map(Room)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
use matrix_sdk::room::RoomMember;
|
|
||||||
use mlua::{UserData, UserDataFields};
|
|
||||||
|
|
||||||
pub struct Member(pub RoomMember);
|
|
||||||
|
|
||||||
impl UserData for Member {
|
|
||||||
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
|
||||||
f.add_field_method_get("id", |_, this| Ok(this.0.user_id().to_string()));
|
|
||||||
f.add_field_method_get("name", |_, this| Ok(this.0.name().to_owned()));
|
|
||||||
f.add_field_method_get("power_level", |_, this| Ok(this.0.power_level()));
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,3 +0,0 @@
|
|||||||
pub mod client;
|
|
||||||
pub mod member;
|
|
||||||
pub mod room;
|
|
@@ -1,89 +0,0 @@
|
|||||||
use matrix_sdk::{
|
|
||||||
RoomMemberships,
|
|
||||||
room::Room as MatrixRoom,
|
|
||||||
ruma::{EventId, UserId, events::room::message::RoomMessageEventContent},
|
|
||||||
};
|
|
||||||
use mlua::{Error, UserData, UserDataFields, UserDataMethods};
|
|
||||||
|
|
||||||
use super::member::Member;
|
|
||||||
|
|
||||||
pub struct Room(pub MatrixRoom);
|
|
||||||
|
|
||||||
impl UserData for Room {
|
|
||||||
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
|
||||||
f.add_field_method_get("id", |_, this| Ok(this.0.room_id().to_string()));
|
|
||||||
f.add_field_method_get("name", |_, this| Ok(this.0.name()));
|
|
||||||
f.add_field_method_get("topic", |_, this| Ok(this.0.topic()));
|
|
||||||
f.add_field_method_get("type", |_, this| {
|
|
||||||
Ok(this.0.room_type().map(|room_type| room_type.to_string()))
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
|
|
||||||
m.add_async_method(
|
|
||||||
"ban_user",
|
|
||||||
async |_, this, (user_id, reason): (String, Option<String>)| {
|
|
||||||
this.0
|
|
||||||
.ban_user(
|
|
||||||
&UserId::parse(user_id).map_err(Error::external)?,
|
|
||||||
reason.as_deref(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(Error::external)
|
|
||||||
},
|
|
||||||
);
|
|
||||||
m.add_async_method("get_members", async |_, this, (): ()| {
|
|
||||||
this.0
|
|
||||||
.members(RoomMemberships::all())
|
|
||||||
.await
|
|
||||||
.map_err(Error::external)
|
|
||||||
.map(|members| members.into_iter().map(Member).collect::<Vec<_>>())
|
|
||||||
});
|
|
||||||
m.add_async_method(
|
|
||||||
"kick_user",
|
|
||||||
async |_, this, (user_id, reason): (String, Option<String>)| {
|
|
||||||
this.0
|
|
||||||
.kick_user(
|
|
||||||
&UserId::parse(user_id).map_err(Error::external)?,
|
|
||||||
reason.as_deref(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(Error::external)
|
|
||||||
},
|
|
||||||
);
|
|
||||||
m.add_async_method("leave", async |_, this, (): ()| {
|
|
||||||
this.0.leave().await.map_err(Error::external)
|
|
||||||
});
|
|
||||||
m.add_async_method(
|
|
||||||
"redact",
|
|
||||||
async |_, this, (event_id, reason): (String, Option<String>)| {
|
|
||||||
this.0
|
|
||||||
.redact(
|
|
||||||
&EventId::parse(event_id).map_err(Error::external)?,
|
|
||||||
reason.as_deref(),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(Error::external)
|
|
||||||
.map(|response| response.event_id.to_string())
|
|
||||||
},
|
|
||||||
);
|
|
||||||
m.add_async_method("send", async |_, this, body: String| {
|
|
||||||
this.0
|
|
||||||
.send(RoomMessageEventContent::text_plain(body))
|
|
||||||
.await
|
|
||||||
.map_err(Error::external)
|
|
||||||
.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())
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -7,22 +7,14 @@ pub mod logging;
|
|||||||
pub mod nochatreports;
|
pub mod nochatreports;
|
||||||
pub mod player;
|
pub mod player;
|
||||||
pub mod system;
|
pub mod system;
|
||||||
pub mod thread;
|
|
||||||
pub mod vec3;
|
pub mod vec3;
|
||||||
|
|
||||||
#[cfg(feature = "matrix")]
|
|
||||||
pub mod matrix;
|
|
||||||
|
|
||||||
use std::{
|
|
||||||
fmt::{self, Display, Formatter},
|
|
||||||
io,
|
|
||||||
};
|
|
||||||
|
|
||||||
use mlua::{Lua, Table};
|
|
||||||
|
|
||||||
use crate::{ListenerMap, build_info::built};
|
use crate::{ListenerMap, build_info::built};
|
||||||
|
use mlua::{Lua, Table};
|
||||||
|
use std::{io, time::Duration};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
CreateEnv(mlua::Error),
|
CreateEnv(mlua::Error),
|
||||||
EvalChunk(mlua::Error),
|
EvalChunk(mlua::Error),
|
||||||
@@ -32,23 +24,6 @@ pub enum Error {
|
|||||||
ReadFile(io::Error),
|
ReadFile(io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
|
||||||
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(
|
|
||||||
formatter,
|
|
||||||
"failed to {}",
|
|
||||||
match self {
|
|
||||||
Self::CreateEnv(error) => format!("create environment: {error}"),
|
|
||||||
Self::EvalChunk(error) => format!("evaluate chunk: {error}"),
|
|
||||||
Self::ExecChunk(error) => format!("execute chunk: {error}"),
|
|
||||||
Self::LoadChunk(error) => format!("load chunk: {error}"),
|
|
||||||
Self::MissingPath(error) => format!("get SCRIPT_PATH global: {error}"),
|
|
||||||
Self::ReadFile(error) => format!("read script file: {error}"),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn register_globals(
|
pub fn register_globals(
|
||||||
lua: &Lua,
|
lua: &Lua,
|
||||||
globals: &Table,
|
globals: &Table,
|
||||||
@@ -58,12 +33,19 @@ pub fn register_globals(
|
|||||||
globals.set("GIT_COMMIT_HASH", built::GIT_COMMIT_HASH)?;
|
globals.set("GIT_COMMIT_HASH", built::GIT_COMMIT_HASH)?;
|
||||||
globals.set("GIT_COMMIT_HASH_SHORT", built::GIT_COMMIT_HASH_SHORT)?;
|
globals.set("GIT_COMMIT_HASH_SHORT", built::GIT_COMMIT_HASH_SHORT)?;
|
||||||
|
|
||||||
|
globals.set(
|
||||||
|
"sleep",
|
||||||
|
lua.create_async_function(async |_, duration: u64| {
|
||||||
|
tokio::time::sleep(Duration::from_millis(duration)).await;
|
||||||
|
Ok(())
|
||||||
|
})?,
|
||||||
|
)?;
|
||||||
|
|
||||||
block::register_globals(lua, globals)?;
|
block::register_globals(lua, globals)?;
|
||||||
events::register_globals(lua, globals, event_listeners)?;
|
events::register_globals(lua, globals, event_listeners)?;
|
||||||
logging::register_globals(lua, globals)?;
|
logging::register_globals(lua, globals)?;
|
||||||
nochatreports::register_globals(lua, globals)?;
|
nochatreports::register_globals(lua, globals)?;
|
||||||
system::register_globals(lua, globals)?;
|
system::register_globals(lua, globals)
|
||||||
thread::register_globals(lua, globals)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reload(lua: &Lua, sender: Option<String>) -> Result<(), Error> {
|
pub fn reload(lua: &Lua, sender: Option<String>) -> Result<(), Error> {
|
||||||
|
@@ -4,7 +4,7 @@ macro_rules! crypt {
|
|||||||
macro_rules! crypt_with {
|
macro_rules! crypt_with {
|
||||||
($algo:ident) => {{
|
($algo:ident) => {{
|
||||||
let encoding = $options.get("encoding").unwrap_or_default();
|
let encoding = $options.get("encoding").unwrap_or_default();
|
||||||
let key = &$options.get::<UserDataRef<AesKey>>("key")?.0;
|
let key = &$options.get::<UserDataRef<AesKey>>("$key")?.inner;
|
||||||
match encoding {
|
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),
|
||||||
|
@@ -1,10 +1,12 @@
|
|||||||
use mlua::{UserData, UserDataFields};
|
use mlua::UserData;
|
||||||
|
|
||||||
pub struct AesKey(pub ncr::AesKey);
|
pub struct AesKey {
|
||||||
|
pub inner: ncr::AesKey,
|
||||||
|
}
|
||||||
|
|
||||||
impl UserData for AesKey {
|
impl UserData for AesKey {
|
||||||
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
fn add_fields<F: mlua::UserDataFields<Self>>(f: &mut F) {
|
||||||
f.add_field_method_get("base64", |_, this| Ok(this.0.encode_base64()));
|
f.add_field_method_get("base64", |_, this| Ok(this.inner.encode_base64()));
|
||||||
f.add_field_method_get("bytes", |_, this| Ok(this.0.as_ref().to_vec()));
|
f.add_field_method_get("bytes", |_, this| Ok(this.inner.as_ref().to_vec()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -14,23 +14,29 @@ pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
|
|||||||
globals.set(
|
globals.set(
|
||||||
"ncr_aes_key_from_passphrase",
|
"ncr_aes_key_from_passphrase",
|
||||||
lua.create_function(|_, passphrase: Vec<u8>| {
|
lua.create_function(|_, passphrase: Vec<u8>| {
|
||||||
Ok(AesKey(ncr::AesKey::gen_from_passphrase(&passphrase)))
|
Ok(AesKey {
|
||||||
|
inner: ncr::AesKey::gen_from_passphrase(&passphrase),
|
||||||
|
})
|
||||||
})?,
|
})?,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
globals.set(
|
globals.set(
|
||||||
"ncr_aes_key_from_base64",
|
"ncr_aes_key_from_base64",
|
||||||
lua.create_function(|_, base64: String| {
|
lua.create_function(|_, base64: String| {
|
||||||
Ok(AesKey(
|
Ok(AesKey {
|
||||||
ncr::AesKey::decode_base64(&base64)
|
inner: ncr::AesKey::decode_base64(&base64)
|
||||||
.map_err(|error| Error::external(error.to_string()))?,
|
.map_err(|error| Error::external(error.to_string()))?,
|
||||||
))
|
})
|
||||||
})?,
|
})?,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
globals.set(
|
globals.set(
|
||||||
"ncr_generate_random_aes_key",
|
"ncr_generate_random_aes_key",
|
||||||
lua.create_function(|_, (): ()| Ok(AesKey(ncr::AesKey::gen_random_key())))?,
|
lua.create_function(|_, (): ()| {
|
||||||
|
Ok(AesKey {
|
||||||
|
inner: ncr::AesKey::gen_random_key(),
|
||||||
|
})
|
||||||
|
})?,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
globals.set(
|
globals.set(
|
||||||
|
@@ -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",
|
||||||
|
@@ -1,28 +0,0 @@
|
|||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use mlua::{Error, Function, Lua, Result, Table};
|
|
||||||
use tokio::time::{sleep, timeout};
|
|
||||||
|
|
||||||
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
|
|
||||||
globals.set(
|
|
||||||
"sleep",
|
|
||||||
lua.create_async_function(async |_, duration: u64| {
|
|
||||||
sleep(Duration::from_millis(duration)).await;
|
|
||||||
Ok(())
|
|
||||||
})?,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
globals.set(
|
|
||||||
"timeout",
|
|
||||||
lua.create_async_function(async |_, (duration, function): (u64, Function)| {
|
|
||||||
timeout(
|
|
||||||
Duration::from_millis(duration),
|
|
||||||
function.call_async::<()>(()),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(Error::external)
|
|
||||||
})?,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@@ -1,5 +1,5 @@
|
|||||||
use azalea::{BlockPos, entity::Position};
|
use azalea::{BlockPos, entity::Position};
|
||||||
use mlua::{Error, FromLua, IntoLua, Lua, Result, Value};
|
use mlua::{FromLua, IntoLua, Lua, Result, Value};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Vec3 {
|
pub struct Vec3 {
|
||||||
@@ -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),
|
||||||
@@ -63,7 +63,7 @@ impl FromLua for Vec3 {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::FromLuaConversionError {
|
Err(mlua::Error::FromLuaConversionError {
|
||||||
from: value.type_name(),
|
from: value.type_name(),
|
||||||
to: "Vec3".to_string(),
|
to: "Vec3".to_string(),
|
||||||
message: None,
|
message: None,
|
||||||
|
77
src/main.rs
77
src/main.rs
@@ -1,28 +1,15 @@
|
|||||||
#![feature(if_let_guard, let_chains)]
|
#![feature(let_chains)]
|
||||||
#![warn(clippy::pedantic, clippy::nursery)]
|
|
||||||
#![allow(clippy::significant_drop_tightening)]
|
|
||||||
|
|
||||||
mod arguments;
|
mod arguments;
|
||||||
mod build_info;
|
mod build_info;
|
||||||
mod commands;
|
mod commands;
|
||||||
mod events;
|
mod events;
|
||||||
mod hacks;
|
|
||||||
mod http;
|
mod http;
|
||||||
mod lua;
|
mod lua;
|
||||||
mod particle;
|
mod particle;
|
||||||
mod replay;
|
mod replay;
|
||||||
|
|
||||||
#[cfg(feature = "matrix")]
|
use anyhow::Context;
|
||||||
mod matrix;
|
|
||||||
|
|
||||||
use std::{
|
|
||||||
collections::HashMap,
|
|
||||||
env,
|
|
||||||
fs::{OpenOptions, read_to_string},
|
|
||||||
sync::Arc,
|
|
||||||
};
|
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
|
||||||
use arguments::Arguments;
|
use arguments::Arguments;
|
||||||
use azalea::{
|
use azalea::{
|
||||||
DefaultBotPlugins, DefaultPlugins, brigadier::prelude::CommandDispatcher, prelude::*,
|
DefaultBotPlugins, DefaultPlugins, brigadier::prelude::CommandDispatcher, prelude::*,
|
||||||
@@ -36,55 +23,56 @@ 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 mlua::{Function, Lua, Table};
|
use mlua::{Function, Lua, Table};
|
||||||
use replay::{plugin::RecordPlugin, recorder::Recorder};
|
use replay::{Recorder, plugin::RecordPlugin};
|
||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
env,
|
||||||
|
fs::{OpenOptions, read_to_string},
|
||||||
|
path::PathBuf,
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg(feature = "mimalloc")]
|
#[cfg(feature = "mimalloc")]
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
static GLOBAL: 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();
|
||||||
|
|
||||||
let args = Arguments::parse();
|
let args = Arguments::parse();
|
||||||
|
let script_path = args.script.unwrap_or(PathBuf::from("errornowatcher.lua"));
|
||||||
let event_listeners = Arc::new(RwLock::new(HashMap::new()));
|
let event_listeners = Arc::new(RwLock::new(HashMap::new()));
|
||||||
let lua = unsafe { Lua::unsafe_new() };
|
let lua = unsafe { Lua::unsafe_new() };
|
||||||
let globals = lua.globals();
|
let globals = lua.globals();
|
||||||
lua::register_globals(&lua, &globals, event_listeners.clone())?;
|
|
||||||
|
|
||||||
if let Some(path) = args.script {
|
lua::register_globals(&lua, &globals, event_listeners.clone())?;
|
||||||
globals.set("SCRIPT_PATH", &*path)?;
|
globals.set("SCRIPT_PATH", &*script_path)?;
|
||||||
lua.load(read_to_string(path)?).exec()?;
|
lua.load(
|
||||||
} else if let Some(code) = ["main.lua", "errornowatcher.lua"].iter().find_map(|path| {
|
read_to_string(&script_path).with_context(|| format!("failed to read {script_path:?}"))?,
|
||||||
debug!("trying to load code from {path}");
|
)
|
||||||
globals.set("SCRIPT_PATH", *path).ok()?;
|
.exec()?;
|
||||||
read_to_string(path).ok()
|
|
||||||
}) {
|
|
||||||
lua.load(code).exec()?;
|
|
||||||
}
|
|
||||||
if let Some(code) = args.exec {
|
if let Some(code) = args.exec {
|
||||||
lua.load(code).exec()?;
|
lua.load(code).exec()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let server = globals
|
let server = globals
|
||||||
.get::<String>("Server")
|
.get::<String>("Server")
|
||||||
.context("lua globals missing Server variable")?;
|
.expect("Server should be in lua globals");
|
||||||
let username = globals
|
let username = globals
|
||||||
.get::<String>("Username")
|
.get::<String>("Username")
|
||||||
.context("lua globals missing Username variable")?;
|
.expect("Username should be in lua globals");
|
||||||
|
|
||||||
let mut commands = CommandDispatcher::new();
|
let mut commands = CommandDispatcher::new();
|
||||||
register(&mut commands);
|
register(&mut commands);
|
||||||
@@ -126,25 +114,26 @@ async fn main() -> Result<()> {
|
|||||||
},
|
},
|
||||||
)),
|
)),
|
||||||
};
|
};
|
||||||
let account = if username.contains('@') {
|
let Err(error) = ClientBuilder::new_without_plugins()
|
||||||
Account::microsoft(&username).await?
|
|
||||||
} else {
|
|
||||||
Account::offline(&username)
|
|
||||||
};
|
|
||||||
let Err(err) = ClientBuilder::new_without_plugins()
|
|
||||||
.add_plugins(DefaultBotPlugins)
|
|
||||||
.add_plugins(HacksPlugin)
|
|
||||||
.add_plugins(default_plugins)
|
.add_plugins(default_plugins)
|
||||||
.add_plugins(record_plugin)
|
.add_plugins(record_plugin)
|
||||||
|
.add_plugins(DefaultBotPlugins)
|
||||||
.set_handler(events::handle_event)
|
.set_handler(events::handle_event)
|
||||||
.set_state(State {
|
.set_state(State {
|
||||||
lua: Arc::new(lua),
|
lua: Arc::new(lua),
|
||||||
event_listeners,
|
event_listeners,
|
||||||
commands: Arc::new(commands),
|
commands: Arc::new(commands),
|
||||||
})
|
})
|
||||||
.start(account, server)
|
.start(
|
||||||
|
if username.contains('@') {
|
||||||
|
Account::microsoft(&username).await?
|
||||||
|
} else {
|
||||||
|
Account::offline(&username)
|
||||||
|
},
|
||||||
|
server.as_ref(),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
eprintln!("{err}");
|
eprintln!("{error}");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@@ -1,117 +0,0 @@
|
|||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use anyhow::Result;
|
|
||||||
use log::{debug, error};
|
|
||||||
use matrix_sdk::{
|
|
||||||
Client, Room, RoomState,
|
|
||||||
event_handler::Ctx,
|
|
||||||
ruma::events::room::{
|
|
||||||
member::StrippedRoomMemberEvent,
|
|
||||||
message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
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(
|
|
||||||
event: OriginalSyncRoomMessageEvent,
|
|
||||||
room: Room,
|
|
||||||
ctx: Ctx<Context>,
|
|
||||||
) -> Result<()> {
|
|
||||||
if room.state() != RoomState::Joined {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
let MessageType::Text(text_content) = event.content.msgtype else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
|
|
||||||
if text_content.body.starts_with(&ctx.name) && ctx.is_owner(&event.sender.to_string()) {
|
|
||||||
let body = text_content.body[ctx.name.len()..]
|
|
||||||
.trim_start_matches(':')
|
|
||||||
.trim();
|
|
||||||
let split = body.split_once(char::is_whitespace).unzip();
|
|
||||||
let code = split
|
|
||||||
.1
|
|
||||||
.map(|body| body.trim_start_matches("```lua").trim_matches(['`', '\n']));
|
|
||||||
|
|
||||||
let mut output = None;
|
|
||||||
match split.0.unwrap_or(body).to_lowercase().as_str() {
|
|
||||||
"reload" => {
|
|
||||||
output = Some(
|
|
||||||
reload(&ctx.state.lua, None)
|
|
||||||
.map_or_else(|error| error.to_string(), |()| String::from("ok")),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
"eval" if let Some(code) = code => {
|
|
||||||
output = Some(
|
|
||||||
eval(&ctx.state.lua, code, None)
|
|
||||||
.await
|
|
||||||
.unwrap_or_else(|error| error.to_string()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
"exec" if let Some(code) = code => {
|
|
||||||
output = Some(
|
|
||||||
exec(&ctx.state.lua, code, None)
|
|
||||||
.await
|
|
||||||
.map_or_else(|error| error.to_string(), |()| String::from("ok")),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
"ping" => {
|
|
||||||
room.send(RoomMessageEventContent::text_plain("pong!"))
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(output) = output {
|
|
||||||
room.send(RoomMessageEventContent::text_html(
|
|
||||||
&output,
|
|
||||||
format!("<pre><code>{output}</code></pre>"),
|
|
||||||
))
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
call_listeners(&ctx.state, "matrix_chat", || {
|
|
||||||
let table = ctx.state.lua.create_table()?;
|
|
||||||
table.set("room", LuaRoom(room))?;
|
|
||||||
table.set("sender_id", event.sender.to_string())?;
|
|
||||||
table.set("body", text_content.body)?;
|
|
||||||
Ok(table)
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn on_stripped_state_member(
|
|
||||||
member: StrippedRoomMemberEvent,
|
|
||||||
client: Client,
|
|
||||||
room: Room,
|
|
||||||
ctx: Ctx<Context>,
|
|
||||||
) -> Result<()> {
|
|
||||||
if let Some(user_id) = client.user_id()
|
|
||||||
&& member.state_key == user_id
|
|
||||||
&& ctx.is_owner(&member.sender.to_string())
|
|
||||||
{
|
|
||||||
debug!("joining room {}", room.room_id());
|
|
||||||
while let Err(error) = room.join().await {
|
|
||||||
error!("failed to join room {}: {error:?}", room.room_id());
|
|
||||||
sleep(Duration::from_secs(10)).await;
|
|
||||||
}
|
|
||||||
debug!("successfully joined room {}", room.room_id());
|
|
||||||
|
|
||||||
call_listeners(&ctx.state, "matrix_join_room", || {
|
|
||||||
let table = ctx.state.lua.create_table()?;
|
|
||||||
table.set("room", LuaRoom(room))?;
|
|
||||||
table.set("sender", member.sender.to_string())?;
|
|
||||||
Ok(table)
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@@ -1,149 +0,0 @@
|
|||||||
mod bot;
|
|
||||||
mod verification;
|
|
||||||
|
|
||||||
use std::{path::Path, sync::Arc, time::Duration};
|
|
||||||
|
|
||||||
use anyhow::{Context as _, Result};
|
|
||||||
use bot::{on_regular_room_message, on_stripped_state_member};
|
|
||||||
use log::{error, warn};
|
|
||||||
use matrix_sdk::{
|
|
||||||
Client, Error, LoopCtrl, authentication::matrix::MatrixSession, config::SyncSettings,
|
|
||||||
};
|
|
||||||
use mlua::Table;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use tokio::fs;
|
|
||||||
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)]
|
|
||||||
struct Context {
|
|
||||||
state: State,
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Context {
|
|
||||||
fn is_owner(&self, name: &String) -> bool {
|
|
||||||
self.state
|
|
||||||
.lua
|
|
||||||
.globals()
|
|
||||||
.get::<Table>("MatrixOptions")
|
|
||||||
.ok()
|
|
||||||
.and_then(|options| {
|
|
||||||
options
|
|
||||||
.get::<Vec<String>>("owners")
|
|
||||||
.ok()
|
|
||||||
.and_then(|owners| owners.contains(name).then_some(()))
|
|
||||||
})
|
|
||||||
.is_some()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize)]
|
|
||||||
struct Session {
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
sync_token: Option<String>,
|
|
||||||
user_session: MatrixSession,
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn persist_sync_token(
|
|
||||||
session_file: &Path,
|
|
||||||
session: &mut Session,
|
|
||||||
sync_token: String,
|
|
||||||
) -> Result<()> {
|
|
||||||
session.sync_token = Some(sync_token);
|
|
||||||
fs::write(session_file, serde_json::to_string(&session)?).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn login(state: &State, options: &Table, globals: &Table, name: String) -> Result<()> {
|
|
||||||
let (homeserver_url, username, password, sync_timeout) = (
|
|
||||||
options.get::<String>("homeserver_url")?,
|
|
||||||
options.get::<String>("username")?,
|
|
||||||
&options.get::<String>("password")?,
|
|
||||||
options.get::<u64>("sync_timeout"),
|
|
||||||
);
|
|
||||||
let root_dir = dirs::data_dir()
|
|
||||||
.context("no data directory")?
|
|
||||||
.join("errornowatcher")
|
|
||||||
.join(&name)
|
|
||||||
.join("matrix");
|
|
||||||
|
|
||||||
let mut builder = Client::builder().homeserver_url(homeserver_url);
|
|
||||||
if !fs::try_exists(&root_dir).await.unwrap_or_default()
|
|
||||||
&& let Err(error) = fs::create_dir_all(&root_dir).await
|
|
||||||
{
|
|
||||||
warn!("failed to create directory for matrix sqlite store: {error:?}");
|
|
||||||
} else {
|
|
||||||
builder = builder.sqlite_store(&root_dir, None);
|
|
||||||
}
|
|
||||||
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 session_file = root_dir.join("session.json");
|
|
||||||
if let Some(session) = fs::read_to_string(&session_file)
|
|
||||||
.await
|
|
||||||
.ok()
|
|
||||||
.and_then(|data| serde_json::from_str::<Session>(&data).ok())
|
|
||||||
{
|
|
||||||
new_session = session.clone();
|
|
||||||
if let Some(sync_token) = session.sync_token {
|
|
||||||
sync_settings = sync_settings.token(sync_token);
|
|
||||||
}
|
|
||||||
client.restore_session(session.user_session).await?;
|
|
||||||
} else {
|
|
||||||
let matrix_auth = client.matrix_auth();
|
|
||||||
matrix_auth
|
|
||||||
.login_username(username, password)
|
|
||||||
.initial_device_display_name(&name)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
new_session = Session {
|
|
||||||
user_session: matrix_auth.session().context("should have session")?,
|
|
||||||
sync_token: None,
|
|
||||||
};
|
|
||||||
fs::write(&session_file, serde_json::to_string(&new_session)?).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
client.add_event_handler_context(Context {
|
|
||||||
state: state.to_owned(),
|
|
||||||
name,
|
|
||||||
});
|
|
||||||
client.add_event_handler(on_stripped_state_member);
|
|
||||||
loop {
|
|
||||||
match client.sync_once(sync_settings.clone()).await {
|
|
||||||
Ok(response) => {
|
|
||||||
sync_settings = sync_settings.token(response.next_batch.clone());
|
|
||||||
persist_sync_token(&session_file, &mut new_session, response.next_batch).await?;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Err(error) => {
|
|
||||||
error!("failed to do initial sync: {error:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
client.add_event_handler(on_device_key_verification_request);
|
|
||||||
client.add_event_handler(on_room_message_verification_request);
|
|
||||||
client.add_event_handler(on_regular_room_message);
|
|
||||||
|
|
||||||
let client = Arc::new(client);
|
|
||||||
globals.set("matrix", LuaClient(client.clone()))?;
|
|
||||||
call_listeners(state, "matrix_init", || Ok(())).await?;
|
|
||||||
|
|
||||||
client
|
|
||||||
.sync_with_result_callback(sync_settings, |sync_result| async {
|
|
||||||
let mut new_session = new_session.clone();
|
|
||||||
persist_sync_token(&session_file, &mut new_session, sync_result?.next_batch)
|
|
||||||
.await
|
|
||||||
.map_err(|err| Error::UnknownError(err.into()))?;
|
|
||||||
Ok(LoopCtrl::Continue)
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@@ -1,156 +0,0 @@
|
|||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
|
||||||
use futures::StreamExt;
|
|
||||||
use log::{error, info, warn};
|
|
||||||
use matrix_sdk::{
|
|
||||||
Client,
|
|
||||||
crypto::{Emoji, SasState, format_emojis},
|
|
||||||
encryption::verification::{
|
|
||||||
SasVerification, Verification, VerificationRequest, VerificationRequestState,
|
|
||||||
},
|
|
||||||
ruma::{
|
|
||||||
UserId,
|
|
||||||
events::{
|
|
||||||
key::verification::request::ToDeviceKeyVerificationRequestEvent,
|
|
||||||
room::message::{MessageType, OriginalSyncRoomMessageEvent},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use tokio::time::sleep;
|
|
||||||
|
|
||||||
async fn confirm_emojis(sas: SasVerification, emoji: [Emoji; 7]) {
|
|
||||||
info!("\n{}", format_emojis(emoji));
|
|
||||||
warn!("automatically confirming emojis in 10 seconds");
|
|
||||||
sleep(Duration::from_secs(10)).await;
|
|
||||||
if let Err(error) = sas.confirm().await {
|
|
||||||
error!("failed to confirm emojis: {error:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn print_devices(user_id: &UserId, client: &Client) -> Result<()> {
|
|
||||||
info!("devices of user {user_id}");
|
|
||||||
|
|
||||||
let own_id = client.device_id().context("missing own device id")?;
|
|
||||||
for device in client
|
|
||||||
.encryption()
|
|
||||||
.get_user_devices(user_id)
|
|
||||||
.await?
|
|
||||||
.devices()
|
|
||||||
.filter(|device| device.device_id() != own_id)
|
|
||||||
{
|
|
||||||
info!(
|
|
||||||
"\t{:<10} {:<30} {:<}",
|
|
||||||
device.device_id(),
|
|
||||||
device.display_name().unwrap_or("-"),
|
|
||||||
if device.is_verified() { "✅" } else { "❌" }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn sas_verification_handler(client: Client, sas: SasVerification) -> Result<()> {
|
|
||||||
info!(
|
|
||||||
"starting verification with {} {}",
|
|
||||||
&sas.other_device().user_id(),
|
|
||||||
&sas.other_device().device_id()
|
|
||||||
);
|
|
||||||
print_devices(sas.other_device().user_id(), &client).await?;
|
|
||||||
sas.accept().await?;
|
|
||||||
|
|
||||||
while let Some(state) = sas.changes().next().await {
|
|
||||||
match state {
|
|
||||||
SasState::KeysExchanged {
|
|
||||||
emojis,
|
|
||||||
decimals: _,
|
|
||||||
} => {
|
|
||||||
tokio::spawn(confirm_emojis(
|
|
||||||
sas.clone(),
|
|
||||||
emojis.context("only emoji verification supported")?.emojis,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
SasState::Done { .. } => {
|
|
||||||
let device = sas.other_device();
|
|
||||||
info!(
|
|
||||||
"successfully verified device {} {} with trust {:?}",
|
|
||||||
device.user_id(),
|
|
||||||
device.device_id(),
|
|
||||||
device.local_trust_state()
|
|
||||||
);
|
|
||||||
print_devices(sas.other_device().user_id(), &client).await?;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
SasState::Cancelled(info) => {
|
|
||||||
warn!("verification cancelled: {}", info.reason());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
SasState::Created { .. }
|
|
||||||
| SasState::Started { .. }
|
|
||||||
| SasState::Accepted { .. }
|
|
||||||
| SasState::Confirmed => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn request_verification_handler(client: Client, request: VerificationRequest) {
|
|
||||||
info!(
|
|
||||||
"accepting verification request from {}",
|
|
||||||
request.other_user_id()
|
|
||||||
);
|
|
||||||
if let Err(error) = request.accept().await {
|
|
||||||
error!("failed to accept verification request: {error:?}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while let Some(state) = request.changes().next().await {
|
|
||||||
match state {
|
|
||||||
VerificationRequestState::Created { .. }
|
|
||||||
| VerificationRequestState::Requested { .. }
|
|
||||||
| VerificationRequestState::Ready { .. } => (),
|
|
||||||
VerificationRequestState::Transitioned { verification } => {
|
|
||||||
if let Verification::SasV1(sas) = verification {
|
|
||||||
tokio::spawn(async move {
|
|
||||||
if let Err(error) = sas_verification_handler(client, sas).await {
|
|
||||||
error!("failed to handle sas verification request: {error:?}");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
VerificationRequestState::Done | VerificationRequestState::Cancelled(_) => break,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn on_device_key_verification_request(
|
|
||||||
event: ToDeviceKeyVerificationRequestEvent,
|
|
||||||
client: Client,
|
|
||||||
) -> Result<()> {
|
|
||||||
let request = client
|
|
||||||
.encryption()
|
|
||||||
.get_verification_request(&event.sender, &event.content.transaction_id)
|
|
||||||
.await
|
|
||||||
.context("request object wasn't created")?;
|
|
||||||
tokio::spawn(request_verification_handler(client, request));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn on_room_message_verification_request(
|
|
||||||
event: OriginalSyncRoomMessageEvent,
|
|
||||||
client: Client,
|
|
||||||
) -> Result<()> {
|
|
||||||
if let MessageType::VerificationRequest(_) = &event.content.msgtype {
|
|
||||||
let request = client
|
|
||||||
.encryption()
|
|
||||||
.get_verification_request(&event.sender, &event.event_id)
|
|
||||||
.await
|
|
||||||
.context("request object wasn't created")?;
|
|
||||||
tokio::spawn(request_verification_handler(client, request));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@@ -1,7 +1,7 @@
|
|||||||
use azalea::{entity::particle::Particle, registry::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::Block(_) => ParticleKind::Block,
|
Particle::Block(_) => ParticleKind::Block,
|
||||||
@@ -34,7 +34,6 @@ pub const fn to_kind(particle: &Particle) -> ParticleKind {
|
|||||||
Particle::Flame => ParticleKind::Flame,
|
Particle::Flame => ParticleKind::Flame,
|
||||||
Particle::CherryLeaves => ParticleKind::CherryLeaves,
|
Particle::CherryLeaves => ParticleKind::CherryLeaves,
|
||||||
Particle::PaleOakLeaves => ParticleKind::PaleOakLeaves,
|
Particle::PaleOakLeaves => ParticleKind::PaleOakLeaves,
|
||||||
Particle::TintedLeaves => ParticleKind::TintedLeaves,
|
|
||||||
Particle::SculkSoul => ParticleKind::SculkSoul,
|
Particle::SculkSoul => ParticleKind::SculkSoul,
|
||||||
Particle::SculkCharge(_) => ParticleKind::SculkCharge,
|
Particle::SculkCharge(_) => ParticleKind::SculkCharge,
|
||||||
Particle::SculkChargePop => ParticleKind::SculkChargePop,
|
Particle::SculkChargePop => ParticleKind::SculkChargePop,
|
||||||
@@ -116,6 +115,5 @@ pub const fn to_kind(particle: &Particle) -> ParticleKind {
|
|||||||
Particle::TrialOmen => ParticleKind::TrialOmen,
|
Particle::TrialOmen => ParticleKind::TrialOmen,
|
||||||
Particle::Trail => ParticleKind::Trail,
|
Particle::Trail => ParticleKind::Trail,
|
||||||
Particle::BlockCrumble => ParticleKind::BlockCrumble,
|
Particle::BlockCrumble => ParticleKind::BlockCrumble,
|
||||||
Particle::Firefly => ParticleKind::Firefly,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,2 +1,89 @@
|
|||||||
pub mod plugin;
|
pub mod plugin;
|
||||||
pub mod recorder;
|
|
||||||
|
use crate::build_info;
|
||||||
|
use anyhow::Result;
|
||||||
|
use azalea::{
|
||||||
|
buf::AzaleaWriteVar,
|
||||||
|
prelude::Resource,
|
||||||
|
protocol::packets::{PROTOCOL_VERSION, ProtocolPacket, VERSION_NAME},
|
||||||
|
};
|
||||||
|
use serde_json::json;
|
||||||
|
use smallvec::SmallVec;
|
||||||
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::Write,
|
||||||
|
time::{Instant, SystemTime, UNIX_EPOCH},
|
||||||
|
};
|
||||||
|
use zip::{ZipWriter, write::SimpleFileOptions};
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct Recorder {
|
||||||
|
zip_writer: ZipWriter<File>,
|
||||||
|
start: Instant,
|
||||||
|
server: String,
|
||||||
|
ignore_compression: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Recorder {
|
||||||
|
pub fn new(path: String, server: String, ignore_compression: bool) -> Result<Self> {
|
||||||
|
let mut zip_writer = ZipWriter::new(
|
||||||
|
File::options()
|
||||||
|
.write(true)
|
||||||
|
.create(true)
|
||||||
|
.truncate(true)
|
||||||
|
.open(path)?,
|
||||||
|
);
|
||||||
|
zip_writer.start_file("recording.tmcpr", SimpleFileOptions::default())?;
|
||||||
|
Ok(Self {
|
||||||
|
zip_writer,
|
||||||
|
start: Instant::now(),
|
||||||
|
server,
|
||||||
|
ignore_compression,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn finish(mut self) -> Result<()> {
|
||||||
|
let elapsed = self.start.elapsed();
|
||||||
|
|
||||||
|
self.zip_writer
|
||||||
|
.start_file("metaData.json", SimpleFileOptions::default())?;
|
||||||
|
self.zip_writer.write_all(
|
||||||
|
json!({
|
||||||
|
"singleplayer": false,
|
||||||
|
"serverName": self.server,
|
||||||
|
"duration": elapsed.as_millis(),
|
||||||
|
"date": SystemTime::now().duration_since(UNIX_EPOCH)? - elapsed,
|
||||||
|
"mcversion": VERSION_NAME,
|
||||||
|
"fileFormat": "MCPR",
|
||||||
|
"fileFormatVersion": 14,
|
||||||
|
"protocol": PROTOCOL_VERSION,
|
||||||
|
"generator": build_info::version_formatted(),
|
||||||
|
})
|
||||||
|
.to_string()
|
||||||
|
.as_bytes(),
|
||||||
|
)?;
|
||||||
|
self.zip_writer.finish()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_timestamp(&self) -> Result<[u8; 4]> {
|
||||||
|
Ok(TryInto::<u32>::try_into(self.start.elapsed().as_millis())?.to_be_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_raw_packet(&mut self, raw_packet: &[u8]) -> Result<()> {
|
||||||
|
let mut data = Vec::with_capacity(raw_packet.len() + 8);
|
||||||
|
data.extend(self.get_timestamp()?);
|
||||||
|
data.extend(&TryInto::<u32>::try_into(raw_packet.len())?.to_be_bytes());
|
||||||
|
data.extend(raw_packet);
|
||||||
|
self.zip_writer.write_all(&data)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_packet<T: ProtocolPacket>(&mut self, packet: &T) -> Result<()> {
|
||||||
|
let mut raw_packet = SmallVec::<[u8; 256]>::new();
|
||||||
|
packet.id().azalea_write_var(&mut raw_packet)?;
|
||||||
|
packet.write(&mut raw_packet)?;
|
||||||
|
self.save_raw_packet(&raw_packet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -1,45 +1,42 @@
|
|||||||
#![allow(clippy::needless_pass_by_value)]
|
use super::Recorder;
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use azalea::{
|
use azalea::{
|
||||||
ecs::event::EventReader,
|
ecs::{event::EventReader, system::Query},
|
||||||
packet::{
|
packet_handling::{
|
||||||
config::ReceiveConfigPacketEvent, game::ReceiveGamePacketEvent,
|
configuration::ConfigurationEvent,
|
||||||
login::ReceiveLoginPacketEvent,
|
game::send_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::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_configuration_packets)
|
|
||||||
.add_systems(First, record_game_packets);
|
|
||||||
}
|
}
|
||||||
|
app.add_systems(First, record_login_packets.before(process_packet_events))
|
||||||
|
.add_systems(First, record_configuration_packets)
|
||||||
|
.add_systems(First, record_game_packets.before(send_packet_events));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn record_login_packets(
|
fn record_login_packets(
|
||||||
recorder: Option<ResMut<Recorder>>,
|
recorder: Option<ResMut<Recorder>>,
|
||||||
mut events: EventReader<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() {
|
||||||
if recorder.should_ignore_compression
|
if recorder.ignore_compression
|
||||||
&& let ClientboundLoginPacket::LoginCompression(_) = *event.packet
|
&& let ClientboundLoginPacket::LoginCompression(_) = *event.packet
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
@@ -54,24 +51,24 @@ fn record_login_packets(
|
|||||||
|
|
||||||
fn record_configuration_packets(
|
fn record_configuration_packets(
|
||||||
recorder: Option<ResMut<Recorder>>,
|
recorder: Option<ResMut<Recorder>>,
|
||||||
mut events: EventReader<ReceiveConfigPacketEvent>,
|
mut events: EventReader<ConfigurationEvent>,
|
||||||
) {
|
) {
|
||||||
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: EventReader<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:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,88 +0,0 @@
|
|||||||
use std::{
|
|
||||||
fs::File,
|
|
||||||
io::{BufWriter, Write},
|
|
||||||
time::{Instant, SystemTime, UNIX_EPOCH},
|
|
||||||
};
|
|
||||||
|
|
||||||
use anyhow::Result;
|
|
||||||
use azalea::{
|
|
||||||
buf::AzaleaWriteVar,
|
|
||||||
prelude::Resource,
|
|
||||||
protocol::packets::{PROTOCOL_VERSION, ProtocolPacket, VERSION_NAME},
|
|
||||||
};
|
|
||||||
use log::debug;
|
|
||||||
use serde_json::json;
|
|
||||||
use zip::{ZipWriter, write::SimpleFileOptions};
|
|
||||||
|
|
||||||
use crate::build_info;
|
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub struct Recorder {
|
|
||||||
zip_writer: BufWriter<ZipWriter<File>>,
|
|
||||||
start: Instant,
|
|
||||||
server: String,
|
|
||||||
pub should_ignore_compression: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Recorder {
|
|
||||||
pub fn new(path: String, server: String, should_ignore_compression: bool) -> Result<Self> {
|
|
||||||
let mut zip_writer = ZipWriter::new(
|
|
||||||
File::options()
|
|
||||||
.write(true)
|
|
||||||
.create(true)
|
|
||||||
.truncate(true)
|
|
||||||
.open(path)?,
|
|
||||||
);
|
|
||||||
zip_writer.start_file("recording.tmcpr", SimpleFileOptions::default())?;
|
|
||||||
Ok(Self {
|
|
||||||
zip_writer: BufWriter::new(zip_writer),
|
|
||||||
start: Instant::now(),
|
|
||||||
server,
|
|
||||||
should_ignore_compression,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn finish(self) -> Result<()> {
|
|
||||||
debug!("finishing replay recording");
|
|
||||||
|
|
||||||
let elapsed = self.start.elapsed().as_millis();
|
|
||||||
let mut zip_writer = self.zip_writer.into_inner()?;
|
|
||||||
zip_writer.start_file("metaData.json", SimpleFileOptions::default())?;
|
|
||||||
zip_writer.write_all(
|
|
||||||
json!({
|
|
||||||
"singleplayer": false,
|
|
||||||
"serverName": self.server,
|
|
||||||
"duration": elapsed,
|
|
||||||
"date": SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis() - elapsed,
|
|
||||||
"mcversion": VERSION_NAME,
|
|
||||||
"fileFormat": "MCPR",
|
|
||||||
"fileFormatVersion": 14,
|
|
||||||
"protocol": PROTOCOL_VERSION,
|
|
||||||
"generator": format!("ErrorNoWatcher {}", build_info::version_formatted()),
|
|
||||||
})
|
|
||||||
.to_string()
|
|
||||||
.as_bytes(),
|
|
||||||
)?;
|
|
||||||
zip_writer.finish()?;
|
|
||||||
|
|
||||||
debug!("finished replay recording");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn save_raw_packet(&mut self, raw_packet: &[u8]) -> Result<()> {
|
|
||||||
self.zip_writer.write_all(
|
|
||||||
&TryInto::<u32>::try_into(self.start.elapsed().as_millis())?.to_be_bytes(),
|
|
||||||
)?;
|
|
||||||
self.zip_writer
|
|
||||||
.write_all(&TryInto::<u32>::try_into(raw_packet.len())?.to_be_bytes())?;
|
|
||||||
self.zip_writer.write_all(raw_packet)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn save_packet<T: ProtocolPacket>(&mut self, packet: &T) -> Result<()> {
|
|
||||||
let mut raw_packet = Vec::with_capacity(64);
|
|
||||||
packet.id().azalea_write_var(&mut raw_packet)?;
|
|
||||||
packet.write(&mut raw_packet)?;
|
|
||||||
self.save_raw_packet(&raw_packet)
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user