Skip to content

Commit d50cf02

Browse files
committed
fix(world): cache the end biome id
1 parent 5db47e3 commit d50cf02

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ async fn main() -> Result<()> {
7474
let world = read_world(&args.map_dir)?;
7575
info!("Done.");
7676

77+
let state = Arc::new(state::State::new(VERSION, VERSION_NUM, args));
78+
7779
info!("Generating world chunk packets");
78-
let world_cache = WorldCache::from(world);
80+
let world_cache = WorldCache::from_anvil(state.clone(), world);
7981
info!("Done.");
8082

81-
let state = Arc::new(state::State::new(VERSION, VERSION_NUM, args));
82-
8383
#[cfg(feature = "lan")]
8484
net::spawn_lan_broadcast(state.clone()).await?;
8585

src/net/cache.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use std::cmp::Ordering;
20+
use std::{cmp::Ordering, sync::Arc};
2121

2222
use rayon::prelude::*;
2323

@@ -31,15 +31,16 @@ use crate::{
3131
Encoder,
3232
},
3333
world::{blocks::Blocks, World},
34+
CrawlState,
3435
};
3536

3637
#[derive(Debug)]
3738
pub struct WorldCache {
3839
pub encoded: Vec<Vec<u8>>,
3940
}
4041

41-
impl From<World> for WorldCache {
42-
fn from(world: World) -> Self {
42+
impl WorldCache {
43+
pub fn from_anvil(crawlstate: CrawlState, world: World) -> Self {
4344
let mut chunks = world.0.iter().collect::<Vec<_>>();
4445

4546
chunks.sort_by(|((ax, az), _), ((bx, bz), _)| {
@@ -54,7 +55,7 @@ impl From<World> for WorldCache {
5455

5556
let chunks = chunks
5657
.par_iter()
57-
.map(|(_, c)| ChunkDataUpdateLightC::new(c, &block_states))
58+
.map(|(_, c)| ChunkDataUpdateLightC::new(crawlstate.clone(), c, &block_states))
5859
.collect::<Vec<ChunkDataUpdateLightC<'_>>>();
5960

6061
let encoded = chunks
@@ -76,13 +77,15 @@ impl From<World> for WorldCache {
7677
pub struct RegistryCache {
7778
pub encoded: Vec<u8>,
7879
pub the_end_id: VarInt,
80+
pub the_end_biome_id: u16,
7981
}
8082

8183
impl From<&AllRegistries> for RegistryCache {
8284
fn from(registry: &AllRegistries) -> Self {
8385
let mut encoder = Encoder::new();
8486

8587
let dimensions = Registry::from(registry.dimension_type.clone());
88+
let biomes = Registry::from(registry.biome.clone());
8689
encoder
8790
.append_packet(&Registry::from(registry.trim_material.clone()))
8891
.expect("Failed to encode trim material");
@@ -92,9 +95,6 @@ impl From<&AllRegistries> for RegistryCache {
9295
encoder
9396
.append_packet(&Registry::from(registry.banner_pattern.clone()))
9497
.expect("Failed to encode banner pattern");
95-
encoder
96-
.append_packet(&Registry::from(registry.biome.clone()))
97-
.expect("Failed to encode biome");
9898
encoder
9999
.append_packet(&Registry::from(registry.chat_type.clone()))
100100
.expect("Failed to encode chat type");
@@ -104,6 +104,9 @@ impl From<&AllRegistries> for RegistryCache {
104104
encoder
105105
.append_packet(&dimensions)
106106
.expect("Failed to encode dimensions");
107+
encoder
108+
.append_packet(&biomes)
109+
.expect("Failed to encode biomes");
107110
encoder
108111
.append_packet(&Registry::from(registry.wolf_variant.clone()))
109112
.expect("Failed to encode wolf variants");
@@ -114,6 +117,7 @@ impl From<&AllRegistries> for RegistryCache {
114117
Self {
115118
encoded: encoder.take().to_vec(),
116119
the_end_id: VarInt(dimensions.index_of("minecraft:the_end")),
120+
the_end_biome_id: biomes.index_of("minecraft:the_end") as u16,
117121
}
118122
}
119123
}

src/protocol/packets/play/world.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use std::collections::HashMap;
20+
use std::{collections::HashMap, sync::Arc};
2121

2222
use bit_vec::BitVec;
2323
use bytes::BufMut;
@@ -32,6 +32,7 @@ use crate::{
3232
self,
3333
blocks::{BlockState, Blocks},
3434
},
35+
CrawlState,
3536
};
3637

3738
#[derive(Debug)]
@@ -258,7 +259,11 @@ impl Encode for ChunkDataUpdateLightC<'_> {
258259
}
259260

260261
impl ChunkSection {
261-
pub fn anvil_to_sec(value: &world::Section, block_states: &Blocks) -> Self {
262+
pub fn anvil_to_sec(
263+
crawlstate: CrawlState,
264+
value: &world::Section,
265+
block_states: &Blocks,
266+
) -> Self {
262267
let mut blocks = Vec::new();
263268
let bit_length = (64 - (value.block_states.palette.len() as u64).leading_zeros()).max(4);
264269

@@ -379,19 +384,21 @@ impl ChunkSection {
379384
},
380385
biomes: PalettedContainer {
381386
bits_per_entry: 0,
382-
palette: Palette::SingleValued(BlockState(0)),
387+
palette: Palette::SingleValued(BlockState(
388+
crawlstate.registry_cache.the_end_biome_id,
389+
)),
383390
data_array: fastnbt::LongArray::new(vec![]),
384391
},
385392
}
386393
}
387394
}
388395

389396
impl ChunkDataUpdateLightC<'_> {
390-
pub fn new(value: &world::Chunk, block_states: &Blocks) -> Self {
397+
pub fn new(crawlstate: CrawlState, value: &world::Chunk, block_states: &Blocks) -> Self {
391398
let data = value
392399
.sections
393400
.iter()
394-
.map(|sec| ChunkSection::anvil_to_sec(sec, block_states))
401+
.map(|sec| ChunkSection::anvil_to_sec(crawlstate.clone(), sec, block_states))
395402
.collect::<Vec<_>>();
396403

397404
let block_entities = value

0 commit comments

Comments
 (0)