Skip to content

Commit 57397d3

Browse files
fix(client): handle borrows with async
1 parent d6c16f0 commit 57397d3

7 files changed

Lines changed: 84 additions & 50 deletions

File tree

src/lua/client/container.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use azalea::{
77
use mlua::{Lua, Result, UserDataRef, Value};
88

99
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
10+
use crate::unpack;
1011

1112
pub fn container(_lua: &Lua, client: &Client) -> Result<ContainerRef> {
1213
Ok(ContainerRef(client.get_inventory()))
@@ -95,9 +96,10 @@ pub async fn open_container_at(
9596
client: UserDataRef<Client>,
9697
position: Vec3,
9798
) -> Result<Option<Container>> {
99+
let client = unpack!(client);
100+
98101
#[allow(clippy::cast_possible_truncation)]
99102
Ok(client
100-
.clone()
101103
.open_container_at(BlockPos::new(
102104
position.x as i32,
103105
position.y as i32,

src/lua/client/interaction.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use azalea::{
66
use mlua::{Lua, Result, UserDataRef};
77

88
use super::{Client, Vec3};
9+
use crate::unpack;
910

1011
pub fn attack(_lua: &Lua, client: &Client, entity_id: i32) -> Result<()> {
1112
if let Some(entity) = client.entity_id_by_minecraft_id(MinecraftEntityId(entity_id)) {
@@ -29,9 +30,10 @@ pub fn has_attack_cooldown(_lua: &Lua, client: &Client) -> Result<bool> {
2930
}
3031

3132
pub async fn mine(_lua: Lua, client: UserDataRef<Client>, position: Vec3) -> Result<()> {
33+
let client = unpack!(client);
34+
3235
#[allow(clippy::cast_possible_truncation)]
3336
client
34-
.clone()
3537
.mine(BlockPos::new(
3638
position.x as i32,
3739
position.y as i32,

src/lua/client/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod movement;
66
mod state;
77
mod world;
88

9-
use std::ops::{Deref, DerefMut};
9+
use std::ops::Deref;
1010

1111
use azalea::{Client as AzaleaClient, core::entity_id::MinecraftEntityId};
1212
use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods};
@@ -28,12 +28,6 @@ impl Deref for Client {
2828
}
2929
}
3030

31-
impl DerefMut for Client {
32-
fn deref_mut(&mut self) -> &mut Self::Target {
33-
self.0.as_mut().expect("should have received init event")
34-
}
35-
}
36-
3731
impl UserData for Client {
3832
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
3933
f.add_field_method_get("air_supply", state::air_supply);
@@ -121,3 +115,12 @@ fn username(_lua: &Lua, client: &Client) -> Result<String> {
121115
fn uuid(_lua: &Lua, client: &Client) -> Result<String> {
122116
Ok(client.uuid().to_string())
123117
}
118+
119+
#[macro_export]
120+
macro_rules! unpack {
121+
($client:ident) => {{
122+
let inner = (**$client).clone();
123+
drop($client);
124+
inner
125+
}};
126+
}

src/lua/client/movement.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use azalea::{
2-
BlockPos, SprintDirection, WalkDirection,
2+
BlockPos, Client as AzaleaClient, SprintDirection, WalkDirection,
33
core::{entity_id::MinecraftEntityId, hit_result::HitResult},
44
entity::Position,
55
interact::pick::HitResultComponent,
@@ -12,6 +12,7 @@ use azalea::{
1212
use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value};
1313

1414
use super::{Client, Direction, Vec3};
15+
use crate::unpack;
1516

1617
#[derive(Debug)]
1718
struct AnyGoal(Box<dyn Goal>);
@@ -27,7 +28,7 @@ impl Goal for AnyGoal {
2728
}
2829

2930
#[allow(clippy::cast_possible_truncation)]
30-
fn to_goal(lua: &Lua, client: &Client, data: Table, kind: u8) -> Result<AnyGoal> {
31+
fn to_goal(lua: &Lua, client: &AzaleaClient, data: Table, kind: u8) -> Result<AnyGoal> {
3132
let goal: Box<dyn Goal> = match kind {
3233
1 => {
3334
let pos = Vec3::from_lua(data.get("position")?, lua)?;
@@ -68,6 +69,7 @@ pub fn go_to_reached(_lua: &Lua, client: &Client) -> Result<bool> {
6869
}
6970

7071
pub async fn wait_until_goal_reached(_lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<()> {
72+
let client = unpack!(client);
7173
client.wait_until_goto_target_reached().await;
7274
Ok(())
7375
}
@@ -77,6 +79,8 @@ pub async fn go_to(
7779
client: UserDataRef<Client>,
7880
(data, metadata): (Table, Option<Table>),
7981
) -> Result<()> {
82+
let client = unpack!(client);
83+
8084
let metadata = metadata.unwrap_or(lua.create_table()?);
8185
let options = metadata.get("options").unwrap_or(lua.create_table()?);
8286
let goal = to_goal(
@@ -85,12 +89,13 @@ pub async fn go_to(
8589
data,
8690
metadata.get("type").unwrap_or_default(),
8791
)?;
88-
if options.get("without_mining").unwrap_or_default() {
89-
client.start_goto_with_opts(goal, PathfinderOpts::new().allow_mining(false));
90-
client.wait_until_goto_target_reached().await;
91-
} else {
92-
client.goto(goal).await;
93-
}
92+
client
93+
.goto_with_opts(
94+
goal,
95+
PathfinderOpts::new().allow_mining(options.get("without_mining").unwrap_or_default()),
96+
)
97+
.await;
98+
9499
Ok(())
95100
}
96101

@@ -99,6 +104,8 @@ pub async fn start_go_to(
99104
client: UserDataRef<Client>,
100105
(data, metadata): (Table, Option<Table>),
101106
) -> Result<()> {
107+
let client = unpack!(client);
108+
102109
let metadata = metadata.unwrap_or(lua.create_table()?);
103110
let options = metadata.get("options").unwrap_or(lua.create_table()?);
104111
let goal = to_goal(

src/lua/client/state.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use azalea_hax::AntiKnockback;
66
use mlua::{Error, Lua, Result, Table, UserDataRef};
77

88
use super::Client;
9+
use crate::unpack;
910

1011
pub fn air_supply(_lua: &Lua, client: &Client) -> Result<i32> {
1112
Ok(client.component::<AirSupply>().0)
@@ -37,6 +38,8 @@ pub async fn set_client_information(
3738
client: UserDataRef<Client>,
3839
info: Table,
3940
) -> Result<()> {
41+
let client = unpack!(client);
42+
4043
let get_bool = |table: &Table, name| table.get(name).unwrap_or(true);
4144
client.set_client_information(ClientInformation {
4245
allows_listing: info.get("allows_listing")?,

src/lua/client/world/find.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use azalea::{
1010
use mlua::{Function, Lua, Result, Table, UserDataRef};
1111

1212
use super::{Client, Direction, Vec3};
13-
use crate::lua::client::MinecraftEntityId;
13+
use crate::{lua::client::MinecraftEntityId, unpack};
1414

1515
pub fn blocks(
1616
_lua: &Lua,
@@ -39,6 +39,8 @@ pub fn blocks(
3939
}
4040

4141
pub async fn all_entities(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
42+
let client = unpack!(client);
43+
4244
let mut matched = Vec::with_capacity(256);
4345
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
4446
get_entities!(client)
@@ -65,6 +67,8 @@ pub async fn entities(
6567
client: UserDataRef<Client>,
6668
filter_fn: Function,
6769
) -> Result<Vec<Table>> {
70+
let client = unpack!(client);
71+
6872
let mut matched = Vec::new();
6973
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
7074
get_entities!(client)
@@ -89,6 +93,8 @@ pub async fn entities(
8993
}
9094

9195
pub async fn all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
96+
let client = unpack!(client);
97+
9298
let mut matched = Vec::new();
9399
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
94100
let table = lua.create_table()?;
@@ -108,6 +114,8 @@ pub async fn players(
108114
client: UserDataRef<Client>,
109115
filter_fn: Function,
110116
) -> Result<Vec<Table>> {
117+
let client = unpack!(client);
118+
111119
let mut matched = Vec::new();
112120
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
113121
let table = lua.create_table()?;

src/lua/client/world/queries.rs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#[macro_export]
22
macro_rules! get_entities {
33
($client:ident) => {{
4-
let mut ecs = $client.ecs.write();
5-
ecs.query::<(
4+
let ecs = $client.ecs.read();
5+
if let Some(mut query) = ecs.try_query::<(
66
&AzaleaPosition,
77
&CustomName,
88
&EntityKindComponent,
@@ -11,49 +11,58 @@ macro_rules! get_entities {
1111
&MinecraftEntityId,
1212
Option<&Owneruuid>,
1313
&Pose,
14-
)>()
15-
.iter(&ecs)
16-
.map(
17-
|(position, custom_name, kind, uuid, direction, id, owner_uuid, pose)| {
18-
(
19-
Vec3::from(*position),
20-
custom_name.as_ref().map(ToString::to_string),
21-
kind.to_string(),
22-
uuid.to_string(),
23-
Direction::from(direction),
24-
id.0,
25-
owner_uuid.map(ToOwned::to_owned),
26-
*pose as u8,
14+
)>() {
15+
query
16+
.iter(&ecs)
17+
.map(
18+
|(position, custom_name, kind, uuid, direction, id, owner_uuid, pose)| {
19+
(
20+
Vec3::from(*position),
21+
custom_name.as_ref().map(ToString::to_string),
22+
kind.to_string(),
23+
uuid.to_string(),
24+
Direction::from(direction),
25+
id.0,
26+
owner_uuid.map(ToOwned::to_owned),
27+
*pose as u8,
28+
)
29+
},
2730
)
28-
},
29-
)
30-
.collect::<Vec<_>>()
31+
.collect::<Vec<_>>()
32+
} else {
33+
Vec::new()
34+
}
3135
}};
3236
}
3337

3438
#[macro_export]
3539
macro_rules! get_players {
3640
($client:ident) => {{
37-
let mut ecs = $client.ecs.write();
38-
ecs.query_filtered::<(
41+
let ecs = $client.ecs.read();
42+
if let Some(mut query) = ecs.try_query_filtered::<(
3943
&MinecraftEntityId,
4044
&EntityUuid,
4145
&EntityKindComponent,
4246
&AzaleaPosition,
4347
&LookDirection,
4448
&Pose,
4549
), (With<Player>, Without<Dead>)>()
46-
.iter(&ecs)
47-
.map(|(id, uuid, kind, position, direction, pose)| {
48-
(
49-
id.0,
50-
uuid.to_string(),
51-
kind.to_string(),
52-
Vec3::from(*position),
53-
Direction::from(direction),
54-
*pose as u8,
55-
)
56-
})
57-
.collect::<Vec<_>>()
50+
{
51+
query
52+
.iter(&ecs)
53+
.map(|(id, uuid, kind, position, direction, pose)| {
54+
(
55+
id.0,
56+
uuid.to_string(),
57+
kind.to_string(),
58+
Vec3::from(*position),
59+
Direction::from(direction),
60+
*pose as u8,
61+
)
62+
})
63+
.collect::<Vec<_>>()
64+
} else {
65+
Vec::new()
66+
}
5867
}};
5968
}

0 commit comments

Comments
 (0)