use std::collections::{HashSet, VecDeque};
use std::iter::FusedIterator;
use std::mem;
use std::time::Duration;
pub use bitfield_struct::bitfield;
pub use event::*;
use flume::{Receiver, Sender, TrySendError};
use rayon::iter::ParallelIterator;
use uuid::Uuid;
use vek::Vec3;
use crate::biome::Biome;
use crate::block_pos::BlockPos;
use crate::chunk_pos::ChunkPos;
use crate::config::Config;
use crate::dimension::DimensionId;
use crate::entity::data::Player;
use crate::entity::{
velocity_to_packet_units, Entities, EntityEvent, EntityId, EntityKind, StatusOrAnimation,
};
use crate::player_list::{PlayerListId, PlayerLists};
use crate::player_textures::SignedPlayerTextures;
use crate::protocol::packets::c2s::play::{
C2sPlayPacket, DiggingStatus, InteractKind, PlayerCommandId,
};
pub use crate::protocol::packets::s2c::play::TitleFade;
use crate::protocol::packets::s2c::play::{
BiomeRegistry, ChatTypeRegistry, ChunkLoadDistance, ChunkRenderDistanceCenter, ClearTitles,
DimensionTypeRegistry, DimensionTypeRegistryEntry, Disconnect, EntitiesDestroy,
EntityAnimation, EntityAttributes, EntityAttributesProperty, EntityPosition, EntitySetHeadYaw,
EntityStatus, EntityTrackerUpdate, EntityVelocityUpdate, GameJoin, GameMessage,
GameStateChange, GameStateChangeReason, KeepAlive, MoveRelative, PlayerActionResponse,
PlayerPositionLook, PlayerPositionLookFlags, PlayerRespawn, PlayerSpawnPosition, RegistryCodec,
Rotate, RotateAndMoveRelative, S2cPlayPacket, UnloadChunk, UpdateSubtitle, UpdateTitle,
};
use crate::protocol::{BoundedInt, ByteAngle, NbtBridge, RawBytes, VarInt};
use crate::server::{C2sPacketChannels, NewClientData, S2cPlayMessage, SharedServer};
use crate::slab_versioned::{Key, VersionedSlab};
use crate::text::Text;
use crate::util::{chunks_in_view_distance, is_chunk_in_view_distance};
use crate::world::{WorldId, Worlds};
use crate::{ident, LIBRARY_NAMESPACE};
mod event;
pub struct Clients<C: Config> {
slab: VersionedSlab<Client<C>>,
}
impl<C: Config> Clients<C> {
pub(crate) fn new() -> Self {
Self {
slab: VersionedSlab::new(),
}
}
pub(crate) fn insert(&mut self, client: Client<C>) -> (ClientId, &mut Client<C>) {
let (k, client) = self.slab.insert(client);
(ClientId(k), client)
}
pub fn remove(&mut self, client: ClientId) -> Option<C::ClientState> {
self.slab.remove(client.0).map(|c| c.state)
}
pub fn retain(&mut self, mut f: impl FnMut(ClientId, &mut Client<C>) -> bool) {
self.slab.retain(|k, v| f(ClientId(k), v))
}
pub fn len(&self) -> usize {
self.slab.len()
}
pub fn is_empty(&self) -> bool {
self.slab.len() == 0
}
pub fn get(&self, client: ClientId) -> Option<&Client<C>> {
self.slab.get(client.0)
}
pub fn get_mut(&mut self, client: ClientId) -> Option<&mut Client<C>> {
self.slab.get_mut(client.0)
}
pub fn iter(
&self,
) -> impl ExactSizeIterator<Item = (ClientId, &Client<C>)> + FusedIterator + Clone + '_ {
self.slab.iter().map(|(k, v)| (ClientId(k), v))
}
pub fn iter_mut(
&mut self,
) -> impl ExactSizeIterator<Item = (ClientId, &mut Client<C>)> + FusedIterator + '_ {
self.slab.iter_mut().map(|(k, v)| (ClientId(k), v))
}
pub fn par_iter(&self) -> impl ParallelIterator<Item = (ClientId, &Client<C>)> + Clone + '_ {
self.slab.par_iter().map(|(k, v)| (ClientId(k), v))
}
pub fn par_iter_mut(
&mut self,
) -> impl ParallelIterator<Item = (ClientId, &mut Client<C>)> + '_ {
self.slab.par_iter_mut().map(|(k, v)| (ClientId(k), v))
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct ClientId(Key);
impl ClientId {
pub const NULL: Self = Self(Key::NULL);
}
pub struct Client<C: Config> {
pub state: C::ClientState,
send: SendOpt,
recv: Receiver<C2sPlayPacket>,
uuid: Uuid,
username: String,
textures: Option<SignedPlayerTextures>,
world: WorldId,
old_player_list: Option<PlayerListId>,
new_player_list: Option<PlayerListId>,
position: Vec3<f64>,
old_position: Vec3<f64>,
velocity: Vec3<f32>,
yaw: f32,
pitch: f32,
view_distance: u8,
teleport_id_counter: u32,
pending_teleports: u32,
spawn_position: BlockPos,
spawn_position_yaw: f32,
death_location: Option<(DimensionId, BlockPos)>,
events: VecDeque<ClientEvent>,
last_keepalive_id: i64,
loaded_entities: HashSet<EntityId>,
loaded_chunks: HashSet<ChunkPos>,
new_game_mode: GameMode,
old_game_mode: GameMode,
settings: Option<Settings>,
dug_blocks: Vec<i32>,
msgs_to_send: Vec<Text>,
attack_speed: f64,
movement_speed: f64,
bits: ClientBits,
player_data: Player,
entity_events: Vec<EntityEvent>,
}
#[bitfield(u16)]
struct ClientBits {
spawn: bool,
flat: bool,
teleported_this_tick: bool,
modified_spawn_position: bool,
got_keepalive: bool,
hardcore: bool,
attack_speed_modified: bool,
movement_speed_modified: bool,
velocity_modified: bool,
created_this_tick: bool,
view_distance_modified: bool,
#[bits(5)]
_pad: u8,
}
impl<C: Config> Client<C> {
pub(crate) fn new(
packet_channels: C2sPacketChannels,
ncd: NewClientData,
state: C::ClientState,
) -> Self {
let (send, recv) = packet_channels;
Self {
state,
send: Some(send),
recv,
uuid: ncd.uuid,
username: ncd.username,
textures: ncd.textures,
world: WorldId::default(),
old_player_list: None,
new_player_list: None,
position: Vec3::default(),
old_position: Vec3::default(),
velocity: Vec3::default(),
yaw: 0.0,
pitch: 0.0,
view_distance: 2,
teleport_id_counter: 0,
pending_teleports: 0,
spawn_position: BlockPos::default(),
spawn_position_yaw: 0.0,
death_location: None,
events: VecDeque::new(),
last_keepalive_id: 0,
loaded_entities: HashSet::new(),
loaded_chunks: HashSet::new(),
new_game_mode: GameMode::Survival,
old_game_mode: GameMode::Survival,
settings: None,
dug_blocks: Vec::new(),
msgs_to_send: Vec::new(),
attack_speed: 4.0,
movement_speed: 0.7,
bits: ClientBits::new()
.with_modified_spawn_position(true)
.with_got_keepalive(true)
.with_created_this_tick(true),
player_data: Player::new(),
entity_events: Vec::new(),
}
}
pub fn created_this_tick(&self) -> bool {
self.bits.created_this_tick()
}
pub fn uuid(&self) -> Uuid {
self.uuid
}
pub fn username(&self) -> &str {
&self.username
}
pub fn textures(&self) -> Option<&SignedPlayerTextures> {
self.textures.as_ref()
}
pub fn world(&self) -> WorldId {
self.world
}
pub fn player_list(&self) -> Option<&PlayerListId> {
self.new_player_list.as_ref()
}
pub fn set_player_list(&mut self, id: impl Into<Option<PlayerListId>>) -> Option<PlayerListId> {
mem::replace(&mut self.new_player_list, id.into())
}
pub fn set_flat(&mut self, flat: bool) {
self.bits.set_flat(flat);
}
pub fn is_flat(&self) -> bool {
self.bits.flat()
}
pub fn spawn(&mut self, world: WorldId) {
self.world = world;
self.bits.set_spawn(true);
}
pub fn send_message(&mut self, msg: impl Into<Text>) {
self.msgs_to_send.push(msg.into());
}
pub fn position(&self) -> Vec3<f64> {
self.position
}
pub fn teleport(&mut self, pos: impl Into<Vec3<f64>>, yaw: f32, pitch: f32) {
self.position = pos.into();
self.yaw = yaw;
self.pitch = pitch;
self.bits.set_teleported_this_tick(true);
}
pub fn velocity(&self) -> Vec3<f32> {
self.velocity
}
pub fn set_velocity(&mut self, velocity: impl Into<Vec3<f32>>) {
self.velocity = velocity.into();
self.bits.set_velocity_modified(true);
}
pub fn yaw(&self) -> f32 {
self.yaw
}
pub fn pitch(&self) -> f32 {
self.pitch
}
pub fn spawn_position(&self) -> BlockPos {
self.spawn_position
}
pub fn set_spawn_position(&mut self, pos: impl Into<BlockPos>, yaw_degrees: f32) {
let pos = pos.into();
if pos != self.spawn_position || yaw_degrees != self.spawn_position_yaw {
self.spawn_position = pos;
self.spawn_position_yaw = yaw_degrees;
self.bits.set_modified_spawn_position(true);
}
}
pub fn death_location(&self) -> Option<(DimensionId, BlockPos)> {
self.death_location
}
pub fn set_death_location(&mut self, location: Option<(DimensionId, BlockPos)>) {
self.death_location = location;
}
pub fn game_mode(&self) -> GameMode {
self.new_game_mode
}
pub fn set_game_mode(&mut self, game_mode: GameMode) {
self.new_game_mode = game_mode;
}
pub fn set_title(
&mut self,
title: impl Into<Text>,
subtitle: impl Into<Text>,
animation: impl Into<Option<TitleFade>>,
) {
let title = title.into();
let subtitle = subtitle.into();
self.send_packet(UpdateTitle { text: title });
if !subtitle.is_empty() {
self.send_packet(UpdateSubtitle {
subtitle_text: subtitle,
});
}
if let Some(anim) = animation.into() {
self.send_packet(anim);
}
}
pub fn attack_speed(&self) -> f64 {
self.attack_speed
}
pub fn set_attack_speed(&mut self, speed: f64) {
if self.attack_speed != speed {
self.attack_speed = speed;
self.bits.set_attack_speed_modified(true);
}
}
pub fn movement_speed(&self) -> f64 {
self.movement_speed
}
pub fn set_movement_speed(&mut self, speed: f64) {
if self.movement_speed != speed {
self.movement_speed = speed;
self.bits.set_movement_speed_modified(true);
}
}
pub fn clear_title(&mut self) {
self.send_packet(ClearTitles { reset: true });
}
pub fn is_disconnected(&self) -> bool {
self.send.is_none()
}
pub fn events(
&self,
) -> impl DoubleEndedIterator<Item = &ClientEvent> + ExactSizeIterator + FusedIterator + Clone + '_
{
self.events.iter()
}
pub fn pop_event(&mut self) -> Option<ClientEvent> {
self.events.pop_front()
}
pub fn push_entity_event(&mut self, event: EntityEvent) {
self.entity_events.push(event);
}
pub fn view_distance(&self) -> u8 {
self.view_distance
}
pub fn set_view_distance(&mut self, dist: u8) {
let dist = dist.clamp(2, 32);
if self.view_distance != dist {
self.view_distance = dist;
self.bits.set_view_distance_modified(true);
}
}
pub fn set_hardcore(&mut self, hardcore: bool) {
self.bits.set_hardcore(hardcore);
}
pub fn is_hardcore(&self) -> bool {
self.bits.hardcore()
}
pub fn settings(&self) -> Option<&Settings> {
self.settings.as_ref()
}
pub fn disconnect(&mut self, reason: impl Into<Text>) {
if self.send.is_some() {
let txt = reason.into();
log::info!("disconnecting client '{}': \"{txt}\"", self.username);
self.send_packet(Disconnect { reason: txt });
self.send = None;
}
}
pub fn disconnect_no_reason(&mut self) {
if self.send.is_some() {
log::info!("disconnecting client '{}'", self.username);
self.send = None;
}
}
pub fn player(&self) -> &Player {
&self.player_data
}
pub fn player_mut(&mut self) -> &mut Player {
&mut self.player_data
}
pub fn send_packet(&mut self, packet: impl Into<S2cPlayPacket>) {
send_packet(&mut self.send, packet);
}
pub(crate) fn handle_serverbound_packets(&mut self, entities: &Entities<C>) {
self.events.clear();
for _ in 0..self.recv.len() {
self.handle_serverbound_packet(entities, self.recv.try_recv().unwrap());
}
}
fn handle_serverbound_packet(&mut self, entities: &Entities<C>, pkt: C2sPlayPacket) {
match pkt {
C2sPlayPacket::TeleportConfirm(p) => {
if self.pending_teleports == 0 {
log::warn!("unexpected teleport confirmation from {}", self.username());
self.disconnect_no_reason();
return;
}
let got = p.teleport_id.0 as u32;
let expected = self
.teleport_id_counter
.wrapping_sub(self.pending_teleports);
if got == expected {
self.pending_teleports -= 1;
} else {
log::warn!(
"unexpected teleport ID from {} (expected {expected}, got {got})",
self.username()
);
self.disconnect_no_reason();
}
}
C2sPlayPacket::QueryBlockNbt(_) => {}
C2sPlayPacket::UpdateDifficulty(_) => {}
C2sPlayPacket::MessageAcknowledgment(_) => {}
C2sPlayPacket::CommandExecution(_) => {}
C2sPlayPacket::ChatMessage(p) => self.events.push_back(ClientEvent::ChatMessage {
message: p.message.0,
timestamp: Duration::from_millis(p.timestamp),
}),
C2sPlayPacket::RequestChatPreview(_) => {}
C2sPlayPacket::ClientStatus(_) => {}
C2sPlayPacket::ClientSettings(p) => {
self.events.push_back(ClientEvent::SettingsChanged {
locale: p.locale.0,
view_distance: p.view_distance.0,
chat_mode: p.chat_mode,
chat_colors: p.chat_colors,
main_hand: p.main_hand,
displayed_skin_parts: p.displayed_skin_parts,
allow_server_listings: p.allow_server_listings,
})
}
C2sPlayPacket::RequestCommandCompletion(_) => {}
C2sPlayPacket::ButtonClick(_) => {}
C2sPlayPacket::ClickSlot(_) => {}
C2sPlayPacket::CloseHandledScreen(_) => {}
C2sPlayPacket::CustomPayload(_) => {}
C2sPlayPacket::BookUpdate(_) => {}
C2sPlayPacket::QueryEntityNbt(_) => {}
C2sPlayPacket::PlayerInteractEntity(p) => {
if let Some(id) = entities.get_with_network_id(p.entity_id.0) {
self.events.push_back(ClientEvent::InteractWithEntity {
id,
sneaking: p.sneaking,
kind: match p.kind {
InteractKind::Interact(hand) => InteractWithEntityKind::Interact(hand),
InteractKind::Attack => InteractWithEntityKind::Attack,
InteractKind::InteractAt((target, hand)) => {
InteractWithEntityKind::InteractAt { target, hand }
}
},
});
}
}
C2sPlayPacket::JigsawGenerate(_) => {}
C2sPlayPacket::KeepAlive(p) => {
let last_keepalive_id = self.last_keepalive_id;
if self.bits.got_keepalive() {
log::warn!("unexpected keepalive from player {}", self.username());
self.disconnect_no_reason();
} else if p.id != last_keepalive_id {
log::warn!(
"keepalive ids for player {} don't match (expected {}, got {})",
self.username(),
last_keepalive_id,
p.id
);
self.disconnect_no_reason();
} else {
self.bits.set_got_keepalive(true);
}
}
C2sPlayPacket::UpdateDifficultyLock(_) => {}
C2sPlayPacket::MovePlayerPosition(p) => {
if self.pending_teleports == 0 {
self.position = p.position;
self.events.push_back(ClientEvent::MovePosition {
position: p.position,
on_ground: p.on_ground,
});
}
}
C2sPlayPacket::MovePlayerPositionAndRotation(p) => {
if self.pending_teleports == 0 {
self.position = p.position;
self.yaw = p.yaw;
self.pitch = p.pitch;
self.events.push_back(ClientEvent::MovePositionAndRotation {
position: p.position,
yaw: p.yaw,
pitch: p.pitch,
on_ground: p.on_ground,
});
}
}
C2sPlayPacket::MovePlayerRotation(p) => {
if self.pending_teleports == 0 {
self.yaw = p.yaw;
self.pitch = p.pitch;
self.events.push_back(ClientEvent::MoveRotation {
yaw: p.yaw,
pitch: p.pitch,
on_ground: p.on_ground,
});
}
}
C2sPlayPacket::MovePlayerOnGround(p) => {
if self.pending_teleports == 0 {
self.events.push_back(ClientEvent::MoveOnGround {
on_ground: p.on_ground,
});
}
}
C2sPlayPacket::MoveVehicle(p) => {
if self.pending_teleports == 0 {
self.position = p.position;
self.yaw = p.yaw;
self.pitch = p.pitch;
self.events.push_back(ClientEvent::MoveVehicle {
position: p.position,
yaw: p.yaw,
pitch: p.pitch,
});
}
}
C2sPlayPacket::BoatPaddleState(p) => {
self.events.push_back(ClientEvent::SteerBoat {
left_paddle_turning: p.left_paddle_turning,
right_paddle_turning: p.right_paddle_turning,
});
}
C2sPlayPacket::PickFromInventory(_) => {}
C2sPlayPacket::CraftRequest(_) => {}
C2sPlayPacket::UpdatePlayerAbilities(_) => {}
C2sPlayPacket::PlayerAction(p) => {
if p.sequence.0 != 0 {
self.dug_blocks.push(p.sequence.0);
}
self.events.push_back(match p.status {
DiggingStatus::StartedDigging => ClientEvent::Digging {
status: event::DiggingStatus::Start,
position: p.location,
face: p.face,
},
DiggingStatus::CancelledDigging => ClientEvent::Digging {
status: event::DiggingStatus::Cancel,
position: p.location,
face: p.face,
},
DiggingStatus::FinishedDigging => ClientEvent::Digging {
status: event::DiggingStatus::Finish,
position: p.location,
face: p.face,
},
DiggingStatus::DropItemStack => return,
DiggingStatus::DropItem => return,
DiggingStatus::ShootArrowOrFinishEating => return,
DiggingStatus::SwapItemInHand => return,
});
}
C2sPlayPacket::PlayerCommand(c) => {
self.events.push_back(match c.action_id {
PlayerCommandId::StartSneaking => ClientEvent::StartSneaking,
PlayerCommandId::StopSneaking => ClientEvent::StopSneaking,
PlayerCommandId::LeaveBed => ClientEvent::LeaveBed,
PlayerCommandId::StartSprinting => ClientEvent::StartSprinting,
PlayerCommandId::StopSprinting => ClientEvent::StopSprinting,
PlayerCommandId::StartJumpWithHorse => ClientEvent::StartJumpWithHorse {
jump_boost: c.jump_boost.0 .0 as u8,
},
PlayerCommandId::StopJumpWithHorse => ClientEvent::StopJumpWithHorse,
PlayerCommandId::OpenHorseInventory => ClientEvent::OpenHorseInventory,
PlayerCommandId::StartFlyingWithElytra => ClientEvent::StartFlyingWithElytra,
});
}
C2sPlayPacket::PlayerInput(_) => {}
C2sPlayPacket::PlayPong(_) => {}
C2sPlayPacket::RecipeBookChangeSettings(_) => {}
C2sPlayPacket::RecipeBookSeenRecipe(_) => {}
C2sPlayPacket::RenameItem(_) => {}
C2sPlayPacket::ResourcePackStatus(_) => {}
C2sPlayPacket::AdvancementTab(_) => {}
C2sPlayPacket::SelectMerchantTrade(_) => {}
C2sPlayPacket::UpdateBeacon(_) => {}
C2sPlayPacket::UpdateSelectedSlot(_) => {}
C2sPlayPacket::UpdateCommandBlock(_) => {}
C2sPlayPacket::UpdateCommandBlockMinecart(_) => {}
C2sPlayPacket::UpdateCreativeModeSlot(_) => {}
C2sPlayPacket::UpdateJigsaw(_) => {}
C2sPlayPacket::UpdateStructureBlock(_) => {}
C2sPlayPacket::UpdateSign(_) => {}
C2sPlayPacket::HandSwing(p) => self.events.push_back(ClientEvent::ArmSwing(p.hand)),
C2sPlayPacket::SpectatorTeleport(_) => {}
C2sPlayPacket::PlayerInteractBlock(_) => {}
C2sPlayPacket::PlayerInteractItem(_) => {}
}
}
pub(crate) fn update(
&mut self,
shared: &SharedServer<C>,
entities: &Entities<C>,
worlds: &Worlds<C>,
player_lists: &PlayerLists<C>,
) {
if self.recv.is_disconnected() || self.send.as_ref().map_or(true, |s| s.is_disconnected()) {
self.send = None;
return;
}
let world = match worlds.get(self.world) {
Some(world) => world,
None => {
log::warn!(
"client {} is in an invalid world and must be disconnected",
self.username()
);
self.disconnect_no_reason();
return;
}
};
let current_tick = shared.current_tick();
if self.created_this_tick() {
self.bits.set_spawn(false);
if let Some(id) = &self.new_player_list {
player_lists
.get(id)
.initial_packets(|p| send_packet(&mut self.send, p));
}
let mut dimension_names: Vec<_> = shared
.dimensions()
.map(|(id, _)| ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0))
.collect();
dimension_names.push(ident!("{LIBRARY_NAMESPACE}:dummy_dimension"));
self.send_packet(GameJoin {
entity_id: 0, is_hardcore: self.bits.hardcore(),
gamemode: self.new_game_mode,
previous_gamemode: self.old_game_mode,
dimension_names,
registry_codec: NbtBridge(make_registry_codec(shared)),
dimension_type_name: ident!(
"{LIBRARY_NAMESPACE}:dimension_type_{}",
world.meta.dimension().0
),
dimension_name: ident!(
"{LIBRARY_NAMESPACE}:dimension_{}",
world.meta.dimension().0
),
hashed_seed: 0,
max_players: VarInt(0),
view_distance: BoundedInt(VarInt(self.view_distance() as i32)),
simulation_distance: VarInt(16),
reduced_debug_info: false,
enable_respawn_screen: false,
is_debug: false,
is_flat: self.bits.flat(),
last_death_location: self
.death_location
.map(|(id, pos)| (ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0), pos)),
});
self.teleport(self.position(), self.yaw(), self.pitch());
} else {
if self.bits.spawn() {
self.bits.set_spawn(false);
self.loaded_entities.clear();
self.loaded_chunks.clear();
self.send_packet(PlayerRespawn {
dimension_type_name: ident!("{LIBRARY_NAMESPACE}:dimension_type_0"),
dimension_name: ident!("{LIBRARY_NAMESPACE}:dummy_dimension"),
hashed_seed: 0,
game_mode: self.game_mode(),
previous_game_mode: self.game_mode(),
is_debug: false,
is_flat: self.bits.flat(),
copy_metadata: true,
last_death_location: None,
});
self.send_packet(PlayerRespawn {
dimension_type_name: ident!(
"{LIBRARY_NAMESPACE}:dimension_type_{}",
world.meta.dimension().0
),
dimension_name: ident!(
"{LIBRARY_NAMESPACE}:dimension_{}",
world.meta.dimension().0
),
hashed_seed: 0,
game_mode: self.game_mode(),
previous_game_mode: self.game_mode(),
is_debug: false,
is_flat: self.bits.flat(),
copy_metadata: true,
last_death_location: self
.death_location
.map(|(id, pos)| (ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0), pos)),
});
self.teleport(self.position(), self.yaw(), self.pitch());
}
if self.old_game_mode != self.new_game_mode {
self.old_game_mode = self.new_game_mode;
self.send_packet(GameStateChange {
reason: GameStateChangeReason::ChangeGameMode,
value: self.new_game_mode as i32 as f32,
});
}
if self.old_player_list != self.new_player_list {
if let Some(id) = &self.old_player_list {
player_lists
.get(id)
.clear_packets(|p| send_packet(&mut self.send, p));
}
if let Some(id) = &self.new_player_list {
player_lists
.get(id)
.initial_packets(|p| send_packet(&mut self.send, p));
}
self.old_player_list = self.new_player_list.clone();
} else if let Some(id) = &self.new_player_list {
player_lists
.get(id)
.update_packets(|p| send_packet(&mut self.send, p));
}
}
if self.bits.attack_speed_modified() {
self.bits.set_attack_speed_modified(false);
self.send_packet(EntityAttributes {
entity_id: VarInt(0),
properties: vec![EntityAttributesProperty {
key: ident!("generic.attack_speed"),
value: self.attack_speed,
modifiers: Vec::new(),
}],
});
}
if self.bits.movement_speed_modified() {
self.bits.set_movement_speed_modified(false);
self.send_packet(EntityAttributes {
entity_id: VarInt(0),
properties: vec![EntityAttributesProperty {
key: ident!("generic.movement_speed"),
value: self.movement_speed,
modifiers: Vec::new(),
}],
});
}
if self.bits.modified_spawn_position() {
self.bits.set_modified_spawn_position(false);
self.send_packet(PlayerSpawnPosition {
location: self.spawn_position,
angle: self.spawn_position_yaw,
})
}
if self.bits.view_distance_modified() {
self.bits.set_view_distance_modified(false);
if !self.created_this_tick() {
self.send_packet(ChunkLoadDistance {
view_distance: BoundedInt(VarInt(self.view_distance() as i32)),
});
}
}
if current_tick % (shared.tick_rate() * 8) == 0 {
if self.bits.got_keepalive() {
let id = rand::random();
self.send_packet(KeepAlive { id });
self.last_keepalive_id = id;
self.bits.set_got_keepalive(false);
} else {
log::warn!(
"player {} timed out (no keepalive response)",
self.username()
);
self.disconnect_no_reason();
}
}
let center = ChunkPos::at(self.position.x, self.position.z);
{
let old_section = self.old_position.map(|n| (n / 16.0).floor() as i32);
let new_section = self.position.map(|n| (n / 16.0).floor() as i32);
if old_section != new_section {
self.send_packet(ChunkRenderDistanceCenter {
chunk_x: VarInt(new_section.x),
chunk_z: VarInt(new_section.z),
})
}
}
let dimension = shared.dimension(world.meta.dimension());
self.loaded_chunks.retain(|&pos| {
let cache = 2;
if let Some(chunk) = world.chunks.get(pos) {
if is_chunk_in_view_distance(center, pos, self.view_distance + cache)
&& !chunk.created_this_tick()
{
chunk.block_change_packets(pos, dimension.min_y, |pkt| {
send_packet(&mut self.send, pkt)
});
return true;
}
}
send_packet(
&mut self.send,
UnloadChunk {
chunk_x: pos.x,
chunk_z: pos.z,
},
);
false
});
for pos in chunks_in_view_distance(center, self.view_distance) {
if let Some(chunk) = world.chunks.get(pos) {
if self.loaded_chunks.insert(pos) {
self.send_packet(chunk.chunk_data_packet(pos));
chunk.block_change_packets(pos, dimension.min_y, |pkt| self.send_packet(pkt));
}
}
}
for seq in self.dug_blocks.drain(..) {
send_packet(
&mut self.send,
PlayerActionResponse {
sequence: VarInt(seq),
},
)
}
if self.bits.teleported_this_tick() {
self.bits.set_teleported_this_tick(false);
self.send_packet(PlayerPositionLook {
position: self.position,
yaw: self.yaw,
pitch: self.pitch,
flags: PlayerPositionLookFlags::new(false, false, false, false, false),
teleport_id: VarInt(self.teleport_id_counter as i32),
dismount_vehicle: false,
});
self.pending_teleports = self.pending_teleports.wrapping_add(1);
if self.pending_teleports == 0 {
log::warn!("too many pending teleports for {}", self.username());
self.disconnect_no_reason();
return;
}
self.teleport_id_counter = self.teleport_id_counter.wrapping_add(1);
}
if self.bits.velocity_modified() {
self.bits.set_velocity_modified(false);
self.send_packet(EntityVelocityUpdate {
entity_id: VarInt(0),
velocity: velocity_to_packet_units(self.velocity),
});
}
for msg in self.msgs_to_send.drain(..) {
send_packet(
&mut self.send,
GameMessage {
chat: msg,
kind: VarInt(0),
},
);
}
let mut entities_to_unload = Vec::new();
self.loaded_entities.retain(|&id| {
if let Some(entity) = entities.get(id) {
debug_assert!(entity.kind() != EntityKind::Marker);
if self.position.distance(entity.position()) <= self.view_distance as f64 * 16.0 {
if let Some(meta) = entity.updated_tracked_data_packet(id) {
send_packet(&mut self.send, meta);
}
let position_delta = entity.position() - entity.old_position();
let needs_teleport = position_delta.map(f64::abs).reduce_partial_max() >= 8.0;
let flags = entity.bits();
if entity.position() != entity.old_position()
&& !needs_teleport
&& flags.yaw_or_pitch_modified()
{
send_packet(
&mut self.send,
RotateAndMoveRelative {
entity_id: VarInt(id.to_network_id()),
delta: (position_delta * 4096.0).as_(),
yaw: ByteAngle::from_degrees(entity.yaw()),
pitch: ByteAngle::from_degrees(entity.pitch()),
on_ground: entity.on_ground(),
},
);
} else {
if entity.position() != entity.old_position() && !needs_teleport {
send_packet(
&mut self.send,
MoveRelative {
entity_id: VarInt(id.to_network_id()),
delta: (position_delta * 4096.0).as_(),
on_ground: entity.on_ground(),
},
);
}
if flags.yaw_or_pitch_modified() {
send_packet(
&mut self.send,
Rotate {
entity_id: VarInt(id.to_network_id()),
yaw: ByteAngle::from_degrees(entity.yaw()),
pitch: ByteAngle::from_degrees(entity.pitch()),
on_ground: entity.on_ground(),
},
);
}
}
if needs_teleport {
send_packet(
&mut self.send,
EntityPosition {
entity_id: VarInt(id.to_network_id()),
position: entity.position(),
yaw: ByteAngle::from_degrees(entity.yaw()),
pitch: ByteAngle::from_degrees(entity.pitch()),
on_ground: entity.on_ground(),
},
);
}
if flags.velocity_modified() {
send_packet(
&mut self.send,
EntityVelocityUpdate {
entity_id: VarInt(id.to_network_id()),
velocity: velocity_to_packet_units(entity.velocity()),
},
);
}
if flags.head_yaw_modified() {
send_packet(
&mut self.send,
EntitySetHeadYaw {
entity_id: VarInt(id.to_network_id()),
head_yaw: ByteAngle::from_degrees(entity.head_yaw()),
},
)
}
send_entity_events(&mut self.send, id.to_network_id(), entity.events());
return true;
}
}
entities_to_unload.push(VarInt(id.to_network_id()));
false
});
if !entities_to_unload.is_empty() {
self.send_packet(EntitiesDestroy {
entities: entities_to_unload,
});
}
let mut data = Vec::new();
self.player_data.updated_tracked_data(&mut data);
if !data.is_empty() {
data.push(0xff);
self.send_packet(EntityTrackerUpdate {
entity_id: VarInt(0),
metadata: RawBytes(data),
});
}
let pos = self.position();
let view_dist = self.view_distance;
world.spatial_index.query::<_, _, ()>(
|bb| bb.projected_point(pos).distance(pos) <= view_dist as f64 * 16.0,
|id, _| {
let entity = entities
.get(id)
.expect("entity IDs in spatial index should be valid at this point");
if entity.kind() != EntityKind::Marker
&& entity.uuid() != self.uuid
&& self.loaded_entities.insert(id)
{
self.send_packet(
entity
.spawn_packet(id)
.expect("should not be a marker entity"),
);
if let Some(meta) = entity.initial_tracked_data_packet(id) {
self.send_packet(meta);
}
send_entity_events(&mut self.send, id.to_network_id(), entity.events());
}
None
},
);
send_entity_events(&mut self.send, 0, &self.entity_events);
self.entity_events.clear();
self.player_data.clear_modifications();
self.old_position = self.position;
self.bits.set_created_this_tick(false);
send_packet(&mut self.send, S2cPlayMessage::Flush);
}
}
type SendOpt = Option<Sender<S2cPlayMessage>>;
fn send_packet(send_opt: &mut SendOpt, pkt: impl Into<S2cPlayMessage>) {
if let Some(send) = send_opt {
match send.try_send(pkt.into()) {
Err(TrySendError::Full(_)) => {
log::warn!("max outbound packet capacity reached for client");
*send_opt = None;
}
Err(TrySendError::Disconnected(_)) => {
*send_opt = None;
}
Ok(_) => {}
}
}
}
fn send_entity_events(send_opt: &mut SendOpt, entity_id: i32, events: &[EntityEvent]) {
for &event in events {
match event.status_or_animation() {
StatusOrAnimation::Status(code) => send_packet(
send_opt,
EntityStatus {
entity_id,
entity_status: code,
},
),
StatusOrAnimation::Animation(code) => send_packet(
send_opt,
EntityAnimation {
entity_id: VarInt(entity_id),
animation: code,
},
),
}
}
}
fn make_registry_codec<C: Config>(shared: &SharedServer<C>) -> RegistryCodec {
let mut dims = Vec::new();
for (id, dim) in shared.dimensions() {
let id = id.0 as i32;
dims.push(DimensionTypeRegistryEntry {
name: ident!("{LIBRARY_NAMESPACE}:dimension_type_{id}"),
id,
element: dim.to_dimension_registry_item(),
})
}
let mut biomes: Vec<_> = shared
.biomes()
.map(|(id, biome)| biome.to_biome_registry_item(id.0 as i32))
.collect();
if !biomes.iter().any(|b| b.name == ident!("plains")) {
let biome = Biome::default();
assert_eq!(biome.name, ident!("plains"));
biomes.push(biome.to_biome_registry_item(biomes.len() as i32));
}
RegistryCodec {
dimension_type_registry: DimensionTypeRegistry {
kind: ident!("dimension_type"),
value: dims,
},
biome_registry: BiomeRegistry {
kind: ident!("worldgen/biome"),
value: biomes,
},
chat_type_registry: ChatTypeRegistry {
kind: ident!("chat_type"),
value: Vec::new(),
},
}
}