Compare commits

..

No commits in common. "a3559794aa33ad45a2c2395b9b7c03c8bfb2ba8a" and "ce98afb11d88c0ba64a3cc638899ebfaeef1f1a9" have entirely different histories.

6 changed files with 145 additions and 217 deletions

View File

@ -56,10 +56,10 @@ impl UserData for Client {
} }
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) { fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_async_method("find_all_entities", world::find::all_entities); m.add_async_method("find_all_entities", world::find_all_entities);
m.add_async_method("find_all_players", world::find::all_players); m.add_async_method("find_all_players", world::find_all_players);
m.add_async_method("find_entities", world::find::entities); m.add_async_method("find_entities", world::find_entities);
m.add_async_method("find_players", world::find::players); m.add_async_method("find_players", world::find_players);
m.add_async_method("go_to", movement::go_to); m.add_async_method("go_to", movement::go_to);
m.add_async_method("mine", interaction::mine); m.add_async_method("mine", interaction::mine);
m.add_async_method("open_container_at", container::open_container_at); m.add_async_method("open_container_at", container::open_container_at);
@ -67,7 +67,7 @@ impl UserData for Client {
m.add_method("best_tool_for_block", world::best_tool_for_block); m.add_method("best_tool_for_block", world::best_tool_for_block);
m.add_method("chat", chat); m.add_method("chat", chat);
m.add_method("disconnect", disconnect); m.add_method("disconnect", disconnect);
m.add_method("find_blocks", world::find::blocks); m.add_method("find_blocks", world::find_blocks);
m.add_method("get_block_state", world::get_block_state); m.add_method("get_block_state", world::get_block_state);
m.add_method("get_fluid_state", world::get_fluid_state); m.add_method("get_fluid_state", world::get_fluid_state);
m.add_method("set_component", state::set_component); m.add_method("set_component", state::set_component);

View File

@ -1,121 +0,0 @@
use super::{Client, Direction, Vec3};
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};
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.iter().map(|&id| BlockState { id }).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

@ -1,10 +1,19 @@
#[macro_use] #[macro_use]
mod queries; mod queries;
pub mod find;
use super::{Client, Direction, Vec3}; use super::{Client, Direction, Vec3};
use azalea::{BlockPos, auto_tool::AutoToolClientExt, blocks::BlockState, world::InstanceName}; use azalea::{
use mlua::{Lua, Result, Table}; 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> { 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 result = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
@ -19,6 +28,119 @@ pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> {
Ok(client.component::<InstanceName>().to_string()) Ok(client.component::<InstanceName>().to_string())
} }
pub fn find_blocks(
_lua: &Lua,
client: &Client,
(nearest_to, block_states): (Vec3, Vec<u16>),
) -> Result<Vec<Vec3>> {
#[allow(clippy::cast_possible_truncation)]
Ok(client
.world()
.read()
.find_blocks(
BlockPos::new(
nearest_to.x as i32,
nearest_to.y as i32,
nearest_to.z as i32,
),
&BlockStates {
set: block_states.iter().map(|&id| BlockState { id }).collect(),
},
)
.map(Vec3::from)
.collect())
}
pub async fn find_all_entities(
lua: Lua,
client: UserDataRef<Client>,
(): (),
) -> Result<Vec<Table>> {
let mut matched = Vec::with_capacity(256);
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
get_entities!(client)
{
let table = lua.create_table()?;
table.set("position", position)?;
table.set("custom_name", custom_name)?;
table.set("kind", kind)?;
table.set("uuid", uuid)?;
table.set("direction", direction)?;
table.set("id", id)?;
table.set(
"owner_uuid",
owner_uuid.and_then(|v| *v).map(|v| v.to_string()),
)?;
table.set("pose", pose)?;
matched.push(table);
}
Ok(matched)
}
pub async fn find_entities(
lua: Lua,
client: UserDataRef<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
let mut matched = Vec::new();
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
get_entities!(client)
{
let table = lua.create_table()?;
table.set("position", position)?;
table.set("custom_name", custom_name)?;
table.set("kind", kind)?;
table.set("uuid", uuid)?;
table.set("direction", direction)?;
table.set("id", id)?;
table.set(
"owner_uuid",
owner_uuid.and_then(|v| *v).map(|v| v.to_string()),
)?;
table.set("pose", pose)?;
if filter_fn.call_async::<bool>(&table).await? {
matched.push(table);
}
}
Ok(matched)
}
pub async fn find_all_players(lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<Vec<Table>> {
let mut matched = Vec::new();
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
let table = lua.create_table()?;
table.set("id", id)?;
table.set("uuid", uuid)?;
table.set("kind", kind)?;
table.set("position", position)?;
table.set("direction", direction)?;
table.set("pose", pose)?;
matched.push(table);
}
Ok(matched)
}
pub async fn find_players(
lua: Lua,
client: UserDataRef<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
let mut matched = Vec::new();
for (id, uuid, kind, position, direction, pose) in get_players!(client) {
let table = lua.create_table()?;
table.set("id", id)?;
table.set("uuid", uuid)?;
table.set("kind", kind)?;
table.set("position", position)?;
table.set("direction", direction)?;
table.set("pose", pose)?;
if filter_fn.call_async::<bool>(&table).await? {
matched.push(table);
}
}
Ok(matched)
}
pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> { pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Option<u16>> {
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
Ok(client Ok(client

View File

@ -1,8 +1,5 @@
use super::room::Room; use super::room::Room;
use matrix_sdk::{ use matrix_sdk::{Client as MatrixClient, ruma::UserId};
Client as MatrixClient,
ruma::{RoomId, UserId},
};
use mlua::{Error, UserData, UserDataFields, UserDataMethods}; use mlua::{Error, UserData, UserDataFields, UserDataMethods};
use std::sync::Arc; use std::sync::Arc;
@ -10,35 +7,11 @@ pub struct Client(pub Arc<MatrixClient>);
impl UserData for Client { impl UserData for Client {
fn add_fields<F: UserDataFields<Self>>(f: &mut F) { fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
f.add_field_method_get("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| { f.add_field_method_get("rooms", |_, this| {
Ok(this.0.rooms().into_iter().map(Room).collect::<Vec<_>>()) Ok(this.0.rooms().into_iter().map(Room).collect::<Vec<_>>())
}); });
f.add_field_method_get("user_id", |_, this| { f.add_field_method_get("user_id", |_, this| {
Ok(this.0.user_id().map(ToString::to_string)) Ok(this.0.user_id().map(std::string::ToString::to_string))
}); });
} }
@ -50,12 +23,5 @@ impl UserData for Client {
.map_err(Error::external) .map_err(Error::external)
.map(Room) .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)
});
} }
} }

View File

@ -1,8 +1,6 @@
use super::member::Member; use super::member::Member;
use matrix_sdk::{ use matrix_sdk::{
RoomMemberships, RoomMemberships, room::Room as MatrixRoom, ruma::events::room::message::RoomMessageEventContent,
room::Room as MatrixRoom,
ruma::{EventId, UserId, events::room::message::RoomMessageEventContent},
}; };
use mlua::{Error, UserData, UserDataFields, UserDataMethods}; use mlua::{Error, UserData, UserDataFields, UserDataMethods};
@ -19,18 +17,16 @@ impl UserData for Room {
} }
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) { fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_async_method( m.add_async_method("send", async |_, this, body: String| {
"ban_user",
async |_, this, (user_id, reason): (String, Option<String>)| {
this.0 this.0
.ban_user( .send(RoomMessageEventContent::text_plain(body))
&UserId::parse(user_id).map_err(Error::external)?,
reason.as_deref(),
)
.await .await
.map_err(Error::external) .map_err(Error::external)
}, .map(|response| response.event_id.to_string())
); });
m.add_async_method("leave", async |_, this, (): ()| {
this.0.leave().await.map_err(Error::external)
});
m.add_async_method("get_members", async |_, this, (): ()| { m.add_async_method("get_members", async |_, this, (): ()| {
this.0 this.0
.members(RoomMemberships::all()) .members(RoomMemberships::all())
@ -43,41 +39,5 @@ impl UserData for Room {
.collect::<Vec<_>>() .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())
});
} }
} }

View File

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