Skip to content

Commit

Permalink
Move world into entity
Browse files Browse the repository at this point in the history
Also fixes most of error when leaving while chunks are loading
  • Loading branch information
Snowiiii committed Sep 7, 2024
1 parent 0d6f7b4 commit 39b4cb3
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 101 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ and customizable experience. It prioritizes performance and player enjoyment whi
- [x] Player Inventory
- [x] Player Combat
- Server
- [x] Plugins
- [ ] Query
- [x] RCON
- [x] Inventories
Expand Down
60 changes: 31 additions & 29 deletions pumpkin-world/src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,41 +149,43 @@ impl Level {
) {
chunks.into_par_iter().for_each(|at| {
if is_alive {
dbg!("a");

This comment has been minimized.

Copy link
@Bryntet

Bryntet Sep 7, 2024

Contributor

why is this on master :<

return;
}
let channel = channel.clone();

// Check if chunks is already loaded
let mut loaded_chunks = self.loaded_chunks.lock().unwrap();
if loaded_chunks.contains_key(at) {
channel
.blocking_send(Ok(loaded_chunks.get(at).unwrap().clone()))
.expect("Failed sending ChunkData.");
return;
}
let at = *at;
let data = match &self.save_file {
Some(save_file) => {
match Self::read_chunk(save_file, at) {
Err(WorldError::ChunkNotGenerated(_)) => {
// This chunk was not generated yet.
Ok(self.world_gen.generate_chunk(at))
if let Ok(mut loaded_chunks) = self.loaded_chunks.lock() {
let channel = channel.clone();

// Check if chunks is already loaded
if loaded_chunks.contains_key(at) {
channel
.blocking_send(Ok(loaded_chunks.get(at).unwrap().clone()))
.expect("Failed sending ChunkData.");
return;
}
let at = *at;
let data = match &self.save_file {
Some(save_file) => {
match Self::read_chunk(save_file, at) {
Err(WorldError::ChunkNotGenerated(_)) => {
// This chunk was not generated yet.
Ok(self.world_gen.generate_chunk(at))
}
// TODO this doesn't warn the user about the error. fix.
result => result,
}
// TODO this doesn't warn the user about the error. fix.
result => result,
}
None => {
// There is no savefile yet -> generate the chunks
Ok(self.world_gen.generate_chunk(at))
}
}
None => {
// There is no savefile yet -> generate the chunks
Ok(self.world_gen.generate_chunk(at))
}
.unwrap();
let data = Arc::new(data);
channel
.blocking_send(Ok(data.clone()))
.expect("Failed sending ChunkData.");
loaded_chunks.insert(at, data);
}
.unwrap();
let data = Arc::new(data);
channel
.blocking_send(Ok(data.clone()))
.expect("Failed sending ChunkData.");
loaded_chunks.insert(at, data);
})
}

Expand Down
4 changes: 4 additions & 0 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ impl Client {

/// Send a Clientbound Packet to the Client
pub fn send_packet<P: ClientPacket>(&mut self, packet: &P) {
// assert!(!self.closed);

self.enc
.append_packet(packet)
.unwrap_or_else(|e| self.kick(&e.to_string()));
Expand All @@ -137,6 +139,8 @@ impl Client {
}

pub fn try_send_packet<P: ClientPacket>(&mut self, packet: &P) -> Result<(), PacketError> {
// assert!(!self.closed);

self.enc.append_packet(packet)?;
self.connection
.write_all(&self.enc.take())
Expand Down
40 changes: 15 additions & 25 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Player {
let entity_id = entity.entity_id;
let (x, y, z) = entity.pos.into();
let (lastx, lasty, lastz) = self.last_position.into();
let world = self.world.clone();
let world = self.entity.world.clone();
let world = world.lock().await;

// let delta = Vector3::new(x - lastx, y - lasty, z - lastz);
Expand Down Expand Up @@ -151,7 +151,7 @@ impl Player {
let yaw = modulus(entity.yaw * 256.0 / 360.0, 256.0);
let pitch = modulus(entity.pitch * 256.0 / 360.0, 256.0);
// let head_yaw = (entity.head_yaw * 256.0 / 360.0).floor();
let world = self.world.clone();
let world = self.entity.world.clone();
let world = world.lock().await;

// let delta = Vector3::new(x - lastx, y - lasty, z - lastz);
Expand Down Expand Up @@ -204,7 +204,7 @@ impl Player {
let pitch = modulus(entity.pitch * 256.0 / 360.0, 256.0);
// let head_yaw = modulus(entity.head_yaw * 256.0 / 360.0, 256.0);

let world = self.world.lock().await;
let world = self.entity.world.lock().await;
let packet = CUpdateEntityRot::new(entity_id.into(), yaw as u8, pitch as u8, on_ground);
// self.client.send_packet(&packet);
world.broadcast_packet(&[self.client.token], &packet);
Expand All @@ -231,31 +231,23 @@ impl Player {
match action {
pumpkin_protocol::server::play::Action::StartSneaking => {
if !self.entity.sneaking {
self.entity
.set_sneaking(&mut self.client, self.world.clone(), true)
.await
self.entity.set_sneaking(&mut self.client, true).await
}
}
pumpkin_protocol::server::play::Action::StopSneaking => {
if self.entity.sneaking {
self.entity
.set_sneaking(&mut self.client, self.world.clone(), false)
.await
self.entity.set_sneaking(&mut self.client, false).await
}
}
pumpkin_protocol::server::play::Action::LeaveBed => todo!(),
pumpkin_protocol::server::play::Action::StartSprinting => {
if !self.entity.sprinting {
self.entity
.set_sprinting(&mut self.client, self.world.clone(), true)
.await
self.entity.set_sprinting(&mut self.client, true).await
}
}
pumpkin_protocol::server::play::Action::StopSprinting => {
if self.entity.sprinting {
self.entity
.set_sprinting(&mut self.client, self.world.clone(), false)
.await
self.entity.set_sprinting(&mut self.client, false).await
}
}
pumpkin_protocol::server::play::Action::StartHorseJump => todo!(),
Expand All @@ -265,7 +257,7 @@ impl Player {
let fall_flying = self.entity.check_fall_flying();
if self.entity.fall_flying != fall_flying {
self.entity
.set_fall_flying(&mut self.client, self.world.clone(), fall_flying)
.set_fall_flying(&mut self.client, fall_flying)
.await;
}
} // TODO
Expand All @@ -283,7 +275,7 @@ impl Player {
Hand::Off => Animation::SwingOffhand,
};
let id = self.entity_id();
let world = self.world.lock().await;
let world = self.entity.world.lock().await;
world.broadcast_packet(
&[self.client.token],
&CEntityAnimation::new(id.into(), animation as u8),
Expand All @@ -307,7 +299,7 @@ impl Player {
// TODO: filter message & validation
let gameprofile = &self.gameprofile;

let world = self.world.lock().await;
let world = self.entity.world.lock().await;
world.broadcast_packet(
&[self.client.token],
&CPlayerChatMessage::new(
Expand Down Expand Up @@ -364,9 +356,7 @@ impl Player {
pub async fn handle_interact(&mut self, _: &mut Server, interact: SInteract) {
let sneaking = interact.sneaking;
if self.entity.sneaking != sneaking {
self.entity
.set_sneaking(&mut self.client, self.world.clone(), sneaking)
.await;
self.entity.set_sneaking(&mut self.client, sneaking).await;
}
match ActionType::from_i32(interact.typ.0) {
Some(action) => match action {
Expand All @@ -375,7 +365,7 @@ impl Player {
// TODO: do validation and stuff
let config = &ADVANCED_CONFIG.pvp;
if config.enabled {
let world = self.world.clone();
let world = self.entity.world.clone();
let world = world.lock().await;
let attacked_player = world.get_by_entityid(self, entity_id.0 as EntityId);
if let Some(mut player) = attacked_player {
Expand Down Expand Up @@ -448,7 +438,7 @@ impl Player {
let location = player_action.location;
// Block break & block break sound
// TODO: currently this is always dirt replace it
let world = self.world.lock().await;
let world = self.entity.world.lock().await;
world.broadcast_packet(
&[self.client.token],
&CWorldEvent::new(2001, &location, 11, false),
Expand Down Expand Up @@ -476,7 +466,7 @@ impl Player {
}
// Block break & block break sound
// TODO: currently this is always dirt replace it
let world = self.world.lock().await;
let world = self.entity.world.lock().await;
world.broadcast_packet(
&[self.client.token],
&CWorldEvent::new(2001, &location, 11, false),
Expand Down Expand Up @@ -528,7 +518,7 @@ impl Player {
)
.expect("All item ids are in the global registry");
if let Ok(block_state_id) = BlockId::new(minecraft_id, None) {
let world = self.world.lock().await;
let world = self.entity.world.lock().await;
world.broadcast_packet(
&[self.client.token],
&CBlockUpdate::new(&location, block_state_id.get_id_mojang_repr().into()),
Expand Down
56 changes: 19 additions & 37 deletions pumpkin/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub mod player;
pub struct Entity {
pub entity_id: EntityId,
pub entity_type: EntityType,
pub world: Arc<tokio::sync::Mutex<World>>,

pub pos: Vector3<f64>,
pub block_pos: WorldPosition,
pub chunk_pos: Vector2<i32>,
Expand All @@ -38,7 +40,12 @@ pub struct Entity {

// TODO: Remove client: &mut Client, world: Arc<tokio::sync::Mutex<World>> bs
impl Entity {
pub fn new(entity_id: EntityId, entity_type: EntityType, standing_eye_height: f32) -> Self {
pub fn new(
entity_id: EntityId,
world: Arc<tokio::sync::Mutex<World>>,
entity_type: EntityType,
standing_eye_height: f32,
) -> Self {
Self {
entity_id,
entity_type,
Expand All @@ -47,6 +54,7 @@ impl Entity {
block_pos: WorldPosition(Vector3::new(0, 0, 0)),
chunk_pos: Vector2::new(0, 0),
sneaking: false,
world,
sprinting: false,
fall_flying: false,
yaw: 0.0,
Expand Down Expand Up @@ -101,15 +109,10 @@ impl Entity {
);
}

pub async fn set_sneaking(
&mut self,
client: &mut Client,
world: Arc<tokio::sync::Mutex<World>>,
sneaking: bool,
) {
pub async fn set_sneaking(&mut self, client: &mut Client, sneaking: bool) {
assert!(self.sneaking != sneaking);
self.sneaking = sneaking;
self.set_flag(client, world, Self::SNEAKING_FLAG_INDEX, sneaking)
self.set_flag(client, Self::SNEAKING_FLAG_INDEX, sneaking)
.await;
// if sneaking {
// self.set_pose(EntityPose::Crouching).await;
Expand All @@ -118,31 +121,21 @@ impl Entity {
// }
}

pub async fn set_sprinting(
&mut self,
client: &mut Client,
world: Arc<tokio::sync::Mutex<World>>,
sprinting: bool,
) {
pub async fn set_sprinting(&mut self, client: &mut Client, sprinting: bool) {
assert!(self.sprinting != sprinting);
self.sprinting = sprinting;
self.set_flag(client, world, Self::SPRINTING_FLAG_INDEX, sprinting)
self.set_flag(client, Self::SPRINTING_FLAG_INDEX, sprinting)
.await;
}

pub fn check_fall_flying(&self) -> bool {
!self.on_ground
}

pub async fn set_fall_flying(
&mut self,
client: &mut Client,
world: Arc<tokio::sync::Mutex<World>>,
fall_flying: bool,
) {
pub async fn set_fall_flying(&mut self, client: &mut Client, fall_flying: bool) {
assert!(self.fall_flying != fall_flying);
self.fall_flying = fall_flying;
self.set_flag(client, world, Self::FALL_FLYING_FLAG_INDEX, fall_flying)
self.set_flag(client, Self::FALL_FLYING_FLAG_INDEX, fall_flying)
.await;
}

Expand All @@ -153,13 +146,7 @@ impl Entity {
pub const INVISIBLE_FLAG_INDEX: u32 = 5;
pub const GLOWING_FLAG_INDEX: u32 = 6;
pub const FALL_FLYING_FLAG_INDEX: u32 = 7;
async fn set_flag(
&mut self,
client: &mut Client,
world: Arc<tokio::sync::Mutex<World>>,
index: u32,
value: bool,
) {
async fn set_flag(&mut self, client: &mut Client, index: u32, value: bool) {
let mut b = 0i8;
if value {
b |= 1 << index;
Expand All @@ -168,26 +155,21 @@ impl Entity {
}
let packet = CSetEntityMetadata::new(self.entity_id.into(), Metadata::new(0, 0.into(), b));
client.send_packet(&packet);
world
self.world
.lock()
.await
.broadcast_packet(&[client.token], &packet);
}

pub async fn set_pose(
&mut self,
client: &mut Client,
world: Arc<tokio::sync::Mutex<World>>,
pose: EntityPose,
) {
pub async fn set_pose(&mut self, client: &mut Client, pose: EntityPose) {
self.pose = pose;
let pose = self.pose as i32;
let packet = CSetEntityMetadata::<VarInt>::new(
self.entity_id.into(),
Metadata::new(6, 20.into(), (pose).into()),
);
client.send_packet(&packet);
world
self.world
.lock()
.await
.broadcast_packet(&[client.token], &packet)
Expand Down
Loading

0 comments on commit 39b4cb3

Please sign in to comment.