use yog_core::Server;
use yog_entity::Entity;
pub struct Player<'a> {
server: &'a dyn Server,
name: String,
uuid: Option<String>,
}
impl<'a> Player<'a> {
pub fn new(server: &'a dyn Server, name: impl Into<String>) -> Self {
Self { server, name: name.into(), uuid: None }
}
pub fn with_uuid(
server: &'a dyn Server,
name: impl Into<String>,
uuid: impl Into<String>,
) -> Self {
Self { server, name: name.into(), uuid: Some(uuid.into()) }
}
pub fn name(&self) -> &str {
&self.name
}
pub fn uuid(&self) -> Option<&str> {
self.uuid.as_deref()
}
pub fn entity(&self) -> Option<Entity<'_>> {
self.uuid.as_deref().map(|u| Entity::new(self.server, u))
}
pub fn give(&self, item_id: &str, count: u32) -> bool {
self.server.give_item(&self.name, item_id, count)
}
pub fn send_packet(&self, channel: &str, payload: &[u8]) -> bool {
self.server.send_to_player(&self.name, channel, payload)
}
pub fn teleport(&self, x: f64, y: f64, z: f64) -> bool {
match &self.uuid {
Some(u) => self.server.entity_teleport(u, x, y, z),
None => self.server.teleport(&self.name, x, y, z),
}
}
pub fn position(&self) -> Option<(f64, f64, f64)> {
self.entity()?.position()
}
pub fn health(&self) -> Option<f32> {
self.entity()?.health()
}
pub fn set_health(&self, health: f32) -> bool {
self.entity().map_or(false, |e: Entity<'_>| e.set_health(health))
}
pub fn kill(&self) -> bool {
self.entity().map_or(false, |e: Entity<'_>| e.kill())
}
pub fn send_title(
&self,
title: &str,
subtitle: &str,
fadein: i32,
stay: i32,
fadeout: i32,
) -> bool {
self.server.send_title(&self.name, title, subtitle, fadein, stay, fadeout)
}
pub fn send_actionbar(&self, message: &str) -> bool {
self.server.send_actionbar(&self.name, message)
}
pub fn kick(&self, reason: &str) -> bool {
self.server.kick_player(&self.name, reason)
}
pub fn set_gamemode(&self, gamemode: &str) -> bool {
self.server.set_gamemode(&self.name, gamemode)
}
pub fn play_sound(&self, sound_id: &str, volume: f32, pitch: f32) -> bool {
self.server.play_sound_to_player(&self.name, sound_id, volume, pitch)
}
pub fn add_effect(
&self,
effect_id: &str,
duration_ticks: i32,
amplifier: u8,
show_particles: bool,
) -> bool {
self.entity().map_or(false, |e: Entity<'_>| {
e.add_effect(effect_id, duration_ticks, amplifier, show_particles)
})
}
pub fn remove_effect(&self, effect_id: &str) -> bool {
self.entity().map_or(false, |e: Entity<'_>| e.remove_effect(effect_id))
}
pub fn clear_effects(&self) -> bool {
self.entity().map_or(false, |e: Entity<'_>| e.clear_effects())
}
pub fn inventory(&self) -> Vec<(u32, String, u32)> {
self.server.player_inventory(&self.name)
}
pub fn set_slot(&self, slot: u32, item_id: &str, count: u32) -> bool {
self.server.player_set_slot(&self.name, slot, item_id, count)
}
pub fn teleport_to_dim(&self, dimension: &str, x: f64, y: f64, z: f64) -> bool {
self.server.teleport_to_dim(&self.name, dimension, x, y, z)
}
}