Skip to content

Commit

Permalink
Fix anvil
Browse files Browse the repository at this point in the history
  • Loading branch information
rj00a committed Jul 1, 2023
1 parent cf0985b commit 15fe2be
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
14 changes: 3 additions & 11 deletions crates/valence_anvil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ impl AnvilLevel {
.iter()
.map(|(id, name, _)| (name.to_string_ident(), id))
.collect(),
height: 0, // Assigned later.
min_y: 0, // Assigned later.
}),
ignored_chunks: HashSet::new(),
pending: HashMap::new(),
Expand Down Expand Up @@ -139,10 +137,6 @@ struct ChunkWorkerState {
decompress_buf: Vec<u8>,
/// Mapping of biome names to their biome ID.
biome_to_id: BTreeMap<Ident<String>, BiomeId>,
/// Height of chunks in the instance.
height: u32,
/// Minimum Y position of chunks in the instance.
min_y: i32,
}

impl ChunkWorkerState {
Expand Down Expand Up @@ -291,11 +285,9 @@ impl Plugin for AnvilPlugin {
}
}

fn init_anvil(mut query: Query<(&mut AnvilLevel, &Instance), Added<AnvilLevel>>) {
for (mut level, inst) in &mut query {
if let Some(mut state) = level.worker_state.take() {
state.height = inst.height();
state.min_y = inst.min_y();
fn init_anvil(mut query: Query<&mut AnvilLevel, (Added<AnvilLevel>, With<Instance>)>) {
for mut level in &mut query {
if let Some(state) = level.worker_state.take() {
thread::spawn(move || anvil_worker(state));
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/valence_anvil/src/parse_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub(crate) fn parse_chunk(
let z = i / 16 % 16;
let y = i / (16 * 16);

chunk.set_block_state(x, y, z, block);
chunk.set_block_state(x, sect_y * 16 + y, z, block);

i += 1;
}
Expand Down Expand Up @@ -255,7 +255,7 @@ pub(crate) fn parse_chunk(
let z = i / 4 % 4;
let y = i / (4 * 4);

chunk.set_biome(x, y, z, biome);
chunk.set_biome(x, sect_y * 4 + y, z, biome);

i += 1;
}
Expand Down
9 changes: 5 additions & 4 deletions crates/valence_instance/src/chunk/loaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,17 @@ impl LoadedChunk {
let old_sections = self
.sections
.iter_mut()
.map(|sect| {
.zip(chunk.sections)
.map(|(sect, other_sect)| {
sect.section_updates.clear();

unloaded::Section {
block_states: mem::take(&mut sect.block_states),
biomes: mem::take(&mut sect.biomes),
block_states: mem::replace(&mut sect.block_states, other_sect.block_states),
biomes: mem::replace(&mut sect.biomes, other_sect.biomes),
}
})
.collect();
let old_block_entities = mem::take(&mut self.block_entities);
let old_block_entities = mem::replace(&mut self.block_entities, chunk.block_entities);
self.changed_block_entities.clear();
self.changed_biomes = false;
self.packet_buf.clear();
Expand Down
6 changes: 5 additions & 1 deletion examples/anvil_loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ fn handle_chunk_loads(
}
ChunkLoadStatus::Failed(e) => {
// Something went wrong.
eprintln!(
let errmsg = format!(
"failed to load chunk at ({}, {}): {e:#}",
event.pos.x, event.pos.z
);

Check warning on line 107 in examples/anvil_loading.rs

View workflow job for this annotation

GitHub Actions / valence-fmt (ubuntu-latest, default, nightly-2023-06-20)

Diff in /home/runner/work/valence/valence/examples/anvil_loading.rs
eprintln!("{errmsg}");
inst.send_chat_message(errmsg.color(Color::RED));

inst.insert_chunk(event.pos, UnloadedChunk::new());
}
}
Expand Down

0 comments on commit 15fe2be

Please sign in to comment.