refactor: clean up a few things

This commit is contained in:
Ryan 2025-03-06 20:14:18 -05:00
parent 4d3947c4ef
commit cce75bd8b9
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
4 changed files with 73 additions and 68 deletions

View File

@ -64,11 +64,15 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
call_listeners(&state, "chat", formatted_message.to_string()).await; call_listeners(&state, "chat", formatted_message.to_string()).await;
} }
Event::Death(Some(packet)) => { Event::Death(packet) => {
if let Some(packet) = packet {
let table = state.lua.create_table()?; let table = state.lua.create_table()?;
table.set("message", packet.message.to_string())?; table.set("message", packet.message.to_string())?;
table.set("player_id", packet.player_id.0)?; table.set("player_id", packet.player_id.0)?;
call_listeners(&state, "death", table).await; call_listeners(&state, "death", table).await;
} else {
call_listeners(&state, "death", ()).await;
}
} }
Event::Disconnect(message) => { Event::Disconnect(message) => {
call_listeners(&state, "disconnect", message.map(|m| m.to_string())).await; call_listeners(&state, "disconnect", message.map(|m| m.to_string())).await;

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,14 +1,10 @@
pub mod click;
pub mod item_stack; pub mod item_stack;
use azalea::{ use azalea::container::{ContainerHandle, ContainerHandleRef};
container::{ContainerHandle, ContainerHandleRef}, use click::operation_from_table;
inventory::operations::{
ClickOperation, CloneClick, PickupAllClick, PickupClick, QuickCraftClick, QuickCraftKind,
QuickCraftStatus, QuickMoveClick, SwapClick, ThrowClick,
},
};
use item_stack::ItemStack; use item_stack::ItemStack;
use mlua::{Result, Table, UserData, UserDataFields, UserDataMethods}; use mlua::{Table, UserData, UserDataFields, UserDataMethods};
pub struct Container { pub struct Container {
pub inner: ContainerHandle, pub inner: ContainerHandle,
@ -36,7 +32,7 @@ impl UserData for Container {
"click", "click",
|_, this, (operation, operation_type): (Table, Option<u8>)| { |_, this, (operation, operation_type): (Table, Option<u8>)| {
this.inner this.inner
.click(click_operation_from_table(operation, operation_type)?); .click(operation_from_table(operation, operation_type)?);
Ok(()) Ok(())
}, },
); );
@ -74,59 +70,9 @@ impl UserData for ContainerRef {
"click", "click",
|_, this, (operation, operation_type): (Table, Option<u8>)| { |_, this, (operation, operation_type): (Table, Option<u8>)| {
this.inner this.inner
.click(click_operation_from_table(operation, operation_type)?); .click(operation_from_table(operation, operation_type)?);
Ok(()) 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

@ -76,14 +76,14 @@ async fn main() -> anyhow::Result<()> {
} else { } else {
DefaultPlugins.set(LogPlugin { DefaultPlugins.set(LogPlugin {
custom_layer: |_| { custom_layer: |_| {
env::var("LOG_FILE").ok().map(|log_file| { env::var("LOG_FILE").ok().map(|path| {
layer() layer()
.with_writer( .with_writer(
OpenOptions::new() OpenOptions::new()
.append(true) .append(true)
.create(true) .create(true)
.open(&log_file) .open(&path)
.expect(&(log_file + " should be accessible")), .expect(&(path + " should be accessible")),
) )
.boxed() .boxed()
}) })