Compare commits

...

130 Commits

Author SHA1 Message Date
b3b2252220 build(deps)!: update azalea and fix ecs changes 2025-05-19 21:27:52 -04:00
505b1a26af build(deps)!: update azalea and refactor go_to 2025-04-16 02:14:19 -04:00
f9495a36f2 refactor: cargo clippy improvements 2025-04-15 12:37:44 -04:00
85e1f082a7 fix(lib): avoid multiple food checks 2025-04-12 13:39:20 -04:00
33838e5aed refactor(client): remove redundant mut's 2025-04-12 12:34:05 -04:00
7cf7254dce chore(deps): update everything 2025-04-12 12:29:52 -04:00
94d1727d87 feat(matrix): make sync_timeout configurable 2025-04-03 20:30:44 -04:00
cc03ba6e72 chore(deps): disable termination feature for ctrlc 2025-03-31 16:54:55 -04:00
e213578646 feat(matrix): add matrix_init event 2025-03-31 16:54:52 -04:00
49a4400246 chore(deps): update for azalea entity deindexing fix 2025-03-27 21:22:10 -04:00
5e57678d5c refactor(http): directly use Error from import 2025-03-27 18:01:25 -04:00
1dc6519d0c chore(deps): update everything
Should work on 1.21.5 now.
2025-03-26 17:39:03 -04:00
170c1194ef refactor: remove redundant pub 2025-03-26 17:09:52 -04:00
e3d3e7fe5d refactor(matrix): increase sync timeout 2025-03-26 08:05:00 -04:00
7a365eab42 refactor: remove unused imports in minimal feature 2025-03-25 17:04:36 -04:00
65c4654e72 chore(deps): enable all features on tokio 2025-03-25 16:41:25 -04:00
940b4eb49e refactor(matrix): keep trying to log in 2025-03-25 16:41:25 -04:00
709b4a1d0d feat(lua/matrix): allow sending html messages 2025-03-24 20:20:56 -04:00
2fd0dec502 chore: use nightly toolchain 2025-03-24 16:55:06 -04:00
4da563ae0e style: set group_imports rustfmt option 2025-03-24 16:55:06 -04:00
1eca3ab5a4 refactor(replay)!: capitalize generator field in metadata 2025-03-24 16:55:06 -04:00
e618a8a27b chore(deps): clean up versions 2025-03-22 20:09:39 -04:00
ad24daae33 refactor(matrix): check prefix before owner 2025-03-21 07:58:47 -04:00
2814f4f43a refactor(lib): utilize spawn event 2025-03-20 22:33:55 -04:00
c8aec76075 chore(deps): update azalea 2025-03-20 22:33:54 -04:00
685f0a9cca refactor!(matrix): move options into a table 2025-03-20 22:33:54 -04:00
228b3e3e54 refactor: expand a few variable names 2025-03-20 18:32:07 -04:00
e7133ecc5f chore(deps): update everything 2025-03-19 18:22:35 -04:00
c9a5640436 feat(item_stack): add custom consumable component 2025-03-19 18:22:32 -04:00
417a234cd2 feat!: display newlines properly 2025-03-18 20:18:08 -04:00
e3cdf4260e refactor: bring azalea-hax crate in-tree 2025-03-18 17:53:50 -04:00
d1a64ee3a4 refactor(events): properly print lua error 2025-03-18 17:53:50 -04:00
f367fce138 refactor: minor changes 2025-03-18 17:53:50 -04:00
c2c9ca609e chore(deps): enable anyhow feature on matrix-sdk 2025-03-18 17:53:41 -04:00
c0a63bd756 refactor(lua): pass set client information error 2025-03-17 07:20:45 -04:00
3f0bfe937e chore(readme): update matrix integration 2025-03-16 12:25:13 -04:00
e2f908a9de refactor(matrix): clean up a few messages 2025-03-16 04:17:13 -04:00
2040eb0078 refactor: tweak script file handling 2025-03-15 23:44:44 -04:00
c7358fd4c0 feat(matrix): respond based on client username 2025-03-15 23:44:44 -04:00
ee82685b4e fix(matrix): properly handle sessions 2025-03-15 23:44:44 -04:00
e7300e1a37 chore(deps): only use dirs crate in matrix feature 2025-03-15 17:05:10 -04:00
8150e5c09c refactor(lua/client/world): split finders into module 2025-03-15 16:59:18 -04:00
0f2a5a0dc5 feat(lua/matrix): add more fields and methods 2025-03-15 16:59:18 -04:00
638dc75cb7 style: minor changes 2025-03-15 16:59:17 -04:00
ce98afb11d ci: separate lint workflow cache 2025-03-15 16:31:14 -04:00
9886f251b8 ci: check and build with features 2025-03-15 16:20:41 -04:00
dfac6e0413 refactor(lib/utils): simplify tps calculation 2025-03-15 15:59:49 -04:00
41b3375749 refactor: clean up import paths 2025-03-15 15:59:49 -04:00
e70b8eca84 feat: add matrix support 2025-03-15 15:08:54 -04:00
571581767e feat(lua): add timeout function 2025-03-14 18:04:04 -04:00
823a63e93d fix: add HaxPlugin for proper AntiKnockback 2025-03-14 17:29:04 -04:00
3e672c4d1a refactor(replay/recorder): write directly to zip file 2025-03-14 16:57:55 -04:00
a957aaeaec refactor: minor changes 2025-03-13 21:34:43 -04:00
3400541a79 feat(client): add AntiKnockback component from azalea-hax 2025-03-13 20:54:16 -04:00
5ec14d979d feat(client): add set_component method 2025-03-13 20:54:15 -04:00
34dc14d6b2 perf(replay/recorder): buffer writes to zip file 2025-03-13 19:57:28 -04:00
e1683f41c6 refactor: get rid of smallvec
Doesn't seem to be doing much.
2025-03-13 19:57:28 -04:00
477790db0e refactor(arguments): directly set default value for script in arguments 2025-03-13 17:34:11 -04:00
de5dbe600a perf: lazily evaluate listener arguments 2025-03-13 17:14:17 -04:00
10946ea7a4 refactor: clean up clippy lints 2025-03-13 16:38:31 -04:00
ac5533834d refactor: minor changes 2025-03-12 22:03:22 -04:00
7b76108b41 chore(arguments): update description 2025-03-12 21:32:32 -04:00
833835d8cb refactor(replay/recorder): use SmallVec for save_raw_packet 2025-03-12 18:32:39 -04:00
f9884227ef ci: also include toml files 2025-03-12 18:32:39 -04:00
b794cda10b refactor: move debug logging 2025-03-12 18:32:38 -04:00
85729401e5 refactor: directly wrap structs 2025-03-12 18:32:38 -04:00
44f7b9a00f refactor(http): clean up routing 2025-03-12 18:32:38 -04:00
5c03ecfd71 build: also use mold linker for aarch64-unknown-linux-gnu 2025-03-11 23:21:07 -04:00
4b4193f56d ci: upload build artifacts 2025-03-11 20:52:46 -04:00
bbb026b7bc ci: also build on ubuntu-24.04-arm 2025-03-11 20:40:08 -04:00
90512d631d feat: finish replay recording automatically on exit
Some checks failed
Lint / Cargo.toml (push) Has been cancelled
Lint / Rust (push) Has been cancelled
Build / errornowatcher (push) Has been cancelled
2025-03-11 20:18:58 -04:00
d8ac556884 perf(replay/recorder): reduce SmallVec size to 64 2025-03-11 20:18:58 -04:00
15cd2e673e fix(replay/plugin): still check if recorder exists
This partially backs out commit f77aea28d1.
2025-03-11 20:18:58 -04:00
9b6991ae80 chore(deps): remove redundant feature from zip 2025-03-11 20:18:57 -04:00
f5e787f2df refactor: use .display() on PathBuf 2025-03-11 20:18:57 -04:00
6cd4394b86 refactor(replay/recorder): inline self.get_timestamp 2025-03-11 20:18:57 -04:00
0fbd632c59 chore(deps): switch to git for built for pedantic clippy fix 2025-03-11 18:46:08 -04:00
7b6041989e ci: add build and lint workflows
Some checks are pending
Build / errornowatcher (push) Waiting to run
Lint / Cargo.toml (push) Waiting to run
Lint / Rust (push) Waiting to run
2025-03-11 18:37:14 -04:00
f77aea28d1 perf(replay): only add systems if recorder exists 2025-03-11 18:16:53 -04:00
ca1162e99a refactor: minor code improvements 2025-03-11 17:36:53 -04:00
e81fab7bf8 refactor(replay): move recorder into separate module 2025-03-11 17:33:23 -04:00
dda43999fe fix(nochatreports): remove extra $ from key 2025-03-11 17:33:23 -04:00
029a80456d fix(replay): properly save date and version 2025-03-11 17:33:23 -04:00
8ad28a43c2 chore(deps): use native rust deflate implementation 2025-03-11 17:33:23 -04:00
6600edabe6 feat: add feature for mimalloc 2025-03-11 17:33:23 -04:00
c2d1b415aa fix: properly print error when script read fails 2025-03-11 17:33:23 -04:00
4e5b076b60 feat(events): add set_time 2025-03-11 17:33:22 -04:00
6d8ce9b2be fix(lib): require lib first 2025-03-11 17:33:22 -04:00
09543b2dcd refactor(replay): use Instant for timestamps 2025-03-11 17:33:22 -04:00
b2d8618bba perf: slightly optimize Vec usage 2025-03-11 17:33:22 -04:00
2b2cf1d069 chore(lib): update examples 2025-03-11 17:33:22 -04:00
5c052de95d feat!: allow executing code after loading script 2025-03-11 17:33:22 -04:00
31e962fb9f chore(build): use .expect 2025-03-11 17:33:22 -04:00
18540aa223 refactor(http)!: get address from lua 2025-03-11 17:33:21 -04:00
caec5fa7f8 feat: add replaymod compatible recorder 2025-03-10 18:41:26 -04:00
1a2af8b7aa fix(events): properly check encryption status 2025-03-10 17:13:38 -04:00
884081d414 refactor(nochatreports): simplify crypt macro 2025-03-09 14:10:25 -04:00
c1268a8970 refactor(events)!: pass messages as tables 2025-03-09 13:57:25 -04:00
7af59c3ba2 refactor(events): don't force exit on disconnect 2025-03-09 03:41:43 -04:00
85e1efd9c1 feat(item_stack): add custom food component 2025-03-08 20:17:14 -05:00
f8c9dab689 feat(events/chat): pass encryption status 2025-03-08 16:33:42 -05:00
97961b0a49 refactor(events): don't return when no Owners 2025-03-08 16:00:05 -05:00
74a810b956 refactor: ignore errors for unknown commands 2025-03-08 15:53:34 -05:00
137493320c style: format files 2025-03-08 15:53:02 -05:00
a0fda6f285 refactor: print client error without formatting 2025-03-08 15:39:26 -05:00
9ce39ebb67 feat(client): add username field 2025-03-08 15:32:23 -05:00
225edd5529 feat!(events/chat): pass more message fields 2025-03-08 15:31:51 -05:00
8ae882a56a refactor(state): clean up client information tables 2025-03-07 18:20:39 -05:00
0ea2c81d2a refactor(movement): clean up goto matching with macro 2025-03-07 18:14:07 -05:00
73e7e17da4 refactor: clean up encryption/decryption 2025-03-07 17:45:03 -05:00
14e3781e05 refactor: remove finicky tick broadcasters from some methods 2025-03-07 17:35:21 -05:00
944f595841 feat(lib): update examples 2025-03-06 22:09:35 -05:00
492fecc32c refactor(world)!: create find_all variants of entity filters 2025-03-06 22:07:50 -05:00
1ebe7dd0b9 feat(container): add more menu types 2025-03-06 20:35:30 -05:00
1bd4e36676 feat(events): add keep_alive 2025-03-06 20:34:48 -05:00
cce75bd8b9 refactor: clean up a few things 2025-03-06 20:34:29 -05:00
4d3947c4ef feat: expose build information 2025-03-06 19:31:13 -05:00
b20beb6e88 refactor: slight log message tweaking 2025-03-06 19:02:46 -05:00
9001cd9701 feat: call event listeners in tasks 2025-03-06 18:30:25 -05:00
9ce4a7aa1f refactor: minor tweaks 2025-03-06 18:06:56 -05:00
c4454fe217 feat: add support for NoChatReports encryption 2025-03-06 13:17:01 -05:00
426c19304d refactor(container/item_stack): use map on Option 2025-03-05 16:51:21 -05:00
e8ff6664c9 feat(client): add id field 2025-03-03 18:11:36 -05:00
7278bfe62d feat: add/remove entity events 2025-03-03 17:20:25 -05:00
387e43e312 refactor: minor variable name changes 2025-03-03 16:47:56 -05:00
67ebdfac72 refactor(block): clean up variables 2025-03-03 07:41:43 -05:00
59e0d597a1 feat: allow loading all (C) modules 2025-03-01 23:25:59 -05:00
8162995e78 refactor!: remove_listener -> remove_listener 2025-03-01 17:00:39 -05:00
5b8e2280e3 fix(client/movemnet): allow missing goal types 2025-03-01 16:52:39 -05:00
04f5e0e8c2 fix(client/world): wrap Owneruuid in Option 2025-03-01 16:33:55 -05:00
60 changed files with 5712 additions and 1326 deletions

View File

@@ -1,3 +1,7 @@
[target.x86_64-unknown-linux-gnu]
linker = "clang"
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 Normal file
View File

@@ -0,0 +1,52 @@
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 Normal file
View File

@@ -0,0 +1,75 @@
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

3403
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

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

View File

@@ -5,12 +5,15 @@ A Minecraft bot with Lua scripting support, written in Rust with [azalea](https:
## Features
- Running Lua from
- `errornowatcher.lua`
- in-game chat messages
- Matrix chat messages
- POST requests to HTTP server
- Listening to in-game events
- Pathfinding (from azalea)
- Entity and chest interaction
- NoChatReports encryption
- Saving ReplayMod recordings
- Matrix integration (w/ E2EE)
## Usage
@@ -21,4 +24,4 @@ $ cargo build --release
$ # ./target/release/errornowatcher
```
Make sure the `Server` and `Username` globals are defined in `errornowatcher.lua` before starting the bot.
Make sure the `Server` and `Username` globals are defined in `main.lua` before starting the bot.

3
build.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
built::write_built_file().expect("appropriate environment variables should have been set");
}

136
lib/automation.lua Normal file
View File

@@ -0,0 +1,136 @@
FishingBobber = nil
FishingTicks = 0
FishLastCaught = 0
LastEaten = 0
function auto_fish()
stop_auto_fish()
FishingTicks = 0
function hold_fishing_rod()
if client.held_item.kind == "minecraft:fishing_rod" or hold_items({ "minecraft:fishing_rod" }) then
return true
end
warn("no fishing rod found!")
end
if not hold_fishing_rod() then
return
end
add_listener("add_entity", function(entity)
if entity.kind == "minecraft:fishing_bobber" and entity.data == client.id then
FishingBobber = entity
end
end, "auto-fish_watch-bobber")
add_listener("remove_entities", function(entity_ids)
if table.contains(entity_ids, FishingBobber.id) then
if os.time() - LastEaten < 3 then
sleep(3000)
end
hold_fishing_rod()
client:start_use_item()
end
end, "auto-fish_watch-bobber")
add_listener("level_particles", function(particle)
if particle.kind == 30 and particle.count == 6 then
local current_bobber = client:find_entities(function(e)
return e.id == FishingBobber.id
end)[1]
if distance(current_bobber.position, particle.position) <= 0.75 then
FishLastCaught = os.time()
client:start_use_item()
end
end
end, "auto-fish")
add_listener("tick", function()
FishingTicks = FishingTicks + 1
if FishingTicks % 600 ~= 0 then
return
end
if os.time() - FishLastCaught >= 60 then
hold_fishing_rod()
client:start_use_item()
end
end, "auto-fish_watchdog")
client:start_use_item()
end
function stop_auto_fish()
remove_listeners("add_entity", "auto-fish_watch-bobber")
remove_listeners("remove_entities", "auto-fish_watch-bobber")
remove_listeners("level_particles", "auto-fish")
remove_listeners("tick", "auto-fish_watchdog")
if FishingBobber and client:find_entities(function(e)
return e.id == FishingBobber.id
end)[1] then
FishingBobber = nil
client:start_use_item()
end
end
function attack_entities(target_kind, minimum)
if not minimum then
minimum = 0
end
function hold_sword()
if client.held_item.kind == "minecraft:iron_sword" or hold_items({ "minecraft:iron_sword" }) then
return true
end
warn("no sword found!")
end
while true do
local self_pos = client.position
local entities = client:find_entities(function(e)
return e.kind == target_kind and distance(e.position, self_pos) < 5
end)
if #entities > minimum then
local e = entities[1]
local pos = e.position
pos.y = pos.y + 1.5
hold_sword()
client:look_at(pos)
client:attack(e.id)
while client.has_attack_cooldown do
sleep(100)
end
else
sleep(1000)
end
end
end
function check_food(hunger)
if not hunger then
hunger = client.hunger
end
if hunger.food >= 20 then
return
end
local current_time = os.time()
if current_time - LastEaten >= 3 then
LastEaten = current_time
while not hold_items({
"minecraft:golden_carrot",
"minecraft:cooked_beef",
"minecraft:bread",
}) do
sleep(1000)
LastEaten = current_time
end
client:start_use_item()
end
end

View File

@@ -32,3 +32,14 @@ QUICK_CRAFT_START = 0
QUICK_CRAFT_ADD = 1
QUICK_CRAFT_END = 2
PICKUP_ALL = 11
POSE_NAMES = {
"standing",
"flying",
"sleeping",
"swimming",
"attacking",
"sneaking",
"jumping",
"dying",
}

View File

@@ -1,30 +1,90 @@
local center = { x = 0, z = 0 }
local radius = 100
Center = { x = 0, y = 64, z = 0 }
Radius = 100
Whitelist = table.shallow_copy(Owners)
Ticks = -1
function log_player_positions()
local entities = client:find_entities(function(e)
return e.kind == "minecraft:player"
and e.position.x > center.x - radius + 1
and e.position.x < center.x + radius
and e.position.z > center.z - radius
and e.position.z < center.z + radius
function check_radius()
Ticks = Ticks + 1
if Ticks % 20 ~= 0 then
return
end
local self_id = client.id
local players = client:find_players(function(p)
return self_id ~= p.id
and p.position.x > Center.x - Radius + 1
and p.position.x < Center.x + Radius
and p.position.z > Center.z - Radius
and p.position.z < Center.z + Radius
end)
for _, e in ipairs(entities) do
client:chat(string.format("%s (%s) at %.1f %.1f %.1f", e.kind, e.id, e.position.x, e.position.y, e.position.z))
local tab_list = client.tab_list
for _, player in ipairs(players) do
local target
for _, tab_player in ipairs(tab_list) do
if tab_player.uuid == player.uuid and not table.contains(Whitelist, tab_player.name) then
target = tab_player
break
end
end
if not target then
goto continue
end
client:chat(
string.format(
"%s is %s %d blocks away at %.2f %.2f %.2f facing %.2f %.2f",
target.name,
POSE_NAMES[player.pose + 1],
distance(Center, player.position),
player.position.x,
player.position.y,
player.position.z,
player.direction.x,
player.direction.y
)
)
::continue::
end
end
add_listener("init", function()
info("client initialized, setting information...")
client:set_client_information({ view_distance = 16 })
end)
function update_listeners()
for type, listeners in pairs(get_listeners()) do
for id, _ in pairs(listeners) do
remove_listeners(type, id)
end
end
add_listener("login", function()
info("player successfully logged in!")
end)
add_listener("death", function()
warn(string.format("player died at %.1f %.1f %.1f!", client.position.x, client.position.y, client.position.z))
end, "warn_player_died")
add_listener("tick", log_player_positions)
for type, listeners in pairs({
login = {
message = function()
info("bot successfully logged in!")
end,
eat = function()
sleep(5000)
check_food()
end,
},
death = {
warn_bot_died = function()
warn(
string.format(
"bot died at %.2f %.2f %.2f facing %.2f %.2f!",
client.position.x,
client.position.y,
client.position.z,
client.direction.x,
client.direction.y
)
)
end,
},
set_health = { auto_eat = check_food },
tick = { log_player_positions = check_radius },
}) do
for id, callback in pairs(listeners) do
add_listener(type, callback, id)
end
end
end

View File

@@ -1,11 +1,40 @@
function hold_items_in_hotbar(target_kinds, inventory)
if not inventory then
inventory = client:open_inventory()
end
for index, item in ipairs(inventory.contents) do
if index >= 37 and index <= 45 and table.contains(target_kinds, item.kind) then
inventory = nil
sleep(500)
client:set_held_slot(index - 37)
return true
end
end
return false
end
function hold_items(target_kinds)
local inventory = client:open_inventory()
if hold_items_in_hotbar(target_kinds, inventory) then
return true
end
for index, item in ipairs(inventory.contents) do
if table.contains(target_kinds, item.kind) then
inventory:click({ source_slot = index - 1, target_slot = client.held_slot }, SWAP)
sleep(100)
inventory = nil
sleep(500)
return true
end
end
inventory = nil
sleep(500)
return false
end
function steal(item_name)
for _, chest_pos in ipairs(client:find_blocks(client.position, get_block_states({ "chest" }))) do
client:chat(dump(chest_pos))
client:go_to({ position = chest_pos, radius = 3 }, { type = RADIUS_GOAL })
while client.pathfinder.is_calculating or client.pathfinder.is_executing do
sleep(50)
end
client:look_at(chest_pos)
local container = client:open_container_at(chest_pos)
@@ -23,6 +52,23 @@ function steal(item_name)
end
end
function dump_inventory(hide_empty)
local inventory = client:open_inventory()
for index, item in ipairs(inventory.contents) do
if hide_empty and item.count == 0 then
goto continue
end
local item_damage = ""
if item.damage then
item_damage = item.damage
end
info(string.format("%-2d = %2dx %-32s %s", index - 1, item.count, item.kind, item_damage))
::continue::
end
end
function drop_all_hotbar()
local inventory = client:open_inventory()
for i = 0, 9 do

72
lib/lib.lua Normal file
View File

@@ -0,0 +1,72 @@
function clock_gettime(clock)
local status, module = pcall(require, "posix")
posix = status and module or nil
if posix then
local s, ns = posix.clock_gettime(clock)
return s + ns / (10 ^ (math.floor(math.log10(ns)) + 1))
else
warn("failed to load posix module! falling back to os.time()")
return os.time()
end
end
function distance(p1, p2)
return math.sqrt((p2.x - p1.x) ^ 2 + (p2.y - p1.y) ^ 2 + (p2.z - p1.z) ^ 2)
end
function table.shallow_copy(t)
local t2 = {}
for k, v in pairs(t) do
t2[k] = v
end
return t2
end
function table.map(t, f)
local t2 = {}
for k, v in pairs(t) do
t2[k] = f(v)
end
return t2
end
function table.contains(t, target)
for _, v in pairs(t) do
if v == target then
return true
end
end
return false
end
function dump(object)
if type(object) == "table" then
local string = "{ "
local parts = {}
for key, value in pairs(object) do
table.insert(parts, key .. " = " .. dump(value))
end
string = string .. table.concat(parts, ", ")
return string .. " " .. "}"
else
return tostring(object)
end
end
function dumpp(object, level)
if not level then
level = 0
end
if type(object) == "table" then
local string = "{\n" .. string.rep(" ", level + 1)
local parts = {}
for key, value in pairs(object) do
table.insert(parts, key .. " = " .. dumpp(value, level + 1))
end
string = string .. table.concat(parts, ",\n" .. string.rep(" ", level + 1))
return string .. "\n" .. string.rep(" ", level) .. "}"
else
return tostring(object)
end
end

View File

@@ -3,16 +3,9 @@ function look_at_player(name)
if player then
player.position.y = player.position.y + 1
client:look_at(player.position)
else
client:chat(string.format("/w %s player not found!", sender))
end
end
function go_to_player(name, opts)
local player = get_player(name)
if player then
client:go_to(player.position, opts)
else
client:chat(string.format("/w %s player not found!", sender))
end
function go_to_player(name, go_to_opts)
client:go_to(get_player(name).position, go_to_opts)
end

View File

@@ -1,3 +1,168 @@
SpeedTracking = {}
TpsTracking = {}
function entity_speed(uuid, seconds)
if not seconds then
seconds = 1
end
local callback = function()
local old_entity = SpeedTracking[uuid]
local new_entity = client:find_entities(function(e)
return e.uuid == uuid
end)[1]
if not new_entity then
remove_listeners("tick", "speed-tracking_" .. uuid)
SpeedTracking[uuid] = -1
return
end
if old_entity then
old_entity._distance = old_entity._distance + distance(old_entity.position, new_entity.position)
old_entity.position = new_entity.position
if old_entity._ticks < seconds * 20 then
old_entity._ticks = old_entity._ticks + 1
else
remove_listeners("tick", "speed-tracking_" .. uuid)
SpeedTracking[uuid] = old_entity._distance / seconds
end
else
new_entity._ticks = 1
new_entity._distance = 0
SpeedTracking[uuid] = new_entity
end
end
add_listener("tick", callback, "speed-tracking_" .. uuid)
repeat
sleep(seconds * 1000 / 10)
until type(SpeedTracking[uuid]) == "number"
local speed = SpeedTracking[uuid]
SpeedTracking[uuid] = nil
return speed
end
function tps(ms)
if not ms then
ms = 1000
end
add_listener("tick", function()
if not TpsTracking.ticks then
TpsTracking.ticks = 0
sleep(ms)
TpsTracking.result = TpsTracking.ticks
remove_listeners("tick", "tps_tracking")
else
TpsTracking.ticks = TpsTracking.ticks + 1
end
end, "tps_tracking")
sleep(ms)
repeat
sleep(20)
until TpsTracking.result
local tps = TpsTracking.result / (ms / 1000)
TpsTracking = {}
return tps
end
function nether_travel(pos, go_to_opts)
info(string.format("going to %.2f %.2f %.2f through nether", pos.x, pos.y, pos.z))
local portal_block_states = get_block_states({ "nether_portal" })
local nether_pos = table.shallow_copy(pos)
nether_pos.x = nether_pos.x / 8
nether_pos.z = nether_pos.z / 8
if client.dimension == "minecraft:overworld" then
info("currently in overworld, finding nearest portal")
local portals = client:find_blocks(client.position, portal_block_states)
info(string.format("going to %.2f %.2f %.2f through nether", portals[1].x, portals[1].y, portals[1].z))
client:go_to(portals[1], go_to_opts)
while client.dimension ~= "minecraft:the_nether" do
sleep(1000)
end
sleep(3000)
end
info(string.format("currently in nether, going to %.2f %.2f", nether_pos.x, nether_pos.z))
client:go_to(nether_pos, { type = XZ_GOAL })
info("arrived, looking for nearest portal")
local portals_nether = client:find_blocks(client.position, portal_block_states)
if not next(portals_nether) then
warn("failed to find portals in the nether")
return
end
local found_portal = false
for _, portal in ipairs(portals_nether) do
if (client.position.y > 127) == (portal.y > 127) then
found_portal = true
info(string.format("found valid portal, going to %.2f %.2f %.2f", portal.x, portal.y, portal.z))
client:go_to(portal)
while client.dimension ~= "minecraft:overworld" do
sleep(1000)
end
sleep(3000)
end
if found_portal then
break
end
end
if not found_portal then
warn("failed to find valid portals in the nether")
return
end
info(string.format("back in overworld, going to %.2f %.2f %.2f", pos.x, pos.y, pos.z))
client:go_to(pos, go_to_opts)
end
function interact_bed()
local bed = client:find_blocks(
client.position,
get_block_states({
"brown_bed",
"white_bed",
"yellow_bed",
})
)[1]
if not bed then
return
end
client:go_to({ position = bed, radius = 2 }, { type = RADIUS_GOAL, options = { without_mining = true } })
client:look_at(bed)
client:block_interact(bed)
end
function closest_entity(target_kind)
local self_pos = client.position
local entities = client:find_entities(function(e)
return e.kind == target_kind
end)
local closest_entity = entities[1]
local closest_distance = distance(closest_entity.position, self_pos)
for _, entity in ipairs(entities) do
local dist = distance(entity.position, self_pos)
if dist <= closest_distance then
closest_entity = entity
closest_distance = dist
end
end
return closest_entity
end
function get_player(name)
local target_uuid = nil
for _, player in ipairs(client.tab_list) do

View File

@@ -1,8 +1,12 @@
Server = "localhost"
Username = "ErrorNoWatcher"
HttpAddress = "127.0.0.1:8080"
Owners = { "ErrorNoInternet" }
MatrixOptions = { owners = { "@errornointernet:envs.net" } }
for _, module in ipairs({
"lib",
"automation",
"enum",
"events",
"inventory",
@@ -13,3 +17,5 @@ for _, module in ipairs({
package.loaded[module] = nil
require(module)
end
update_listeners()

2
rust-toolchain.toml Normal file
View File

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

1
rustfmt.toml Normal file
View File

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

View File

@@ -1,14 +1,18 @@
use clap::Parser;
use std::{net::SocketAddr, path::PathBuf};
use std::path::PathBuf;
/// A Minecraft utility bot
use clap::Parser;
use crate::build_info;
/// A Minecraft bot with Lua scripting support
#[derive(Parser)]
#[command(version = build_info::version_formatted())]
pub struct Arguments {
/// Path to main Lua file
#[arg(short, long)]
pub script: Option<PathBuf>,
/// Socket address to bind HTTP server to
#[arg(short = 'a', long)]
pub http_address: Option<SocketAddr>,
/// Code to execute (after script)
#[arg(short, long)]
pub exec: Option<String>,
}

11
src/build_info.rs Normal file
View File

@@ -0,0 +1,11 @@
pub mod built {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
pub fn version_formatted() -> String {
format!(
"v{} ({})",
env!("CARGO_PKG_VERSION"),
built::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown commit")
)
}

View File

@@ -1,9 +1,12 @@
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
use futures::lock::Mutex;
use mlua::{Function, Table};
use ncr::utils::prepend_header;
use crate::{
State,
lua::{eval, exec, reload},
};
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
use futures::lock::Mutex;
pub type Ctx = CommandContext<Mutex<CommandSource>>;
@@ -11,19 +14,26 @@ pub struct CommandSource {
pub client: Client,
pub message: ChatPacket,
pub state: State,
pub ncr_options: Option<Table>,
}
impl CommandSource {
pub fn reply(&self, message: &str) {
for chunk in message
for mut chunk in message
.chars()
.collect::<Vec<char>>()
.chunks(236)
.chunks(if self.ncr_options.is_some() { 150 } else { 236 })
.map(|chars| chars.iter().collect::<String>())
{
if let Some(options) = &self.ncr_options
&& let Ok(encrypt) = self.state.lua.globals().get::<Function>("ncr_encrypt")
&& let Ok(ciphertext) = encrypt.call::<String>((options, prepend_header(&chunk)))
{
chunk = ciphertext;
}
self.client.chat(
&(if self.message.is_whisper()
&& let Some(username) = self.message.username()
&& let Some(username) = self.message.sender()
{
format!("/w {username} {chunk}")
} else {
@@ -39,10 +49,10 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
let source = ctx.source.clone();
tokio::spawn(async move {
let source = source.lock().await;
source.reply(&format!(
"{:?}",
reload(&source.state.lua, source.message.username())
));
source.reply(
&reload(&source.state.lua, source.message.sender())
.map_or_else(|error| error.to_string(), |()| String::from("ok")),
);
});
1
}));
@@ -53,10 +63,11 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
let code = get_string(ctx, "code").expect("argument should exist");
tokio::spawn(async move {
let source = source.lock().await;
source.reply(&format!(
"{:?}",
eval(&source.state.lua, &code, source.message.username()).await
));
source.reply(
&eval(&source.state.lua, &code, source.message.sender())
.await
.unwrap_or_else(|error| error.to_string()),
);
});
1
})),
@@ -68,10 +79,11 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
let code = get_string(ctx, "code").expect("argument should exist");
tokio::spawn(async move {
let source = source.lock().await;
source.reply(&format!(
"{:?}",
exec(&source.state.lua, &code, source.message.username()).await
));
source.reply(
&exec(&source.state.lua, &code, source.message.sender())
.await
.map_or_else(|error| error.to_string(), |()| String::from("ok")),
);
});
1
})),

View File

@@ -1,120 +1,250 @@
use std::{net::SocketAddr, process::exit};
use anyhow::{Context, Result};
use azalea::{
brigadier::exceptions::BuiltInExceptions::DispatcherUnknownCommand, prelude::*,
protocol::packets::game::ClientboundGamePacket,
};
use hyper::{server::conn::http1, service::service_fn};
use hyper_util::rt::TokioIo;
use log::{debug, error, info, trace};
use mlua::{Error, Function, IntoLuaMulti, Table};
use ncr::utils::trim_header;
use tokio::net::TcpListener;
#[cfg(feature = "matrix")]
use {crate::matrix, std::time::Duration, tokio::time::sleep};
use crate::{
State,
commands::CommandSource,
http::serve,
lua::{self, player::Player, vec3::Vec3},
lua::{client, direction::Direction, player::Player, vec3::Vec3},
particle,
replay::recorder::Recorder,
};
use azalea::{prelude::*, protocol::packets::game::ClientboundGamePacket};
use hyper::{server::conn::http1, service::service_fn};
use hyper_util::rt::TokioIo;
use log::{debug, error, info, trace};
use mlua::IntoLuaMulti;
use std::process::exit;
use tokio::net::TcpListener;
#[allow(clippy::too_many_lines)]
pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow::Result<()> {
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]
pub async fn handle_event(client: Client, event: Event, state: State) -> Result<()> {
match event {
Event::AddPlayer(player_info) => {
call_listeners(&state, "add_player", Player::from(player_info)).await;
call_listeners(&state, "add_player", || Ok(Player::from(player_info))).await
}
Event::Chat(message) => {
let formatted_message = message.message();
info!("{}", formatted_message.to_ansi());
let globals = state.lua.globals();
let (sender, mut content) = message.split_sender_and_content();
let uuid = message.sender_uuid().map(|uuid| uuid.to_string());
let is_whisper = message.is_whisper();
let text = message.message();
let html_text = text.to_html();
let ansi_text = text.to_ansi();
info!("{ansi_text}");
if message.is_whisper()
&& let (Some(sender), content) = message.split_sender_and_content()
&& state
.lua
.globals()
.get::<Vec<String>>("Owners")?
.contains(&sender)
{
if let Err(error) = state.commands.execute(
content,
CommandSource {
client: client.clone(),
message: message.clone(),
state: state.clone(),
}
.into(),
) {
let mut is_encrypted = false;
if let Some(ref sender) = sender {
let mut ncr_options = None;
if let Ok(options) = globals.get::<Table>("NcrOptions")
&& let Ok(decrypt) = globals.get::<Function>("ncr_decrypt")
&& let Some(plaintext) = decrypt
.call::<String>((options.clone(), content.clone()))
.ok()
.as_deref()
.and_then(|string| trim_header(string).ok())
{
is_encrypted = true;
ncr_options = Some(options);
plaintext.clone_into(&mut content);
info!("decrypted message from {sender}: {content}");
}
if is_whisper
&& globals
.get::<Vec<String>>("Owners")
.unwrap_or_default()
.contains(sender)
&& let Err(error) = state.commands.execute(
content.clone(),
CommandSource {
client: client.clone(),
message: message.clone(),
state: state.clone(),
ncr_options: ncr_options.clone(),
}
.into(),
)
&& error.type_ != DispatcherUnknownCommand
{
CommandSource {
client,
message,
state: state.clone(),
ncr_options,
}
.reply(&format!("{error:?}"));
}
}
call_listeners(&state, "chat", formatted_message.to_string()).await;
call_listeners(&state, "chat", || {
let table = state.lua.create_table()?;
table.set("text", text.to_string())?;
table.set("ansi_text", ansi_text)?;
table.set("html_text", html_text)?;
table.set("sender", sender)?;
table.set("content", content)?;
table.set("uuid", uuid)?;
table.set("is_whisper", is_whisper)?;
table.set("is_encrypted", is_encrypted)?;
Ok(table)
})
.await
}
Event::Death(Some(packet)) => {
let table = state.lua.create_table()?;
table.set("message", packet.message.to_string())?;
table.set("player_id", packet.player_id.0)?;
call_listeners(&state, "death", table).await;
Event::Death(packet) => {
if let Some(packet) = packet {
call_listeners(&state, "death", || {
let message_table = state.lua.create_table()?;
message_table.set("text", packet.message.to_string())?;
message_table.set("ansi_text", packet.message.to_ansi())?;
message_table.set("html_text", packet.message.to_html())?;
let table = state.lua.create_table()?;
table.set("message", message_table)?;
table.set("player_id", packet.player_id.0)?;
Ok(table)
})
.await
} else {
call_listeners(&state, "death", || Ok(())).await
}
}
Event::Disconnect(message) => {
call_listeners(&state, "disconnect", message.map(|m| m.to_string())).await;
exit(0)
if let Some(message) = message {
call_listeners(&state, "disconnect", || {
let table = state.lua.create_table()?;
table.set("text", message.to_string())?;
table.set("ansi_text", message.to_ansi())?;
table.set("html_text", message.to_html())?;
Ok(table)
})
.await
} else {
call_listeners(&state, "disconnect", || Ok(())).await
}
}
Event::Login => call_listeners(&state, "login", ()).await,
Event::KeepAlive(id) => call_listeners(&state, "keep_alive", || Ok(id)).await,
Event::RemovePlayer(player_info) => {
call_listeners(&state, "remove_player", Player::from(player_info)).await;
call_listeners(&state, "remove_player", || Ok(Player::from(player_info))).await
}
Event::Tick => call_listeners(&state, "tick", ()).await,
Event::Spawn => call_listeners(&state, "spawn", || Ok(())).await,
Event::Tick => call_listeners(&state, "tick", || Ok(())).await,
Event::UpdatePlayer(player_info) => {
call_listeners(&state, "update_player", Player::from(player_info)).await;
call_listeners(&state, "update_player", || Ok(Player::from(player_info))).await
}
Event::Packet(packet) => match packet.as_ref() {
ClientboundGamePacket::SetHealth(packet) => {
let table = state.lua.create_table()?;
table.set("food", packet.food)?;
table.set("health", packet.health)?;
table.set("saturation", packet.saturation)?;
call_listeners(&state, "set_health", table).await;
}
ClientboundGamePacket::SetPassengers(packet) => {
let table = state.lua.create_table()?;
table.set("vehicle", packet.vehicle)?;
table.set("passengers", &*packet.passengers)?;
call_listeners(&state, "set_passengers", table).await;
ClientboundGamePacket::AddEntity(packet) => {
call_listeners(&state, "add_entity", || {
let table = state.lua.create_table()?;
table.set("id", packet.id.0)?;
table.set("uuid", packet.uuid.to_string())?;
table.set("kind", packet.entity_type.to_string())?;
table.set("position", Vec3::from(packet.position))?;
table.set(
"direction",
Direction {
y: f32::from(packet.y_rot) / (256.0 / 360.0),
x: f32::from(packet.x_rot) / (256.0 / 360.0),
},
)?;
table.set("data", packet.data)?;
Ok(table)
})
.await
}
ClientboundGamePacket::LevelParticles(packet) => {
let table = state.lua.create_table()?;
table.set("position", Vec3::from(packet.pos))?;
table.set("count", packet.count)?;
table.set("kind", particle::to_kind(&packet.particle) as u8)?;
call_listeners(&state, "level_particles", table).await;
call_listeners(&state, "level_particles", || {
let table = state.lua.create_table()?;
table.set("position", Vec3::from(packet.pos))?;
table.set("count", packet.count)?;
table.set("kind", particle::to_kind(&packet.particle) as u8)?;
Ok(table)
})
.await
}
_ => (),
ClientboundGamePacket::RemoveEntities(packet) => {
call_listeners(&state, "remove_entities", || {
Ok(packet.entity_ids.iter().map(|id| id.0).collect::<Vec<_>>())
})
.await
}
ClientboundGamePacket::SetHealth(packet) => {
call_listeners(&state, "set_health", || {
let table = state.lua.create_table()?;
table.set("food", packet.food)?;
table.set("health", packet.health)?;
table.set("saturation", packet.saturation)?;
Ok(table)
})
.await
}
ClientboundGamePacket::SetPassengers(packet) => {
call_listeners(&state, "set_passengers", || {
let table = state.lua.create_table()?;
table.set("vehicle", packet.vehicle)?;
table.set("passengers", &*packet.passengers)?;
Ok(table)
})
.await
}
ClientboundGamePacket::SetTime(packet) => {
call_listeners(&state, "set_time", || {
let table = state.lua.create_table()?;
table.set("day_time", packet.day_time)?;
table.set("game_time", packet.game_time)?;
table.set("tick_day_time", packet.tick_day_time)?;
Ok(table)
})
.await
}
_ => Ok(()),
},
Event::Login => {
#[cfg(feature = "matrix")]
matrix_init(&client, state.clone());
call_listeners(&state, "login", || Ok(())).await
}
Event::Init => {
debug!("received initialize event");
debug!("received init event");
state.lua.globals().set(
"client",
lua::client::Client {
inner: Some(client),
},
)?;
call_listeners(&state, "init", ()).await;
let ecs = client.ecs.clone();
ctrlc::set_handler(move || {
ecs.lock()
.remove_resource::<Recorder>()
.map(Recorder::finish);
exit(0);
})?;
let Some(address) = state.http_address else {
let globals = state.lua.globals();
lua_init(client, &state, &globals).await?;
let Some(address): Option<SocketAddr> = globals
.get::<String>("HttpAddress")
.ok()
.and_then(|string| string.parse().ok())
else {
return Ok(());
};
let listener = TcpListener::bind(address).await.map_err(|error| {
let listener = TcpListener::bind(address).await.inspect_err(|error| {
error!("failed to listen on {address}: {error:?}");
error
})?;
debug!("http server listening on {address}");
loop {
let (stream, peer) = listener.accept().await?;
let (stream, peer) = match listener.accept().await {
Ok(pair) => pair,
Err(error) => {
error!("failed to accept connection: {error:?}");
continue;
}
};
trace!("http server got connection from {peer}");
let conn_state = state.clone();
@@ -133,18 +263,59 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
});
}
}
_ => (),
_ => todo!(),
}
Ok(())
}
async fn call_listeners<T: Clone + IntoLuaMulti>(state: &State, event_type: &str, data: T) {
if let Some(listeners) = state.event_listeners.read().await.get(event_type) {
for (id, callback) in listeners {
if let Err(error) = callback.call_async::<()>(data.clone()).await {
error!("failed to call lua event listener {id} for {event_type}: {error:?}");
async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()> {
let ecs = client.ecs.clone();
globals.set(
"finish_replay_recording",
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() {
let data = getter()?;
for (id, callback) in listeners {
let data = data.clone();
tokio::spawn(async move {
if let Err(error) = callback.call_async::<()>(data).await {
error!("failed to call lua event listener {id} for {event_type}: {error}");
}
});
}
}
Ok(())
}

View File

@@ -0,0 +1,20 @@
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());
}
}
}

19
src/hacks/mod.rs Normal file
View File

@@ -0,0 +1,19 @@
#![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),
);
}
}

View File

@@ -1,58 +1,61 @@
use http_body_util::{BodyExt, Empty, Full, combinators::BoxBody};
use hyper::{
Error, Method, Request, Response, StatusCode,
body::{Bytes, Incoming},
};
use crate::{
State,
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(
request: Request<hyper::body::Incoming>,
request: Request<Incoming>,
state: State,
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
let path = request.uri().path().to_owned();
Ok(match (request.method(), path.as_str()) {
(&Method::POST, "/reload") => {
Response::new(full(format!("{:#?}", reload(&state.lua, None))))
}
(&Method::POST, "/eval" | "/exec") => {
let bytes = request.into_body().collect().await?.to_bytes();
match std::str::from_utf8(&bytes) {
Ok(code) => Response::new(full(match path.as_str() {
"/eval" => format!("{:#?}", eval(&state.lua, code, None).await),
"/exec" => format!("{:#?}", exec(&state.lua, code, None).await),
_ => unreachable!(),
})),
Err(error) => status_code_response(
StatusCode::BAD_REQUEST,
full(format!("invalid utf-8 data received: {error:?}")),
),
}
}
) -> Result<Response<BoxBody<Bytes, Error>>, Error> {
Ok(match (request.method(), request.uri().path()) {
(&Method::POST, "/reload") => Response::new(
reload(&state.lua, None).map_or_else(|error| full(error.to_string()), |()| empty()),
),
(&Method::POST, "/eval") => Response::new(full(
eval(
&state.lua,
&String::from_utf8_lossy(&request.into_body().collect().await?.to_bytes()),
None,
)
.await
.unwrap_or_else(|error| error.to_string()),
)),
(&Method::POST, "/exec") => Response::new(
exec(
&state.lua,
&String::from_utf8_lossy(&request.into_body().collect().await?.to_bytes()),
None,
)
.await
.map_or_else(|error| full(error.to_string()), |()| empty()),
),
(&Method::GET, "/ping") => Response::new(full("pong!")),
_ => status_code_response(StatusCode::NOT_FOUND, empty()),
})
}
fn status_code_response(
status_code: StatusCode,
bytes: BoxBody<Bytes, hyper::Error>,
) -> Response<BoxBody<Bytes, hyper::Error>> {
bytes: BoxBody<Bytes, Error>,
) -> Response<BoxBody<Bytes, Error>> {
let mut response = Response::new(bytes);
*response.status_mut() = status_code;
response
}
fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> {
fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, Error> {
Full::new(chunk.into())
.map_err(|never| match never {})
.boxed()
}
fn empty() -> BoxBody<Bytes, hyper::Error> {
fn empty() -> BoxBody<Bytes, Error> {
Empty::<Bytes>::new()
.map_err(|never| match never {})
.boxed()

View File

@@ -4,7 +4,7 @@ use azalea::blocks::{
};
use mlua::{Function, Lua, Result, Table};
pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set(
"get_block_from_state",
lua.create_function(get_block_from_state)?,
@@ -41,23 +41,26 @@ pub async fn get_block_states(
lua: Lua,
(block_names, filter_fn): (Vec<String>, Option<Function>),
) -> Result<Vec<u16>> {
let mut matched = Vec::new();
let mut matched = Vec::with_capacity(16);
for block_name in block_names {
for b in
for block in
(u32::MIN..u32::MAX).map_while(|possible_id| BlockState::try_from(possible_id).ok())
{
if block_name == Into::<Box<dyn AzaleaBlock>>::into(b).id()
if block_name == Into::<Box<dyn AzaleaBlock>>::into(block).id()
&& (if let Some(filter_fn) = &filter_fn {
let p = lua.create_table()?;
p.set("chest_type", b.property::<ChestType>().map(|v| v as u8))?;
p.set("facing", b.property::<Facing>().map(|v| v as u8))?;
p.set("light_level", b.property::<LightLevel>().map(|v| v as u8))?;
filter_fn.call_async::<bool>(p.clone()).await?
let table = lua.create_table()?;
table.set("chest_type", block.property::<ChestType>().map(|v| v as u8))?;
table.set("facing", block.property::<Facing>().map(|v| v as u8))?;
table.set(
"light_level",
block.property::<LightLevel>().map(|v| v as u8),
)?;
filter_fn.call_async::<bool>(table).await?
} else {
true
})
{
matched.push(b.id);
matched.push(block.id());
}
}
}

View File

@@ -1,31 +1,31 @@
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
use azalea::{
BlockPos,
inventory::{Inventory, Menu, Player, SlotList},
prelude::ContainerClientExt,
protocol::packets::game::ServerboundSetCarriedItem,
};
use log::error;
use mlua::{Lua, Result, Table, UserDataRef};
use mlua::{Lua, Result, UserDataRef, Value};
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
pub fn container(_lua: &Lua, client: &Client) -> Result<Option<ContainerRef>> {
Ok(client
.get_open_container()
.map(|c| ContainerRef { inner: c }))
Ok(client.get_open_container().map(ContainerRef))
}
pub fn held_item(_lua: &Lua, client: &Client) -> Result<ItemStack> {
Ok(ItemStack::from(client.component::<Inventory>().held_item()))
Ok(ItemStack(client.component::<Inventory>().held_item()))
}
pub fn held_slot(_lua: &Lua, client: &Client) -> Result<u8> {
Ok(client.component::<Inventory>().selected_hotbar_slot)
}
pub fn menu(lua: &Lua, client: &Client) -> Result<Table> {
fn from_slot_list<const N: usize>(s: SlotList<N>) -> Vec<ItemStack> {
s.iter()
.map(|i| ItemStack::from(i.to_owned()))
#[allow(clippy::too_many_lines)]
pub fn menu(lua: &Lua, client: &Client) -> Result<Value> {
fn from_slot_list<const N: usize>(slot_list: SlotList<N>) -> Vec<ItemStack> {
slot_list
.iter()
.map(|item_stack| ItemStack(item_stack.to_owned()))
.collect::<Vec<_>>()
}
@@ -39,20 +39,55 @@ pub fn menu(lua: &Lua, client: &Client) -> Result<Table> {
offhand,
}) => {
table.set("type", 0)?;
table.set("craft_result", ItemStack::from(craft_result))?;
table.set("craft_result", ItemStack(craft_result))?;
table.set("craft", from_slot_list(craft))?;
table.set("armor", from_slot_list(armor))?;
table.set("inventory", from_slot_list(inventory))?;
table.set("offhand", ItemStack::from(offhand))?;
table.set("offhand", ItemStack(offhand))?;
}
Menu::Generic9x3 { contents, player } => {
table.set("type", 3)?;
table.set("contents", from_slot_list(contents))?;
table.set("player", from_slot_list(player))?;
}
Menu::Generic9x6 { contents, player } => {
table.set("type", 6)?;
table.set("contents", from_slot_list(contents))?;
table.set("player", from_slot_list(player))?;
}
_ => (),
Menu::Crafting {
result,
grid,
player,
} => {
table.set("type", 13)?;
table.set("result", ItemStack(result))?;
table.set("grid", from_slot_list(grid))?;
table.set("player", from_slot_list(player))?;
}
Menu::Hopper { contents, player } => {
table.set("type", 17)?;
table.set("contents", from_slot_list(contents))?;
table.set("player", from_slot_list(player))?;
}
Menu::Merchant {
payments,
result,
player,
} => {
table.set("type", 20)?;
table.set("payments", from_slot_list(payments))?;
table.set("result", ItemStack(result))?;
table.set("player", from_slot_list(player))?;
}
Menu::ShulkerBox { contents, player } => {
table.set("type", 21)?;
table.set("contents", from_slot_list(contents))?;
table.set("player", from_slot_list(player))?;
}
_ => return Ok(Value::Nil),
}
Ok(table)
Ok(Value::Table(table))
}
pub async fn open_container_at(
@@ -69,11 +104,11 @@ pub async fn open_container_at(
position.z as i32,
))
.await
.map(|c| Container { inner: c }))
.map(Container))
}
pub fn open_inventory(_lua: &Lua, client: &mut Client, _: ()) -> Result<Option<Container>> {
Ok(client.open_inventory().map(|c| Container { inner: c }))
pub fn open_inventory(_lua: &Lua, client: &Client, (): ()) -> Result<Option<Container>> {
Ok(client.open_inventory().map(Container))
}
pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> {
@@ -90,11 +125,8 @@ pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> {
inventory.selected_hotbar_slot = slot;
};
if let Err(error) = client.write_packet(ServerboundSetCarriedItem {
client.write_packet(ServerboundSetCarriedItem {
slot: u16::from(slot),
}) {
error!("failed to send SetCarriedItem packet: {error:?}");
}
});
Ok(())
}

View File

@@ -1,31 +1,17 @@
use super::{Client, Vec3};
use azalea::{
BlockPos, BotClientExt,
attack::AttackEvent,
protocol::packets::game::{ServerboundUseItem, s_interact::InteractionHand},
world::MinecraftEntityId,
BlockPos, BotClientExt, interact::StartUseItemEvent,
protocol::packets::game::s_interact::InteractionHand, world::MinecraftEntityId,
};
use log::error;
use mlua::{Lua, Result, UserDataRef};
pub async fn attack(_lua: Lua, client: UserDataRef<Client>, entity_id: i32) -> Result<()> {
client.clone().attack(MinecraftEntityId(entity_id));
while client.get_tick_broadcaster().recv().await.is_ok() {
if client
.ecs
.lock()
.get::<AttackEvent>(client.entity)
.is_none()
{
break;
}
}
use super::{Client, Vec3};
pub fn attack(_lua: &Lua, client: &Client, entity_id: i32) -> Result<()> {
client.attack(MinecraftEntityId(entity_id));
Ok(())
}
pub fn block_interact(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> {
pub fn block_interact(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
#[allow(clippy::cast_possible_truncation)]
client.block_interact(BlockPos::new(
position.x as i32,
@@ -52,12 +38,12 @@ pub async fn mine(_lua: Lua, client: UserDataRef<Client>, position: Vec3) -> Res
Ok(())
}
pub fn set_mining(_lua: &Lua, client: &Client, mining: bool) -> Result<()> {
client.left_click_mine(mining);
pub fn set_mining(_lua: &Lua, client: &Client, state: bool) -> Result<()> {
client.left_click_mine(state);
Ok(())
}
pub fn start_mining(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> {
pub fn start_mining(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
#[allow(clippy::cast_possible_truncation)]
client.start_mining(BlockPos::new(
position.x as i32,
@@ -67,18 +53,14 @@ pub fn start_mining(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<(
Ok(())
}
pub fn use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> {
let d = client.direction();
if let Err(error) = client.write_packet(ServerboundUseItem {
pub fn start_use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> {
client.ecs.lock().send_event(StartUseItemEvent {
entity: client.entity,
hand: match hand {
Some(1) => InteractionHand::OffHand,
_ => InteractionHand::MainHand,
},
sequence: 0,
yaw: d.0,
pitch: d.1,
}) {
error!("failed to send UseItem packet: {error:?}");
}
force_block: None,
});
Ok(())
}

View File

@@ -1,38 +1,36 @@
#![allow(clippy::needless_pass_by_value, clippy::unnecessary_wraps)]
mod container;
mod interaction;
mod movement;
mod state;
mod world;
use std::ops::{Deref, DerefMut};
use azalea::{Client as AzaleaClient, world::MinecraftEntityId};
use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods};
use super::{
container::{Container, ContainerRef, item_stack::ItemStack},
direction::Direction,
player::Player,
vec3::Vec3,
};
use azalea::Client as AzaleaClient;
use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods};
use std::ops::{Deref, DerefMut};
pub struct Client {
pub inner: Option<AzaleaClient>,
}
pub struct Client(pub Option<AzaleaClient>);
impl Deref for Client {
type Target = AzaleaClient;
fn deref(&self) -> &Self::Target {
self.inner
.as_ref()
.expect("should have received init event")
self.0.as_ref().expect("should have received init event")
}
}
impl DerefMut for Client {
fn deref_mut(&mut self) -> &mut Self::Target {
self.inner
.as_mut()
.expect("should have received init event")
self.0.as_mut().expect("should have received init event")
}
}
@@ -43,50 +41,58 @@ impl UserData for Client {
f.add_field_method_get("dimension", world::dimension);
f.add_field_method_get("direction", movement::direction);
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("health", state::health);
f.add_field_method_get("held_item", container::held_item);
f.add_field_method_get("held_slot", container::held_slot);
f.add_field_method_get("hunger", state::hunger);
f.add_field_method_get("id", id);
f.add_field_method_get("looking_at", movement::looking_at);
f.add_field_method_get("menu", container::menu);
f.add_field_method_get("pathfinder", movement::pathfinder);
f.add_field_method_get("position", movement::position);
f.add_field_method_get("score", state::score);
f.add_field_method_get("tab_list", tab_list);
f.add_field_method_get("username", username);
f.add_field_method_get("uuid", uuid);
}
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_async_method("attack", interaction::attack);
m.add_async_method("find_entities", world::find_entities);
m.add_async_method("find_players", world::find_players);
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_entities", world::find::entities);
m.add_async_method("find_players", world::find::players);
m.add_async_method("go_to", movement::go_to);
m.add_async_method("look_at", movement::look_at);
m.add_async_method("mine", interaction::mine);
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("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("block_interact", interaction::block_interact);
m.add_method("chat", chat);
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_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_jumping", movement::set_jumping);
m.add_method("set_mining", interaction::set_mining);
m.add_method("set_position", movement::set_position);
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_sleeping", movement::stop_sleeping);
m.add_method("use_item", interaction::use_item);
m.add_method_mut("block_interact", interaction::block_interact);
m.add_method_mut("jump", movement::jump);
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);
m.add_method("walk", movement::walk);
}
}
@@ -95,17 +101,21 @@ fn chat(_lua: &Lua, client: &Client, message: String) -> Result<()> {
Ok(())
}
fn disconnect(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
fn disconnect(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.disconnect();
Ok(())
}
fn id(_lua: &Lua, client: &Client) -> Result<i32> {
Ok(client.component::<MinecraftEntityId>().0)
}
fn tab_list(_lua: &Lua, client: &Client) -> Result<Vec<Player>> {
let mut tab_list = Vec::new();
for (_, player_info) in client.tab_list() {
tab_list.push(Player::from(player_info));
}
Ok(tab_list)
Ok(client.tab_list().into_values().map(Player::from).collect())
}
fn username(_lua: &Lua, client: &Client) -> Result<String> {
Ok(client.username())
}
fn uuid(_lua: &Lua, client: &Client) -> Result<String> {

View File

@@ -1,162 +1,164 @@
use super::{Client, Direction, Vec3};
use azalea::{
BlockPos, BotClientExt, LookAtEvent, SprintDirection, WalkDirection,
BlockPos, BotClientExt, SprintDirection, WalkDirection,
core::hit_result::HitResult,
entity::Position,
interact::HitResultComponent,
pathfinder::{
ExecutingPath, GotoEvent, Pathfinder, PathfinderClientExt,
ExecutingPath, Pathfinder, PathfinderClientExt,
goals::{BlockPosGoal, Goal, InverseGoal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal},
},
protocol::packets::game::{ServerboundPlayerCommand, s_player_command::Action},
world::MinecraftEntityId,
};
use log::error;
use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value};
use super::{Client, Direction, Vec3};
#[derive(Debug)]
struct AnyGoal(Box<dyn Goal>);
impl Goal for AnyGoal {
fn success(&self, n: BlockPos) -> bool {
self.0.success(n)
}
fn heuristic(&self, n: BlockPos) -> f32 {
self.0.heuristic(n)
}
}
#[allow(clippy::cast_possible_truncation)]
fn to_goal(lua: &Lua, client: &Client, data: Table, options: &Table, kind: u8) -> Result<AnyGoal> {
let goal: Box<dyn Goal> = match kind {
1 => {
let pos = Vec3::from_lua(data.get("position")?, lua)?;
Box::new(RadiusGoal {
pos: azalea::Vec3::new(pos.x, pos.y, pos.z),
radius: data.get("radius")?,
})
}
2 => {
let distance = data.get("distance").unwrap_or(4.5);
let pos = Vec3::from_lua(Value::Table(data), lua)?;
Box::new(ReachBlockPosGoal::new_with_distance(
BlockPos::new(pos.x as i32, pos.y as i32, pos.z as i32),
distance,
client.world().read().chunks.clone(),
))
}
3 => Box::new(XZGoal {
x: data.get("x")?,
z: data.get("z")?,
}),
4 => Box::new(YGoal { y: data.get("y")? }),
_ => {
let pos = Vec3::from_lua(Value::Table(data), lua)?;
Box::new(BlockPosGoal(BlockPos::new(
pos.x as i32,
pos.y as i32,
pos.z as i32,
)))
}
};
Ok(AnyGoal(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(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Table, Option<Table>),
) -> Result<()> {
let metadata = metadata.unwrap_or(lua.create_table()?);
let options = metadata.get("options").unwrap_or(lua.create_table()?);
let goal = to_goal(
&lua,
&client,
data,
&options,
metadata.get("type").unwrap_or_default(),
)?;
if options.get("without_mining").unwrap_or_default() {
client.start_goto_without_mining(goal);
client.wait_until_goto_target_reached().await;
} else {
client.goto(goal).await;
}
Ok(())
}
pub async fn start_go_to(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Table, Option<Table>),
) -> Result<()> {
let metadata = metadata.unwrap_or(lua.create_table()?);
let options = metadata.get("options").unwrap_or(lua.create_table()?);
let goal = to_goal(
&lua,
&client,
data,
&options,
metadata.get("type").unwrap_or_default(),
)?;
if options.get("without_mining").unwrap_or_default() {
client.start_goto_without_mining(goal);
} else {
client.start_goto(goal);
}
let _ = client.get_tick_broadcaster().recv().await;
Ok(())
}
pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
let d = client.direction();
Ok(Direction { y: d.0, x: d.1 })
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 async fn go_to(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Value, Option<Table>),
) -> Result<()> {
fn goto_with_options<G: Goal + Send + Sync + 'static>(
client: &Client,
options: &Table,
goal: G,
) {
if options.get("without_mining").unwrap_or_default() {
client.goto_without_mining(goal);
} else {
client.goto(goal);
}
}
fn goto<G: Goal + Send + Sync + 'static>(client: &Client, options: Table, goal: G) {
if options.get("inverse").unwrap_or_default() {
goto_with_options(client, &options, InverseGoal(goal));
} else {
goto_with_options(client, &options, goal);
}
}
let error = mlua::Error::FromLuaConversionError {
from: data.type_name(),
to: "Table".to_string(),
message: None,
};
let (goal_type, options) = if let Some(metadata) = metadata {
(
metadata.get("type")?,
metadata.get("options").unwrap_or(lua.create_table()?),
)
} else {
(0, lua.create_table()?)
};
#[allow(clippy::cast_possible_truncation)]
match goal_type {
1 => {
let t = data.as_table().ok_or(error)?;
let p = Vec3::from_lua(t.get("position")?, &lua)?;
goto(
&client,
options,
RadiusGoal {
pos: azalea::Vec3::new(p.x, p.y, p.z),
radius: t.get("radius")?,
},
);
}
2 => {
let p = Vec3::from_lua(data, &lua)?;
goto(
&client,
options,
ReachBlockPosGoal {
pos: BlockPos::new(p.x as i32, p.y as i32, p.z as i32),
chunk_storage: client.world().read().chunks.clone(),
},
);
}
3 => {
let t = data.as_table().ok_or(error)?;
goto(
&client,
options,
XZGoal {
x: t.get("x")?,
z: t.get("z")?,
},
);
}
4 => goto(
&client,
options,
YGoal {
y: data.as_table().ok_or(error)?.get("y")?,
},
),
_ => {
let p = Vec3::from_lua(data, &lua)?;
goto(
&client,
options,
BlockPosGoal(BlockPos::new(p.x as i32, p.y as i32, p.z as i32)),
);
}
}
while client.get_tick_broadcaster().recv().await.is_ok() {
if client.ecs.lock().get::<GotoEvent>(client.entity).is_none() {
break;
}
}
Ok(())
}
pub fn jump(_lua: &Lua, client: &mut Client, _: ()) -> Result<()> {
pub fn jump(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.jump();
Ok(())
}
pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
let result = client.component::<HitResultComponent>();
Ok(if result.miss {
None
} else {
let table = lua.create_table()?;
table.set("position", Vec3::from(result.block_pos))?;
table.set("inside", result.inside)?;
table.set("world_border", result.world_border)?;
Some(table)
})
Ok(
if let HitResult::Block(ref result) = *client.component::<HitResultComponent>() {
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("world_border", result.world_border)?;
Some(table)
} else {
None
},
)
}
pub async fn look_at(_lua: Lua, client: UserDataRef<Client>, position: Vec3) -> Result<()> {
client
.clone()
.look_at(azalea::Vec3::new(position.x, position.y, position.z));
while client.get_tick_broadcaster().recv().await.is_ok() {
if client
.ecs
.lock()
.get::<LookAtEvent>(client.entity)
.is_none()
{
break;
}
}
pub fn look_at(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
client.look_at(azalea::Vec3::new(position.x, position.y, position.z));
Ok(())
}
@@ -190,12 +192,12 @@ pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {
Ok(Vec3::from(&client.component::<Position>()))
}
pub fn set_direction(_lua: &Lua, client: &mut Client, direction: Direction) -> Result<()> {
pub fn set_direction(_lua: &Lua, client: &Client, direction: Direction) -> Result<()> {
client.set_direction(direction.y, direction.x);
Ok(())
}
pub fn set_jumping(_lua: &Lua, client: &mut Client, jumping: bool) -> Result<()> {
pub fn set_jumping(_lua: &Lua, client: &Client, jumping: bool) -> Result<()> {
client.set_jumping(jumping);
Ok(())
}
@@ -210,7 +212,7 @@ pub fn set_position(_lua: &Lua, client: &Client, new_position: Vec3) -> Result<(
}
pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
action: if sneaking {
Action::PressShiftKey
@@ -218,13 +220,11 @@ pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
Action::ReleaseShiftKey
},
data: 0,
}) {
error!("failed to send PlayerCommand packet: {error:?}");
}
});
Ok(())
}
pub fn sprint(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> {
pub fn sprint(_lua: &Lua, client: &Client, direction: u8) -> Result<()> {
client.sprint(match direction {
5 => SprintDirection::ForwardRight,
6 => SprintDirection::ForwardLeft,
@@ -233,23 +233,21 @@ pub fn sprint(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> {
Ok(())
}
pub fn stop_pathfinding(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
pub fn stop_pathfinding(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.stop_pathfinding();
Ok(())
}
pub fn stop_sleeping(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
pub fn stop_sleeping(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
action: Action::StopSleeping,
data: 0,
}) {
error!("failed to send PlayerCommand packet: {error:?}");
}
});
Ok(())
}
pub fn walk(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> {
pub fn walk(_lua: &Lua, client: &Client, direction: u8) -> Result<()> {
client.walk(match direction {
1 => WalkDirection::Forward,
2 => WalkDirection::Backward,

View File

@@ -1,11 +1,13 @@
use super::Client;
use azalea::{
ClientInformation,
entity::metadata::{AirSupply, Score},
pathfinder::debug::PathfinderDebugParticles,
protocol::common::client_information::ModelCustomization,
};
use log::error;
use mlua::{Lua, Result, Table, UserDataRef};
use mlua::{Error, Lua, Result, Table, UserDataRef};
use super::Client;
use crate::hacks::anti_knockback::AntiKnockback;
pub fn air_supply(_lua: &Lua, client: &Client) -> Result<i32> {
Ok(client.component::<AirSupply>().0)
@@ -17,7 +19,6 @@ pub fn health(_lua: &Lua, client: &Client) -> Result<f32> {
pub fn hunger(lua: &Lua, client: &Client) -> Result<Table> {
let hunger = client.hunger();
let table = lua.create_table()?;
table.set("food", hunger.food)?;
table.set("saturation", hunger.saturation)?;
@@ -31,31 +32,53 @@ pub fn score(_lua: &Lua, client: &Client) -> Result<i32> {
pub async fn set_client_information(
_lua: Lua,
client: UserDataRef<Client>,
ci: Table,
info: Table,
) -> Result<()> {
let model_customization = if let Some(mc) = ci.get::<Option<Table>>("model_customization")? {
ModelCustomization {
cape: mc.get("cape")?,
jacket: mc.get("jacket")?,
left_sleeve: mc.get("left_sleeve")?,
right_sleeve: mc.get("right_sleeve")?,
left_pants: mc.get("left_pants")?,
right_pants: mc.get("right_pants")?,
hat: mc.get("hat")?,
}
} else {
ModelCustomization::default()
};
if let Err(error) = client
let get_bool = |table: &Table, name| table.get(name).unwrap_or(true);
client
.set_client_information(ClientInformation {
allows_listing: ci.get("allows_listing")?,
model_customization,
view_distance: ci.get("view_distance").unwrap_or(8),
allows_listing: info.get("allows_listing")?,
model_customization: info
.get::<Table>("model_customization")
.as_ref()
.map(|t| ModelCustomization {
cape: get_bool(t, "cape"),
jacket: get_bool(t, "jacket"),
left_sleeve: get_bool(t, "left_sleeve"),
right_sleeve: get_bool(t, "right_sleeve"),
left_pants: get_bool(t, "left_pants"),
right_pants: get_bool(t, "right_pants"),
hat: get_bool(t, "hat"),
})
.unwrap_or_default(),
view_distance: info.get("view_distance").unwrap_or(8),
..ClientInformation::default()
})
.await
{
error!("failed to set client client information: {error:?}");
}
.await;
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")),
}
}

View File

@@ -1,175 +0,0 @@
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 });
let table = lua.create_table()?;
table.set("index", result.index)?;
table.set("percentage_per_tick", result.percentage_per_tick)?;
Ok(table)
}
pub fn dimension(_lua: &Lua, client: &Client) -> Result<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_entities(
lua: Lua,
client: UserDataRef<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
let entities = {
let mut ecs = client.ecs.lock();
ecs.query_filtered::<(
&AzaleaPosition,
&CustomName,
&EntityKind,
&EntityUuid,
&LookDirection,
&MinecraftEntityId,
&Owneruuid,
&Pose,
), Without<Dead>>()
.iter(&ecs)
.map(
|(position, custom_name, kind, uuid, direction, id, owner_uuid, pose)| {
(
Vec3::from(position),
custom_name.as_ref().map(ToString::to_string),
kind.to_string(),
uuid.to_string(),
Direction::from(direction),
id.0,
owner_uuid.to_owned(),
*pose as u8,
)
},
)
.collect::<Vec<_>>()
};
let mut matched = Vec::new();
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in entities {
let entity = lua.create_table()?;
entity.set("position", position)?;
entity.set("custom_name", custom_name)?;
entity.set("kind", kind)?;
entity.set("uuid", uuid)?;
entity.set("direction", direction)?;
entity.set("id", id)?;
if let Some(uuid) = *owner_uuid {
entity.set("owner_uuid", uuid.to_string())?;
}
entity.set("pose", pose)?;
if filter_fn.call_async::<bool>(&entity).await? {
matched.push(entity);
}
}
Ok(matched)
}
pub async fn find_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
let entities = {
let mut ecs = client.ecs.lock();
ecs.query_filtered::<(
&MinecraftEntityId,
&EntityUuid,
&EntityKind,
&AzaleaPosition,
&LookDirection,
&Pose,
), (With<Player>, Without<Dead>)>()
.iter(&ecs)
.map(|(id, uuid, kind, position, direction, pose)| {
(
id.0,
uuid.to_string(),
kind.to_string(),
Vec3::from(position),
Direction::from(direction),
*pose as u8,
)
})
.collect::<Vec<_>>()
};
let mut players = Vec::new();
for (id, uuid, kind, position, direction, pose) in entities {
let entity = lua.create_table()?;
entity.set("id", id)?;
entity.set("uuid", uuid)?;
entity.set("kind", kind)?;
entity.set("position", position)?;
entity.set("direction", direction)?;
entity.set("pose", pose)?;
players.push(entity);
}
Ok(players)
}
pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> {
#[allow(clippy::cast_possible_truncation)]
Ok(client
.world()
.read()
.get_block_state(&BlockPos::new(
position.x as i32,
position.y as i32,
position.z as i32,
))
.map(|b| b.id))
}
pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Option<Table>> {
#[allow(clippy::cast_possible_truncation)]
Ok(
if let Some(state) = client.world().read().get_fluid_state(&BlockPos::new(
position.x as i32,
position.y as i32,
position.z as i32,
)) {
let table = lua.create_table()?;
table.set("kind", state.kind as u8)?;
table.set("amount", state.amount)?;
table.set("falling", state.falling)?;
Some(table)
} else {
None
},
)
}

View File

@@ -0,0 +1,125 @@
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)
}

View File

@@ -0,0 +1,54 @@
#[macro_use]
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};
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()?;
table.set("index", result.index)?;
table.set("percentage_per_tick", result.percentage_per_tick)?;
Ok(Value::Table(table))
}
pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> {
Ok(client.component::<InstanceName>().to_string())
}
pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> {
#[allow(clippy::cast_possible_truncation)]
Ok(client
.world()
.read()
.get_block_state(&BlockPos::new(
position.x as i32,
position.y as i32,
position.z as i32,
))
.map(|block| block.id()))
}
#[allow(clippy::cast_possible_truncation)]
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(
position.x as i32,
position.y as i32,
position.z as i32,
));
Ok(if let Some(state) = fluid_state {
let table = lua.create_table()?;
table.set("kind", state.kind as u8)?;
table.set("amount", state.amount)?;
table.set("falling", state.falling)?;
Some(table)
} else {
None
})
}

View File

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

View File

@@ -0,0 +1,55 @@
use azalea::inventory::operations::{
ClickOperation, CloneClick, PickupAllClick, PickupClick, QuickCraftClick, QuickCraftKind,
QuickCraftStatus, QuickMoveClick, SwapClick, ThrowClick,
};
use mlua::{Result, Table};
pub fn operation_from_table(op: &Table, op_type: Option<u8>) -> Result<ClickOperation> {
Ok(match op_type.unwrap_or_default() {
0 => ClickOperation::Pickup(PickupClick::Left {
slot: op.get("slot")?,
}),
1 => ClickOperation::Pickup(PickupClick::Right {
slot: op.get("slot")?,
}),
2 => ClickOperation::Pickup(PickupClick::LeftOutside),
3 => ClickOperation::Pickup(PickupClick::RightOutside),
5 => ClickOperation::QuickMove(QuickMoveClick::Right {
slot: op.get("slot")?,
}),
6 => ClickOperation::Swap(SwapClick {
source_slot: op.get("source_slot")?,
target_slot: op.get("target_slot")?,
}),
7 => ClickOperation::Clone(CloneClick {
slot: op.get("slot")?,
}),
8 => ClickOperation::Throw(ThrowClick::Single {
slot: op.get("slot")?,
}),
9 => ClickOperation::Throw(ThrowClick::All {
slot: op.get("slot")?,
}),
10 => ClickOperation::QuickCraft(QuickCraftClick {
kind: match op.get("kind").unwrap_or_default() {
1 => QuickCraftKind::Right,
2 => QuickCraftKind::Middle,
_ => QuickCraftKind::Left,
},
status: match op.get("status").unwrap_or_default() {
1 => QuickCraftStatus::Add {
slot: op.get("slot")?,
},
2 => QuickCraftStatus::End,
_ => QuickCraftStatus::Start,
},
}),
11 => ClickOperation::PickupAll(PickupAllClick {
slot: op.get("slot")?,
reversed: op.get("reversed").unwrap_or_default(),
}),
_ => ClickOperation::QuickMove(QuickMoveClick::Left {
slot: op.get("slot")?,
}),
})
}

View File

@@ -1,53 +1,79 @@
use azalea::inventory::components::{CustomName, Damage, MaxDamage};
use azalea::inventory::{
self,
components::{Consumable, CustomName, Damage, Food, MaxDamage},
};
use mlua::{UserData, UserDataFields, UserDataMethods};
pub struct ItemStack {
pub inner: azalea::inventory::ItemStack,
}
impl From<azalea::inventory::ItemStack> for ItemStack {
fn from(inner: azalea::inventory::ItemStack) -> Self {
Self { inner }
}
}
pub struct ItemStack(pub inventory::ItemStack);
impl UserData for ItemStack {
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
f.add_field_method_get("is_empty", |_, this| Ok(this.inner.is_empty()));
f.add_field_method_get("is_present", |_, this| Ok(this.inner.is_present()));
f.add_field_method_get("count", |_, this| Ok(this.inner.count()));
f.add_field_method_get("kind", |_, this| Ok(this.inner.kind().to_string()));
f.add_field_method_get("is_empty", |_, this| Ok(this.0.is_empty()));
f.add_field_method_get("is_present", |_, this| Ok(this.0.is_present()));
f.add_field_method_get("count", |_, this| Ok(this.0.count()));
f.add_field_method_get("kind", |_, this| Ok(this.0.kind().to_string()));
f.add_field_method_get("custom_name", |_, this| {
Ok(if let Some(data) = this.inner.as_present() {
Ok(this.0.as_present().map(|data| {
data.components
.get::<CustomName>()
.map(|n| n.name.to_string())
} else {
None
})
.map(|c| c.name.to_string())
}))
});
f.add_field_method_get("damage", |_, this| {
Ok(if let Some(data) = this.inner.as_present() {
data.components.get::<Damage>().map(|d| d.amount)
} else {
None
})
Ok(this
.0
.as_present()
.map(|data| data.components.get::<Damage>().map(|d| d.amount)))
});
f.add_field_method_get("max_damage", |_, this| {
Ok(if let Some(data) = this.inner.as_present() {
data.components.get::<MaxDamage>().map(|d| d.amount)
} else {
None
})
Ok(this
.0
.as_present()
.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| {
Ok(
if let Some(food) = this
.0
.as_present()
.and_then(|data| data.components.get::<Food>())
{
let table = lua.create_table()?;
table.set("nutrition", food.nutrition)?;
table.set("saturation", food.saturation)?;
table.set("can_always_eat", food.can_always_eat)?;
table.set("eat_seconds", food.eat_seconds)?;
Some(table)
} else {
None
},
)
});
}
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_method_mut("split", |_, this, count: u32| {
Ok(ItemStack::from(this.inner.split(count)))
});
m.add_method_mut("split", |_, this, count: u32| Ok(Self(this.0.split(count))));
m.add_method_mut("update_empty", |_, this, (): ()| {
this.inner.update_empty();
this.0.update_empty();
Ok(())
});
}

View File

@@ -1,31 +1,25 @@
pub mod click;
pub mod item_stack;
use azalea::{
container::{ContainerHandle, ContainerHandleRef},
inventory::operations::{
ClickOperation, CloneClick, PickupAllClick, PickupClick, QuickCraftClick, QuickCraftKind,
QuickCraftStatus, QuickMoveClick, SwapClick, ThrowClick,
},
};
use azalea::container::{ContainerHandle, ContainerHandleRef};
use click::operation_from_table;
use item_stack::ItemStack;
use mlua::{Result, Table, UserData, UserDataFields, UserDataMethods};
use mlua::{Table, UserData, UserDataFields, UserDataMethods};
pub struct Container {
pub inner: ContainerHandle,
}
pub struct Container(pub ContainerHandle);
impl UserData for Container {
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
f.add_field_method_get("id", |_, this| Ok(this.inner.id()));
f.add_field_method_get("id", |_, this| Ok(this.0.id()));
f.add_field_method_get("menu", |_, this| {
Ok(this.inner.menu().map(|m| format!("{m:?}")))
Ok(this.0.menu().map(|m| format!("{m:?}")))
});
f.add_field_method_get("contents", |_, this| {
Ok(this.inner.contents().map(|v| {
Ok(this.0.contents().map(|v| {
v.iter()
.map(|i| ItemStack::from(i.to_owned()))
.map(|i| ItemStack(i.to_owned()))
.collect::<Vec<_>>()
}))
});
@@ -35,30 +29,28 @@ impl UserData for Container {
m.add_method(
"click",
|_, this, (operation, operation_type): (Table, Option<u8>)| {
this.inner
.click(click_operation_from_table(operation, operation_type)?);
this.0
.click(operation_from_table(&operation, operation_type)?);
Ok(())
},
);
}
}
pub struct ContainerRef {
pub inner: ContainerHandleRef,
}
pub struct ContainerRef(pub ContainerHandleRef);
impl UserData for ContainerRef {
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
f.add_field_method_get("id", |_, this| Ok(this.inner.id()));
f.add_field_method_get("id", |_, this| Ok(this.0.id()));
f.add_field_method_get("menu", |_, this| {
Ok(this.inner.menu().map(|m| format!("{m:?}")))
Ok(this.0.menu().map(|m| format!("{m:?}")))
});
f.add_field_method_get("contents", |_, this| {
Ok(this.inner.contents().map(|v| {
Ok(this.0.contents().map(|v| {
v.iter()
.map(|i| ItemStack::from(i.to_owned()))
.map(|i| ItemStack(i.to_owned()))
.collect::<Vec<_>>()
}))
});
@@ -66,67 +58,17 @@ impl UserData for ContainerRef {
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_method("close", |_, this, (): ()| {
this.inner.close();
this.0.close();
Ok(())
});
m.add_method(
"click",
|_, this, (operation, operation_type): (Table, Option<u8>)| {
this.inner
.click(click_operation_from_table(operation, operation_type)?);
this.0
.click(operation_from_table(&operation, operation_type)?);
Ok(())
},
);
}
}
fn click_operation_from_table(op: Table, op_type: Option<u8>) -> Result<ClickOperation> {
Ok(match op_type.unwrap_or_default() {
0 => ClickOperation::Pickup(PickupClick::Left {
slot: op.get("slot")?,
}),
1 => ClickOperation::Pickup(PickupClick::Right {
slot: op.get("slot")?,
}),
2 => ClickOperation::Pickup(PickupClick::LeftOutside),
3 => ClickOperation::Pickup(PickupClick::RightOutside),
5 => ClickOperation::QuickMove(QuickMoveClick::Right {
slot: op.get("slot")?,
}),
6 => ClickOperation::Swap(SwapClick {
source_slot: op.get("source_slot")?,
target_slot: op.get("target_slot")?,
}),
7 => ClickOperation::Clone(CloneClick {
slot: op.get("slot")?,
}),
8 => ClickOperation::Throw(ThrowClick::Single {
slot: op.get("slot")?,
}),
9 => ClickOperation::Throw(ThrowClick::All {
slot: op.get("slot")?,
}),
10 => ClickOperation::QuickCraft(QuickCraftClick {
kind: match op.get("kind").unwrap_or_default() {
1 => QuickCraftKind::Right,
2 => QuickCraftKind::Middle,
_ => QuickCraftKind::Left,
},
status: match op.get("status").unwrap_or_default() {
1 => QuickCraftStatus::Add {
slot: op.get("slot")?,
},
2 => QuickCraftStatus::End,
_ => QuickCraftStatus::Start,
},
}),
11 => ClickOperation::PickupAll(PickupAllClick {
slot: op.get("slot")?,
reversed: op.get("reversed").unwrap_or_default(),
}),
_ => ClickOperation::QuickMove(QuickMoveClick::Left {
slot: op.get("slot")?,
}),
})
}

View File

@@ -1,5 +1,5 @@
use azalea::entity::LookDirection;
use mlua::{FromLua, IntoLua, Lua, Result, Value};
use mlua::{Error, FromLua, IntoLua, Lua, Result, Value};
#[derive(Clone)]
pub struct Direction {
@@ -37,7 +37,7 @@ impl FromLua for Direction {
}
})
} else {
Err(mlua::Error::FromLuaConversionError {
Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "Direction".to_string(),
message: None,

View File

@@ -1,9 +1,11 @@
use crate::ListenerMap;
use futures::executor::block_on;
use mlua::{Function, Lua, Result, Table};
use std::time::{SystemTime, UNIX_EPOCH};
pub fn register_functions(lua: &Lua, globals: &Table, event_listeners: ListenerMap) -> Result<()> {
use futures::executor::block_on;
use mlua::{Function, Lua, Result, Table};
use crate::ListenerMap;
pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap) -> Result<()> {
let m = event_listeners.clone();
globals.set(
"add_listener",
@@ -11,13 +13,15 @@ pub fn register_functions(lua: &Lua, globals: &Table, event_listeners: ListenerM
move |_, (event_type, callback, optional_id): (String, Function, Option<String>)| {
let m = m.clone();
let id = optional_id.unwrap_or_else(|| {
callback.info().name.unwrap_or(format!(
"anonymous @ {}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis()
))
callback.info().name.unwrap_or_else(|| {
format!(
"anonymous @ {}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis()
)
})
});
tokio::spawn(async move {
m.write()
@@ -33,17 +37,15 @@ pub fn register_functions(lua: &Lua, globals: &Table, event_listeners: ListenerM
let m = event_listeners.clone();
globals.set(
"remove_listener",
"remove_listeners",
lua.create_function(move |_, (event_type, target_id): (String, String)| {
let m = m.clone();
tokio::spawn(async move {
let mut m = m.write().await;
let empty = if let Some(listeners) = m.get_mut(&event_type) {
let empty = m.get_mut(&event_type).is_some_and(|listeners| {
listeners.retain(|(id, _)| target_id != *id);
listeners.is_empty()
} else {
false
};
});
if empty {
m.remove(&event_type);
}
@@ -56,28 +58,20 @@ pub fn register_functions(lua: &Lua, globals: &Table, event_listeners: ListenerM
"get_listeners",
lua.create_function(move |lua, (): ()| {
let m = block_on(event_listeners.read());
let listeners = lua.create_table()?;
let listeners_table = lua.create_table()?;
for (event_type, callbacks) in m.iter() {
let type_listeners = lua.create_table()?;
let type_listeners_table = lua.create_table()?;
for (id, callback) in callbacks {
let listener = lua.create_table()?;
let i = callback.info();
if let Some(n) = i.name {
listener.set("name", n)?;
}
if let Some(l) = i.line_defined {
listener.set("line_defined", l)?;
}
if let Some(s) = i.source {
listener.set("source", s)?;
}
type_listeners.set(id.to_owned(), listener)?;
let info = callback.info();
let table = lua.create_table()?;
table.set("name", info.name)?;
table.set("line_defined", info.line_defined)?;
table.set("source", info.source)?;
type_listeners_table.set(id.to_owned(), table)?;
}
listeners.set(event_type.to_owned(), type_listeners)?;
listeners_table.set(event_type.to_owned(), type_listeners_table)?;
}
Ok(listeners)
Ok(listeners_table)
})?,
)?;

View File

@@ -1,7 +1,7 @@
use log::{debug, error, info, trace, warn};
use mlua::{Lua, Result, Table};
pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set(
"error",
lua.create_function(|_, message: String| {

63
src/lua/matrix/client.rs Normal file
View File

@@ -0,0 +1,63 @@
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)
});
}
}

12
src/lua/matrix/member.rs Normal file
View File

@@ -0,0 +1,12 @@
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()));
}
}

3
src/lua/matrix/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod client;
pub mod member;
pub mod room;

89
src/lua/matrix/room.rs Normal file
View File

@@ -0,0 +1,89 @@
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())
},
);
}
}

View File

@@ -4,16 +4,25 @@ pub mod container;
pub mod direction;
pub mod events;
pub mod logging;
pub mod nochatreports;
pub mod player;
pub mod system;
pub mod thread;
pub mod vec3;
use crate::ListenerMap;
#[cfg(feature = "matrix")]
pub mod matrix;
use std::{
fmt::{self, Display, Formatter},
io,
};
use mlua::{Lua, Table};
use std::{io, time::Duration};
use crate::{ListenerMap, build_info::built};
#[derive(Debug)]
#[allow(dead_code)]
pub enum Error {
CreateEnv(mlua::Error),
EvalChunk(mlua::Error),
@@ -23,30 +32,45 @@ pub enum Error {
ReadFile(io::Error),
}
pub fn register_functions(
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(
lua: &Lua,
globals: &Table,
event_listeners: ListenerMap,
) -> mlua::Result<()> {
globals.set(
"sleep",
lua.create_async_function(async |_, duration: u64| {
tokio::time::sleep(Duration::from_millis(duration)).await;
Ok(())
})?,
)?;
globals.set("CARGO_PKG_VERSION", env!("CARGO_PKG_VERSION"))?;
globals.set("GIT_COMMIT_HASH", built::GIT_COMMIT_HASH)?;
globals.set("GIT_COMMIT_HASH_SHORT", built::GIT_COMMIT_HASH_SHORT)?;
block::register_functions(lua, globals)?;
events::register_functions(lua, globals, event_listeners)?;
logging::register_functions(lua, globals)?;
system::register_functions(lua, globals)
block::register_globals(lua, globals)?;
events::register_globals(lua, globals, event_listeners)?;
logging::register_globals(lua, globals)?;
nochatreports::register_globals(lua, globals)?;
system::register_globals(lua, globals)?;
thread::register_globals(lua, globals)
}
pub fn reload(lua: &Lua, sender: Option<String>) -> Result<(), Error> {
lua.load(
&std::fs::read_to_string(
lua.globals()
.get::<String>("script_path")
.get::<String>("SCRIPT_PATH")
.map_err(Error::MissingPath)?,
)
.map_err(Error::ReadFile)?,

View File

@@ -0,0 +1,25 @@
#[macro_export]
macro_rules! crypt {
($op:ident, $options:expr, $text:expr) => {{
macro_rules! crypt_with {
($algo:ident) => {{
let encoding = $options.get("encoding").unwrap_or_default();
let key = &$options.get::<UserDataRef<AesKey>>("key")?.0;
match encoding {
1 => $algo::<Base64Encoding>::$op($text, &key),
2 => $algo::<Base64rEncoding>::$op($text, &key),
_ => $algo::<NewBase64rEncoding>::$op($text, &key),
}
.map_err(|error| Error::external(error.to_string()))?
}};
}
match $options.get("encryption").unwrap_or_default() {
1 => CaesarEncryption::$op(&$text, &$options.get("key")?)
.map_err(|error| Error::external(error.to_string()))?,
2 => crypt_with!(EcbEncryption),
3 => crypt_with!(GcmEncryption),
_ => crypt_with!(Cfb8Encryption),
}
}};
}

View File

@@ -0,0 +1,10 @@
use mlua::{UserData, UserDataFields};
pub struct AesKey(pub ncr::AesKey);
impl UserData for AesKey {
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
f.add_field_method_get("base64", |_, this| Ok(this.0.encode_base64()));
f.add_field_method_get("bytes", |_, this| Ok(this.0.as_ref().to_vec()));
}
}

View File

@@ -0,0 +1,65 @@
#[macro_use]
pub mod crypt;
pub mod key;
use key::AesKey;
use mlua::{Error, Lua, Result, Table, UserDataRef};
use ncr::{
encoding::{Base64Encoding, Base64rEncoding, NewBase64rEncoding},
encryption::{CaesarEncryption, Cfb8Encryption, EcbEncryption, Encryption, GcmEncryption},
utils::{prepend_header, trim_header},
};
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set(
"ncr_aes_key_from_passphrase",
lua.create_function(|_, passphrase: Vec<u8>| {
Ok(AesKey(ncr::AesKey::gen_from_passphrase(&passphrase)))
})?,
)?;
globals.set(
"ncr_aes_key_from_base64",
lua.create_function(|_, base64: String| {
Ok(AesKey(
ncr::AesKey::decode_base64(&base64)
.map_err(|error| Error::external(error.to_string()))?,
))
})?,
)?;
globals.set(
"ncr_generate_random_aes_key",
lua.create_function(|_, (): ()| Ok(AesKey(ncr::AesKey::gen_random_key())))?,
)?;
globals.set(
"ncr_encrypt",
lua.create_function(|_, (options, plaintext): (Table, String)| {
Ok(crypt!(encrypt, options, &plaintext))
})?,
)?;
globals.set(
"ncr_decrypt",
lua.create_function(|_, (options, ciphertext): (Table, String)| {
Ok(crypt!(decrypt, options, &ciphertext))
})?,
)?;
globals.set(
"ncr_prepend_header",
lua.create_function(|_, text: String| Ok(prepend_header(&text)))?,
)?;
globals.set(
"ncr_trim_header",
lua.create_function(|_, text: String| {
Ok(trim_header(&text)
.map_err(|error| Error::external(error.to_string()))?
.to_owned())
})?,
)?;
Ok(())
}

View File

@@ -13,7 +13,7 @@ pub struct Player {
impl From<PlayerInfo> for Player {
fn from(p: PlayerInfo) -> Self {
Self {
display_name: p.display_name.map(|n| n.to_string()),
display_name: p.display_name.map(|text| text.to_string()),
gamemode: p.gamemode.to_id(),
latency: p.latency,
name: p.profile.name,

View File

@@ -1,12 +1,13 @@
use log::error;
use mlua::{Lua, Result, Table};
use std::{
ffi::OsString,
process::{Command, Stdio},
thread,
};
pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
use log::error;
use mlua::{Lua, Result, Table};
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set(
"system",
lua.create_function(|_, (command, args): (String, Option<Vec<OsString>>)| {

28
src/lua/thread.rs Normal file
View File

@@ -0,0 +1,28 @@
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(())
}

View File

@@ -1,5 +1,5 @@
use azalea::{BlockPos, entity::Position};
use mlua::{FromLua, IntoLua, Lua, Result, Value};
use mlua::{Error, FromLua, IntoLua, Lua, Result, Value};
#[derive(Clone)]
pub struct Vec3 {
@@ -40,7 +40,7 @@ impl From<&Position> for Vec3 {
impl From<BlockPos> for Vec3 {
fn from(p: BlockPos) -> Self {
Vec3 {
Self {
x: f64::from(p.x),
y: f64::from(p.y),
z: f64::from(p.z),
@@ -63,7 +63,7 @@ impl FromLua for Vec3 {
},
)
} else {
Err(mlua::Error::FromLuaConversionError {
Err(Error::FromLuaConversionError {
from: value.type_name(),
to: "Vec3".to_string(),
message: None,

View File

@@ -1,12 +1,29 @@
#![feature(let_chains)]
#![feature(if_let_guard, let_chains)]
#![warn(clippy::pedantic, clippy::nursery)]
#![allow(clippy::significant_drop_tightening)]
mod arguments;
mod build_info;
mod commands;
mod events;
mod hacks;
mod http;
mod lua;
mod particle;
mod replay;
#[cfg(feature = "matrix")]
mod matrix;
use std::{
collections::HashMap,
env,
fs::{OpenOptions, read_to_string},
sync::Arc,
};
use anyhow::{Context, Result};
use arguments::Arguments;
use azalea::{
DefaultBotPlugins, DefaultPlugins, brigadier::prelude::CommandDispatcher, prelude::*,
};
@@ -19,52 +36,55 @@ use clap::Parser;
use commands::{CommandSource, register};
use futures::lock::Mutex;
use futures_locks::RwLock;
use mlua::{Function, Lua};
use std::{
collections::HashMap,
env,
fs::{OpenOptions, read_to_string},
net::SocketAddr,
path::PathBuf,
sync::Arc,
};
use hacks::HacksPlugin;
use log::debug;
use mlua::{Function, Lua, Table};
use replay::{plugin::RecordPlugin, recorder::Recorder};
const DEFAULT_SCRIPT_PATH: &str = "errornowatcher.lua";
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
type ListenerMap = Arc<RwLock<HashMap<String, Vec<(String, Function)>>>>;
#[derive(Default, Clone, Component)]
pub struct State {
struct State {
lua: Arc<Lua>,
event_listeners: ListenerMap,
commands: Arc<CommandDispatcher<Mutex<CommandSource>>>,
http_address: Option<SocketAddr>,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> Result<()> {
#[cfg(feature = "console-subscriber")]
console_subscriber::init();
let args = arguments::Arguments::parse();
let script_path = args.script.unwrap_or(PathBuf::from(DEFAULT_SCRIPT_PATH));
let args = Arguments::parse();
let event_listeners = Arc::new(RwLock::new(HashMap::new()));
let lua = Lua::new();
let lua = unsafe { Lua::unsafe_new() };
let globals = lua.globals();
lua::register_globals(&lua, &globals, event_listeners.clone())?;
if let Some(path) = args.script {
globals.set("SCRIPT_PATH", &*path)?;
lua.load(read_to_string(path)?).exec()?;
} else if let Some(code) = ["main.lua", "errornowatcher.lua"].iter().find_map(|path| {
debug!("trying to load code from {path}");
globals.set("SCRIPT_PATH", *path).ok()?;
read_to_string(path).ok()
}) {
lua.load(code).exec()?;
}
if let Some(code) = args.exec {
lua.load(code).exec()?;
}
globals.set("script_path", &*script_path)?;
lua::register_functions(&lua, &globals, event_listeners.clone())?;
lua.load(
read_to_string(script_path)
.expect(&(DEFAULT_SCRIPT_PATH.to_owned() + " should be in current directory")),
)
.exec()?;
let server = globals
.get::<String>("Server")
.expect("Server should be in lua globals");
.context("lua globals missing Server variable")?;
let username = globals
.get::<String>("Username")
.expect("Username should be in lua globals");
.context("lua globals missing Username variable")?;
let mut commands = CommandDispatcher::new();
register(&mut commands);
@@ -74,14 +94,14 @@ async fn main() -> anyhow::Result<()> {
} else {
DefaultPlugins.set(LogPlugin {
custom_layer: |_| {
env::var("LOG_FILE").ok().map(|log_file| {
env::var("LOG_FILE").ok().map(|path| {
layer()
.with_writer(
OpenOptions::new()
.append(true)
.create(true)
.open(log_file)
.expect("log file should be accessible"),
.open(&path)
.expect(&(path + " should be accessible")),
)
.boxed()
})
@@ -89,26 +109,42 @@ async fn main() -> anyhow::Result<()> {
..Default::default()
})
};
let Err(error) = ClientBuilder::new_without_plugins()
.add_plugins(default_plugins)
let record_plugin = RecordPlugin {
recorder: Arc::new(parking_lot::Mutex::new(
if let Ok(options) = globals.get::<Table>("ReplayRecordingOptions")
&& let Ok(path) = options.get::<String>("path")
{
Some(Recorder::new(
path,
server.clone(),
options
.get::<bool>("ignore_compression")
.unwrap_or_default(),
)?)
} else {
None
},
)),
};
let account = if username.contains('@') {
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(record_plugin)
.set_handler(events::handle_event)
.set_state(State {
lua: Arc::new(lua),
event_listeners,
commands: Arc::new(commands),
http_address: args.http_address,
})
.start(
if username.contains('@') {
Account::microsoft(&username).await?
} else {
Account::offline(&username)
},
server.as_ref(),
)
.start(account, server)
.await;
eprintln!("{error:?}");
eprintln!("{err}");
Ok(())
}

117
src/matrix/bot.rs Normal file
View File

@@ -0,0 +1,117 @@
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(())
}

149
src/matrix/mod.rs Normal file
View File

@@ -0,0 +1,149 @@
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(())
}

156
src/matrix/verification.rs Normal file
View File

@@ -0,0 +1,156 @@
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(())
}

View File

@@ -1,7 +1,7 @@
use azalea::{entity::particle::Particle, registry::ParticleKind};
#[allow(clippy::too_many_lines)]
pub fn to_kind(particle: &Particle) -> ParticleKind {
pub const fn to_kind(particle: &Particle) -> ParticleKind {
match particle {
Particle::AngryVillager => ParticleKind::AngryVillager,
Particle::Block(_) => ParticleKind::Block,
@@ -34,6 +34,7 @@ pub fn to_kind(particle: &Particle) -> ParticleKind {
Particle::Flame => ParticleKind::Flame,
Particle::CherryLeaves => ParticleKind::CherryLeaves,
Particle::PaleOakLeaves => ParticleKind::PaleOakLeaves,
Particle::TintedLeaves => ParticleKind::TintedLeaves,
Particle::SculkSoul => ParticleKind::SculkSoul,
Particle::SculkCharge(_) => ParticleKind::SculkCharge,
Particle::SculkChargePop => ParticleKind::SculkChargePop,
@@ -115,5 +116,6 @@ pub fn to_kind(particle: &Particle) -> ParticleKind {
Particle::TrialOmen => ParticleKind::TrialOmen,
Particle::Trail => ParticleKind::Trail,
Particle::BlockCrumble => ParticleKind::BlockCrumble,
Particle::Firefly => ParticleKind::Firefly,
}
}

2
src/replay/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod plugin;
pub mod recorder;

79
src/replay/plugin.rs Normal file
View File

@@ -0,0 +1,79 @@
#![allow(clippy::needless_pass_by_value)]
use std::sync::Arc;
use azalea::{
ecs::event::EventReader,
packet::{
config::ReceiveConfigPacketEvent, game::ReceiveGamePacketEvent,
login::ReceiveLoginPacketEvent,
},
protocol::packets::login::ClientboundLoginPacket,
};
use bevy_app::{App, First, Plugin};
use bevy_ecs::system::ResMut;
use log::error;
use parking_lot::Mutex;
use super::recorder::Recorder;
pub struct RecordPlugin {
pub recorder: Arc<Mutex<Option<Recorder>>>,
}
impl Plugin for RecordPlugin {
fn build(&self, app: &mut App) {
let recorder = self.recorder.lock().take();
if let Some(recorder) = recorder {
app.insert_resource(recorder)
.add_systems(First, record_login_packets)
.add_systems(First, record_configuration_packets)
.add_systems(First, record_game_packets);
}
}
}
fn record_login_packets(
recorder: Option<ResMut<Recorder>>,
mut events: EventReader<ReceiveLoginPacketEvent>,
) {
if let Some(mut recorder) = recorder {
for event in events.read() {
if recorder.should_ignore_compression
&& let ClientboundLoginPacket::LoginCompression(_) = *event.packet
{
continue;
}
if let Err(error) = recorder.save_packet(event.packet.as_ref()) {
error!("failed to record login packet: {error:?}");
}
}
}
}
fn record_configuration_packets(
recorder: Option<ResMut<Recorder>>,
mut events: EventReader<ReceiveConfigPacketEvent>,
) {
if let Some(mut recorder) = recorder {
for event in events.read() {
if let Err(error) = recorder.save_packet(event.packet.as_ref()) {
error!("failed to record configuration packet: {error:?}");
}
}
}
}
fn record_game_packets(
recorder: Option<ResMut<Recorder>>,
mut events: EventReader<ReceiveGamePacketEvent>,
) {
if let Some(mut recorder) = recorder {
for event in events.read() {
if let Err(error) = recorder.save_packet(event.packet.as_ref()) {
error!("failed to record game packet: {error:?}");
}
}
}
}

88
src/replay/recorder.rs Normal file
View File

@@ -0,0 +1,88 @@
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)
}
}