mcapi 0.3.1

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, BMCLAPI, and Java Runtimes)
Documentation
use serde::Serialize;

use super::models::{Player, TextureSlot};
use super::{Authed, NameBody};
use crate::error::Result;

#[derive(Serialize)]
struct SetTexturesBody {
    #[serde(skip_serializing_if = "Option::is_none")]
    skin: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    cape: Option<u64>,
}

#[derive(Serialize)]
struct ClearTexturesBody<'a> {
    #[serde(rename = "type")]
    types: &'a [TextureSlot],
}

impl<'a> Authed<'a> {
    /// Lists all game profiles belonging to the user via `GET /api/players`.
    pub async fn players(&self) -> Result<Vec<Player>> {
        self.get_envelope("/players").await
    }

    /// Creates a new player character via `POST /api/players`.
    pub async fn create_player(&self, name: &str) -> Result<Player> {
        self.post_envelope("/players", &NameBody { name }).await
    }

    /// Renames a player character via `PUT /api/players/{pid}/name`.
    pub async fn rename_player(&self, pid: u64, name: &str) -> Result<()> {
        self.put_unit(&format!("/players/{pid}/name"), &NameBody { name })
            .await
    }

    /// Assigns skin and/or cape texture IDs to a player via `PUT /api/players/{pid}/textures`.
    pub async fn set_player_textures(
        &self,
        pid: u64,
        skin_tid: Option<u64>,
        cape_tid: Option<u64>,
    ) -> Result<()> {
        self.put_unit(
            &format!("/players/{pid}/textures"),
            &SetTexturesBody {
                skin: skin_tid,
                cape: cape_tid,
            },
        )
        .await
    }

    /// Clears selected texture slots ([`TextureSlot::Skin`] and/or [`TextureSlot::Cape`]) via
    /// `DELETE /api/players/{pid}/textures`.
    pub async fn clear_player_textures(&self, pid: u64, slots: &[TextureSlot]) -> Result<()> {
        let body = ClearTexturesBody { types: slots };
        self.send_unit(&format!("/players/{pid}/textures"), |c, t| {
            c.delete(t).json(&body)
        })
        .await
    }

    /// Deletes a player character via `DELETE /api/players/{pid}`.
    pub async fn delete_player(&self, pid: u64) -> Result<()> {
        self.delete_unit(&format!("/players/{pid}")).await
    }
}