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

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Nov 16, 2023
1 parent d4d1a93 commit d23b5e9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion minecraft-server/src/entities/monsters/zombies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn zombie_ai_task<T: EntityDescendant + ZombieDescendant>(h: Handler<T
loop {
let msg = server_msg_rcvr.recv().await.unwrap();

if !matches!(&msg, &ServerMessage::Tick) {
if !matches!(&msg, &ServerMessage::Tick(_)) {
continue;
}

Expand Down
35 changes: 32 additions & 3 deletions minecraft-server/src/entities/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Player {
render_distance: i32,
loaded_chunks: HashSet<ChunkColumnPosition>,
center_chunk: ChunkPosition,
packets_sent: usize,
}

impl Player {
Expand All @@ -38,7 +39,7 @@ impl Player {
server_msg_rcvr: BroadcastReceiver<ServerMessage>,
change_receiver: MpscReceiver<WorldChange>
) -> Eid {
let (packet_sender, packet_receiver) = mpsc_channel(100);
let (packet_sender, packet_receiver) = mpsc_channel(1000);
let uuid = player_info.uuid;

let mut player = Player {
Expand Down Expand Up @@ -66,6 +67,7 @@ impl Player {
loaded_chunks: HashSet::new(),

info: player_info,
packets_sent: 0,
};

// TODO: player should load existing entities
Expand Down Expand Up @@ -172,14 +174,28 @@ impl Handler<Player> {

async fn send_packet<'a>(&self, packet: PlayClientbound<'a>) {
let packet = packet.serialize_minecraft_packet().unwrap();
let packets_sent = self.mutate(|player| {
player.packets_sent += 1;
(player.packets_sent, EntityChanges::other())
}).await.unwrap_or(0);
if packets_sent > 500 {
warn!("Many packets sent ({packets_sent})");
}
let Some(packet_sender) = self.observe(|player| player.packet_sender.clone()).await else {return};
packet_sender.send(packet).await.unwrap();
}

async fn on_server_message(self, message: ServerMessage) {
use ServerMessage::*;
match message {
Tick => {
Tick(tick_id) => {
if tick_id % (20*10) == 0 {
self.send_packet(PlayClientbound::KeepAlive { keep_alive_id: tick_id as u64 }).await;
}
self.mutate(|player| {
player.packets_sent = 0;
(player.packets_sent, EntityChanges::other())
}).await;
self.send_packet(PlayClientbound::BundleDelimiter).await;
}
}
Expand Down Expand Up @@ -327,9 +343,22 @@ impl Handler<Player> {
position.y += 20.0;
zombie.get_entity_mut().position = position;
self.world.spawn_entity::<Zombie>(AnyEntity::Zombie(zombie)).await;
debug!("zombie spawned");
} else if message == "stress" {
tokio::spawn(async move {
for i in 0..1000 {
let mut zombie = Zombie::default();
let Some(mut position) = self.observe(|player| player.get_entity().position.clone()).await else {return};
position.y += 20.0;
zombie.get_entity_mut().position = position;
self.world.spawn_entity::<Zombie>(AnyEntity::Zombie(zombie)).await;
tokio::time::sleep(Duration::from_millis(50)).await;
}
});
}
}
RequestPing { payload } => {
self.send_packet(PlayClientbound::Ping { id: payload as i32 }).await;
}
packet => warn!("Unsupported packet received: {packet:?}"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion minecraft-server/src/entities/tasks/newton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub async fn newton_task<T: EntityDescendant>(h: Handler<T>, mut server_msg_rcvr
loop {
let msg = server_msg_rcvr.recv().await.unwrap();

if !matches!(&msg, &ServerMessage::Tick) {
if !matches!(&msg, &ServerMessage::Tick(_)) {
continue;
}

Expand Down
6 changes: 4 additions & 2 deletions minecraft-server/src/server_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::prelude::*;
#[derive(Clone, Debug)]
pub enum ServerMessage {
/// Message indicating a new tick has started
Tick,
Tick(usize),
}

pub struct ServerBehavior {
Expand All @@ -22,10 +22,12 @@ impl ServerBehavior {

// Send ticks to player handlers
tokio::spawn(async move {
let mut tick_id = 0;
let mut tick = tokio::time::interval(Duration::from_millis(50));
loop {
tick.tick().await;
let _ = sender.send(ServerMessage::Tick);
let _ = sender.send(ServerMessage::Tick(tick_id));
tick_id += 1;
}
});

Expand Down
2 changes: 1 addition & 1 deletion minecraft-server/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl World {
let Some(loaders) = loading_manager.get_loaders(position) else {return};
for loader in loaders {
if let Some(sender) = senders.get_mut(loader) {
let _ = sender.send(change.clone()).await;
let _ = sender.try_send(change.clone());
}
}
}
Expand Down

0 comments on commit d23b5e9

Please sign in to comment.