1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use crate::map_decoder;
use minicbor::decode::{Decode, Decoder, Error};

/// Information about an item stack.
///
/// The `'buffer` lifetime is the lifetime of the buffer holding strings to which the object
/// refers.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ItemStack<'buffer> {
	/// The internal (Minecraft system) name of the item.
	///
	/// For example, this might be `minecraft:cobblestone`.
	pub name: &'buffer str,

	/// The human-readable name of the item.
	///
	/// For example, this might be `Cobblestone`.
	pub label: &'buffer str,

	/// The number of items in the stack.
	pub size: u32,

	/// The maximum number of items that can be held in the stack.
	pub max_size: u32,

	/// The damage value of the item, if it is a tool, or zero if not.
	pub damage: u32,

	/// The damage value at which the item breaks, if it is a tool, or zero if not.
	pub max_damage: u32,

	/// Whether the item has extra NBT data attached.
	pub has_tag: bool,
}

impl<'buffer> Decode<'buffer> for ItemStack<'buffer> {
	fn decode(d: &mut Decoder<'buffer>) -> Result<Self, Error> {
		match OptionItemStack::decode(d)?.into() {
			Some(s) => Ok(s),
			None => Err(Error::message("missing item stack")),
		}
	}
}

/// Information about an item stack which may or may not exist.
///
/// This type exists, rather than just using `Option<ItemStack>` directly, because `Option` has a
/// blanket `Decode` implementation, and we need a different implementation which also maps a
/// non-null empty map to `None`.
///
/// The `'buffer` lifetime is the lifetime of the buffer holding strings to which the object
/// refers.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct OptionItemStack<'buffer>(pub Option<ItemStack<'buffer>>);

impl<'buffer> Decode<'buffer> for OptionItemStack<'buffer> {
	fn decode(d: &mut Decoder<'buffer>) -> Result<Self, Error> {
		map_decoder::decode_nullable::<OptionItemStackBuilder<'buffer>>(d)
	}
}

impl<'buffer> From<OptionItemStack<'buffer>> for Option<ItemStack<'buffer>> {
	fn from(x: OptionItemStack<'buffer>) -> Option<ItemStack<'buffer>> {
		x.0
	}
}

/// A map-decoding builder for an [`OptionItemStack`](OptionItemStack).
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct OptionItemStackBuilder<'buffer> {
	/// The internal (Minecraft system) name of the item.
	///
	/// For example, this might be `minecraft:cobblestone`.
	name: Option<&'buffer str>,

	/// The human-readable name of the item.
	///
	/// For example, this might be `Cobblestone`.
	label: Option<&'buffer str>,

	/// The number of items in the stack.
	size: Option<u32>,

	/// The maximum number of items that can be held in the stack.
	max_size: Option<u32>,

	/// The damage value of the item, if it is a tool, or zero if not.
	damage: Option<u32>,

	/// The damage value at which the item breaks, if it is a tool, or zero if not.
	max_damage: Option<u32>,

	/// Whether the item has extra NBT data attached.
	has_tag: Option<bool>,
}

impl<'buffer> map_decoder::Builder<'buffer> for OptionItemStackBuilder<'buffer> {
	type Output = OptionItemStack<'buffer>;

	fn entry(&mut self, key: &str, d: &mut Decoder<'buffer>) -> Result<bool, Error> {
		match key {
			"name" => {
				self.name = Some(d.str()?);
				Ok(true)
			}
			"label" => {
				self.label = Some(d.str()?);
				Ok(true)
			}
			"size" => {
				self.size = Some(d.u32()?);
				Ok(true)
			}
			"maxSize" => {
				self.max_size = Some(d.u32()?);
				Ok(true)
			}
			"damage" => {
				self.damage = Some(d.u32()?);
				Ok(true)
			}
			"maxDamage" => {
				self.max_damage = Some(d.u32()?);
				Ok(true)
			}
			"hasTag" => {
				self.has_tag = Some(d.bool()?);
				Ok(true)
			}
			_ => Ok(false),
		}
	}

	fn build(self) -> Result<Self::Output, Error> {
		// If all the required keys are present, return an item stack.
		if let Some(name) = self.name {
			if let Some(label) = self.label {
				if let Some(size) = self.size {
					if let Some(max_size) = self.max_size {
						if let Some(damage) = self.damage {
							if let Some(max_damage) = self.max_damage {
								if let Some(has_tag) = self.has_tag {
									// Some APIs map an empty slot to “zero air” instead of an
									// empty table.
									return Ok(OptionItemStack(
										if name == "minecraft:air" && size == 0 {
											None
										} else {
											Some(ItemStack {
												name,
												label,
												size,
												max_size,
												damage,
												max_damage,
												has_tag,
											})
										},
									));
								}
							}
						}
					}
				}
			}
		}

		// If all the required keys are absent, return None.
		if (
			self.name,
			self.label,
			self.size,
			self.max_size,
			self.damage,
			self.max_damage,
			self.has_tag,
		) == (None, None, None, None, None, None, None)
		{
			return Ok(OptionItemStack(None));
		}

		// If some but not all of the required keys are present, fail.
		Err(Error::message("missing key in item stack"))
	}
}

impl<'buffer> map_decoder::NullableBuilder<'buffer> for OptionItemStackBuilder<'buffer> {
	fn build_null() -> OptionItemStack<'buffer> {
		OptionItemStack(None)
	}
}