Skip to content

Commit

Permalink
separate utils to different crates (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu authored Oct 3, 2023
1 parent 193860c commit d7b5c86
Show file tree
Hide file tree
Showing 64 changed files with 2,939 additions and 1,808 deletions.
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
[workspace]
resolver = "2"
members = ["core"]
members = [
"core",
"primitives",
# Utils
"util/caches",
"util/nbt_ext",
"util/event",
"util/edcode",
"util/collections",
"util/freezer",
]
14 changes: 14 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rimecraft-primitives = { path = "../primitives", features = [
"serde",
"edcode",
] }
rimecraft-caches = { path = "../util/caches", features = ["arc"] }
rimecraft-collections = { path = "../util/collections", features = [
"id_list",
"packed_array",
] }
rimecraft-edcode = { path = "../util/edcode", features = ["uuid"] }
rimecraft-event = { path = "../util/event" }
rimecraft-freezer = { path = "../util/freezer" }
rimecraft-nbt-ext = { path = "../util/nbt_ext" }
chrono = { version = "0.4", features = ["serde"] }
tracing = "0.1"
tracing-subscriber = "0.3"
Expand All @@ -29,3 +42,4 @@ dashmap = "5.4"
bimap = "0.6"
thiserror = "1.0"
rsa = "0.9"
enumn = "0.1"
12 changes: 6 additions & 6 deletions core/src/block/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ impl BlockEntity {
self.be_type
}

fn read_nbt(&mut self, nbt: &crate::nbt::NbtCompound) {
fn read_nbt(&mut self, nbt: &rimecraft_nbt_ext::Compound) {
self.data.read(nbt)
}

fn write_nbt(&self, nbt: &mut crate::nbt::NbtCompound) {
fn write_nbt(&self, nbt: &mut rimecraft_nbt_ext::Compound) {
self.data.write(nbt)
}
}

pub trait Data: std::any::Any + Send + Sync + 'static {
fn read(&mut self, nbt: &crate::nbt::NbtCompound);
fn read(&mut self, nbt: &rimecraft_nbt_ext::Compound);

fn write(&self, nbt: &mut crate::nbt::NbtCompound);
fn write(&self, nbt: &mut rimecraft_nbt_ext::Compound);
}

#[derive(Clone, Copy, Eq)]
pub struct Type {
id: usize,
blocks: crate::Ref<'static, Vec<super::Block>>,
blocks: rimecraft_primitives::Ref<'static, Vec<super::Block>>,
}

impl Type {
Expand All @@ -77,7 +77,7 @@ impl crate::registry::Registration for Type {
self.id = id
}

fn raw_id(&self) -> usize {
fn index_of(&self) -> usize {
self.id
}
}
Expand Down
19 changes: 8 additions & 11 deletions core/src/block/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@ impl Events {
/// The required `item` can be `None` for some events
/// so that all items will be affected by this callback.
pub fn register(&mut self, item: Option<super::Block>, callback: Callback) {
self.0.push((item.map(|e| e.raw_id()), callback));
self.0.push((item.map(|e| e.index_of()), callback));
}

pub fn block_item_map(&self, state: &super::BlockState) -> crate::item::ItemStack {
let id = state.block().raw_id();
let id = state.block().index_of();

self.0
.iter()
.find(|e| {
e.0.map_or(false, |ee| ee == id) && matches!(e.1, Callback::BlockStateItemMap(_))
})
.map_or_else(
|| crate::item::ItemStack::default(),
|e| match &e.1 {
Callback::BlockStateItemMap(c) => c(state),
_ => unreachable!(),
},
)
.map_or_else(crate::item::ItemStack::default, |e| match &e.1 {
Callback::BlockStateItemMap(c) => c(state),
_ => unreachable!(),
})
}

pub fn is_air(&self, state: &super::BlockState) -> bool {
let id = state.block().raw_id();
let id = state.block().index_of();

self.0
.iter()
Expand All @@ -45,7 +42,7 @@ impl Events {
}

pub fn has_random_ticks(&self, state: &super::BlockState) -> bool {
let id = state.block().raw_id();
let id = state.block().index_of();

self.0
.iter()
Expand Down
25 changes: 12 additions & 13 deletions core/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ mod event;

use std::{hash::Hash, ops::Deref};

use crate::{
prelude::*,
registry::{Registration, RegistryAccess},
};
use crate::registry::{Registration, RegistryAccess};

pub use event::*;

use once_cell::sync::Lazy;
use rimecraft_collections::IdList;
use rimecraft_freezer::Freezer;

//TODO: Build and freeze STATE_IDS

/// An `ID <-> BlockState` list.
pub static STATE_IDS: Lazy<crate::util::Freezer<crate::collections::IdList<SharedBlockState>>> =
once_cell::sync::Lazy::new(|| crate::util::Freezer::new(crate::collections::IdList::new()));
pub static STATE_IDS: Lazy<Freezer<IdList<SharedBlockState>>> =
once_cell::sync::Lazy::new(|| Freezer::new(IdList::new()));

/// Represents a block.
#[derive(Clone, Copy)]
pub struct Block {
id: usize,
pub states: crate::util::Ref<'static, crate::state::States<BlockState>>,
pub states: rimecraft_primitives::Ref<'static, crate::state::States<BlockState>>,
}

impl Block {
Expand All @@ -47,7 +46,7 @@ impl Block {
pub fn default_state(&self) -> SharedBlockState {
crate::state::Shared {
entries: self.states,
value: crate::Ref(self.states.0.default_state()),
value: rimecraft_primitives::Ref(self.states.0.default_state()),
}
}
}
Expand All @@ -61,7 +60,7 @@ impl Registration for Block {
.for_each(|state| state.block.store(id, std::sync::atomic::Ordering::Relaxed))
}

fn raw_id(&self) -> usize {
fn index_of(&self) -> usize {
self.id
}
}
Expand All @@ -84,7 +83,7 @@ impl serde::Serialize for Block {
S: serde::Serializer,
{
crate::registry::BLOCK
.get_from_raw(self.raw_id())
.get_from_raw(self.index_of())
.unwrap()
.key()
.value()
Expand All @@ -97,20 +96,20 @@ impl<'de> serde::Deserialize<'de> for Block {
where
D: serde::Deserializer<'de>,
{
let id = Id::deserialize(deserializer)?;
let id = rimecraft_primitives::Id::deserialize(deserializer)?;
Ok(crate::registry::BLOCK.get_from_id(&id).map_or_else(
|| {
tracing::debug!("Tried to load invalid block: {id}");
*crate::registry::BLOCK.default_entry().1.deref()
},
|e| e.1.deref().clone(),
|e| *e.1.deref(),
))
}
}

impl Default for Block {
fn default() -> Self {
crate::registry::BLOCK.default_entry().1.deref().clone()
*crate::registry::BLOCK.default_entry().1.deref()
}
}

Expand Down
Loading

0 comments on commit d7b5c86

Please sign in to comment.