@@ -5,7 +5,7 @@ use crate::{
55
66#[ derive( Clone , Debug ) ]
77pub struct FluidState {
8- pub fluid : azalea_registry :: Fluid ,
8+ pub kind : FluidKind ,
99 /// 0 = empty, 8 = full, 9 = max.
1010 ///
1111 /// 9 is meant to be used when there's another fluid block of the same type
@@ -23,42 +23,44 @@ pub struct FluidState {
2323 /// set (see FlowingFluid.getFlowing)
2424 pub falling : bool ,
2525}
26+ #[ derive( Default , Clone , Debug , PartialEq , Eq ) ]
27+ pub enum FluidKind {
28+ #[ default]
29+ Empty ,
30+ Water ,
31+ Lava ,
32+ }
2633impl FluidState {
34+ pub fn new_source_block ( kind : FluidKind , falling : bool ) -> Self {
35+ Self {
36+ kind,
37+ amount : 8 ,
38+ falling,
39+ }
40+ }
41+
2742 /// A floating point number in between 0 and 1 representing the height (as a
2843 /// percentage of a full block) of the fluid.
2944 pub fn height ( & self ) -> f32 {
3045 self . amount as f32 / 9.
3146 }
47+ pub fn is_empty ( & self ) -> bool {
48+ self . amount == 0
49+ }
3250
3351 pub fn affects_flow ( & self , other : & FluidState ) -> bool {
3452 other. amount == 0 || self . is_same_kind ( other)
3553 }
3654
3755 pub fn is_same_kind ( & self , other : & FluidState ) -> bool {
38- ( other. is_water ( ) && self . is_water ( ) )
39- || ( other. is_lava ( ) && self . is_lava ( ) )
40- || ( self . amount == 0 && other. amount == 0 )
41- }
42-
43- pub fn is_water ( & self ) -> bool {
44- matches ! (
45- self . fluid,
46- azalea_registry:: Fluid :: Water | azalea_registry:: Fluid :: FlowingWater
47- )
48- }
49-
50- pub fn is_lava ( & self ) -> bool {
51- matches ! (
52- self . fluid,
53- azalea_registry:: Fluid :: Lava | azalea_registry:: Fluid :: FlowingLava
54- )
56+ ( other. kind == self . kind ) || ( self . amount == 0 && other. amount == 0 )
5557 }
5658}
5759
5860impl Default for FluidState {
5961 fn default ( ) -> Self {
6062 Self {
61- fluid : azalea_registry :: Fluid :: Empty ,
63+ kind : FluidKind :: Empty ,
6264 amount : 0 ,
6365 falling : false ,
6466 }
@@ -74,38 +76,47 @@ impl From<BlockState> for FluidState {
7476 . property :: < crate :: properties:: Waterlogged > ( )
7577 . unwrap_or_default ( )
7678 {
77- Self {
78- fluid : azalea_registry :: Fluid :: Water ,
79+ return Self {
80+ kind : FluidKind :: Water ,
7981 amount : 8 ,
8082 falling : false ,
81- }
82- } else {
83- let block = Box :: < dyn Block > :: from ( state) ;
84- if let Some ( water) = block. downcast_ref :: < crate :: blocks:: Water > ( ) {
85- Self {
86- fluid : azalea_registry:: Fluid :: Water ,
87- amount : to_or_from_legacy_fluid_level ( water. level as u8 ) ,
88- falling : false ,
89- }
90- } else if let Some ( lava) = block. downcast_ref :: < crate :: blocks:: Lava > ( ) {
91- Self {
92- fluid : azalea_registry:: Fluid :: Lava ,
93- amount : to_or_from_legacy_fluid_level ( lava. level as u8 ) ,
83+ } ;
84+ }
85+
86+ let registry_block = azalea_registry:: Block :: from ( state) ;
87+ match registry_block {
88+ azalea_registry:: Block :: Water => {
89+ let level = state
90+ . property :: < crate :: properties:: WaterLevel > ( )
91+ . expect ( "water block should always have WaterLevel" ) ;
92+ return Self {
93+ kind : FluidKind :: Water ,
94+ amount : to_or_from_legacy_fluid_level ( level as u8 ) ,
9495 falling : false ,
95- }
96- } else {
97- Self {
98- fluid : azalea_registry:: Fluid :: Empty ,
99- amount : 0 ,
96+ } ;
97+ }
98+ azalea_registry:: Block :: Lava => {
99+ let level = state
100+ . property :: < crate :: properties:: LavaLevel > ( )
101+ . expect ( "lava block should always have LavaLevel" ) ;
102+ return Self {
103+ kind : FluidKind :: Lava ,
104+ amount : to_or_from_legacy_fluid_level ( level as u8 ) ,
100105 falling : false ,
101- }
106+ } ;
102107 }
108+ azalea_registry:: Block :: BubbleColumn => {
109+ return Self :: new_source_block ( FluidKind :: Water , false ) ;
110+ }
111+ _ => { }
103112 }
113+
114+ Self :: default ( )
104115 }
105116}
106117
107- /// Sometimes Minecraft represents fluids with 0 being the empty and 8 being
108- /// full, and sometimes it's the opposite. You can use this function to convert
118+ /// Sometimes Minecraft represents fluids with 0 being empty and 8 being full,
119+ /// and sometimes it's the opposite. You can use this function to convert
109120/// in between those two representations.
110121///
111122/// You usually don't need to call this yourself, see [`FluidState`].
@@ -116,22 +127,14 @@ pub fn to_or_from_legacy_fluid_level(level: u8) -> u8 {
116127
117128impl From < FluidState > for BlockState {
118129 fn from ( state : FluidState ) -> Self {
119- match state. fluid {
120- azalea_registry:: Fluid :: Empty => BlockState :: AIR ,
121- azalea_registry:: Fluid :: Water | azalea_registry:: Fluid :: FlowingWater => {
122- BlockState :: from ( crate :: blocks:: Water {
123- level : crate :: properties:: WaterLevel :: from (
124- state. amount as BlockStateIntegerRepr ,
125- ) ,
126- } )
127- }
128- azalea_registry:: Fluid :: Lava | azalea_registry:: Fluid :: FlowingLava => {
129- BlockState :: from ( crate :: blocks:: Lava {
130- level : crate :: properties:: LavaLevel :: from (
131- state. amount as BlockStateIntegerRepr ,
132- ) ,
133- } )
134- }
130+ match state. kind {
131+ FluidKind :: Empty => BlockState :: AIR ,
132+ FluidKind :: Water => BlockState :: from ( crate :: blocks:: Water {
133+ level : crate :: properties:: WaterLevel :: from ( state. amount as BlockStateIntegerRepr ) ,
134+ } ) ,
135+ FluidKind :: Lava => BlockState :: from ( crate :: blocks:: Lava {
136+ level : crate :: properties:: LavaLevel :: from ( state. amount as BlockStateIntegerRepr ) ,
137+ } ) ,
135138 }
136139 }
137140}
0 commit comments