Skip to content

Commit

Permalink
block
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Feb 12, 2024
1 parent d75a404 commit d07e02a
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 10 deletions.
25 changes: 25 additions & 0 deletions core/block/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "rimecraft-block"
version = "0.1.0"
edition = "2021"
authors = ["JieningYu <[email protected]>"]
description = "Minecraft Block primitives"
repository = "https://github.com/rimecraft-rs/rimecraft/"
license = "AGPL-3.0-or-later"
categories = []

[badges]
maintenance = { status = "passively-maintained" }

[dependencies]
rimecraft-registry = { path = "../../util/registry" }
rimecraft-state = { path = "../state", optional = true }
serde = { version = "1.0", optional = true }

[features]
default = ["state"]
serde = ["dep:serde"]
state = ["dep:rimecraft-state"]

[lints]
workspace = true
63 changes: 63 additions & 0 deletions core/block/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! Minecraft Block primitives.

use rimecraft_registry::Reg;

use std::marker::PhantomData;

/// Block containing settings.
#[derive(Debug)]
pub struct RawBlock<P> {
settings: Settings,
_marker: PhantomData<P>,
}

impl<P> RawBlock<P> {
/// Creates a new `Block` with the given settings.
#[inline]
pub const fn new(settings: Settings) -> Self {
Self {
settings,
_marker: PhantomData,
}
}

/// Returns the settings of the block.
#[inline]
pub fn settings(&self) -> &Settings {
&self.settings
}
}

impl<P> From<Settings> for RawBlock<P> {
#[inline]
fn from(settings: Settings) -> Self {
Self::new(settings)
}
}

/// A voxel in a `World`.
pub type Block<'r, K, P> = Reg<'r, K, RawBlock<P>>;

/// Settings of a block.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Settings {
/// Whether this block can be collided with.
pub collidable: bool,
/// The resistance of this block.
pub resistance: f32,
/// The hardness of this block.
pub hardness: f32,
/// Whether this block accepts random ticks.
pub random_ticks: bool,
/// Whether this block is air.
pub is_air: bool,
/// Whether this block is opaque.
pub opaque: bool,
}

#[doc(alias = "BlockProperties")]
pub use Settings as BlockSettings;

/// Represents a block state.
#[cfg(feature = "state")]
pub type BlockState<'s, 'r, K, P> = rimecraft_state::State<'s, Reg<'r, K, RawBlock<P>>>;
4 changes: 1 addition & 3 deletions core/item/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "rimecraft-item"
version = "0.1.0"
edition = "2021"
authors = ["JieningYu <[email protected]>"]
description = "Minecraft Item primitives and registry"
description = "Minecraft Item primitives"
repository = "https://github.com/rimecraft-rs/rimecraft/"
license = "AGPL-3.0-or-later"
categories = []
Expand All @@ -14,7 +14,6 @@ maintenance = { status = "passively-maintained" }
[dependencies]
rimecraft-registry = { path = "../../util/registry" }
rimecraft-fmt = { path = "../../util/fmt" }
rimecraft-freezer = { path = "../../util/freezer" }
rimecraft-attachment = { path = "../../util/attachment" }
rimecraft-serde-update = { path = "../../util/serde-update", optional = true }
rimecraft-serde-humanreadctl = { path = "../../util/serde-humanreadctl", optional = true }
Expand All @@ -23,7 +22,6 @@ rimecraft-nbt-ext = { path = "../../util/nbt-ext" }
serde = { version = "1.0", optional = true, features = ["derive"] }

[features]
default = ["serde"]
serde = [
"dep:serde",
"rimecraft-registry/serde",
Expand Down
9 changes: 7 additions & 2 deletions core/item/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Minecraft Item primitives and registry.
//! Minecraft Item primitives.

use std::{marker::PhantomData, num::NonZeroU32};

Expand Down Expand Up @@ -54,6 +54,8 @@ where
pub type Item<'r, K, P> = Reg<'r, K, RawItem<P>>;

/// A trait for converting a value to an [`Item`].
#[doc(alias = "ItemConvertible")]
#[doc(alias = "ItemLike")]
pub trait ToItem<'s, 'r, K, P> {
/// Converts the value to an [`Item`].
fn to_item(&'s self) -> Item<'r, K, P>;
Expand Down Expand Up @@ -83,7 +85,7 @@ pub const MAX_STACK_COUNT: u32 = 64;
///
/// A setting configure behaviors common to all items, such as the
/// stack's max count.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Settings {
/// The maximum count of the item that can be stacked in a single slot.
pub max_count: NonZeroU32,
Expand All @@ -105,6 +107,9 @@ impl Default for Settings {
}
}

#[doc(alias = "ItemProperties")]
pub use Settings as ItemSettings;

/// Rarity of an item.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
pub enum Rarity {
Expand Down
2 changes: 1 addition & 1 deletion core/item/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rimecraft_attachment::Attachments;
use rimecraft_nbt_ext::Compound;
use rimecraft_registry::ProvideRegistry;

use std::{hash::Hash, marker::PhantomData};
use std::marker::PhantomData;

use crate::{Item, RawItem, ToItem};

Expand Down
2 changes: 2 additions & 0 deletions util/attachment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct Type<K, T> {
_marker: PhantomData<T>,
}

pub use Type as AttachmentType;

impl<K, T> Type<K, T> {
/// Creates a new [`Type`] from given key.
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion util/fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ maintenance = { status = "passively-maintained" }

[dependencies]
serde = { version = "1.0", features = ["derive"], optional = true }
hex_color = "3.0"
rgb = "0.8"
regex-lite = "0.1"

[features]
Expand Down
8 changes: 5 additions & 3 deletions util/fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use std::{fmt::Display, ops::Deref, sync::OnceLock};

pub use hex_color::HexColor;
use regex_lite::Regex;
use rgb::RGB8;

/// Color index of a formatting.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -113,10 +113,12 @@ macro_rules! formattings {
/// Returns the color of the formatted text, or
/// `None` if the formatting has no associated color.
#[inline]
pub const fn color_value(self) -> Option<HexColor> {
pub const fn color_value(self) -> Option<RGB8> {
if let Some(value) = match self { $(Formatting::$i => $cv),* }
{
Some(HexColor::from_u24(value))
let value: u32 = value;
let [_, r, g, b] = value.to_be_bytes();
Some(RGB8 { r, g, b })
} else {
None
}
Expand Down

0 comments on commit d07e02a

Please sign in to comment.