Compare commits

...

3 Commits

6 changed files with 217 additions and 145 deletions

View File

@ -56,10 +56,10 @@ impl UserData for Client {
}
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_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("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("mine", interaction::mine);
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("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("set_component", state::set_component);

View File

@ -0,0 +1,121 @@
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,19 +1,10 @@
#[macro_use]
mod queries;
pub mod find;
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};
use azalea::{BlockPos, auto_tool::AutoToolClientExt, blocks::BlockState, world::InstanceName};
use mlua::{Lua, Result, Table};
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Table> {
let result = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
@ -28,119 +19,6 @@ 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_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>> {
#[allow(clippy::cast_possible_truncation)]
Ok(client

View File

@ -1,5 +1,8 @@
use super::room::Room;
use matrix_sdk::{Client as MatrixClient, ruma::UserId};
use matrix_sdk::{
Client as MatrixClient,
ruma::{RoomId, UserId},
};
use mlua::{Error, UserData, UserDataFields, UserDataMethods};
use std::sync::Arc;
@ -7,11 +10,35 @@ 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(std::string::ToString::to_string))
Ok(this.0.user_id().map(ToString::to_string))
});
}
@ -23,5 +50,12 @@ impl UserData for Client {
.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)
});
}
}

View File

@ -1,6 +1,8 @@
use super::member::Member;
use matrix_sdk::{
RoomMemberships, room::Room as MatrixRoom, ruma::events::room::message::RoomMessageEventContent,
RoomMemberships,
room::Room as MatrixRoom,
ruma::{EventId, UserId, events::room::message::RoomMessageEventContent},
};
use mlua::{Error, UserData, UserDataFields, UserDataMethods};
@ -17,16 +19,18 @@ impl UserData for Room {
}
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
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("leave", async |_, this, (): ()| {
this.0.leave().await.map_err(Error::external)
});
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())
@ -39,5 +43,41 @@ impl UserData for Room {
.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,5 +1,3 @@
use std::time::Duration;
use anyhow::{Context, Result};
use futures::StreamExt;
use log::{error, info, warn};
@ -17,6 +15,7 @@ use matrix_sdk::{
},
},
};
use std::time::Duration;
use tokio::time::sleep;
async fn confirm_emojis(sas: SasVerification, emoji: [Emoji; 7]) {