Skip to content

Commit

Permalink
Merge branch 'main' of github.com:rimecraft-rs/rimecraft
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Oct 3, 2023
2 parents dd75a61 + d7b5c86 commit 8efdd44
Show file tree
Hide file tree
Showing 62 changed files with 2,801 additions and 1,141 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
8 changes: 4 additions & 4 deletions core/src/block/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ 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()
Expand All @@ -30,7 +30,7 @@ impl Events {
}

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 @@ -42,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
21 changes: 10 additions & 11 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,7 +96,7 @@ 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}");
Expand Down
6 changes: 3 additions & 3 deletions core/src/entity/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ impl Events {
/// The required `entity` can be `None` for some events
/// so that all entities will be affected by this callback.
pub fn register(&mut self, entity: Option<super::Type>, callback: Callback) {
self.0.push((entity.map(|e| e.raw_id()), callback));
self.0.push((entity.map(|e| e.index_of()), callback));
}

pub fn is_summonable(&self, entity_type: super::Type) -> bool {
let id = entity_type.raw_id();
let id = entity_type.index_of();

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

pub fn spawnable_blocks(&self, entity_type: super::Type) -> Vec<crate::block::Block> {
let id = entity_type.raw_id();
let id = entity_type.index_of();

let mut vec = Vec::new();

Expand Down
2 changes: 1 addition & 1 deletion core/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl crate::registry::Registration for Type {
self.id = id
}

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

pub fn is_empty(&self, state: &super::FluidState) -> bool {
let id = state.fluid().raw_id();
let id = state.fluid().index_of();

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

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

self.0
.iter()
Expand Down
8 changes: 3 additions & 5 deletions core/src/fluid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ mod event;

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

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

pub use event::*;
use rimecraft_primitives::Id;

/// Represents a type of fluid.
#[derive(Clone)]
Expand Down Expand Up @@ -49,7 +47,7 @@ impl Registration for Fluid {
.for_each(|state| state.fluid.store(id, std::sync::atomic::Ordering::Relaxed))
}

fn raw_id(&self) -> usize {
fn index_of(&self) -> usize {
self.id
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/item/event.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::util::Event;
use rimecraft_event::Event;

use super::Item;

pub static POST_PROCESS_NBT: Event<dyn Fn(Item, &mut crate::nbt::NbtCompound)> =
pub static POST_PROCESS_NBT: Event<dyn Fn(Item, &mut rimecraft_nbt_ext::Compound)> =
Event::new(|listeners| {
Box::new(move |item, nbt| {
for listener in listeners {
Expand Down
31 changes: 15 additions & 16 deletions core/src/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ mod event;

use std::ops::Deref;

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

pub use event::*;
use rimecraft_nbt_ext::CompoundExt;
use rimecraft_primitives::Id;

/// Represents an item.
#[derive(Clone, Copy)]
pub struct Item {
id: usize,
properties: crate::Ref<'static, ItemDescriptor>,
properties: rimecraft_primitives::Ref<'static, ItemDescriptor>,
}

/// Describes some basic properties of an item.
Expand Down Expand Up @@ -56,7 +55,7 @@ impl Registration for Item {
self.id = id
}

fn raw_id(&self) -> usize {
fn index_of(&self) -> usize {
self.id
}
}
Expand All @@ -73,7 +72,7 @@ impl serde::Serialize for Item {
S: serde::Serializer,
{
crate::registry::ITEM
.get_from_raw(self.raw_id())
.get_from_raw(self.index_of())
.unwrap()
.key()
.value()
Expand Down Expand Up @@ -124,7 +123,7 @@ impl AsItem for crate::registry::Entry<Item> {
impl std::fmt::Display for Item {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
crate::registry::ITEM
.get_from_raw(self.raw_id())
.get_from_raw(self.index_of())
.ok_or(std::fmt::Error)?
.key()
.value()
Expand Down Expand Up @@ -154,7 +153,7 @@ pub struct ItemStack {
/// Count of this stack.
pub count: u8,
item: Item,
nbt: Option<crate::nbt::NbtCompound>,
nbt: Option<rimecraft_nbt_ext::Compound>,
}

impl ItemStack {
Expand Down Expand Up @@ -201,27 +200,27 @@ impl ItemStack {
/// Whether the target item holder matches the provided predicate.
pub fn matches<F: Fn(&crate::registry::Entry<Item>) -> bool>(&self, f: F) -> bool {
f(crate::registry::ITEM
.get_from_raw(self.item.raw_id())
.get_from_raw(self.item.index_of())
.unwrap())
}

#[inline]
pub fn nbt(&self) -> Option<&crate::nbt::NbtCompound> {
pub fn nbt(&self) -> Option<&rimecraft_nbt_ext::Compound> {
self.nbt.as_ref()
}

#[inline]
pub fn nbt_mut(&mut self) -> Option<&mut crate::nbt::NbtCompound> {
pub fn nbt_mut(&mut self) -> Option<&mut rimecraft_nbt_ext::Compound> {
self.nbt.as_mut()
}

#[inline]
pub fn get_or_init_nbt(&mut self) -> &mut crate::nbt::NbtCompound {
pub fn get_or_init_nbt(&mut self) -> &mut rimecraft_nbt_ext::Compound {
self.nbt
.get_or_insert_with(|| crate::nbt::NbtCompound::new())
.get_or_insert_with(|| rimecraft_nbt_ext::Compound::new())
}

pub fn set_nbt(&mut self, nbt: Option<crate::nbt::NbtCompound>) {
pub fn set_nbt(&mut self, nbt: Option<rimecraft_nbt_ext::Compound>) {
self.nbt = nbt;

if self.is_damageable() {
Expand Down Expand Up @@ -350,5 +349,5 @@ struct RawItemStack {
#[serde(rename = "Count")]
count: i8,
#[serde(default)]
tag: Option<crate::nbt::NbtCompound>,
tag: Option<rimecraft_nbt_ext::Compound>,
}
8 changes: 1 addition & 7 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ pub mod component;
pub mod entity;
pub mod fluid;
pub mod item;
/// Thin wrapper between Rimecraft modules
/// and [`fastnbt`] and [`fastsnbt`].
pub mod nbt;
pub mod net;
/// Registry stuffs for managing almost all parts of in-game components.
pub mod registry;
Expand All @@ -19,8 +16,5 @@ pub use util::*;

/// Core types of Rimecraft.
pub mod prelude {
pub use crate::{
nbt::NbtCompoundExt,
util::{math::BlockPos, EnumValues, Id},
};
pub use crate::util::math::BlockPos;
}
Loading

0 comments on commit 8efdd44

Please sign in to comment.