skyblock_rs/objects/
items.rs

1#[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	// The new rarity coming out in Dungeons
19	#[serde(rename = "ARTIFACT")]
20	Artifact,
21	// Cakes and Flakes
22	#[serde(rename = "SPECIAL")]
23	Special,
24}
25
26#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
27pub struct Item {
28	/// The name of an item
29	/// Does not include minecraft colour codes.
30	#[serde(rename = "item_name")]
31	pub name: String,
32	/// The "lore" of an item, that is, the description of the items.
33	/// Includes minecraft colour codes.
34	#[serde(rename = "item_lore")]
35	pub lore: String,
36	/// The count of items in the stack
37	#[serde(rename = "item_count", skip_serializing_if = "Option::is_none")]
38	pub count: Option<i8>,
39	/// Field to assist database text searches,
40	/// it includes enchants and the literal minecraft item's name
41	pub extra: String,
42	/// The auction category of an item
43	pub category: String,
44	/// The rarity of the item auctioned
45	pub tier: Rarity,
46	/// The item's gzipped NBT representation
47	#[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	/// Deflates the bytes into a partial NBT tag
61	#[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	/// Returns the count of items in the stack.
69	/// Attempts to count the items in the stack if no cached version is available.
70	/// Returns None otherwise
71	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// Ugly hack because hitting skyblock/auction and skyblock/auctions returns slightly different data.
90#[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}