Compare commits

..

No commits in common. "5e48377969c6c7fc698eb0a5c5805665ffd10288" and "b55207a55936105549d2724c0723a78e9a12c45c" have entirely different histories.

7 changed files with 82 additions and 100 deletions

View File

@ -14,8 +14,6 @@ use tokio::net::TcpListener;
#[allow(clippy::too_many_lines)]
pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow::Result<()> {
state.lua.gc_stop();
match event {
Event::AddPlayer(player_info) => {
call_listeners(&state, "add_player", Player::from(player_info)).await;
@ -24,13 +22,10 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
let formatted_message = message.message();
info!("{}", formatted_message.to_ansi());
let owners = state.lua.globals().get::<Vec<String>>("Owners")?;
if message.is_whisper()
&& let (Some(sender), content) = message.split_sender_and_content()
&& state
.lua
.globals()
.get::<Vec<String>>("Owners")?
.contains(&sender)
&& owners.contains(&sender)
{
if let Err(error) = state.commands.execute(
content,
@ -60,7 +55,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
}
Event::Disconnect(message) => {
call_listeners(&state, "disconnect", message.map(|m| m.to_string())).await;
exit(0)
exit(1)
}
Event::Login => call_listeners(&state, "login", ()).await,
Event::RemovePlayer(player_info) => {
@ -89,7 +84,8 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
Event::Init => {
debug!("received initialize event");
state.lua.globals().set(
let globals = state.lua.globals();
globals.set(
"client",
lua::client::Client {
inner: Some(client),

View File

@ -9,10 +9,7 @@ pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
"get_block_from_state",
lua.create_function(get_block_from_state)?,
)?;
globals.set(
"get_block_states",
lua.create_async_function(get_block_states)?,
)?;
globals.set("get_block_states", lua.create_function(get_block_states)?)?;
Ok(())
}
@ -21,24 +18,24 @@ pub fn get_block_from_state(lua: &Lua, state: u32) -> Result<Option<Table>> {
let Ok(state) = BlockState::try_from(state) else {
return Ok(None);
};
let block: Box<dyn AzaleaBlock> = state.into();
let behavior = block.behavior();
let b: Box<dyn AzaleaBlock> = state.into();
let bh = b.behavior();
let table = lua.create_table()?;
table.set("id", block.id())?;
table.set("friction", behavior.friction)?;
table.set("jump_factor", behavior.jump_factor)?;
table.set("destroy_time", behavior.destroy_time)?;
table.set("explosion_resistance", behavior.explosion_resistance)?;
table.set(
let block = lua.create_table()?;
block.set("id", b.id())?;
block.set("friction", bh.friction)?;
block.set("jump_factor", bh.jump_factor)?;
block.set("destroy_time", bh.destroy_time)?;
block.set("explosion_resistance", bh.explosion_resistance)?;
block.set(
"requires_correct_tool_for_drops",
behavior.requires_correct_tool_for_drops,
bh.requires_correct_tool_for_drops,
)?;
Ok(Some(table))
Ok(Some(block))
}
pub async fn get_block_states(
lua: Lua,
pub fn get_block_states(
lua: &Lua,
(block_names, filter_fn): (Vec<String>, Option<Function>),
) -> Result<Vec<u16>> {
let mut matched = Vec::new();
@ -52,7 +49,7 @@ pub async fn get_block_states(
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?
filter_fn.call::<bool>(p.clone())?
} else {
true
})

View File

@ -59,7 +59,6 @@ impl UserData for Client {
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("go_to", movement::go_to);
m.add_async_method("look_at", movement::look_at);
m.add_async_method("mine", interaction::mine);
@ -69,6 +68,7 @@ impl UserData for Client {
m.add_method("chat", chat);
m.add_method("disconnect", disconnect);
m.add_method("find_blocks", world::find_blocks);
m.add_method("find_entities", world::find_entities);
m.add_method("get_block_state", world::get_block_state);
m.add_method("get_fluid_state", world::get_fluid_state);
m.add_method("set_held_slot", container::set_held_slot);

View File

@ -117,15 +117,15 @@ pub fn jump(_lua: &Lua, client: &mut Client, _: ()) -> Result<()> {
}
pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
let result = client.component::<HitResultComponent>();
Ok(if result.miss {
let r = client.component::<HitResultComponent>();
Ok(if r.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)
let result = lua.create_table()?;
result.set("position", Vec3::from(r.block_pos))?;
result.set("inside", r.inside)?;
result.set("world_border", r.world_border)?;
Some(result)
})
}
@ -149,29 +149,26 @@ pub async fn look_at(_lua: Lua, client: UserDataRef<Client>, position: Vec3) ->
}
pub fn pathfinder(lua: &Lua, client: &Client) -> Result<Table> {
let table = lua.create_table()?;
table.set(
let pathfinder = lua.create_table()?;
pathfinder.set(
"is_calculating",
client.component::<Pathfinder>().is_calculating,
)?;
table.set(
pathfinder.set(
"is_executing",
if let Some(pathfinder) = client.get_component::<ExecutingPath>() {
table.set(
"last_reached_node",
Vec3::from(pathfinder.last_reached_node),
)?;
table.set(
if let Some(p) = client.get_component::<ExecutingPath>() {
pathfinder.set("last_reached_node", Vec3::from(p.last_reached_node))?;
pathfinder.set(
"last_node_reach_elapsed",
pathfinder.last_node_reached_at.elapsed().as_millis(),
p.last_node_reached_at.elapsed().as_millis(),
)?;
table.set("is_path_partial", pathfinder.is_path_partial)?;
pathfinder.set("is_path_partial", p.is_path_partial)?;
true
} else {
false
},
)?;
Ok(table)
Ok(pathfinder)
}
pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {

View File

@ -16,12 +16,12 @@ pub fn health(_lua: &Lua, client: &Client) -> Result<f32> {
}
pub fn hunger(lua: &Lua, client: &Client) -> Result<Table> {
let hunger = client.hunger();
let h = client.hunger();
let table = lua.create_table()?;
table.set("food", hunger.food)?;
table.set("saturation", hunger.saturation)?;
Ok(table)
let hunger = lua.create_table()?;
hunger.set("food", h.food)?;
hunger.set("saturation", h.saturation)?;
Ok(hunger)
}
pub fn score(_lua: &Lua, client: &Client) -> Result<i32> {

View File

@ -10,15 +10,15 @@ use azalea::{
},
world::{InstanceName, MinecraftEntityId},
};
use mlua::{Function, Lua, Result, Table, UserDataRef};
use mlua::{Function, 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 });
let tr = 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)
let tool_result = lua.create_table()?;
tool_result.set("index", tr.index)?;
tool_result.set("percentage_per_tick", tr.percentage_per_tick)?;
Ok(tool_result)
}
pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> {
@ -48,44 +48,35 @@ pub fn find_blocks(
.collect())
}
pub async fn find_entities(
lua: Lua,
client: UserDataRef<Client>,
filter_fn: Function,
) -> Result<Vec<Table>> {
let mut entities = Vec::new();
{
let mut ecs = client.ecs.lock();
let mut query = ecs.query_filtered::<(
&MinecraftEntityId,
&EntityUuid,
&EntityKind,
&CustomName,
&AzaleaPosition,
&LookDirection,
&Pose,
), Without<Dead>>();
for (id, uuid, kind, custom_name, position, direction, pose) in query.iter(&ecs) {
let entity = lua.create_table()?;
entity.set("id", id.0)?;
entity.set("uuid", uuid.to_string())?;
entity.set("kind", kind.to_string())?;
entity.set("custom_name", custom_name.as_ref().map(ToString::to_string))?;
entity.set("position", Vec3::from(position))?;
entity.set("direction", Direction::from(direction))?;
entity.set("pose", *pose as u8)?;
entities.push(entity);
}
}
pub fn find_entities(lua: &Lua, client: &Client, filter_fn: Function) -> Result<Vec<Table>> {
let mut matched = Vec::new();
for entity in entities {
if filter_fn.call_async::<bool>(&entity).await? {
matched.push(entity)
let mut ecs = client.ecs.lock();
let mut query = ecs.query_filtered::<(
&MinecraftEntityId,
&EntityUuid,
&EntityKind,
&CustomName,
&AzaleaPosition,
&LookDirection,
&Pose,
), Without<Dead>>();
for (&id, uuid, kind, custom_name, position, direction, pose) in query.iter(&ecs) {
let entity = lua.create_table()?;
entity.set("id", id.0)?;
entity.set("uuid", uuid.to_string())?;
entity.set("kind", kind.to_string())?;
entity.set("custom_name", custom_name.as_ref().map(ToString::to_string))?;
entity.set("position", Vec3::from(position))?;
entity.set("direction", Direction::from(direction))?;
entity.set("pose", *pose as u8)?;
if filter_fn.call::<bool>(&entity)? {
matched.push(entity);
}
}
Ok(matched)
}
@ -105,16 +96,16 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
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(
if let Some(fs) = 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)
let fluid_state = lua.create_table()?;
fluid_state.set("kind", fs.kind as u8)?;
fluid_state.set("amount", fs.amount)?;
fluid_state.set("falling", fs.falling)?;
Some(fluid_state)
} else {
None
},

View File

@ -46,11 +46,12 @@ async fn main() -> anyhow::Result<()> {
console_subscriber::init();
let args = arguments::Arguments::parse();
let script_path = args.script.unwrap_or(PathBuf::from(DEFAULT_SCRIPT_PATH));
let event_listeners = Arc::new(RwLock::new(HashMap::new()));
let lua = Lua::new();
let globals = lua.globals();
globals.set("script_path", &*script_path)?;
lua::register_functions(&lua, &globals, event_listeners.clone())?;
lua.load(