aboutsummaryrefslogtreecommitdiff
path: root/azalea-block/src/lib.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2023-07-14 22:20:40 -0500
committerGitHub <noreply@github.com>2023-07-14 22:20:40 -0500
commit7405427199e5a994d4a6a706f84434a69cb7a7d9 (patch)
treeca537e5d761bc053187d952fced0915c850b92aa /azalea-block/src/lib.rs
parentd1afd02aa84e7b4450c1607277f078eb2a0f1bf3 (diff)
downloadazalea-drasl-7405427199e5a994d4a6a706f84434a69cb7a7d9.tar.xz
Mining (#95)
* more mining stuff * initialize azalea-tags crate * more mining stuff 2 * mining in ecs * well technically mining works but no codegen for how long it takes to mine each block yet * rename downloads to __cache__ it was bothering me since it's not *just* downloads * codegen block behavior * fix not sending packet to finish breaking block * mining animation 🎉 * clippy * cleanup, move Client::mine into a client extension * add azalea/src/mining.rs --------- Co-authored-by: mat <git@matdoes.dev>
Diffstat (limited to 'azalea-block/src/lib.rs')
-rwxr-xr-xazalea-block/src/lib.rs80
1 files changed, 70 insertions, 10 deletions
diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs
index 43099db5..ef0041f1 100755
--- a/azalea-block/src/lib.rs
+++ b/azalea-block/src/lib.rs
@@ -24,6 +24,9 @@ pub trait Block: Debug + Any {
/// Convert the block to a block state. This is lossless, as the block
/// contains all the state data.
fn as_block_state(&self) -> BlockState;
+ /// Convert the block to an [`azalea_registry::Block`]. This is lossy, as
+ /// `azalea_registry::Block` doesn't contain any state data.
+ fn as_registry_block(&self) -> azalea_registry::Block;
}
impl dyn Block {
pub fn downcast_ref<T: Block>(&self) -> Option<&T> {
@@ -45,19 +48,14 @@ pub struct BlockState {
impl BlockState {
pub const AIR: BlockState = BlockState { id: 0 };
- /// Transmutes a u32 to a block state.
- ///
- /// # Safety
- /// The `state_id` should be a valid block state.
- #[inline]
- pub unsafe fn from_u32_unchecked(state_id: u32) -> Self {
- BlockState { id: state_id }
- }
-
#[inline]
pub fn is_valid_state(state_id: u32) -> bool {
state_id <= Self::max_state()
}
+
+ pub fn is_air(&self) -> bool {
+ self == &Self::AIR
+ }
}
impl TryFrom<u32> for BlockState {
@@ -66,7 +64,7 @@ impl TryFrom<u32> for BlockState {
/// Safely converts a state id to a block state.
fn try_from(state_id: u32) -> Result<Self, Self::Error> {
if Self::is_valid_state(state_id) {
- Ok(unsafe { Self::from_u32_unchecked(state_id) })
+ Ok(BlockState { id: state_id })
} else {
Err(())
}
@@ -98,6 +96,68 @@ impl std::fmt::Debug for BlockState {
}
}
+#[derive(Clone, Debug)]
+pub struct FluidState {
+ pub fluid: azalea_registry::Fluid,
+ pub height: u8,
+}
+
+impl Default for FluidState {
+ fn default() -> Self {
+ Self {
+ fluid: azalea_registry::Fluid::Empty,
+ height: 0,
+ }
+ }
+}
+
+impl From<BlockState> for FluidState {
+ fn from(state: BlockState) -> Self {
+ if state.waterlogged() {
+ Self {
+ fluid: azalea_registry::Fluid::Water,
+ height: 15,
+ }
+ } else {
+ let block = Box::<dyn Block>::from(state);
+ if let Some(water) = block.downcast_ref::<crate::blocks::Water>() {
+ Self {
+ fluid: azalea_registry::Fluid::Water,
+ height: water.level as u8,
+ }
+ } else if let Some(lava) = block.downcast_ref::<crate::blocks::Lava>() {
+ Self {
+ fluid: azalea_registry::Fluid::Lava,
+ height: lava.level as u8,
+ }
+ } else {
+ Self {
+ fluid: azalea_registry::Fluid::Empty,
+ height: 0,
+ }
+ }
+ }
+ }
+}
+
+impl From<FluidState> for BlockState {
+ fn from(state: FluidState) -> Self {
+ match state.fluid {
+ azalea_registry::Fluid::Empty => BlockState::AIR,
+ azalea_registry::Fluid::Water | azalea_registry::Fluid::FlowingWater => {
+ BlockState::from(crate::blocks::Water {
+ level: crate::properties::WaterLevel::from(state.height as u32),
+ })
+ }
+ azalea_registry::Fluid::Lava | azalea_registry::Fluid::FlowingLava => {
+ BlockState::from(crate::blocks::Lava {
+ level: crate::properties::LavaLevel::from(state.height as u32),
+ })
+ }
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;