Skip to content

Commit fe7fa9d

Browse files
authored
Merge pull request feather-rs#387 from Masplus/dimensions
Added dimensions enum.
2 parents 6380e45 + bde08d7 commit fe7fa9d

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

libcraft/core/src/dimension.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use serde::{Deserialize, Serialize};
2+
use std::convert::TryFrom;
3+
4+
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
5+
#[serde(try_from = "String", into = "&'static str")]
6+
pub enum Dimension {
7+
Overworld,
8+
TheNether,
9+
TheEnd,
10+
}
11+
12+
impl Dimension {
13+
pub fn id(&self) -> i32 {
14+
match self {
15+
Self::Overworld => 0,
16+
Self::TheNether => -1,
17+
Self::TheEnd => 1,
18+
}
19+
}
20+
21+
pub fn from_id(id: i32) -> Option<Self> {
22+
match id {
23+
0 => Some(Self::Overworld),
24+
-1 => Some(Self::TheNether),
25+
1 => Some(Self::TheEnd),
26+
_ => None,
27+
}
28+
}
29+
30+
pub fn namespaced_id(&self) -> &'static str {
31+
match self {
32+
Self::Overworld => "minecraft:overworld",
33+
Self::TheNether => "minecraft:the_nether",
34+
Self::TheEnd => "minecraft:the_end",
35+
}
36+
}
37+
38+
pub fn from_namespaced_id(id: &str) -> Option<Self> {
39+
match id {
40+
"minecraft:overworld" => Some(Self::Overworld),
41+
"minecraft:the_nether" => Some(Self::TheNether),
42+
"minecraft:the_end" => Some(Self::TheEnd),
43+
_ => None,
44+
}
45+
}
46+
}
47+
48+
impl TryFrom<String> for Dimension {
49+
type Error = &'static str;
50+
51+
fn try_from(namespaced_value: String) -> Result<Self, Self::Error> {
52+
if let Some(val) = Self::from_namespaced_id(namespaced_value.as_str()) {
53+
Ok(val)
54+
} else {
55+
Err("Unknown dimension namespaced_id.")
56+
}
57+
}
58+
}
59+
60+
impl From<Dimension> for &'static str {
61+
fn from(value: Dimension) -> Self {
62+
value.namespaced_id()
63+
}
64+
}
65+
66+
impl From<Dimension> for i32 {
67+
fn from(value: Dimension) -> Self {
68+
value.id()
69+
}
70+
}

libcraft/core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mod biome;
44
pub mod block;
55
mod consts;
6+
mod dimension;
67
mod entity;
78
mod gamemode;
89
mod gamerules;
@@ -12,6 +13,7 @@ mod positions;
1213

1314
pub use biome::Biome;
1415
pub use consts::*;
16+
pub use dimension::Dimension;
1517
pub use entity::EntityKind;
1618
pub use gamemode::Gamemode;
1719
pub use gamerules::GameRules;

0 commit comments

Comments
 (0)