Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
roll back
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriTimoz committed Nov 14, 2023
1 parent 366c161 commit 903eaa1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 43 deletions.
40 changes: 33 additions & 7 deletions minecraft-server/src/player_handler/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,43 @@ pub async fn handshake(stream: &mut TcpStream, logged_in_player_info: LoggedInPl
}
}
world.update_loaded_chunks(logged_in_player_info.uuid, loaded_chunks).await;

let mut heightmaps = HashMap::new();
heightmaps.insert(String::from("MOTION_BLOCKING"), NbtTag::LongArray(vec![0; 37]));
let heightmaps = NbtTag::Compound(heightmaps);
for cx in -3..=3 {
for cz in -3..=3 {
let chunk_column = world.get_network_chunk_column_data(ChunkColumnPosition { cx, cz }).await.unwrap_or_else(|| {
error!("Chunk not loaded: {cx} {cz}");
panic!("Chunk not loaded: {cx} {cz}");
});

send_packet_raw(stream, &chunk_column).await;
let mut column = Vec::new();
for cy in -4..20 {
let chunk = world.get_network_chunk(ChunkPosition { cx, cy, cz }).await.unwrap_or_else(|| {
error!("Chunk not loaded: {cx} {cy} {cz}");
NetworkChunk { // TODO hard error
block_count: 0,
blocks: PalettedData::Single { value: 0 },
biomes: PalettedData::Single { value: 4 },
}
});
column.push(chunk);
}
let serialized: Vec<u8> = NetworkChunk::into_data(column).unwrap();
let chunk_data = PlayClientbound::ChunkData {
value: NetworkChunkColumnData {
chunk_x: cx,
chunk_z: cz,
heightmaps: heightmaps.clone(),
data: Array::from(serialized.clone()),
block_entities: Array::default(),
sky_light_mask: Array::default(),
block_light_mask: Array::default(),
empty_sky_light_mask: Array::default(),
empty_block_light_mask: Array::default(),
sky_light: Array::default(),
block_light: Array::default(),
}
};
send_packet(stream, chunk_data).await;
}
}

debug!("ChunkData sent");

// Chunk batch end
Expand Down
45 changes: 37 additions & 8 deletions minecraft-server/src/player_handler/play.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use minecraft_protocol::components::chunk;

use super::*;

struct PlayerHandler {
Expand All @@ -21,8 +23,8 @@ impl PlayerHandler {
self.packet_sender.send(packet).await.unwrap();
}

async fn send_packet_raw(&mut self, packet: &[u8]) {
self.packet_sender.send(packet.to_vec()).await.unwrap();
async fn send_packet_raw(&mut self, packet: Vec<u8>) {
self.packet_sender.send(packet).await.unwrap();
}

async fn on_server_message(&mut self, message: ServerMessage) {
Expand Down Expand Up @@ -75,13 +77,39 @@ impl PlayerHandler {
self.world.update_loaded_chunks(self.info.uuid, loaded_chunks_after.clone()).await;

// Send the chunks to the client
let mut heightmaps = HashMap::new();
heightmaps.insert(String::from("MOTION_BLOCKING"), NbtTag::LongArray(vec![0; 37]));
let heightmaps = NbtTag::Compound(heightmaps);
for newly_loaded_chunk in newly_loaded_chunks {
let chunk_column_data = self.world.get_network_chunk_column_data(newly_loaded_chunk.clone()).await.unwrap_or_else(|| {
error!("Chunk not loaded: {newly_loaded_chunk:?}");
panic!("Chunk not loaded: {newly_loaded_chunk:?}");
});

self.send_packet_raw(&chunk_column_data).await;
let mut column = Vec::new();
for cy in -4..20 {
let chunk = self.world.get_network_chunk(newly_loaded_chunk.chunk(cy)).await.unwrap_or_else(|| {
error!("Chunk not loaded: {newly_loaded_chunk:?}");
NetworkChunk { // TODO hard error
block_count: 0,
blocks: PalettedData::Single { value: 0 },
biomes: PalettedData::Single { value: 4 },
}
});
column.push(chunk);
}
let serialized: Vec<u8> = NetworkChunk::into_data(column).unwrap();
let chunk_data = PlayClientbound::ChunkData {
value: NetworkChunkColumnData {
chunk_x: newly_loaded_chunk.cx,
chunk_z: newly_loaded_chunk.cz,
heightmaps: heightmaps.clone(),
data: Array::from(serialized.clone()),
block_entities: Array::default(),
sky_light_mask: Array::default(),
block_light_mask: Array::default(),
empty_sky_light_mask: Array::default(),
empty_block_light_mask: Array::default(),
sky_light: Array::default(),
block_light: Array::default(),
}
};
self.send_packet(chunk_data).await;
}

// Tell the client to unload chunks
Expand All @@ -93,6 +121,7 @@ impl PlayerHandler {
}

self.loaded_chunks = loaded_chunks_after;

}

async fn on_packet<'a>(&mut self, packet: PlayServerbound<'a>) {
Expand Down
2 changes: 0 additions & 2 deletions minecraft-server/src/world/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ impl ChunkColumn {
}

fn explore_sky_light_from_heap(&mut self, to_explore: &mut BinaryHeap<LightPositionInChunkColumn>) -> Result<(), ()> {
error!("start loop");
// We get the neighbors and determine the light source from them
// The neighbor with the highest light level is the light source
// So we explore from it
Expand All @@ -308,7 +307,6 @@ impl ChunkColumn {
}
}
}
error!("end loop");
Ok(())
}

Expand Down
35 changes: 11 additions & 24 deletions minecraft-server/src/world/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, cmp::Ordering};
use std::{collections::HashMap, cmp::Ordering, vec};
use minecraft_protocol::{components::chunk::PalettedData, ids::blocks::Block};
use tokio::sync::RwLock;
use crate::prelude::*;
Expand Down Expand Up @@ -429,6 +429,7 @@ impl ChunkColumn {
},
_ => {}
}
trace!("setting");
self.update_light_as_block_changed_at(position, !not_filter_sunlight);
}
}
Expand Down Expand Up @@ -456,30 +457,16 @@ impl WorldMap {
inner_get_block(self, position).await.unwrap_or(BlockWithState::Air)
}

pub async fn get_network_chunk_column_data(&self, position: ChunkColumnPosition) -> Option<Vec<u8>> {
let shard = position.shard(self.shard_count);
pub async fn get_network_chunk(&self, position: ChunkPosition) -> Option<NetworkChunk> {
let chunk_column_position = position.chunk_column();
let shard = chunk_column_position.shard(self.shard_count);
let cy_in_vec: usize = position.cy.saturating_add(4).try_into().ok()?;

let shard = self.shards[shard].read().await;
let chunk_column = shard.get(&position)?;

let serialized = NetworkChunk::into_data(chunk_column.chunks.iter().map(|c| c.data.clone()).collect()).unwrap();
let chunk_data = PlayClientbound::ChunkData { value: NetworkChunkColumnData {
chunk_x: position.cx,
chunk_z: position.cz,
heightmaps: chunk_column.heightmap.to_tag(),
data: Array::from(serialized.clone()),
block_entities: Array::default(),
sky_light_mask: Array::default(),
block_light_mask: Array::default(),
empty_sky_light_mask: Array::default(),
empty_block_light_mask: Array::default(),
sky_light: Array::default(),
block_light: Array::default(),
}};

let chunk_data = chunk_data.serialize_minecraft_packet().map_err(|e| {
error!("Failed to serialize chunk column data: {:?}", e);
}).ok()?;
Some(chunk_data)
let chunk_column = shard.get(&chunk_column_position)?;
let chunk = chunk_column.chunks.get(cy_in_vec)?;

Some(chunk.as_network_chunk().clone())
}

pub async fn set_block(&self, position: BlockPosition, block: BlockWithState) {
Expand Down
4 changes: 2 additions & 2 deletions minecraft-server/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ impl World {
Some(self.map.get_block(position).await)
}

pub async fn get_network_chunk_column_data(&self, position: ChunkColumnPosition) -> Option<Vec<u8>> {
self.map.get_network_chunk_column_data(position).await
pub async fn get_network_chunk(&self, position: ChunkPosition) -> Option<NetworkChunk> {
self.map.get_network_chunk(position).await
}

pub async fn set_block(&self, position: BlockPosition, block: BlockWithState) {
Expand Down

0 comments on commit 903eaa1

Please sign in to comment.