From 2f9e4f50cf519a19f2a7bdf9f474a327a3b6bc88 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet <errornointernet@envs.net> Date: Fri, 21 Feb 2025 21:36:24 -0500 Subject: [PATCH] refactor: convert to Player struct --- lib/utils.lua | 4 ++-- src/lua/client/mod.rs | 19 ++++++------------- src/lua/mod.rs | 1 + src/lua/player.rs | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 src/lua/player.rs diff --git a/lib/utils.lua b/lib/utils.lua index 03a48bc..c0afebd 100644 --- a/lib/utils.lua +++ b/lib/utils.lua @@ -1,8 +1,8 @@ function get_player(name) local target_uuid = nil - for uuid, player in client.tab_list do + for _, player in client.tab_list do if player.name == name then - target_uuid = uuid + target_uuid = player.uuid break end end diff --git a/src/lua/client/mod.rs b/src/lua/client/mod.rs index 717ad4f..364d70b 100644 --- a/src/lua/client/mod.rs +++ b/src/lua/client/mod.rs @@ -7,10 +7,11 @@ mod world; use super::{ container::{Container, ContainerRef, item_stack::ItemStack}, direction::Direction, + player::Player, vec3::Vec3, }; use azalea::Client as AzaleaClient; -use mlua::{Lua, Result, Table, UserData, UserDataFields, UserDataMethods}; +use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods}; use std::ops::{Deref, DerefMut}; pub struct Client { @@ -92,18 +93,10 @@ fn disconnect(_lua: &Lua, client: &Client, _: ()) -> Result<()> { Ok(()) } -fn tab_list(lua: &Lua, client: &Client) -> Result<Table> { - let tab_list = lua.create_table()?; - for (uuid, player_info) in client.tab_list() { - let player = lua.create_table()?; - player.set("gamemode", player_info.gamemode.name())?; - player.set("latency", player_info.latency)?; - player.set("name", player_info.profile.name)?; - player.set( - "display_name", - player_info.display_name.map(|n| n.to_string()), - )?; - tab_list.set(uuid.to_string(), player)?; +fn tab_list(_lua: &Lua, client: &Client) -> Result<Vec<Player>> { + let mut tab_list = Vec::new(); + for (_, player_info) in client.tab_list() { + tab_list.push(Player::from(player_info)); } Ok(tab_list) } diff --git a/src/lua/mod.rs b/src/lua/mod.rs index c83b62d..b526ee7 100644 --- a/src/lua/mod.rs +++ b/src/lua/mod.rs @@ -4,6 +4,7 @@ pub mod container; pub mod direction; pub mod events; pub mod logging; +pub mod player; pub mod vec3; use mlua::{Lua, Table}; diff --git a/src/lua/player.rs b/src/lua/player.rs new file mode 100644 index 0000000..8981e00 --- /dev/null +++ b/src/lua/player.rs @@ -0,0 +1,35 @@ +use azalea::PlayerInfo; +use mlua::{IntoLua, Lua, Result, Value}; + +#[derive(Clone)] +pub struct Player { + pub display_name: Option<String>, + pub gamemode: String, + pub latency: i32, + pub name: String, + pub uuid: String, +} + +impl From<PlayerInfo> for Player { + fn from(p: PlayerInfo) -> Self { + Self { + display_name: p.display_name.map(|n| n.to_string()), + gamemode: p.gamemode.name().to_owned(), + latency: p.latency, + name: p.profile.name, + uuid: p.uuid.to_string(), + } + } +} + +impl IntoLua for Player { + fn into_lua(self, lua: &Lua) -> Result<Value> { + let table = lua.create_table()?; + table.set("display_name", self.display_name)?; + table.set("gamemode", self.gamemode)?; + table.set("latency", self.latency)?; + table.set("name", self.name)?; + table.set("uuid", self.uuid)?; + Ok(Value::Table(table)) + } +}