use std::collections::HashMap;
use nbt_lib::{convert_list_to, traits::FromNbtValue, unwrap_to_empty, unwrap_to_empty_if_exists, NbtValue};
#[derive(Default, Clone, Debug, PartialEq)]
pub struct Item {
pub count: u8,
pub slot: u8,
pub id: String,
pub tag: Option<NbtValue>,
}
impl FromNbtValue for Item {
fn from_nbt_value(value: NbtValue) -> Result<Self, ()> where Self: Sized {
let (_, data) = unwrap_to_empty!(Some(value), compound);
Ok(Self {
count: unwrap_to_empty!(data, "Count", i8) as u8,
slot: unwrap_to_empty!(data, "Slot", i8) as u8,
id: unwrap_to_empty!(data, "id", string),
tag: if data.contains_key("tag") { data.get("tag").map(|v|v.clone()) } else { None },
})
}
}
#[derive(Default, Clone, Debug, PartialEq)]
pub enum VanillaLootTable {
#[default] Stronghold,
}
#[derive(Clone, Debug, PartialEq)]
pub enum LootTable {
Vanilla(VanillaLootTable),
Custom,
}
impl Default for LootTable {
fn default() -> Self {
Self::Vanilla(VanillaLootTable::default())
}
}
#[derive(Default, Clone, Debug, PartialEq)]
pub struct ChestData {
pub custom_name: Option<String>,
pub items: Vec<Item>,
pub lock: Option<String>,
pub loottable: Option<String>,
pub loottable_seed: Option<i64>,
}
impl TryFrom<&HashMap<String, NbtValue>> for ChestData {
type Error = ();
fn try_from(value: &HashMap<String, NbtValue>) -> Result<Self, Self::Error> {
Ok(Self {
custom_name: unwrap_to_empty_if_exists!(value, "CustomName", string),
lock: unwrap_to_empty_if_exists!(value, "Lock", string),
loottable: unwrap_to_empty_if_exists!(value, "LootTable", string),
loottable_seed: unwrap_to_empty_if_exists!(value, "LootTableSeed", i64),
items: convert_list_to!(value, "Items", Item)
})
}
}