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 all 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 @@ -22,6 +22,7 @@ default = [
"player_list",
"scoreboard",
"world_border",
"schem",
"weather",
"testing",
]
Expand All @@ -34,6 +35,7 @@ network = ["dep:valence_network"]
player_list = ["dep:valence_player_list"]
scoreboard = ["dep:valence_scoreboard"]
world_border = ["dep:valence_world_border"]
schem = ["dep:valence_schem"]
weather = ["dep:valence_weather"]
testing = []

Expand All @@ -56,6 +58,7 @@ valence_registry.workspace = true
valence_scoreboard = { workspace = true, optional = true }
valence_weather = { workspace = true, optional = true }
valence_world_border = { workspace = true, optional = true }
valence_schem = { workspace = true, optional = true }
valence_lang.workspace = true
valence_text.workspace = true
valence_ident.workspace = true
Expand Down Expand Up @@ -169,6 +172,7 @@ uuid = "1.3.1"
valence = { path = ".", version = "0.2.0-alpha.1" }
valence_advancement = { path = "crates/valence_advancement", version = "0.2.0-alpha.1" }
valence_anvil = { path = "crates/valence_anvil", version = "0.2.0-alpha.1" }
valence_schem = { path = "crates/valence_schem", version = "0.2.0-alpha.1" }
valence_boss_bar = { path = "crates/valence_boss_bar", version = "0.2.0-alpha.1" }
valence_build_utils = { path = "crates/valence_build_utils", version = "0.2.0-alpha.1" }
valence_entity = { path = "crates/valence_entity", version = "0.2.0-alpha.1" }
Expand Down
Binary file added assets/example_schem.schem
Binary file not shown.
1 change: 1 addition & 0 deletions crates/valence_generated/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ build = "build/main.rs"
[dependencies]
valence_math.workspace = true
valence_ident.workspace = true
thiserror.workspace = true

[build-dependencies]
anyhow.workspace = true
Expand Down
49 changes: 49 additions & 0 deletions crates/valence_generated/src/block.rs
rj00a marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use std::fmt;
use std::fmt::Display;
use std::iter::FusedIterator;
use std::str::FromStr;

use thiserror::Error;
use valence_ident::{ident, Ident};

use crate::item::ItemKind;
Expand Down Expand Up @@ -48,6 +50,53 @@ fn fmt_block_state(bs: BlockState, f: &mut fmt::Formatter) -> fmt::Result {
}
}

#[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> {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add some tests for this?

Copy link
Member

Choose a reason for hiding this comment

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

We should make this tolerant of the minecraft: namespace in the input.

A bit out of scope, but we should really replace BlockKind::from_str and BlockKind::to_str with BlockKind::from_ident and BlockKind::to_ident. Ident<&'static str> wasn't a thing when those were written.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem that it's not necessarily Idents which we get in here, but also the block state props
(so like fence[west=true] which wouldn't parse as Ident, right?)

Copy link
Contributor Author

@MrlnHi MrlnHi Aug 15, 2023

Choose a reason for hiding this comment

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

We should make this tolerant of the minecraft: namespace in the input.

as for this: I believe that would be a better fit for the BlockKind::from_str method

Copy link
Member

Choose a reason for hiding this comment

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

The whole thing isn't an ident, but presumably the string before the [ is, which you have access to in the kind variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The whole thing isn't an ident, but presumably the string before the [ is, which you have access to in the kind variable.

ahh right I misread part of your first message 😄 I see know

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]
Copy link
Member

@rj00a rj00a Aug 15, 2023

Choose a reason for hiding this comment

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

This will panic if props is empty. Also it doesn't check if the last char is actually a ].

.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
20 changes: 20 additions & 0 deletions crates/valence_schem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[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_server.workspace = true
valence_nbt = {workspace = true, features = ["valence_ident"]}
Comment on lines +14 to +17
Copy link
Member

Choose a reason for hiding this comment

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

glam and valence_nbt can be used via the re-exports in valence_server, so no explicit dependency needed. (glam stuff is in valence_math)`.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

glam yes, but with from valence_nbt I explicitly need the valence_ident feature, which is not enabled in valence_server


[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