Skip to content

Commit 58c6cf9

Browse files
committed
Final Fixes (I think)
1 parent cb42582 commit 58c6cf9

File tree

5 files changed

+28
-51
lines changed

5 files changed

+28
-51
lines changed

src/bin/src/packet_handlers/play_packets/player_action.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use ferrumc_net::PlayerActionReceiver;
99
use ferrumc_net_codec::net_types::var_int::VarInt;
1010
use ferrumc_state::GlobalStateResource;
1111
use ferrumc_world::block_id::BlockId;
12-
use ferrumc_world::vanilla_chunk_format::BlockData;
1312
use tracing::{debug, error, trace};
1413

1514
pub fn handle(

src/lib/world/src/block_id.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use deepsize::DeepSizeOf;
55
use ferrumc_net_codec::net_types::var_int::VarInt;
66
use lazy_static::lazy_static;
77
use std::collections::HashMap;
8+
use std::fmt::Display;
89
use std::process::exit;
910
use tracing::error;
1011

@@ -70,6 +71,16 @@ impl BlockId {
7071
}
7172
}
7273

74+
impl Display for BlockId {
75+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76+
if let Some(block_data) = self.to_block_data() {
77+
write!(f, "BlockId({}: {:?})", self.0, block_data)
78+
} else {
79+
write!(f, "BlockId({}: Unknown)", self.0)
80+
}
81+
}
82+
}
83+
7384
impl BlockData {
7485
/// Converts a BlockData to a BlockId. Will panic if the ID is not found.
7586
pub fn to_block_id(&self) -> BlockId {

src/lib/world/src/chunk_format.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ use crate::{errors::WorldError, vanilla_chunk_format::VanillaHeightmaps};
55
use bitcode_derive::{Decode, Encode};
66
use deepsize::DeepSizeOf;
77
use ferrumc_general_purpose::data_packing::i32::read_nbit_i32;
8-
use ferrumc_macros::{NBTDeserialize, NBTSerialize};
8+
use ferrumc_macros::{block, NBTDeserialize, NBTSerialize};
99
use ferrumc_net_codec::net_types::var_int::VarInt;
1010
use std::cmp::max;
1111
use std::collections::HashMap;
1212
use tracing::error;
13-
use vanilla_chunk_format::BlockData;
1413
// #[cfg(test)]
1514
// const BLOCKSFILE: &[u8] = &[0];
1615

@@ -76,7 +75,9 @@ pub struct BiomeStates {
7675
pub palette: Vec<VarInt>,
7776
}
7877

79-
fn convert_to_net_palette(vanilla_palettes: Vec<BlockData>) -> Result<Vec<VarInt>, WorldError> {
78+
fn convert_to_net_palette(
79+
vanilla_palettes: Vec<vanilla_chunk_format::BlockData>,
80+
) -> Result<Vec<VarInt>, WorldError> {
8081
let mut new_palette = Vec::new();
8182
for palette in vanilla_palettes {
8283
if let Some(id) = BLOCK2ID.get(&palette) {
@@ -135,17 +136,17 @@ impl VanillaChunk {
135136
while i + bits_per_block < 64 {
136137
let palette_index = read_nbit_i32(chunk, bits_per_block as usize, i as u32)?;
137138
let block = match palette.get(palette_index as usize) {
138-
Some(block) => block,
139+
Some(block) => block.to_block_id(),
139140
None => {
140141
error!("Could not find block for palette index: {}", palette_index);
141-
&BlockData::default()
142+
BlockId::default()
142143
}
143144
};
144145

145-
if let Some(count) = block_counts.get_mut(&block.to_block_id()) {
146+
if let Some(count) = block_counts.get_mut(&block) {
146147
*count += 1;
147148
} else {
148-
block_counts.insert(block.to_block_id(), 0);
149+
block_counts.insert(block, 0);
149150
}
150151

151152
i += bits_per_block;
@@ -163,24 +164,8 @@ impl VanillaChunk {
163164
};
164165
// Count the number of blocks that are either air, void air, or cave air
165166
let mut air_blocks = *block_counts.get(&BlockId::default()).unwrap_or(&0) as u16;
166-
air_blocks += *block_counts
167-
.get(
168-
&BlockData {
169-
name: "minecraft:void_air".to_string(),
170-
properties: None,
171-
}
172-
.to_block_id(),
173-
)
174-
.unwrap_or(&0) as u16;
175-
air_blocks += *block_counts
176-
.get(
177-
&BlockData {
178-
name: "minecraft:cave_air".to_string(),
179-
properties: None,
180-
}
181-
.to_block_id(),
182-
)
183-
.unwrap_or(&0) as u16;
167+
air_blocks += *block_counts.get(&block!("void_air")).unwrap_or(&0) as u16;
168+
air_blocks += *block_counts.get(&block!("cave_air")).unwrap_or(&0) as u16;
184169
let non_air_blocks = 4096 - air_blocks;
185170
let block_states = BlockStates {
186171
block_counts,
@@ -253,6 +238,7 @@ impl Chunk {
253238
for section in &mut sections {
254239
section.optimise().expect("Failed to optimise section");
255240
}
241+
block!("stone");
256242
Chunk {
257243
x,
258244
z,
@@ -266,7 +252,6 @@ impl Chunk {
266252
#[cfg(test)]
267253
mod tests {
268254
use super::*;
269-
use crate::block_id::BlockId;
270255
use ferrumc_macros::block;
271256

272257
#[test]
@@ -322,11 +307,7 @@ mod tests {
322307
#[test]
323308
fn test_false_positive() {
324309
let mut chunk = Chunk::new(0, 0, "overworld".to_string());
325-
let block = BlockData {
326-
name: "minecraft:stone".to_string(),
327-
properties: None,
328-
}
329-
.to_block_id();
310+
let block = block!("stone");
330311
chunk.set_block(0, 0, 0, block).unwrap();
331312
assert_ne!(chunk.get_block(0, 1, 0).unwrap(), block);
332313
}

src/lib/world/src/edits.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use crate::block_id::{BlockId, BLOCK2ID, ID2BLOCK};
1+
use crate::block_id::{BlockId, ID2BLOCK};
22
use crate::chunk_format::{BlockStates, Chunk, PaletteType, Section};
33
use crate::errors::WorldError;
4-
use crate::vanilla_chunk_format::BlockData;
54
use crate::World;
65
use ferrumc_general_purpose::data_packing::i32::read_nbit_i32;
7-
use ferrumc_net_codec::net_types::var_int::VarInt;
86
use std::collections::hash_map::Entry;
97
use std::collections::HashMap;
108
use std::sync::Arc;
@@ -94,20 +92,10 @@ impl BlockStates {
9492
pub fn resize(&mut self, new_bit_size: usize) -> Result<(), WorldError> {
9593
match &mut self.block_data {
9694
PaletteType::Single(val) => {
97-
let block = ID2BLOCK
98-
.get(val.0 as usize)
99-
.cloned()
100-
.unwrap_or(BlockData::default());
101-
let mut new_palette = vec![VarInt::from(0); 1];
102-
if let Some(id) = BLOCK2ID.get(&block) {
103-
new_palette[0] = VarInt::from(*id);
104-
} else {
105-
error!("Could not find block id for palette entry: {:?}", block);
106-
}
10795
self.block_data = PaletteType::Indirect {
10896
bits_per_block: new_bit_size as u8,
10997
data: vec![],
110-
palette: new_palette,
98+
palette: vec![*val; 1],
11199
}
112100
}
113101
PaletteType::Indirect {
@@ -224,7 +212,6 @@ impl Chunk {
224212
/// the coordinates to section coordinates isn't really necessary, but you should probably do it
225213
/// anyway for readability's sake.
226214
pub fn set_block(&mut self, x: i32, y: i32, z: i32, block: BlockId) -> Result<(), WorldError> {
227-
let block = block.into();
228215
// Get old block
229216
let old_block = self.get_block(x, y, z)?;
230217
if old_block == block {

src/lib/world/src/errors.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use crate::block_id::BlockId;
12
use crate::errors::WorldError::{CompressionError, GenericIOError, PermissionError};
2-
use crate::vanilla_chunk_format::BlockData;
33
use errors::AnvilError;
44
use ferrumc_anvil::errors;
55
use ferrumc_general_purpose::data_packing::errors::DataPackingError;
@@ -37,7 +37,7 @@ pub enum WorldError {
3737
#[error("Anvil Decode Error: {0}")]
3838
AnvilDecodeError(AnvilError),
3939
#[error("Missing block mapping: {0}")]
40-
MissingBlockMapping(BlockData),
40+
MissingBlockMapping(BlockId),
4141
#[error("Invalid memory map size: {0}")]
4242
InvalidMapSize(u64),
4343
#[error("Task Join Error: {0}")]
@@ -47,7 +47,7 @@ pub enum WorldError {
4747
#[error("Invalid block state data")]
4848
InvalidBlockStateData(String),
4949
#[error("Invalid block: {0}")]
50-
InvalidBlock(BlockData),
50+
InvalidBlock(BlockId),
5151
#[error("Invalid batching operation: {0}")]
5252
InvalidBatchingOperation(String),
5353
#[error("Invalid block ID: {0}")]
@@ -68,7 +68,6 @@ impl From<std::io::Error> for WorldError {
6868
fn from(err: std::io::Error) -> Self {
6969
match err.kind() {
7070
ErrorKind::PermissionDenied => PermissionError(err.to_string()),
71-
// ErrorKind::ReadOnlyFilesystem => PermissionError(err.to_string()),
7271
_ => GenericIOError(err.to_string()),
7372
}
7473
}

0 commit comments

Comments
 (0)