Skip to content

Commit 2b6dcf9

Browse files
committed
provide chunk's packet buffer for entities tick, update_position and update_rotation functions
1 parent 6be7be4 commit 2b6dcf9

File tree

5 files changed

+66
-74
lines changed

5 files changed

+66
-74
lines changed

server/src/entity/entity.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::entity::entity_appearance::EntityAppearance;
2+
use crate::network::packets::packet_buffer::PacketBuffer;
23
use crate::network::protocol::play::clientbound::DestroyEntites;
34
use crate::network::protocol::play::serverbound::EntityInteractionType;
5+
use crate::world::chunk::get_chunk_position;
46
use crate::{Player, World, WorldExtension};
57
use glam::DVec3;
68
use std::ptr::NonNull;
@@ -9,7 +11,7 @@ pub type EntityId = i32;
911

1012
#[allow(unused_variables)]
1113
pub trait EntityExtension<W : WorldExtension> {
12-
fn tick(&mut self, entity: &mut EntityBase<W>);
14+
fn tick(&mut self, entity: &mut EntityBase<W>, chunk_buffer: &mut PacketBuffer);
1315

1416
fn interact(
1517
&mut self,
@@ -86,14 +88,21 @@ impl<W : WorldExtension> Entity<W> {
8688
pub fn tick(&mut self) {
8789
let base = &mut self.base;
8890
base.ticks_existed += 1;
89-
self.extension.tick(base);
91+
92+
let (chunk_x, chunk_z) = get_chunk_position(base.position);
93+
let Some(chunk) = base.world_mut().chunk_grid.get_chunk_mut(chunk_x, chunk_z) else {
94+
// maybe should allow ticking if not in chunk
95+
return;
96+
};
97+
98+
self.extension.tick(base, &mut chunk.packet_buffer);
9099

91100
if base.position != base.last_position {
92-
self.appearance.update_position(base);
101+
self.appearance.update_position(base, &mut chunk.packet_buffer);
93102
base.last_position = base.position;
94103
}
95104
if base.yaw != base.last_yaw || base.pitch != base.last_pitch {
96-
self.appearance.update_rotation(base);
105+
self.appearance.update_rotation(base, &mut chunk.packet_buffer);
97106
base.last_yaw = base.yaw;
98107
base.last_pitch = base.pitch;
99108
}

server/src/entity/entity_appearance.rs

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::constants::EntityVariant;
22
use crate::entity::entity::EntityBase;
33
use crate::entity::entity_metadata::{EntityMetadata, PlayerMetadata};
44
use crate::network::binary::var_int::VarInt;
5+
use crate::network::packets::packet_buffer::PacketBuffer;
56
use crate::network::protocol::play::clientbound::{DestroyEntites, EntityRotate, EntityTeleport, EntityYawRotate, PlayerData, PlayerListItem, SpawnMob, SpawnPlayer};
6-
use crate::world::chunk::get_chunk_position;
77
use crate::{GameProfile, GameProfileProperty, Player, WorldExtension};
88
use fstr::FString;
99
use std::collections::HashMap;
@@ -18,9 +18,9 @@ pub trait EntityAppearance<W: WorldExtension> {
1818

1919
fn leave_player_view(&self, entity: &mut EntityBase<W>, player: &mut Player<W::Player>);
2020

21-
fn update_position(&self, entity: &mut EntityBase<W>);
21+
fn update_position(&self, entity: &mut EntityBase<W>, chunk_buffer: &mut PacketBuffer);
2222

23-
fn update_rotation(&self, entity: &mut EntityBase<W>);
23+
fn update_rotation(&self, entity: &mut EntityBase<W>, chunk_buffer: &mut PacketBuffer);
2424
}
2525

2626
pub struct MobAppearance {
@@ -62,35 +62,29 @@ impl<W: WorldExtension> EntityAppearance<W> for MobAppearance {
6262
})
6363
}
6464

65-
fn update_position(&self, entity: &mut EntityBase<W>) {
66-
let (chunk_x, chunk_z) = get_chunk_position(entity.position);
67-
if let Some(chunk) = entity.world_mut().chunk_grid.get_chunk_mut(chunk_x, chunk_z) {
68-
chunk.packet_buffer.write_packet(&EntityTeleport {
69-
entity_id: entity.id,
70-
pos_x: entity.position.x,
71-
pos_y: entity.position.y,
72-
pos_z: entity.position.z,
73-
yaw: entity.yaw,
74-
pitch: entity.pitch,
75-
on_ground: false,
76-
})
77-
}
65+
fn update_position(&self, entity: &mut EntityBase<W>, packet_buffer: &mut PacketBuffer) {
66+
packet_buffer.write_packet(&EntityTeleport {
67+
entity_id: entity.id,
68+
pos_x: entity.position.x,
69+
pos_y: entity.position.y,
70+
pos_z: entity.position.z,
71+
yaw: entity.yaw,
72+
pitch: entity.pitch,
73+
on_ground: false,
74+
})
7875
}
7976

80-
fn update_rotation(&self, entity: &mut EntityBase<W>) {
81-
let (chunk_x, chunk_z) = get_chunk_position(entity.position);
82-
if let Some(chunk) = entity.world_mut().chunk_grid.get_chunk_mut(chunk_x, chunk_z) {
83-
chunk.packet_buffer.write_packet(&EntityRotate {
84-
entity_id: entity.id,
85-
yaw: entity.yaw,
86-
pitch: entity.pitch,
87-
on_ground: false,
88-
});
89-
chunk.packet_buffer.write_packet(&EntityYawRotate {
90-
entity_id: entity.id,
91-
yaw: entity.yaw,
92-
});
93-
}
77+
fn update_rotation(&self, entity: &mut EntityBase<W>, packet_buffer: &mut PacketBuffer) {
78+
packet_buffer.write_packet(&EntityRotate {
79+
entity_id: entity.id,
80+
yaw: entity.yaw,
81+
pitch: entity.pitch,
82+
on_ground: false,
83+
});
84+
packet_buffer.write_packet(&EntityYawRotate {
85+
entity_id: entity.id,
86+
yaw: entity.yaw,
87+
});
9488
}
9589
}
9690

@@ -112,6 +106,7 @@ impl PlayerAppearance {
112106

113107
impl<W: WorldExtension> EntityAppearance<W> for PlayerAppearance {
114108
fn initialize(&self, _: &mut EntityBase<W>) {}
109+
115110
fn destroy(&self, entity: &mut EntityBase<W>, packet: &mut DestroyEntites) {
116111
packet.entities.push(VarInt(entity.id))
117112
}
@@ -161,34 +156,28 @@ impl<W: WorldExtension> EntityAppearance<W> for PlayerAppearance {
161156
})
162157
}
163158

164-
fn update_position(&self, entity: &mut EntityBase<W>) {
165-
let (chunk_x, chunk_z) = get_chunk_position(entity.position);
166-
if let Some(chunk) = entity.world_mut().chunk_grid.get_chunk_mut(chunk_x, chunk_z) {
167-
chunk.packet_buffer.write_packet(&EntityTeleport {
168-
entity_id: entity.id,
169-
pos_x: entity.position.x,
170-
pos_y: entity.position.y,
171-
pos_z: entity.position.z,
172-
yaw: entity.yaw,
173-
pitch: entity.pitch,
174-
on_ground: false,
175-
})
176-
}
159+
fn update_position(&self, entity: &mut EntityBase<W>, packet_buffer: &mut PacketBuffer) {
160+
packet_buffer.write_packet(&EntityTeleport {
161+
entity_id: entity.id,
162+
pos_x: entity.position.x,
163+
pos_y: entity.position.y,
164+
pos_z: entity.position.z,
165+
yaw: entity.yaw,
166+
pitch: entity.pitch,
167+
on_ground: false,
168+
})
177169
}
178170

179-
fn update_rotation(&self, entity: &mut EntityBase<W>) {
180-
let (chunk_x, chunk_z) = get_chunk_position(entity.position);
181-
if let Some(chunk) = entity.world_mut().chunk_grid.get_chunk_mut(chunk_x, chunk_z) {
182-
chunk.packet_buffer.write_packet(&EntityRotate {
183-
entity_id: entity.id,
184-
yaw: entity.yaw,
185-
pitch: entity.pitch,
186-
on_ground: false,
187-
});
188-
chunk.packet_buffer.write_packet(&EntityYawRotate {
189-
entity_id: entity.id,
190-
yaw: entity.yaw,
191-
});
192-
}
171+
fn update_rotation(&self, entity: &mut EntityBase<W>, packet_buffer: &mut PacketBuffer) {
172+
packet_buffer.write_packet(&EntityRotate {
173+
entity_id: entity.id,
174+
yaw: entity.yaw,
175+
pitch: entity.pitch,
176+
on_ground: false,
177+
});
178+
packet_buffer.write_packet(&EntityYawRotate {
179+
entity_id: entity.id,
180+
yaw: entity.yaw,
181+
});
193182
}
194183
}

src/dungeon/door/door_entity.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use server::entity::entity::{EntityBase, EntityExtension};
77
use server::entity::entity_appearance::EntityAppearance;
88
use server::entity::entity_metadata::EntityMetadata;
99
use server::network::binary::var_int::VarInt;
10+
use server::network::packets::packet_buffer::PacketBuffer;
1011
use server::network::protocol::play::clientbound::{DestroyEntites, EntityAttach, EntityRelativeMove, SpawnMob, SpawnObject};
11-
use server::world::chunk::get_chunk_position;
1212
use server::Player;
1313

1414
pub(super) struct DoorEntityAppearance {
@@ -92,17 +92,12 @@ impl EntityAppearance<Dungeon> for DoorEntityAppearance {
9292
})
9393
}
9494

95-
fn update_position(&self, entity: &mut EntityBase<Dungeon>) {
96-
let (chunk_x, chunk_z) = get_chunk_position(entity.position);
97-
let Some(chunk) = entity.world_mut().chunk_grid.get_chunk_mut(chunk_x, chunk_z) else {
98-
return;
99-
};
100-
95+
fn update_position(&self, entity: &mut EntityBase<Dungeon>, packet_buffer: &mut PacketBuffer) {
10196
// only y can be updated
10297
let difference = entity.position.y - entity.last_position.y;
10398

10499
for entity_id in (entity.id..entity.id + 72).step_by(2) {
105-
chunk.packet_buffer.write_packet(&EntityRelativeMove {
100+
packet_buffer.write_packet(&EntityRelativeMove {
106101
entity_id,
107102
pos_x: 0.0,
108103
pos_y: difference,
@@ -112,14 +107,14 @@ impl EntityAppearance<Dungeon> for DoorEntityAppearance {
112107
}
113108
}
114109

115-
fn update_rotation(&self, _: &mut EntityBase<Dungeon>) {}
110+
fn update_rotation(&self, _: &mut EntityBase<Dungeon>, _: &mut PacketBuffer) {}
116111
}
117112

118113
pub(super) struct DoorEntityExtension;
119114

120115
impl EntityExtension<Dungeon> for DoorEntityExtension {
121116

122-
fn tick(&mut self, entity: &mut EntityBase<Dungeon>) {
117+
fn tick(&mut self, entity: &mut EntityBase<Dungeon>, _: &mut PacketBuffer) {
123118
entity.position.y -= 0.25;
124119

125120
if entity.ticks_existed == 20 {

src/dungeon/entities/npc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::dungeon::dungeon::Dungeon;
22
use crate::dungeon::dungeon_player::DungeonPlayer;
33
use server::entity::entity::{EntityBase, EntityExtension};
4+
use server::network::packets::packet_buffer::PacketBuffer;
45
use server::network::protocol::play::serverbound::EntityInteractionType;
56
use server::Player;
67

@@ -12,7 +13,7 @@ pub struct InteractableNPC {
1213

1314
impl EntityExtension<Dungeon> for InteractableNPC {
1415

15-
fn tick(&mut self, entity: &mut EntityBase<Dungeon>) {
16+
fn tick(&mut self, entity: &mut EntityBase<Dungeon>, _: &mut PacketBuffer) {
1617
if entity.ticks_existed.is_multiple_of(5) {
1718
return;
1819
}

src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ pub fn initialize_world(tx: Sender<NetworkThreadMessage>) -> anyhow::Result<Worl
5151
}
5252

5353
pub fn spawn_mort(world: &mut World<Dungeon>) {
54-
// spawn mort
5554
let entrance = world.extension.entrance_room();
5655
let entrance = entrance.borrow();
5756
let mut position = entrance.get_world_block_position(ivec3(15, 69, 4)).as_dvec3();
@@ -79,7 +78,6 @@ pub fn spawn_mort(world: &mut World<Dungeon>) {
7978
player.open_container(OpenContainer::Menu(Box::new(MortMenu {})))
8079
},
8180
}
82-
8381
);
8482
}
8583

0 commit comments

Comments
 (0)