skyblock_rs/objects/
items.rs1#[cfg(feature = "nbt")]
2use std::result::Result as StdResult;
3#[cfg(feature = "bytes")]
4use crate::Result;
5
6#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
7pub enum Rarity {
8 #[serde(rename = "COMMON")]
9 Common,
10 #[serde(rename = "UNCOMMON")]
11 Uncommon,
12 #[serde(rename = "RARE")]
13 Rare,
14 #[serde(rename = "EPIC")]
15 Epic,
16 #[serde(rename = "LEGENDARY")]
17 Legendary,
18 #[serde(rename = "ARTIFACT")]
20 Artifact,
21 #[serde(rename = "SPECIAL")]
23 Special,
24}
25
26#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
27pub struct Item {
28 #[serde(rename = "item_name")]
31 pub name: String,
32 #[serde(rename = "item_lore")]
35 pub lore: String,
36 #[serde(rename = "item_count", skip_serializing_if = "Option::is_none")]
38 pub count: Option<i8>,
39 pub extra: String,
42 pub category: String,
44 pub tier: Rarity,
46 #[cfg(feature = "bytes")]
48 #[serde(rename = "item_bytes")]
49 pub bytes: ItemBytes,
50}
51
52#[cfg(feature = "nbt")]
53use nbt::from_gzip_reader;
54#[cfg(feature = "nbt")]
55use std::io;
56#[cfg(feature = "nbt")]
57use crate::objects::nbt::PartialNbt;
58
59impl Item {
60 #[cfg(feature = "nbt")]
62 pub fn to_nbt(&self) -> Result<PartialNbt> {
63 let bytes: StdResult<Vec<u8>, _> = self.bytes.clone().into();
64 let nbt: PartialNbt = from_gzip_reader(io::Cursor::new(bytes?))?;
65 Ok(nbt)
66 }
67
68 pub fn count(&mut self) -> Option<i8> {
72 if let Some(ref count) = &self.count {
73 return Some(*count);
74 }
75
76 #[cfg(feature = "nbt")]
77 if let Ok(nbt) = self.to_nbt() {
78 if let Some(pnbt) = nbt.i.first() {
79 self.count = Some(pnbt.count);
80
81 return Some(pnbt.count);
82 }
83 }
84
85 return None;
86 }
87}
88
89#[cfg(feature = "bytes")]
91#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
92#[serde(untagged)]
93pub enum ItemBytes {
94 T0(ItemBytesT0),
95 Data(String),
96}
97
98#[cfg(feature = "bytes")]
99impl Into<String> for ItemBytes {
100 fn into(self) -> String {
101 match self {
102 Self::T0(ibt0) => {
103 let ItemBytesT0::Data(x) = ibt0;
104 return x;
105 }
106 Self::Data(x) => x
107 }
108 }
109}
110
111#[cfg(feature = "bytes")]
112impl Into<Result<Vec<u8>>> for ItemBytes {
113 fn into(self) -> Result<Vec<u8>> {
114 let b64: String = self.into();
115 Ok(base64::decode(&b64)?)
116 }
117}
118
119#[cfg(feature = "bytes")]
120#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
121#[serde(tag = "type", content = "data")]
122pub enum ItemBytesT0 {
123 #[serde(rename = "0")]
124 Data(String)
125}