refactor: convert to Player struct
This commit is contained in:
parent
bd6698c4b4
commit
2f9e4f50cf
@ -1,8 +1,8 @@
|
|||||||
function get_player(name)
|
function get_player(name)
|
||||||
local target_uuid = nil
|
local target_uuid = nil
|
||||||
for uuid, player in client.tab_list do
|
for _, player in client.tab_list do
|
||||||
if player.name == name then
|
if player.name == name then
|
||||||
target_uuid = uuid
|
target_uuid = player.uuid
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -7,10 +7,11 @@ mod world;
|
|||||||
use super::{
|
use super::{
|
||||||
container::{Container, ContainerRef, item_stack::ItemStack},
|
container::{Container, ContainerRef, item_stack::ItemStack},
|
||||||
direction::Direction,
|
direction::Direction,
|
||||||
|
player::Player,
|
||||||
vec3::Vec3,
|
vec3::Vec3,
|
||||||
};
|
};
|
||||||
use azalea::Client as AzaleaClient;
|
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};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
@ -92,18 +93,10 @@ fn disconnect(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tab_list(lua: &Lua, client: &Client) -> Result<Table> {
|
fn tab_list(_lua: &Lua, client: &Client) -> Result<Vec<Player>> {
|
||||||
let tab_list = lua.create_table()?;
|
let mut tab_list = Vec::new();
|
||||||
for (uuid, player_info) in client.tab_list() {
|
for (_, player_info) in client.tab_list() {
|
||||||
let player = lua.create_table()?;
|
tab_list.push(Player::from(player_info));
|
||||||
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)?;
|
|
||||||
}
|
}
|
||||||
Ok(tab_list)
|
Ok(tab_list)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ pub mod container;
|
|||||||
pub mod direction;
|
pub mod direction;
|
||||||
pub mod events;
|
pub mod events;
|
||||||
pub mod logging;
|
pub mod logging;
|
||||||
|
pub mod player;
|
||||||
pub mod vec3;
|
pub mod vec3;
|
||||||
|
|
||||||
use mlua::{Lua, Table};
|
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))
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user