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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
//! Serde-(de)serializable data types for
//! `assets/<namespace>/blockstates/*.json`.
//!
//! Start here: [`BlockStates`]
//!
//! See <https://minecraft.fandom.com/wiki/Model#Block_states>.
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
/// Block states as stored in the `assets/<namespace>/blockstates` directory.
///
/// There are several different variants of some blocks (like [doors], which can
/// be open or closed), hence each block has its own [block state] file, which
/// lists all its existing variants and links them to their corresponding
/// models.
///
/// Blocks can also be compound of several different models at the same
/// time, called "multipart". The models are then used depending on the block
/// states of the block.
///
/// See also the corresponding section of the [wiki page].
///
/// [doors]: https://minecraft.fandom.com/wiki/Door
/// [block state]: https://minecraft.fandom.com/wiki/Block_state
/// [wiki page]: <https://minecraft.fandom.com/wiki/Model#Block_states>
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum BlockStates {
/// One way of representing the different states of a block.
///
/// This uses a map from variant name to block variant. The variant name
/// consists of the relevant block states separated by commas, for example,
/// `"face=wall,facing=east,powered=false"`.
///
/// A block with just one variant uses `""` as the name for its variant.
Variants {
/// Holds all the variants of the block by name.
variants: HashMap<String, Variant>,
},
/// Another way of representing the different states of a block.
///
/// This uses a list of "cases" that specify when a particular model should
/// apply.
Multipart {
/// Holds all the cases and the models that should apply in each case.
#[serde(rename = "multipart")]
cases: Vec<multipart::Case>,
},
}
impl BlockStates {
/// Returns the mapping from block states to [`Variant`]s, or `None` if the
/// block states are specified as [`Multipart`].
///
/// [`Multipart`]: Self::Multipart
pub fn variants(&self) -> Option<&HashMap<String, Variant>> {
match self {
Self::Variants { ref variants } => Some(variants),
Self::Multipart { .. } => None,
}
}
/// Returns the list of [`Case`]s that specify how to display the different
/// [`Variant`]s, or `None` if the block states are specified as
/// [`Variants`].
///
/// [`Case`]: multipart::Case
/// [`Variants`]: Self::Variants
pub fn cases(&self) -> Option<&[multipart::Case]> {
match self {
Self::Variants { .. } => None,
Self::Multipart { cases: multipart } => Some(&multipart[..]),
}
}
/// Consumes `self` and returns a new [`BlockStates::Multipart`] where all
/// of the [`Variants`] have been converted to an equivalent [`Case`]
///
/// [`Variants`]: Self::Variants
/// [`Case`]: multipart::Case
pub fn into_multipart(self) -> Vec<multipart::Case> {
match self {
Self::Multipart { cases } => cases,
Self::Variants { variants } => {
if variants.len() == 1 {
let variant = variants
.into_iter()
.map(|(_, variant)| variant)
.next()
.unwrap();
let case = multipart::Case {
when: None,
apply: variant,
};
vec![case]
} else {
variants
.into_iter()
.map(|(state_values, variant)| {
let state_values: HashMap<String, multipart::StateValue> = state_values
.split(',')
.map(|state_value| {
let split: Vec<&str> = state_value.split('=').collect();
(split[0], split[1])
})
.map(|(state, value)| {
(String::from(state), multipart::StateValue::from(value))
})
.collect();
let condition = multipart::Condition { and: state_values };
let when_clause = multipart::WhenClause::Single(condition);
multipart::Case {
when: Some(when_clause),
apply: variant,
}
})
.collect()
}
}
}
}
}
impl Default for BlockStates {
fn default() -> Self {
Self::Variants {
variants: Default::default(),
}
}
}
/// A block variant.
///
/// Each variant can have **one model** or an **array of models** and contains
/// their properties. If set to an array, the model is chosen randomly from the
/// models contained in the array based on the `Model::weight` field.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[serde(untagged)]
pub enum Variant {
/// A variant with only a single model to choose from.
Single(ModelProperties),
/// A variant with multiple models to choose from.
Multiple(Vec<ModelProperties>),
}
impl Default for Variant {
fn default() -> Self {
Self::Single(Default::default())
}
}
impl Variant {
/// Returns all of the possible [`ModelProperties`] choices for this variant
/// as a slice.
///
/// The slice will contain one element for a [`Single`][Self::Single]
/// variant, and multiple for a [`Multiple`][Self::Multiple] variant.
pub fn models(&self) -> &[ModelProperties] {
match self {
Self::Single(model) => std::slice::from_ref(model),
Self::Multiple(models) => &models[..],
}
}
}
/// Contains the properties of a model that is used to render all or part of a
/// block in a particular state.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct ModelProperties {
/// Specifies the path to the model file of the block, in the form of a
/// [resource location].
///
/// # Version note
///
/// In version 1.13 and beyond, this path includes a prefix such as `block/`
/// or `item/` to disambiguate between different model types. Versions prior
/// to 1.13 do not include this.
///
/// See the [`ModelIdentifier`] documentation for more information.
///
/// [resource location]: <https://minecraft.fandom.com/wiki/Model#File_path>
/// [`ModelIdentifier`]: crate::api::ModelIdentifier
pub model: String,
/// Rotation of the model on the x-axis in increments of 90 degrees.
#[serde(default = "ModelProperties::default_rotation")]
pub x: i32,
/// Rotation of the model on the y-axis in increments of 90 degrees.
#[serde(default = "ModelProperties::default_rotation")]
pub y: i32,
/// Can be `true` or `false` (default). Locks the rotation of the texture of
/// a block, if set to `true`. This way the texture does not rotate with the
/// block when using the `x` and `y` fields above.
///
/// See the example on the [wiki page].
///
/// [wiki page]: <https://minecraft.fandom.com/wiki/Model#Block_states>
#[serde(rename = "uvlock", default = "ModelProperties::default_uv_lock")]
pub uv_lock: bool,
/// Sets the probability of the model for being used in the game.
///
/// The weight defaults to 1 (=100%). If more than one model is used for the
/// same variant, the probability is calculated by dividing the individual
/// model's weight by the sum of the weights of all models. (For example, if
/// three models are used with weights 1, 1, and 2, then their combined
/// weight would be 4 (1+1+2). The probability of each model being used
/// would then be determined by dividing each weight by 4: 1/4, 1/4 and 2/4,
/// or 25%, 25% and 50%, respectively.)
#[serde(default = "ModelProperties::default_weight")]
pub weight: u32,
}
impl ModelProperties {
pub(crate) const fn default_rotation() -> i32 {
0
}
pub(crate) const fn default_uv_lock() -> bool {
false
}
pub(crate) const fn default_weight() -> u32 {
1
}
}
impl Default for ModelProperties {
fn default() -> Self {
Self {
model: Default::default(),
x: Self::default_rotation(),
y: Self::default_rotation(),
uv_lock: Self::default_uv_lock(),
weight: Self::default_weight(),
}
}
}
/// Types used to compose [`BlockStates::Multipart`].
pub mod multipart {
use super::*;
/// Specifies a case and the model that should apply in that case.
#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq)]
pub struct Case {
/// A list of cases that have to be met for the model to be applied.
///
/// If unset, the model always applies.
pub when: Option<WhenClause>,
/// Specifies the model(s) to apply and its properties.
pub apply: Variant,
}
impl Case {
/// Returns `true` if the case applies given the provided state values.
///
/// This can either be when `when` is `None` or if
/// [`WhenClause::applies`] is true.
pub fn applies<'a, I>(&self, state_values: I) -> bool
where
I: IntoIterator<Item = (&'a str, &'a StateValue)> + Clone,
{
if let Some(ref when_clause) = self.when {
when_clause.applies(state_values)
} else {
true
}
}
}
/// A list of conditions that have to be met for a model to be applied.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum WhenClause {
/// A `when` clause that is true when the given condition is true.
Single(Condition),
/// A `when` clause that is true when any of the given conditions is true.
Or {
/// The conditions in the `OR` clause.
#[serde(rename = "OR")]
or: Vec<Condition>,
},
}
impl WhenClause {
/// Returns all of the [`Condition`]s of this when clause as a slice.
///
/// The slice will contain one element for a [`Single`][Self::Single]
/// variant, and multiple for an [`Or`][Self::Or] variant.
pub fn conditions(&self) -> &[Condition] {
match self {
Self::Single(condition) => std::slice::from_ref(condition),
Self::Or { or } => &or[..],
}
}
/// Returns `true` if any of the conditions specified by this `when`
/// clause are satisfied by the provided state values.
///
/// See [`Condition::applies`].
pub fn applies<'a, I>(&self, state_values: I) -> bool
where
I: IntoIterator<Item = (&'a str, &'a StateValue)> + Clone,
{
self.conditions()
.iter()
.any(|condition| condition.applies(state_values.clone()))
}
}
/// A set of conditions that **all** have to match the block to return true.
///
/// The condition is structured as a map from `state` to `value`, so for instance:
///
/// ```json
/// "when": {"north": "side|up", "east": "side|up" }
/// ```
#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq)]
pub struct Condition {
/// Map from state name to state value that forms the list of conditions.
#[serde(flatten)]
pub and: HashMap<String, StateValue>,
}
impl Condition {
/// Returns `true` if
///
/// # Example
///
/// ```
/// # use minecraft_assets::schemas::blockstates::multipart::*;
/// use maplit::hashmap;
///
/// let condition = Condition {
/// and: hashmap! {
/// String::from("var1") => StateValue::from("foo|bar"),
/// String::from("var2") => StateValue::from(false),
/// },
/// };
///
/// let foo_string = StateValue::from("foo");
/// let other_string = StateValue::from("other");
/// let true_string = StateValue::from("true");
/// let false_string = StateValue::from("false");
///
/// let state_values = vec![
/// ("var1", &foo_string),
/// ("var2", &false_string),
/// ("var3", &true_string),
/// ];
/// assert!(condition.applies(state_values.into_iter()));
///
/// let state_values = vec![
/// ("var2", &false_string),
/// ];
/// assert!(!condition.applies(state_values.into_iter()));
///
/// let state_values = vec![
/// ("var1", &other_string),
/// ("var2", &false_string),
/// ];
/// assert!(!condition.applies(state_values.into_iter()));
/// ```
pub fn applies<'a, I>(&self, state_values: I) -> bool
where
I: IntoIterator<Item = (&'a str, &'a StateValue)>,
{
let state_values: HashMap<&'a str, &'a StateValue> = state_values.into_iter().collect();
self.and.iter().all(|(state, required_value)| {
state_values
.get(state.as_str())
.map(|value| *required_value == **value)
.unwrap_or(false)
})
}
}
/// The right-hand side of a [`Condition`] requirement.
///
/// ```txt
/// "when": {"north": "side|up", "east": false }
/// ^^^^^^^^^ ^^^^^
/// ```
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(untagged)]
pub enum StateValue {
/// Unquoted bool value.
Bool(bool),
/// String value (possibly boolean-like, i.e., `"true"` or `"false"`).
String(String),
}
impl StateValue {
/// Returns the value interpreted as a bool, or `None` if this is not
/// possible.
///
/// # Example
///
/// ```
/// # use minecraft_assets::schemas::blockstates::multipart::*;
/// let value = StateValue::from(true);
/// assert_eq!(value.as_bool(), Some(true));
///
/// let value = StateValue::from(false);
/// assert_eq!(value.as_bool(), Some(false));
///
/// let value = StateValue::from("true");
/// assert_eq!(value.as_bool(), Some(true));
///
/// let value = StateValue::from("false");
/// assert_eq!(value.as_bool(), Some(false));
///
/// let value = StateValue::from("not_a_bool");
/// assert_eq!(value.as_bool(), None);
/// ```
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(b) => Some(*b),
Self::String(s) if s == "true" => Some(true),
Self::String(s) if s == "false" => Some(false),
_ => None,
}
}
}
/// # Examples
///
/// Comparing to an unquoted boolean value:
///
/// ```
/// # use minecraft_assets::schemas::blockstates::multipart::*;
/// let left = StateValue::from(true);
///
/// let right = StateValue::from(true);
/// assert!(left == right);
///
/// let right = StateValue::from(false);
/// assert!(left != right);
///
/// let right = StateValue::from("true");
/// assert!(left == right);
///
/// let right = StateValue::from("false");
/// assert!(left != right);
///
/// let right = StateValue::from("not_a_bool");
/// assert!(left != right);
/// ```
///
/// Comparing to a quoted boolean value:
///
/// ```
/// # use minecraft_assets::schemas::blockstates::multipart::*;
/// let left = StateValue::from("true");
///
/// let right = StateValue::from(true);
/// assert!(left == right);
///
/// let right = StateValue::from(false);
/// assert!(left != right);
/// ```
///
/// Comparing to a single string value:
///
/// ```
/// # use minecraft_assets::schemas::blockstates::multipart::*;
/// let left = StateValue::from("foo");
///
/// let right = StateValue::from("foo");
/// assert!(left == right);
///
/// let right = StateValue::from("bar");
/// assert!(left != right);
///
/// let right = StateValue::from(true);
/// assert!(left != right);
/// ```
///
/// Comparing to a multi-string value with `|` bars:
///
/// ```
/// # use minecraft_assets::schemas::blockstates::multipart::*;
/// let left = StateValue::from("foo|bar");
///
/// let right = StateValue::from("foo");
/// assert!(left == right);
///
/// let right = StateValue::from("bar");
/// assert!(left == right);
///
/// let right = StateValue::from("not_foo_or_bar");
/// assert!(left != right);
/// ```
impl PartialEq for StateValue {
fn eq(&self, other: &Self) -> bool {
match self {
Self::String(s) => {
match other {
Self::Bool(other_b) => {
self.as_bool().map(|b| b == *other_b).unwrap_or(false)
}
Self::String(other_s) => {
s == other_s
// Account for "or"s in this value (i.e., `|`).
|| s.split('|').any(|s| s == other_s)
// Account for "or"s in the other value.
|| other_s.split('|').any(|other_s| s == other_s)
}
}
}
Self::Bool(b) => {
if let Some(other_b) = other.as_bool() {
*b == other_b
} else {
false
}
}
}
}
}
impl From<bool> for StateValue {
fn from(source: bool) -> Self {
Self::Bool(source)
}
}
impl<'a> From<&'a str> for StateValue {
fn from(source: &'a str) -> Self {
Self::String(String::from(source))
}
}
impl From<String> for StateValue {
fn from(source: String) -> Self {
Self::String(source)
}
}
}
#[cfg(test)]
mod test {
use super::multipart::*;
use super::*;
use maplit::hashmap;
fn make_single_variant(model_name: &str) -> Variant {
Variant::Single(ModelProperties {
model: String::from(model_name),
..Default::default()
})
}
fn do_test(
blockstates: BlockStates,
state_values: &HashMap<String, StateValue>,
expected_models: &[&'static str],
) {
let cases = blockstates.into_multipart();
let actual_models = cases
.iter()
.filter(|case| {
case.applies(
state_values
.iter()
.map(|(state, value)| (state.as_str(), value)),
)
})
.flat_map(|case| case.apply.models())
.map(|model_properties| model_properties.model.as_str())
.collect::<Vec<_>>();
assert_eq!(&actual_models[..], expected_models);
}
#[test]
fn test_single_variant() {
let blockstates = BlockStates::Variants {
variants: hashmap! {
String::from("") => make_single_variant("model1"),
},
};
let state_values = HashMap::default();
do_test(blockstates, &state_values, &["model1"]);
}
#[test]
fn test_variants() {
let blockstates = BlockStates::Variants {
variants: hashmap! {
String::from("var1=foo,var2=true") => make_single_variant("model1"),
String::from("var1=foo,var2=false") => make_single_variant("model2"),
},
};
let state_values = hashmap! {
String::from("var1") => StateValue::from("foo"),
String::from("var2") => StateValue::from("false"),
};
do_test(blockstates, &state_values, &["model2"]);
}
#[test]
fn test_multipart() {
let blockstates = BlockStates::Multipart {
cases: vec![
Case {
when: None,
apply: make_single_variant("model1"),
},
Case {
when: Some(WhenClause::Single(Condition {
and: hashmap! {
String::from("var1") => StateValue::from("foo|bar"),
String::from("var2") => StateValue::from(true),
},
})),
apply: make_single_variant("model2"),
},
],
};
let state_values = hashmap! {
String::from("var1") => StateValue::from("bar"),
String::from("var2") => StateValue::from("true"),
};
do_test(blockstates, &state_values, &["model1", "model2"]);
}
}