@@ -4,10 +4,10 @@ use ferrumc_macros::{packet, NetEncode};
4
4
use ferrumc_net_codec:: net_types:: bitset:: BitSet ;
5
5
use ferrumc_net_codec:: net_types:: length_prefixed_vec:: LengthPrefixedVec ;
6
6
use ferrumc_net_codec:: net_types:: var_int:: VarInt ;
7
- use ferrumc_world:: chunk_format:: { Chunk , Heightmaps } ;
7
+ use ferrumc_world:: chunk_format:: { Chunk , Heightmaps , PaletteType } ;
8
8
use std:: io:: { Cursor , Write } ;
9
9
use std:: ops:: Not ;
10
- use tracing:: { trace, warn} ;
10
+ use tracing:: { debug , trace, warn} ;
11
11
12
12
const SECTIONS : usize = 24 ; // Number of sections, adjust for your Y range (-64 to 319)
13
13
@@ -64,7 +64,7 @@ impl ChunkAndLightData {
64
64
}
65
65
66
66
pub fn from_chunk ( chunk : & Chunk ) -> Result < Self , NetError > {
67
- let mut data = Cursor :: new ( Vec :: new ( ) ) ;
67
+ let mut raw_data = Cursor :: new ( Vec :: new ( ) ) ;
68
68
let mut sky_light_data = Vec :: new ( ) ;
69
69
let mut block_light_data = Vec :: new ( ) ;
70
70
for section in & chunk. sections {
@@ -89,49 +89,40 @@ impl ChunkAndLightData {
89
89
} ;
90
90
block_light_data. push ( section_block_light_data) ;
91
91
92
- data . write_u16 :: < BigEndian > ( section. block_states . non_air_blocks ) ?;
92
+ raw_data . write_u16 :: < BigEndian > ( section. block_states . non_air_blocks ) ?;
93
93
94
- let bits_per_block = section. block_states . bits_per_block ;
95
- data. write_u8 ( bits_per_block) ?;
96
- // If bits_per_block is 0, the section is using the single-value palette format
97
- // If bits_per_block is greater than 0, the section is using the indirect palette format
98
- if bits_per_block > 0 {
99
- // Write the palette
100
- VarInt :: new ( section. block_states . palette . len ( ) as i32 ) . write ( & mut data) ?;
101
- for palette_entry in & section. block_states . palette {
102
- palette_entry. write ( & mut data) ?;
94
+ match & section. block_states . block_data {
95
+ PaletteType :: Single ( val) => {
96
+ debug ! ( "Single palette type: {:?}" , ( chunk. x, chunk. z) ) ;
97
+ raw_data. write_u8 ( 0 ) ?;
98
+ val. write ( & mut raw_data) ?;
99
+ VarInt :: new ( 0 ) . write ( & mut raw_data) ?;
103
100
}
104
-
105
- // Write the data
106
- VarInt :: new ( section. block_states . data . len ( ) as i32 ) . write ( & mut data) ?;
107
- for data_entry in & section. block_states . data {
108
- data. write_i64 :: < BigEndian > ( * data_entry) ?;
109
- }
110
- } else {
111
- // The 0s for air blocks and bits_per_block are already written
112
- // Get the only palette entry
113
- match section. block_states . palette . first ( ) {
114
- Some ( palette_entry) => {
115
- palette_entry. write ( & mut data) ?;
101
+ PaletteType :: Indirect {
102
+ bits_per_block,
103
+ data,
104
+ palette,
105
+ } => {
106
+ debug ! ( "Indirect palette type: {:?}" , ( chunk. x, chunk. z) ) ;
107
+ raw_data. write_u8 ( * bits_per_block) ?;
108
+ VarInt :: new ( palette. len ( ) as i32 ) . write ( & mut raw_data) ?;
109
+ for palette_entry in palette {
110
+ palette_entry. write ( & mut raw_data) ?;
116
111
}
117
- // If there is no palette entry, write a 0 (air) and log a warning
118
- None => {
119
- VarInt :: new ( 0 ) . write ( & mut data) ?;
120
- trace ! (
121
- "No palette entry found for section at {}, {}, {}" ,
122
- chunk. x,
123
- section. y,
124
- chunk. z
125
- ) ;
112
+ VarInt :: new ( data. len ( ) as i32 ) . write ( & mut raw_data) ?;
113
+ for data_entry in data {
114
+ raw_data. write_i64 :: < BigEndian > ( * data_entry) ?;
126
115
}
127
116
}
128
- // Write the empty data section's length (0)
129
- VarInt :: new ( 0 ) . write ( & mut data) ?;
117
+ PaletteType :: Direct { .. } => {
118
+ todo ! ( "Direct palette type" )
119
+ }
130
120
}
121
+
131
122
// Empty biome data for now
132
- data . write_u8 ( 0 ) ?;
133
- data . write_u8 ( 0 ) ?;
134
- data . write_u8 ( 0 ) ?;
123
+ raw_data . write_u8 ( 0 ) ?;
124
+ raw_data . write_u8 ( 0 ) ?;
125
+ raw_data . write_u8 ( 0 ) ?;
135
126
}
136
127
let mut sky_light_mask = BitSet :: new ( SECTIONS + 2 ) ;
137
128
let mut block_light_mask = BitSet :: new ( SECTIONS + 2 ) ;
@@ -168,7 +159,7 @@ impl ChunkAndLightData {
168
159
chunk_x : chunk. x ,
169
160
chunk_z : chunk. z ,
170
161
heightmaps : chunk. heightmaps . serialize_as_network ( ) ,
171
- data : LengthPrefixedVec :: new ( data . into_inner ( ) ) ,
162
+ data : LengthPrefixedVec :: new ( raw_data . into_inner ( ) ) ,
172
163
block_entities : LengthPrefixedVec :: new ( Vec :: new ( ) ) ,
173
164
sky_light_mask,
174
165
block_light_mask,
0 commit comments