diff --git a/src/events.rs b/src/events.rs index 953da6c..9b46d21 100644 --- a/src/events.rs +++ b/src/events.rs @@ -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; } - Event::Death(Some(packet)) => { - let table = state.lua.create_table()?; - table.set("message", packet.message.to_string())?; - table.set("player_id", packet.player_id.0)?; - call_listeners(&state, "death", table).await; + Event::Death(packet) => { + if let Some(packet) = packet { + let table = state.lua.create_table()?; + table.set("message", packet.message.to_string())?; + table.set("player_id", packet.player_id.0)?; + call_listeners(&state, "death", table).await; + } else { + call_listeners(&state, "death", ()).await; + } } Event::Disconnect(message) => { call_listeners(&state, "disconnect", message.map(|m| m.to_string())).await; diff --git a/src/lua/container/click.rs b/src/lua/container/click.rs new file mode 100644 index 0000000..7205a7a --- /dev/null +++ b/src/lua/container/click.rs @@ -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) -> Result { + 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")?, + }), + }) +} diff --git a/src/lua/container/mod.rs b/src/lua/container/mod.rs index d4cb3f1..d87e03e 100644 --- a/src/lua/container/mod.rs +++ b/src/lua/container/mod.rs @@ -1,14 +1,10 @@ +pub mod click; pub mod item_stack; -use azalea::{ - container::{ContainerHandle, ContainerHandleRef}, - inventory::operations::{ - ClickOperation, CloneClick, PickupAllClick, PickupClick, QuickCraftClick, QuickCraftKind, - QuickCraftStatus, QuickMoveClick, SwapClick, ThrowClick, - }, -}; +use azalea::container::{ContainerHandle, ContainerHandleRef}; +use click::operation_from_table; use item_stack::ItemStack; -use mlua::{Result, Table, UserData, UserDataFields, UserDataMethods}; +use mlua::{Table, UserData, UserDataFields, UserDataMethods}; pub struct Container { pub inner: ContainerHandle, @@ -36,7 +32,7 @@ impl UserData for Container { "click", |_, this, (operation, operation_type): (Table, Option)| { this.inner - .click(click_operation_from_table(operation, operation_type)?); + .click(operation_from_table(operation, operation_type)?); Ok(()) }, ); @@ -74,59 +70,9 @@ impl UserData for ContainerRef { "click", |_, this, (operation, operation_type): (Table, Option)| { this.inner - .click(click_operation_from_table(operation, operation_type)?); + .click(operation_from_table(operation, operation_type)?); Ok(()) }, ); } } - -fn click_operation_from_table(op: Table, op_type: Option) -> Result { - 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")?, - }), - }) -} diff --git a/src/main.rs b/src/main.rs index 7d58bd5..3874a20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,14 +76,14 @@ async fn main() -> anyhow::Result<()> { } else { DefaultPlugins.set(LogPlugin { custom_layer: |_| { - env::var("LOG_FILE").ok().map(|log_file| { + env::var("LOG_FILE").ok().map(|path| { layer() .with_writer( OpenOptions::new() .append(true) .create(true) - .open(&log_file) - .expect(&(log_file + " should be accessible")), + .open(&path) + .expect(&(path + " should be accessible")), ) .boxed() })