forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
64 lines (53 loc) · 1.42 KB
/
Copy pathlib.rs
File metadata and controls
64 lines (53 loc) · 1.42 KB
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
#![forbid(unsafe_code, warnings)]
use num_traits::{FromPrimitive, ToPrimitive};
#[macro_use]
extern crate num_derive;
mod item;
pub use item::Item;
impl Item {
/// Retrieves the 1.13.2 protocol ID for this item.
pub fn native_protocol_id(self) -> i32 {
// Conveniently, the item enum variants are listed
// in the order of the protocol IDs, so we can
// just use the `ToPrimitive` implementation.
self.to_i32().unwrap()
}
/// Attempts to get an item by its 1.13.2 protocol ID.
pub fn from_native_protocol_id(id: i32) -> Option<Self>
where
Self: Sized,
{
Item::from_i32(id)
}
}
/// Represents an item stack.
///
/// An item stack includes a type, an amount, and a bunch of properties (enchantments, etc.)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ItemStack {
/// The type of this item.
pub ty: Item,
/// The number of items in this stack.
pub amount: u8,
// TODO enchantments, more
}
impl Default for ItemStack {
fn default() -> Self {
ItemStack::new(Item::Stone, 1)
}
}
impl ItemStack {
pub const fn new(ty: Item, amount: u8) -> Self {
Self { ty, amount }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_item() {
let item = Item::Air;
assert_eq!(item.native_protocol_id(), 0);
assert_eq!(Item::from_native_protocol_id(0), Some(item));
}
}