Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for schematics file format #263

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ default = [
"network",
"player_list",
"world_border",
"schem",
"weather",
]
advancement = ["dep:valence_advancement"]
Expand All @@ -30,6 +31,7 @@ log = ["dep:bevy_log"]
network = ["dep:valence_network"]
player_list = ["dep:valence_player_list"]
world_border = ["dep:valence_world_border"]
schem = ["dep:valence_schem"]
weather = ["dep:valence_weather"]

[dependencies]
Expand Down Expand Up @@ -57,6 +59,7 @@ valence_network = { workspace = true, optional = true }
valence_player_list = { workspace = true, optional = true }
valence_registry.workspace = true
valence_world_border = { workspace = true, optional = true }
valence_schem = { workspace = true, optional = true }
valence_packet.workspace = true
valence_weather = { workspace = true, optional = true }

Expand Down Expand Up @@ -166,6 +169,7 @@ url = { version = "2.2.2", features = ["serde"] }
uuid = "1.3.1"
valence_advancement.path = "crates/valence_advancement"
valence_anvil.path = "crates/valence_anvil"
valence_schem.path = "crates/valence_schem"
valence_biome.path = "crates/valence_biome"
valence_block.path = "crates/valence_block"
valence_build_utils.path = "crates/valence_build_utils"
Expand Down
Binary file added assets/example_schem.schem
Binary file not shown.
1 change: 1 addition & 0 deletions crates/valence_block/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition.workspace = true

[dependencies]
valence_core.workspace = true
thiserror.workspace = true
anyhow.workspace = true
glam.workspace = true

Expand Down
49 changes: 49 additions & 0 deletions crates/valence_block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ use std::fmt;
use std::fmt::Display;
use std::io::Write;
use std::iter::FusedIterator;
use std::str::FromStr;

use anyhow::Context;
use thiserror::Error;
use valence_core::ident;
use valence_core::ident::Ident;
use valence_core::item::ItemKind;
Expand Down Expand Up @@ -100,6 +102,53 @@ impl Decode<'_> for BlockKind {
}
}

#[derive(Debug, Error, PartialEq, Eq)]
pub enum ParseBlockStateError {
#[error("unknown block kind '{0}'")]
UnknownBlockKind(String),
#[error("invalid prop string '{0}'")]
InvalidPropString(String),
#[error("unknown prop name '{0}'")]
UnknownPropName(String),
#[error("unknown prop value '{0}'")]
UnknownPropValue(String),
}

impl FromStr for BlockState {
type Err = ParseBlockStateError;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let state = match s.split_once('[') {
Some((kind, props)) => {
let Some(kind) = BlockKind::from_str(kind) else {
return Err(ParseBlockStateError::UnknownBlockKind(kind.to_string()));
};
props[..props.len() - 1]
.split(',')
.map(|prop| prop.trim())
.try_fold(kind.to_state(), |state, prop| {
let Some((name, val)) = prop.split_once('=') else {
return Err(ParseBlockStateError::InvalidPropString(prop.to_string()));
};
let Some(name) = PropName::from_str(name) else {
return Err(ParseBlockStateError::UnknownPropName(name.to_string()));
};
let Some(val) = PropValue::from_str(val) else {
return Err(ParseBlockStateError::UnknownPropValue(val.to_string()));
};
Ok(state.set(name, val))
})?
}
None => match BlockKind::from_str(s) {
Some(kind) => kind.to_state(),
None => return Err(ParseBlockStateError::UnknownBlockKind(s.to_string())),
},
};

Ok(state)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
23 changes: 23 additions & 0 deletions crates/valence_schem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "valence_schem"
description = "A library for the Sponge Schematic Format."
documentation.workspace = true
repository = "https://github.com/valence-rs/valence/tree/main/crates/valence_schem"
readme = "README.md"
license.workspace = true
keywords = ["schematics", "minecraft", "deserialization"]
version.workspace = true
edition.workspace = true

[dependencies]
flate2.workspace = true
glam.workspace = true
thiserror.workspace = true
valence_biome.workspace = true
valence_block.workspace = true
valence_core.workspace = true
valence_instance.workspace = true
valence_nbt.workspace = true

[dev-dependencies]
valence.workspace = true
43 changes: 43 additions & 0 deletions crates/valence_schem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# valence_schem

Support for the [Sponge schematic file format](https://github.com/SpongePowered/Schematic-Specification).

This crate implements [Sponge schematics]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is redundant.


Loading schematics (version 1 through 3) from [`Compounds`](Compound) is
supported. Saving schematics to [`Compounds`](Compound) (version 3 only) is
supported.

# Examples

An example that shows how to load and save [schematics] from and to the
filesystem

```rust
# use valence_schem::Schematic;
use flate2::Compression;
Comment on lines +17 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# use valence_schem::Schematic;
use flate2::Compression;
use valence_schem::Schematic;

fn schem_from_file(path: &str) -> Schematic {
Schematic::load(path).unwrap()
}
fn schem_to_file(schematic: &Schematic, path: &str) {
schematic.save(path);
}
```

There are also methods to serialize and deserialize [schematics] from and to
[`Compounds`](Compound):
```rust
# use valence_schem::Schematic;
use valence_nbt::Compound;
Comment on lines +30 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# use valence_schem::Schematic;
use valence_nbt::Compound;
use valence_schem::Schematic;
use valence_nbt::Compound;

fn schem_from_compound(compound: &Compound) {
let schematic = Schematic::deserialize(compound).unwrap();
let comp = schematic.serialize();
}
```

### See also

Examples in the `examples/` directory

[Sponge schematics]: <https://github.com/SpongePowered/Schematic-Specification>
[schematics]: Schematic
Loading
Loading