@@ -8,7 +8,8 @@ mod range;
88use core:: fmt:: Debug ;
99use std:: {
1010 any:: Any ,
11- io:: { Cursor , Write } ,
11+ fmt,
12+ io:: { self , Cursor , Write } ,
1213} ;
1314
1415use azalea_buf:: { AzaleaRead , AzaleaReadVar , AzaleaWrite , AzaleaWriteVar , BufReadError } ;
@@ -115,13 +116,13 @@ impl AzaleaRead for BlockState {
115116 }
116117}
117118impl AzaleaWrite for BlockState {
118- fn azalea_write ( & self , buf : & mut impl Write ) -> Result < ( ) , std :: io:: Error > {
119+ fn azalea_write ( & self , buf : & mut impl Write ) -> Result < ( ) , io:: Error > {
119120 u32:: azalea_write_var ( & ( self . id as u32 ) , buf)
120121 }
121122}
122123
123- impl std :: fmt :: Debug for BlockState {
124- fn fmt ( & self , f : & mut std :: fmt:: Formatter < ' _ > ) -> std :: fmt:: Result {
124+ impl Debug for BlockState {
125+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
125126 write ! (
126127 f,
127128 "BlockState(id: {}, {:?})" ,
@@ -134,65 +135,84 @@ impl std::fmt::Debug for BlockState {
134135#[ derive( Clone , Debug ) ]
135136pub struct FluidState {
136137 pub fluid : azalea_registry:: Fluid ,
137- pub height : u8 ,
138+ /// 0 = empty, 8 = full, 9 = max.
139+ ///
140+ /// 9 is meant to be used when there's another fluid block of the same type
141+ /// above it, but it's usually unused by this struct.
142+ pub amount : u8 ,
143+ }
144+ impl FluidState {
145+ /// A floating point number in between 0 and 1 representing the height (as a
146+ /// percentage of a full block) of the fluid.
147+ pub fn height ( & self ) -> f32 {
148+ self . amount as f32 / 9.
149+ }
138150}
139151
140152impl Default for FluidState {
141153 fn default ( ) -> Self {
142154 Self {
143155 fluid : azalea_registry:: Fluid :: Empty ,
144- height : 0 ,
156+ amount : 0 ,
145157 }
146158 }
147159}
148160
149161impl From < BlockState > for FluidState {
150162 fn from ( state : BlockState ) -> Self {
163+ // note that 8 here might be treated as 9 in some cases if there's another fluid
164+ // block of the same type above it
165+
151166 if state
152167 . property :: < crate :: properties:: Waterlogged > ( )
153168 . unwrap_or_default ( )
154169 {
155170 Self {
156171 fluid : azalea_registry:: Fluid :: Water ,
157- height : 15 ,
172+ amount : 8 ,
158173 }
159174 } else {
160175 let block = Box :: < dyn Block > :: from ( state) ;
161176 if let Some ( water) = block. downcast_ref :: < crate :: blocks:: Water > ( ) {
162177 Self {
163178 fluid : azalea_registry:: Fluid :: Water ,
164- height : water. level as u8 ,
179+ amount : to_or_from_legacy_fluid_level ( water. level as u8 ) ,
165180 }
166181 } else if let Some ( lava) = block. downcast_ref :: < crate :: blocks:: Lava > ( ) {
167182 Self {
168183 fluid : azalea_registry:: Fluid :: Lava ,
169- height : lava. level as u8 ,
184+ amount : to_or_from_legacy_fluid_level ( lava. level as u8 ) ,
170185 }
171186 } else {
172187 Self {
173188 fluid : azalea_registry:: Fluid :: Empty ,
174- height : 0 ,
189+ amount : 0 ,
175190 }
176191 }
177192 }
178193 }
179194}
180195
196+ // see FlowingFluid.getLegacyLevel
197+ fn to_or_from_legacy_fluid_level ( level : u8 ) -> u8 {
198+ 8_u8 . saturating_sub ( level)
199+ }
200+
181201impl From < FluidState > for BlockState {
182202 fn from ( state : FluidState ) -> Self {
183203 match state. fluid {
184204 azalea_registry:: Fluid :: Empty => BlockState :: AIR ,
185205 azalea_registry:: Fluid :: Water | azalea_registry:: Fluid :: FlowingWater => {
186206 BlockState :: from ( crate :: blocks:: Water {
187207 level : crate :: properties:: WaterLevel :: from (
188- state. height as BlockStateIntegerRepr ,
208+ state. amount as BlockStateIntegerRepr ,
189209 ) ,
190210 } )
191211 }
192212 azalea_registry:: Fluid :: Lava | azalea_registry:: Fluid :: FlowingLava => {
193213 BlockState :: from ( crate :: blocks:: Lava {
194214 level : crate :: properties:: LavaLevel :: from (
195- state. height as BlockStateIntegerRepr ,
215+ state. amount as BlockStateIntegerRepr ,
196216 ) ,
197217 } )
198218 }
0 commit comments