refactor: convert to Player struct
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
| @@ -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) | ||||
| } | ||||
|   | ||||
| @@ -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}; | ||||
|   | ||||
							
								
								
									
										35
									
								
								src/lua/player.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/lua/player.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -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)) | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user