2022-07-15 16:18:20 +10:00
|
|
|
//! Connections to the server after logging in.
|
|
|
|
|
2022-06-25 09:11:15 +10:00
|
|
|
/// Contains the [`Event`] enum and related data types.
|
|
|
|
mod event;
|
2022-07-03 04:22:28 +10:00
|
|
|
use std::collections::{HashSet, VecDeque};
|
2022-04-29 17:48:41 +10:00
|
|
|
use std::iter::FusedIterator;
|
2022-06-30 14:33:42 +10:00
|
|
|
use std::time::Duration;
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-07-05 08:51:28 +10:00
|
|
|
use bitfield_struct::bitfield;
|
2022-06-25 09:11:15 +10:00
|
|
|
pub use event::*;
|
2022-04-15 07:55:45 +10:00
|
|
|
use flume::{Receiver, Sender, TrySendError};
|
2022-04-29 17:48:41 +10:00
|
|
|
use rayon::iter::ParallelIterator;
|
2022-05-16 19:36:14 +10:00
|
|
|
use uuid::Uuid;
|
2022-05-16 21:09:51 +10:00
|
|
|
use vek::Vec3;
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
use crate::biome::Biome;
|
2022-07-07 11:27:59 +10:00
|
|
|
use crate::block_pos::BlockPos;
|
|
|
|
use crate::chunk_pos::ChunkPos;
|
2022-07-16 13:40:39 +10:00
|
|
|
use crate::config::Config;
|
2022-07-11 22:08:02 +10:00
|
|
|
use crate::dimension::DimensionId;
|
|
|
|
use crate::entity::types::Player;
|
2022-07-07 11:27:59 +10:00
|
|
|
use crate::entity::{velocity_to_packet_units, Entities, Entity, EntityId, EntityKind};
|
2022-07-01 04:53:57 +10:00
|
|
|
use crate::player_textures::SignedPlayerTextures;
|
2022-07-11 22:08:02 +10:00
|
|
|
use crate::protocol_inner::packets::play::c2s::{
|
2022-07-06 12:21:52 +10:00
|
|
|
C2sPlayPacket, DiggingStatus, InteractKind, PlayerCommandId,
|
2022-07-05 08:51:28 +10:00
|
|
|
};
|
2022-07-11 22:08:02 +10:00
|
|
|
pub use crate::protocol_inner::packets::play::s2c::SetTitleAnimationTimes as TitleAnimationTimes;
|
|
|
|
use crate::protocol_inner::packets::play::s2c::{
|
|
|
|
Animate, BiomeRegistry, BlockChangeAck, ChatType, ChatTypeChat, ChatTypeNarration,
|
|
|
|
ChatTypeRegistry, ChatTypeRegistryEntry, ClearTitles, DimensionTypeRegistry,
|
|
|
|
DimensionTypeRegistryEntry, Disconnect, EntityEvent, ForgetLevelChunk, GameEvent,
|
|
|
|
GameEventReason, KeepAlive, Login, MoveEntityPosition, MoveEntityPositionAndRotation,
|
|
|
|
MoveEntityRotation, PlayerPosition, PlayerPositionFlags, RegistryCodec, RemoveEntities,
|
|
|
|
Respawn, RotateHead, S2cPlayPacket, SetChunkCacheCenter, SetChunkCacheRadius,
|
|
|
|
SetEntityMetadata, SetEntityMotion, SetSubtitleText, SetTitleText, SpawnPosition, SystemChat,
|
2022-07-13 13:47:53 +10:00
|
|
|
TeleportEntity, UpdateAttributes, UpdateAttributesProperty, ENTITY_EVENT_MAX_BOUND,
|
2022-04-15 07:55:45 +10:00
|
|
|
};
|
2022-07-11 22:08:02 +10:00
|
|
|
use crate::protocol_inner::{BoundedInt, ByteAngle, Nbt, RawBytes, VarInt};
|
2022-07-07 11:27:59 +10:00
|
|
|
use crate::server::{C2sPacketChannels, NewClientData, SharedServer};
|
2022-04-29 17:48:41 +10:00
|
|
|
use crate::slotmap::{Key, SlotMap};
|
2022-07-07 11:27:59 +10:00
|
|
|
use crate::text::Text;
|
2022-04-15 07:55:45 +10:00
|
|
|
use crate::util::{chunks_in_view_distance, is_chunk_in_view_distance};
|
2022-07-07 11:27:59 +10:00
|
|
|
use crate::world::{WorldId, Worlds};
|
2022-07-18 14:29:44 +10:00
|
|
|
use crate::{ident, Ticks, LIBRARY_NAMESPACE, STANDARD_TPS};
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// A container for all [`Client`]s on a [`Server`](crate::server::Server).
|
|
|
|
///
|
|
|
|
/// New clients are automatically inserted into this container but
|
|
|
|
/// are not automatically deleted. It is your responsibility to delete them once
|
|
|
|
/// they disconnect. This can be checked with [`Client::is_disconnected`].
|
2022-07-16 13:40:39 +10:00
|
|
|
pub struct Clients<C: Config> {
|
|
|
|
sm: SlotMap<Client<C>>,
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
impl<C: Config> Clients<C> {
|
2022-04-29 17:48:41 +10:00
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
Self { sm: SlotMap::new() }
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
pub(crate) fn insert(&mut self, client: Client<C>) -> (ClientId, &mut Client<C>) {
|
2022-06-30 04:09:00 +10:00
|
|
|
let (id, client) = self.sm.insert(client);
|
|
|
|
(ClientId(id), client)
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Removes a client from the server.
|
|
|
|
///
|
|
|
|
/// If the given client ID is valid, `true` is returned and the client is
|
|
|
|
/// deleted. Otherwise, `false` is returned and the function has no effect.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn delete(&mut self, client: ClientId) -> bool {
|
|
|
|
self.sm.remove(client.0).is_some()
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Deletes all clients from the server (as if by [`Self::delete`]) for
|
|
|
|
/// which `f` returns `true`.
|
|
|
|
///
|
|
|
|
/// All clients are visited in an unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn retain(&mut self, mut f: impl FnMut(ClientId, &mut Client<C>) -> bool) {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.retain(|k, v| f(ClientId(k), v))
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns the number of clients on the server. This includes clients
|
|
|
|
/// which may be disconnected.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
self.sm.len()
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns a shared reference to the client with the given ID. If
|
|
|
|
/// the ID is invalid, then `None` is returned.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn get(&self, client: ClientId) -> Option<&Client<C>> {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.get(client.0)
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns an exclusive reference to the client with the given ID. If the
|
|
|
|
/// ID is invalid, then `None` is returned.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn get_mut(&mut self, client: ClientId) -> Option<&mut Client<C>> {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.get_mut(client.0)
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns an immutable iterator over all clients on the server in an
|
|
|
|
/// unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn iter(&self) -> impl FusedIterator<Item = (ClientId, &Client<C>)> + Clone + '_ {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.iter().map(|(k, v)| (ClientId(k), v))
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns a mutable iterator over all clients on the server in an
|
|
|
|
/// unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn iter_mut(&mut self) -> impl FusedIterator<Item = (ClientId, &mut Client<C>)> + '_ {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.iter_mut().map(|(k, v)| (ClientId(k), v))
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns a parallel immutable iterator over all clients on the server in
|
|
|
|
/// an unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn par_iter(&self) -> impl ParallelIterator<Item = (ClientId, &Client<C>)> + Clone + '_ {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.par_iter().map(|(k, v)| (ClientId(k), v))
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns a parallel mutable iterator over all clients on the server in an
|
|
|
|
/// unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn par_iter_mut(
|
|
|
|
&mut self,
|
|
|
|
) -> impl ParallelIterator<Item = (ClientId, &mut Client<C>)> + '_ {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.par_iter_mut().map(|(k, v)| (ClientId(k), v))
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
|
|
|
}
|
2022-06-30 04:09:00 +10:00
|
|
|
|
2022-07-15 16:18:20 +10:00
|
|
|
/// An identifier for a [`Client`] on the server.
|
2022-07-11 22:08:02 +10:00
|
|
|
///
|
|
|
|
/// Client IDs are either _valid_ or _invalid_. Valid client IDs point to
|
|
|
|
/// clients that have not been deleted, while invalid IDs point to those that
|
|
|
|
/// have. Once an ID becomes invalid, it will never become valid again.
|
|
|
|
///
|
|
|
|
/// The [`Ord`] instance on this type is correct but otherwise unspecified. This
|
|
|
|
/// is useful for storing IDs in containers such as
|
|
|
|
/// [`BTreeMap`](std::collections::BTreeMap).
|
2022-07-04 11:02:00 +10:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
|
2022-04-29 17:48:41 +10:00
|
|
|
pub struct ClientId(Key);
|
|
|
|
|
2022-07-04 11:02:00 +10:00
|
|
|
impl ClientId {
|
2022-07-11 22:08:02 +10:00
|
|
|
/// The value of the default client ID which is always invalid.
|
2022-07-04 11:02:00 +10:00
|
|
|
pub const NULL: Self = Self(Key::NULL);
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Represents a remote connection to a client after successfully logging in.
|
|
|
|
///
|
|
|
|
/// Much like an [`Entity`], clients posess a location, rotation, and UUID.
|
2022-07-15 16:18:20 +10:00
|
|
|
/// However, clients are handled separately from entities and are partially
|
|
|
|
/// managed by the library.
|
2022-07-11 22:08:02 +10:00
|
|
|
///
|
|
|
|
/// By default, clients have no influence over the worlds they reside in. They
|
|
|
|
/// cannot break blocks, hurt entities, or see other clients. Interactions with
|
|
|
|
/// the server must be handled explicitly with [`Self::pop_event`].
|
|
|
|
///
|
|
|
|
/// Additionally, clients posess [`Player`] entity data which is only visible to
|
|
|
|
/// themselves. This can be accessed with [`Self::player`] and
|
|
|
|
/// [`Self::player_mut`].
|
|
|
|
///
|
|
|
|
/// # The Difference Between a "Client" and a "Player"
|
|
|
|
///
|
|
|
|
/// Normally in Minecraft, players and clients are one and the same. Players are
|
|
|
|
/// simply a special type of entity which is backed by a remote connection.
|
|
|
|
///
|
|
|
|
/// In Valence however, clients and players have been decoupled. This separation
|
|
|
|
/// was done primarily to enable multithreaded client updates.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub struct Client<C: Config> {
|
|
|
|
/// Custom data.
|
|
|
|
pub data: C::ClientData,
|
2022-04-15 07:55:45 +10:00
|
|
|
/// Setting this to `None` disconnects the client.
|
2022-07-06 11:08:40 +10:00
|
|
|
send: SendOpt,
|
2022-06-10 13:26:21 +10:00
|
|
|
recv: Receiver<C2sPlayPacket>,
|
2022-04-15 07:55:45 +10:00
|
|
|
/// The tick this client was created.
|
|
|
|
created_tick: Ticks,
|
2022-05-16 19:36:14 +10:00
|
|
|
uuid: Uuid,
|
2022-06-29 11:29:29 +10:00
|
|
|
username: String,
|
|
|
|
textures: Option<SignedPlayerTextures>,
|
2022-07-04 11:45:11 +10:00
|
|
|
world: WorldId,
|
2022-05-16 21:09:51 +10:00
|
|
|
new_position: Vec3<f64>,
|
|
|
|
old_position: Vec3<f64>,
|
2022-07-18 14:29:44 +10:00
|
|
|
/// Measured in m/s.
|
|
|
|
velocity: Vec3<f32>,
|
2022-04-15 07:55:45 +10:00
|
|
|
/// Measured in degrees
|
|
|
|
yaw: f32,
|
|
|
|
/// Measured in degrees
|
|
|
|
pitch: f32,
|
|
|
|
/// Counts up as teleports are made.
|
|
|
|
teleport_id_counter: u32,
|
|
|
|
/// The number of pending client teleports that have yet to receive a
|
|
|
|
/// confirmation. Inbound client position packets are ignored while this
|
|
|
|
/// is nonzero.
|
|
|
|
pending_teleports: u32,
|
|
|
|
spawn_position: BlockPos,
|
|
|
|
spawn_position_yaw: f32,
|
2022-06-22 07:14:16 +10:00
|
|
|
death_location: Option<(DimensionId, BlockPos)>,
|
2022-07-11 22:08:02 +10:00
|
|
|
events: VecDeque<Event>,
|
2022-04-15 07:55:45 +10:00
|
|
|
/// The ID of the last keepalive sent.
|
|
|
|
last_keepalive_id: i64,
|
|
|
|
new_max_view_distance: u8,
|
2022-04-29 17:48:41 +10:00
|
|
|
old_max_view_distance: u8,
|
2022-04-15 07:55:45 +10:00
|
|
|
/// Entities that were visible to this client at the end of the last tick.
|
|
|
|
/// This is used to determine what entity create/destroy packets should be
|
|
|
|
/// sent.
|
|
|
|
loaded_entities: HashSet<EntityId>,
|
2022-05-16 19:36:14 +10:00
|
|
|
loaded_chunks: HashSet<ChunkPos>,
|
2022-04-15 07:55:45 +10:00
|
|
|
new_game_mode: GameMode,
|
2022-04-29 17:48:41 +10:00
|
|
|
old_game_mode: GameMode,
|
2022-04-15 07:55:45 +10:00
|
|
|
settings: Option<Settings>,
|
2022-06-25 09:11:15 +10:00
|
|
|
dug_blocks: Vec<i32>,
|
2022-07-13 13:47:53 +10:00
|
|
|
/// Should be sent after login packet.
|
2022-07-02 12:18:59 +10:00
|
|
|
msgs_to_send: Vec<Text>,
|
2022-07-13 13:47:53 +10:00
|
|
|
attack_speed: f64,
|
|
|
|
movement_speed: f64,
|
2022-07-05 08:51:28 +10:00
|
|
|
flags: ClientFlags,
|
2022-07-06 11:08:40 +10:00
|
|
|
/// The data for the client's own player entity.
|
|
|
|
player_data: Player,
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-06 18:12:05 +10:00
|
|
|
#[bitfield(u16)]
|
2022-07-05 08:51:28 +10:00
|
|
|
pub(crate) struct ClientFlags {
|
|
|
|
spawn: bool,
|
|
|
|
sneaking: bool,
|
|
|
|
sprinting: bool,
|
|
|
|
jumping_with_horse: bool,
|
|
|
|
on_ground: bool,
|
|
|
|
/// If any of position, yaw, or pitch were modified by the
|
|
|
|
/// user this tick.
|
|
|
|
teleported_this_tick: bool,
|
|
|
|
/// If spawn_position or spawn_position_yaw were modified this tick.
|
|
|
|
modified_spawn_position: bool,
|
|
|
|
/// If the last sent keepalive got a response.
|
|
|
|
got_keepalive: bool,
|
2022-07-06 18:12:05 +10:00
|
|
|
hardcore: bool,
|
2022-07-13 13:47:53 +10:00
|
|
|
attack_speed_modified: bool,
|
|
|
|
movement_speed_modified: bool,
|
2022-07-18 14:29:44 +10:00
|
|
|
velocity_modified: bool,
|
|
|
|
#[bits(4)]
|
2022-07-06 18:55:00 +10:00
|
|
|
_pad: u8,
|
2022-07-05 08:51:28 +10:00
|
|
|
}
|
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
impl<C: Config> Client<C> {
|
2022-04-15 07:55:45 +10:00
|
|
|
pub(crate) fn new(
|
2022-06-10 13:26:21 +10:00
|
|
|
packet_channels: C2sPacketChannels,
|
2022-07-16 13:40:39 +10:00
|
|
|
server: &SharedServer<C>,
|
2022-06-29 11:29:29 +10:00
|
|
|
ncd: NewClientData,
|
2022-07-16 13:40:39 +10:00
|
|
|
data: C::ClientData,
|
2022-04-15 07:55:45 +10:00
|
|
|
) -> Self {
|
|
|
|
let (send, recv) = packet_channels;
|
|
|
|
|
|
|
|
Self {
|
2022-07-16 13:40:39 +10:00
|
|
|
data,
|
2022-04-15 07:55:45 +10:00
|
|
|
send: Some(send),
|
|
|
|
recv,
|
|
|
|
created_tick: server.current_tick(),
|
2022-06-29 11:29:29 +10:00
|
|
|
uuid: ncd.uuid,
|
|
|
|
username: ncd.username,
|
|
|
|
textures: ncd.textures,
|
2022-07-04 11:45:11 +10:00
|
|
|
world: WorldId::default(),
|
2022-05-16 21:09:51 +10:00
|
|
|
new_position: Vec3::default(),
|
|
|
|
old_position: Vec3::default(),
|
2022-07-18 14:29:44 +10:00
|
|
|
velocity: Vec3::default(),
|
2022-04-15 07:55:45 +10:00
|
|
|
yaw: 0.0,
|
|
|
|
pitch: 0.0,
|
|
|
|
teleport_id_counter: 0,
|
|
|
|
pending_teleports: 0,
|
|
|
|
spawn_position: BlockPos::default(),
|
|
|
|
spawn_position_yaw: 0.0,
|
2022-06-22 07:14:16 +10:00
|
|
|
death_location: None,
|
2022-07-03 04:22:28 +10:00
|
|
|
events: VecDeque::new(),
|
2022-04-15 07:55:45 +10:00
|
|
|
last_keepalive_id: 0,
|
|
|
|
new_max_view_distance: 16,
|
2022-04-29 17:48:41 +10:00
|
|
|
old_max_view_distance: 0,
|
2022-04-15 07:55:45 +10:00
|
|
|
loaded_entities: HashSet::new(),
|
2022-05-16 19:36:14 +10:00
|
|
|
loaded_chunks: HashSet::new(),
|
2022-04-15 07:55:45 +10:00
|
|
|
new_game_mode: GameMode::Survival,
|
2022-04-29 17:48:41 +10:00
|
|
|
old_game_mode: GameMode::Survival,
|
2022-04-15 07:55:45 +10:00
|
|
|
settings: None,
|
2022-06-25 09:11:15 +10:00
|
|
|
dug_blocks: Vec::new(),
|
2022-06-30 14:33:42 +10:00
|
|
|
msgs_to_send: Vec::new(),
|
2022-07-13 13:47:53 +10:00
|
|
|
attack_speed: 4.0,
|
|
|
|
movement_speed: 0.7,
|
2022-07-05 08:51:28 +10:00
|
|
|
flags: ClientFlags::new()
|
|
|
|
.with_modified_spawn_position(true)
|
|
|
|
.with_got_keepalive(true),
|
2022-07-06 11:08:40 +10:00
|
|
|
player_data: Player::new(),
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the tick that this client was created.
|
2022-04-15 07:55:45 +10:00
|
|
|
pub fn created_tick(&self) -> Ticks {
|
|
|
|
self.created_tick
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the client's UUID.
|
2022-06-29 11:29:29 +10:00
|
|
|
pub fn uuid(&self) -> Uuid {
|
|
|
|
self.uuid
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the username of this client, which is always valid.
|
2022-04-15 07:55:45 +10:00
|
|
|
pub fn username(&self) -> &str {
|
|
|
|
&self.username
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:29:44 +10:00
|
|
|
/// Returns the sneaking state of this client.
|
|
|
|
pub fn is_sneaking(&self) -> bool {
|
|
|
|
self.flags.sneaking()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the sprinting state of this client.
|
|
|
|
pub fn is_sprinting(&self) -> bool {
|
|
|
|
self.flags.sprinting()
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the player textures of this client. If the client does not have
|
|
|
|
/// a skin, then `None` is returned.
|
2022-06-29 11:29:29 +10:00
|
|
|
pub fn textures(&self) -> Option<&SignedPlayerTextures> {
|
|
|
|
self.textures.as_ref()
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the world this client is located in.
|
2022-07-04 11:02:00 +10:00
|
|
|
pub fn world(&self) -> WorldId {
|
2022-07-04 11:45:11 +10:00
|
|
|
self.world
|
2022-07-04 08:31:24 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Changes the world this client is located in.
|
|
|
|
///
|
|
|
|
/// The given [`WorldId`] must be valid. Otherwise, the client is
|
|
|
|
/// disconnected.
|
2022-07-04 11:45:11 +10:00
|
|
|
pub fn spawn(&mut self, world: WorldId) {
|
|
|
|
self.world = world;
|
2022-07-05 08:51:28 +10:00
|
|
|
self.flags.set_spawn(true);
|
2022-07-04 08:31:24 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sends a system message to the player which is visible in the chat.
|
2022-07-02 12:18:59 +10:00
|
|
|
pub fn send_message(&mut self, msg: impl Into<Text>) {
|
2022-07-06 18:55:00 +10:00
|
|
|
// We buffer messages because weird things happen if we send them before the
|
|
|
|
// login packet.
|
2022-07-02 12:18:59 +10:00
|
|
|
self.msgs_to_send.push(msg.into());
|
2022-06-30 14:33:42 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the absolute position of this client in the world it is located
|
|
|
|
/// in.
|
2022-05-16 21:09:51 +10:00
|
|
|
pub fn position(&self) -> Vec3<f64> {
|
2022-04-15 07:55:45 +10:00
|
|
|
self.new_position
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Changes the position and rotation of this client in the world it is
|
|
|
|
/// located in.
|
|
|
|
///
|
|
|
|
/// If you want to change the client's world, use [`Self::spawn`].
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn teleport(&mut self, pos: impl Into<Vec3<f64>>, yaw: f32, pitch: f32) {
|
|
|
|
self.new_position = pos.into();
|
|
|
|
self.yaw = yaw;
|
|
|
|
self.pitch = pitch;
|
2022-07-18 14:29:44 +10:00
|
|
|
self.velocity = Vec3::default();
|
2022-06-30 04:09:00 +10:00
|
|
|
|
2022-07-05 08:51:28 +10:00
|
|
|
if !self.flags.teleported_this_tick() {
|
|
|
|
self.flags.set_teleported_this_tick(true);
|
2022-06-30 04:09:00 +10:00
|
|
|
|
|
|
|
self.pending_teleports = match self.pending_teleports.checked_add(1) {
|
|
|
|
Some(n) => n,
|
|
|
|
None => {
|
2022-07-03 06:41:45 +10:00
|
|
|
log::warn!("too many pending teleports for {}", self.username());
|
|
|
|
self.disconnect_no_reason();
|
2022-06-30 04:09:00 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.teleport_id_counter = self.teleport_id_counter.wrapping_add(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:29:44 +10:00
|
|
|
/// Gets the velocity of this client in m/s.
|
|
|
|
///
|
|
|
|
/// The velocity of a client is derived from their current and previous
|
|
|
|
/// position.
|
|
|
|
pub fn velocity(&self) -> Vec3<f32> {
|
|
|
|
self.velocity
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the client's velocity in m/s.
|
2022-07-15 21:21:32 +10:00
|
|
|
pub fn set_velocity(&mut self, velocity: impl Into<Vec3<f32>>) {
|
2022-07-18 14:29:44 +10:00
|
|
|
self.velocity = velocity.into();
|
|
|
|
self.flags.set_velocity_modified(true);
|
2022-07-15 21:21:32 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets this client's yaw.
|
2022-04-15 07:55:45 +10:00
|
|
|
pub fn yaw(&self) -> f32 {
|
|
|
|
self.yaw
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets this client's pitch.
|
2022-04-15 07:55:45 +10:00
|
|
|
pub fn pitch(&self) -> f32 {
|
|
|
|
self.pitch
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the spawn position. The client will see `minecraft:compass` items
|
|
|
|
/// point at the returned position.
|
2022-06-22 07:14:16 +10:00
|
|
|
pub fn spawn_position(&self) -> BlockPos {
|
|
|
|
self.spawn_position
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the spawn position. The client will see `minecraft:compass` items
|
|
|
|
/// point at the provided position.
|
2022-06-30 04:09:00 +10:00
|
|
|
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;
|
2022-07-05 08:51:28 +10:00
|
|
|
self.flags.set_modified_spawn_position(true);
|
2022-06-30 04:09:00 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the last death location of this client. The client will see
|
|
|
|
/// `minecraft:recovery_compass` items point at the returned position.
|
|
|
|
/// If the client's current dimension differs from the returned
|
|
|
|
/// dimension or the location is `None` then the compass will spin
|
|
|
|
/// randomly.
|
2022-06-22 07:14:16 +10:00
|
|
|
pub fn death_location(&self) -> Option<(DimensionId, BlockPos)> {
|
|
|
|
self.death_location
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the last death location. The client will see
|
|
|
|
/// `minecraft:recovery_compass` items point at the provided position.
|
|
|
|
/// If the client's current dimension differs from the provided
|
|
|
|
/// dimension or the location is `None` then the compass will spin
|
|
|
|
/// randomly.
|
2022-06-30 04:09:00 +10:00
|
|
|
///
|
|
|
|
/// Changes to the last death location take effect when the client
|
|
|
|
/// (re)spawns.
|
|
|
|
pub fn set_death_location(&mut self, location: Option<(DimensionId, BlockPos)>) {
|
|
|
|
self.death_location = location;
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the client's game mode.
|
2022-05-16 19:36:14 +10:00
|
|
|
pub fn game_mode(&self) -> GameMode {
|
|
|
|
self.new_game_mode
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the client's game mode.
|
|
|
|
pub fn set_game_mode(&mut self, game_mode: GameMode) {
|
|
|
|
self.new_game_mode = game_mode;
|
2022-06-30 04:09:00 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the title this client sees.
|
|
|
|
///
|
|
|
|
/// A title is a large piece of text displayed in the center of the screen
|
|
|
|
/// which may also include a subtitle underneath it. The title
|
|
|
|
/// can be configured to fade in and out using the
|
|
|
|
/// [`TitleAnimationTimes`] struct.
|
2022-07-06 17:16:07 +10:00
|
|
|
pub fn set_title(
|
|
|
|
&mut self,
|
|
|
|
title: impl Into<Text>,
|
|
|
|
subtitle: impl Into<Text>,
|
|
|
|
animation: impl Into<Option<TitleAnimationTimes>>,
|
|
|
|
) {
|
|
|
|
let title = title.into();
|
|
|
|
let subtitle = subtitle.into();
|
|
|
|
|
|
|
|
self.send_packet(SetTitleText { text: title });
|
|
|
|
|
|
|
|
if !subtitle.is_empty() {
|
|
|
|
self.send_packet(SetSubtitleText {
|
|
|
|
subtitle_text: subtitle,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(anim) = animation.into() {
|
|
|
|
self.send_packet(anim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 13:47:53 +10:00
|
|
|
/// Gets the attack cooldown speed.
|
|
|
|
pub fn attack_speed(&self) -> f64 {
|
|
|
|
self.attack_speed
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the attack cooldown speed.
|
|
|
|
pub fn set_attack_speed(&mut self, speed: f64) {
|
|
|
|
if self.attack_speed != speed {
|
|
|
|
self.attack_speed = speed;
|
|
|
|
self.flags.set_attack_speed_modified(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the speed at which the client can run on the ground.
|
|
|
|
pub fn movement_speed(&self) -> f64 {
|
|
|
|
self.movement_speed
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the speed at which the client can run on the ground.
|
|
|
|
pub fn set_movement_speed(&mut self, speed: f64) {
|
|
|
|
if self.movement_speed != speed {
|
|
|
|
self.movement_speed = speed;
|
|
|
|
self.flags.set_movement_speed_modified(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Removes the current title from the client's screen.
|
2022-07-06 17:16:07 +10:00
|
|
|
pub fn clear_title(&mut self) {
|
|
|
|
self.send_packet(ClearTitles { reset: true });
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets if the client is on the ground, as determined by the client.
|
2022-05-16 19:36:14 +10:00
|
|
|
pub fn on_ground(&self) -> bool {
|
2022-07-05 08:51:28 +10:00
|
|
|
self.flags.on_ground()
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets whether or not the client is connected to the server.
|
|
|
|
///
|
|
|
|
/// A disconnected client object will never become reconnected. It is your
|
|
|
|
/// responsibility to remove disconnected clients from the [`Clients`]
|
|
|
|
/// container.
|
2022-05-16 19:36:14 +10:00
|
|
|
pub fn is_disconnected(&self) -> bool {
|
|
|
|
self.send.is_none()
|
|
|
|
}
|
|
|
|
|
2022-07-15 16:18:20 +10:00
|
|
|
/// Removes an [`Event`] from the event queue.
|
|
|
|
///
|
|
|
|
/// If there are no remaining events, `None` is returned.
|
2022-07-11 22:08:02 +10:00
|
|
|
///
|
2022-07-15 16:18:20 +10:00
|
|
|
/// Any remaining client events are deleted at the end of the
|
2022-07-11 22:08:02 +10:00
|
|
|
/// current tick.
|
|
|
|
pub fn pop_event(&mut self) -> Option<Event> {
|
2022-07-03 04:22:28 +10:00
|
|
|
self.events.pop_front()
|
2022-06-30 04:09:00 +10:00
|
|
|
}
|
|
|
|
|
2022-06-20 01:40:37 +10:00
|
|
|
/// The current view distance of this client measured in chunks.
|
|
|
|
pub fn view_distance(&self) -> u8 {
|
|
|
|
self.settings
|
|
|
|
.as_ref()
|
|
|
|
.map_or(2, |s| s.view_distance)
|
|
|
|
.min(self.max_view_distance())
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the maximum view distance. The client will not be able to see
|
|
|
|
/// chunks and entities past this distance.
|
|
|
|
///
|
|
|
|
/// The value returned is measured in chunks.
|
2022-05-16 19:36:14 +10:00
|
|
|
pub fn max_view_distance(&self) -> u8 {
|
|
|
|
self.new_max_view_distance
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the maximum view distance. The client will not be able to see
|
|
|
|
/// chunks and entities past this distance.
|
|
|
|
///
|
|
|
|
/// The new view distance is measured in chunks and is clamped to `2..=32`.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn set_max_view_distance(&mut self, dist: u8) {
|
|
|
|
self.new_max_view_distance = dist.clamp(2, 32);
|
2022-06-22 07:14:16 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Enables hardcore mode. This changes the design of the client's hearts.
|
|
|
|
///
|
|
|
|
/// To have any visible effect, this function must be called on the same
|
|
|
|
/// tick the client joins the server.
|
2022-07-06 18:12:05 +10:00
|
|
|
pub fn set_hardcore(&mut self, hardcore: bool) {
|
|
|
|
self.flags.set_hardcore(hardcore);
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets if hardcore mode is enabled.
|
2022-07-06 18:12:05 +10:00
|
|
|
pub fn is_hardcore(&mut self) -> bool {
|
|
|
|
self.flags.hardcore()
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the client's current settings.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn settings(&self) -> Option<&Settings> {
|
|
|
|
self.settings.as_ref()
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Disconnects this client from the server with the provided reason. This
|
|
|
|
/// has no effect if the client is already disconnected.
|
|
|
|
///
|
|
|
|
/// All future calls to [`Self::is_disconnected`] will return `true`.
|
2022-04-15 07:55:45 +10:00
|
|
|
pub fn disconnect(&mut self, reason: impl Into<Text>) {
|
2022-06-30 04:09:00 +10:00
|
|
|
if self.send.is_some() {
|
2022-04-15 07:55:45 +10:00
|
|
|
let txt = reason.into();
|
2022-06-30 04:09:00 +10:00
|
|
|
log::info!("disconnecting client '{}': \"{txt}\"", self.username);
|
2022-04-15 07:55:45 +10:00
|
|
|
|
|
|
|
self.send_packet(Disconnect { reason: txt });
|
|
|
|
|
2022-06-30 04:09:00 +10:00
|
|
|
self.send = None;
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Like [`Self::disconnect`], but no reason for the disconnect is
|
|
|
|
/// displayed.
|
2022-04-15 07:55:45 +10:00
|
|
|
pub fn disconnect_no_reason(&mut self) {
|
2022-06-30 04:09:00 +10:00
|
|
|
if self.send.is_some() {
|
2022-07-03 09:23:58 +10:00
|
|
|
log::info!("disconnecting client '{}'", self.username);
|
2022-06-30 04:09:00 +10:00
|
|
|
self.send = None;
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns an immutable reference to the client's own [`Player`] data.
|
|
|
|
pub fn player(&self) -> &Player {
|
2022-07-06 11:08:40 +10:00
|
|
|
&self.player_data
|
2022-06-30 06:00:41 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns a mutable reference to the client's own [`Player`] data.
|
|
|
|
///
|
|
|
|
/// Changes made to this data is only visible to this client.
|
|
|
|
pub fn player_mut(&mut self) -> &mut Player {
|
2022-07-06 11:08:40 +10:00
|
|
|
&mut self.player_data
|
2022-06-30 06:00:41 +10:00
|
|
|
}
|
|
|
|
|
2022-06-30 04:09:00 +10:00
|
|
|
/// Attempts to enqueue a play packet to be sent to this client. The client
|
|
|
|
/// is disconnected if the clientbound packet buffer is full.
|
2022-07-06 18:55:00 +10:00
|
|
|
#[cfg(feature = "protocol")]
|
|
|
|
pub fn send_packet(&mut self, packet: impl Into<S2cPlayPacket>) {
|
|
|
|
send_packet(&mut self.send, packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "protocol"))]
|
2022-06-30 04:09:00 +10:00
|
|
|
pub(crate) fn send_packet(&mut self, packet: impl Into<S2cPlayPacket>) {
|
|
|
|
send_packet(&mut self.send, packet);
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
pub(crate) fn handle_serverbound_packets(&mut self, entities: &Entities<C>) {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.events.clear();
|
2022-06-25 09:11:15 +10:00
|
|
|
for _ in 0..self.recv.len() {
|
|
|
|
self.handle_serverbound_packet(entities, self.recv.try_recv().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
fn handle_serverbound_packet(&mut self, entities: &Entities<C>, pkt: C2sPlayPacket) {
|
|
|
|
fn handle_movement_packet<C: Config>(
|
|
|
|
client: &mut Client<C>,
|
2022-06-25 09:11:15 +10:00
|
|
|
_vehicle: bool,
|
|
|
|
new_position: Vec3<f64>,
|
|
|
|
new_yaw: f32,
|
|
|
|
new_pitch: f32,
|
|
|
|
new_on_ground: bool,
|
|
|
|
) {
|
|
|
|
if client.pending_teleports == 0 {
|
|
|
|
// TODO: validate movement using swept AABB collision with the blocks.
|
|
|
|
// TODO: validate that the client is actually inside/outside the vehicle?
|
2022-07-18 14:29:44 +10:00
|
|
|
|
|
|
|
// Movement packets should be coming in at a rate of STANDARD_TPS.
|
|
|
|
let new_velocity = (new_position - client.new_position).as_() * STANDARD_TPS as f32;
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
let event = Event::Movement {
|
2022-07-18 14:29:44 +10:00
|
|
|
old_position: client.new_position,
|
|
|
|
old_velocity: client.velocity,
|
|
|
|
old_yaw: client.yaw,
|
|
|
|
old_pitch: client.pitch,
|
|
|
|
old_on_ground: client.flags.on_ground(),
|
|
|
|
new_position,
|
|
|
|
new_velocity,
|
|
|
|
new_yaw,
|
|
|
|
new_pitch,
|
|
|
|
new_on_ground,
|
2022-06-25 09:11:15 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
client.new_position = new_position;
|
2022-07-18 14:29:44 +10:00
|
|
|
client.velocity = new_velocity;
|
2022-06-25 09:11:15 +10:00
|
|
|
client.yaw = new_yaw;
|
|
|
|
client.pitch = new_pitch;
|
2022-07-05 08:51:28 +10:00
|
|
|
client.flags.set_on_ground(new_on_ground);
|
2022-06-25 09:11:15 +10:00
|
|
|
|
2022-07-03 04:22:28 +10:00
|
|
|
client.events.push_back(event);
|
2022-06-25 09:11:15 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match pkt {
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::AcceptTeleportation(p) => {
|
2022-06-30 04:09:00 +10:00
|
|
|
if self.pending_teleports == 0 {
|
2022-07-03 06:41:45 +10:00
|
|
|
log::warn!("unexpected teleport confirmation from {}", self.username());
|
|
|
|
self.disconnect_no_reason();
|
2022-06-25 09:11:15 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let got = p.teleport_id.0 as u32;
|
2022-06-30 04:09:00 +10:00
|
|
|
let expected = self
|
2022-06-25 09:11:15 +10:00
|
|
|
.teleport_id_counter
|
2022-06-30 04:09:00 +10:00
|
|
|
.wrapping_sub(self.pending_teleports);
|
2022-06-25 09:11:15 +10:00
|
|
|
|
|
|
|
if got == expected {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.pending_teleports -= 1;
|
2022-06-25 09:11:15 +10:00
|
|
|
} else {
|
2022-07-03 06:41:45 +10:00
|
|
|
log::warn!(
|
|
|
|
"unexpected teleport ID from {} (expected {expected}, got {got})",
|
|
|
|
self.username()
|
|
|
|
);
|
|
|
|
self.disconnect_no_reason();
|
2022-06-25 09:11:15 +10:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::BlockEntityTagQuery(_) => {}
|
|
|
|
C2sPlayPacket::ChangeDifficulty(_) => {}
|
2022-06-25 09:11:15 +10:00
|
|
|
C2sPlayPacket::ChatCommand(_) => {}
|
2022-07-11 22:08:02 +10:00
|
|
|
C2sPlayPacket::Chat(p) => self.events.push_back(Event::ChatMessage {
|
2022-06-30 14:33:42 +10:00
|
|
|
message: p.message.0,
|
|
|
|
timestamp: Duration::from_millis(p.timestamp),
|
|
|
|
}),
|
2022-06-25 09:11:15 +10:00
|
|
|
C2sPlayPacket::ChatPreview(_) => {}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::ClientCommand(_) => {}
|
|
|
|
C2sPlayPacket::ClientInformation(p) => {
|
2022-06-30 04:09:00 +10:00
|
|
|
let old = self.settings.replace(Settings {
|
2022-06-25 09:11:15 +10:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
self.events.push_back(Event::SettingsChanged(old));
|
2022-06-25 09:11:15 +10:00
|
|
|
}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::CommandSuggestion(_) => {}
|
|
|
|
C2sPlayPacket::ContainerButtonClick(_) => {}
|
|
|
|
C2sPlayPacket::ContainerClose(_) => {}
|
|
|
|
C2sPlayPacket::CustomPayload(_) => {}
|
2022-06-25 09:11:15 +10:00
|
|
|
C2sPlayPacket::EditBook(_) => {}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::EntityTagQuery(_) => {}
|
|
|
|
C2sPlayPacket::Interact(p) => {
|
2022-06-25 09:11:15 +10:00
|
|
|
if let Some(id) = entities.get_with_network_id(p.entity_id.0) {
|
|
|
|
// TODO: verify that the client has line of sight to the targeted entity and
|
|
|
|
// that the distance is <=4 blocks.
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
self.events.push_back(Event::InteractWithEntity {
|
2022-06-25 09:11:15 +10:00
|
|
|
id,
|
|
|
|
sneaking: p.sneaking,
|
2022-07-06 12:21:52 +10:00
|
|
|
kind: match p.kind {
|
2022-07-11 22:08:02 +10:00
|
|
|
InteractKind::Interact(hand) => InteractWithEntityKind::Interact(hand),
|
|
|
|
InteractKind::Attack => InteractWithEntityKind::Attack,
|
2022-07-06 12:21:52 +10:00
|
|
|
InteractKind::InteractAt((target, hand)) => {
|
2022-07-11 22:08:02 +10:00
|
|
|
InteractWithEntityKind::InteractAt { target, hand }
|
2022-06-25 09:11:15 +10:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::JigsawGenerate(_) => {}
|
2022-06-25 09:11:15 +10:00
|
|
|
C2sPlayPacket::KeepAlive(p) => {
|
2022-06-30 04:09:00 +10:00
|
|
|
let last_keepalive_id = self.last_keepalive_id;
|
2022-07-05 08:51:28 +10:00
|
|
|
if self.flags.got_keepalive() {
|
2022-07-03 09:23:58 +10:00
|
|
|
log::warn!("unexpected keepalive from player {}", self.username());
|
|
|
|
self.disconnect_no_reason();
|
2022-06-25 09:11:15 +10:00
|
|
|
} else if p.id != last_keepalive_id {
|
2022-07-03 09:23:58 +10:00
|
|
|
log::warn!(
|
|
|
|
"keepalive ids for player {} don't match (expected {}, got {})",
|
|
|
|
self.username(),
|
|
|
|
last_keepalive_id,
|
|
|
|
p.id
|
|
|
|
);
|
|
|
|
self.disconnect_no_reason();
|
2022-06-25 09:11:15 +10:00
|
|
|
} else {
|
2022-07-05 08:51:28 +10:00
|
|
|
self.flags.set_got_keepalive(true);
|
2022-06-25 09:11:15 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
C2sPlayPacket::LockDifficulty(_) => {}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::MovePlayerPosition(p) => {
|
2022-06-30 04:09:00 +10:00
|
|
|
handle_movement_packet(self, false, p.position, self.yaw, self.pitch, p.on_ground)
|
|
|
|
}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::MovePlayerPositionAndRotation(p) => {
|
2022-06-30 04:09:00 +10:00
|
|
|
handle_movement_packet(self, false, p.position, p.yaw, p.pitch, p.on_ground)
|
|
|
|
}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::MovePlayerRotation(p) => {
|
2022-06-30 04:09:00 +10:00
|
|
|
handle_movement_packet(self, false, self.new_position, p.yaw, p.pitch, p.on_ground)
|
2022-06-25 09:11:15 +10:00
|
|
|
}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::MovePlayerStatusOnly(p) => handle_movement_packet(
|
2022-06-30 04:09:00 +10:00
|
|
|
self,
|
2022-06-25 09:11:15 +10:00
|
|
|
false,
|
2022-06-30 04:09:00 +10:00
|
|
|
self.new_position,
|
|
|
|
self.yaw,
|
|
|
|
self.pitch,
|
2022-06-25 09:11:15 +10:00
|
|
|
p.on_ground,
|
|
|
|
),
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::MoveVehicle(p) => {
|
2022-07-05 08:51:28 +10:00
|
|
|
handle_movement_packet(
|
|
|
|
self,
|
|
|
|
true,
|
|
|
|
p.position,
|
|
|
|
p.yaw,
|
|
|
|
p.pitch,
|
|
|
|
self.flags.on_ground(),
|
|
|
|
);
|
2022-06-25 09:11:15 +10:00
|
|
|
}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::PaddleBoat(p) => {
|
2022-07-11 22:08:02 +10:00
|
|
|
self.events.push_back(Event::SteerBoat {
|
2022-06-25 09:11:15 +10:00
|
|
|
left_paddle_turning: p.left_paddle_turning,
|
|
|
|
right_paddle_turning: p.right_paddle_turning,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
C2sPlayPacket::PickItem(_) => {}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::PlaceRecipe(_) => {}
|
2022-06-25 09:11:15 +10:00
|
|
|
C2sPlayPacket::PlayerAbilities(_) => {}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::PlayerAction(p) => {
|
2022-06-25 09:11:15 +10:00
|
|
|
// TODO: verify dug block is within the correct distance from the client.
|
|
|
|
// TODO: verify that the broken block is allowed to be broken?
|
|
|
|
|
|
|
|
if p.sequence.0 != 0 {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.dug_blocks.push(p.sequence.0);
|
2022-06-25 09:11:15 +10:00
|
|
|
}
|
|
|
|
|
2022-07-03 04:22:28 +10:00
|
|
|
self.events.push_back(match p.status {
|
2022-07-11 22:08:02 +10:00
|
|
|
DiggingStatus::StartedDigging => Event::Digging {
|
2022-06-25 09:11:15 +10:00
|
|
|
status: event::DiggingStatus::Start,
|
|
|
|
position: p.location,
|
|
|
|
face: p.face,
|
2022-07-11 22:08:02 +10:00
|
|
|
},
|
|
|
|
DiggingStatus::CancelledDigging => Event::Digging {
|
2022-06-25 09:11:15 +10:00
|
|
|
status: event::DiggingStatus::Cancel,
|
|
|
|
position: p.location,
|
|
|
|
face: p.face,
|
2022-07-11 22:08:02 +10:00
|
|
|
},
|
|
|
|
DiggingStatus::FinishedDigging => Event::Digging {
|
2022-06-25 09:11:15 +10:00
|
|
|
status: event::DiggingStatus::Finish,
|
|
|
|
position: p.location,
|
|
|
|
face: p.face,
|
2022-07-11 22:08:02 +10:00
|
|
|
},
|
2022-06-25 09:11:15 +10:00
|
|
|
DiggingStatus::DropItemStack => return,
|
|
|
|
DiggingStatus::DropItem => return,
|
|
|
|
DiggingStatus::ShootArrowOrFinishEating => return,
|
|
|
|
DiggingStatus::SwapItemInHand => return,
|
|
|
|
});
|
|
|
|
}
|
2022-07-05 08:51:28 +10:00
|
|
|
C2sPlayPacket::PlayerCommand(e) => {
|
|
|
|
// TODO: validate:
|
|
|
|
// - Can't sprint and sneak at the same time
|
|
|
|
// - Can't leave bed while not in a bed.
|
|
|
|
// - Can't jump with a horse if not on a horse
|
|
|
|
// - Can't open horse inventory if not on a horse.
|
|
|
|
// - Can't fly with elytra if not wearing an elytra.
|
2022-07-18 14:29:44 +10:00
|
|
|
// - Can't jump with horse while already jumping & vice versa?
|
2022-07-05 08:51:28 +10:00
|
|
|
self.events.push_back(match e.action_id {
|
|
|
|
PlayerCommandId::StartSneaking => {
|
2022-07-18 14:29:44 +10:00
|
|
|
if self.flags.sneaking() {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-05 08:51:28 +10:00
|
|
|
self.flags.set_sneaking(true);
|
2022-07-11 22:08:02 +10:00
|
|
|
Event::StartSneaking
|
2022-07-05 08:51:28 +10:00
|
|
|
}
|
|
|
|
PlayerCommandId::StopSneaking => {
|
2022-07-18 14:29:44 +10:00
|
|
|
if !self.flags.sneaking() {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-05 08:51:28 +10:00
|
|
|
self.flags.set_sneaking(false);
|
2022-07-11 22:08:02 +10:00
|
|
|
Event::StopSneaking
|
2022-07-05 08:51:28 +10:00
|
|
|
}
|
2022-07-11 22:08:02 +10:00
|
|
|
PlayerCommandId::LeaveBed => Event::LeaveBed,
|
2022-07-05 08:51:28 +10:00
|
|
|
PlayerCommandId::StartSprinting => {
|
2022-07-18 14:29:44 +10:00
|
|
|
if self.flags.sprinting() {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-05 08:51:28 +10:00
|
|
|
self.flags.set_sprinting(true);
|
2022-07-11 22:08:02 +10:00
|
|
|
Event::StartSprinting
|
2022-07-05 08:51:28 +10:00
|
|
|
}
|
|
|
|
PlayerCommandId::StopSprinting => {
|
2022-07-18 14:29:44 +10:00
|
|
|
if !self.flags.sprinting() {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-05 08:51:28 +10:00
|
|
|
self.flags.set_sprinting(false);
|
2022-07-11 22:08:02 +10:00
|
|
|
Event::StopSprinting
|
2022-07-05 08:51:28 +10:00
|
|
|
}
|
|
|
|
PlayerCommandId::StartJumpWithHorse => {
|
|
|
|
self.flags.set_jumping_with_horse(true);
|
2022-07-11 22:08:02 +10:00
|
|
|
Event::StartJumpWithHorse {
|
|
|
|
jump_boost: e.jump_boost.0 .0 as u8,
|
|
|
|
}
|
2022-07-05 08:51:28 +10:00
|
|
|
}
|
|
|
|
PlayerCommandId::StopJumpWithHorse => {
|
|
|
|
self.flags.set_jumping_with_horse(false);
|
2022-07-11 22:08:02 +10:00
|
|
|
Event::StopJumpWithHorse
|
2022-07-05 08:51:28 +10:00
|
|
|
}
|
2022-07-11 22:08:02 +10:00
|
|
|
PlayerCommandId::OpenHorseInventory => Event::OpenHorseInventory,
|
|
|
|
PlayerCommandId::StartFlyingWithElytra => Event::StartFlyingWithElytra,
|
2022-07-05 08:51:28 +10:00
|
|
|
});
|
|
|
|
}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::PlayerInput(_) => {}
|
2022-06-25 09:11:15 +10:00
|
|
|
C2sPlayPacket::Pong(_) => {}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::RecipeBookChangeSettings(_) => {}
|
|
|
|
C2sPlayPacket::RecipeBookSeenRecipe(_) => {}
|
|
|
|
C2sPlayPacket::RenameItem(_) => {}
|
|
|
|
C2sPlayPacket::ResourcePack(_) => {}
|
|
|
|
C2sPlayPacket::SeenAdvancements(_) => {}
|
2022-06-25 09:11:15 +10:00
|
|
|
C2sPlayPacket::SelectTrade(_) => {}
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::SetBeacon(_) => {}
|
|
|
|
C2sPlayPacket::SetCarriedItem(_) => {}
|
|
|
|
C2sPlayPacket::SetCommandBlock(_) => {}
|
|
|
|
C2sPlayPacket::SetCommandBlockMinecart(_) => {}
|
|
|
|
C2sPlayPacket::SetCreativeModeSlot(_) => {}
|
|
|
|
C2sPlayPacket::SetJigsawBlock(_) => {}
|
|
|
|
C2sPlayPacket::SetStructureBlock(_) => {}
|
|
|
|
C2sPlayPacket::SignUpdate(_) => {}
|
2022-07-11 22:08:02 +10:00
|
|
|
C2sPlayPacket::Swing(p) => self.events.push_back(Event::ArmSwing(p.hand)),
|
2022-07-01 07:18:29 +10:00
|
|
|
C2sPlayPacket::TeleportToEntity(_) => {}
|
|
|
|
C2sPlayPacket::UseItemOn(_) => {}
|
2022-06-25 09:11:15 +10:00
|
|
|
C2sPlayPacket::UseItem(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
pub(crate) fn update(
|
|
|
|
&mut self,
|
|
|
|
shared: &SharedServer<C>,
|
|
|
|
entities: &Entities<C>,
|
|
|
|
worlds: &Worlds<C>,
|
|
|
|
) {
|
2022-04-29 17:48:41 +10:00
|
|
|
// Mark the client as disconnected when appropriate.
|
2022-05-16 19:36:14 +10:00
|
|
|
if self.recv.is_disconnected() || self.send.as_ref().map_or(true, |s| s.is_disconnected()) {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.send = None;
|
2022-04-29 17:48:41 +10:00
|
|
|
return;
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-04 11:45:11 +10:00
|
|
|
let world = match worlds.get(self.world) {
|
2022-07-04 08:31:24 +10:00
|
|
|
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();
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-04-29 17:48:41 +10:00
|
|
|
// Send the join game packet and other initial packets. We defer this until now
|
|
|
|
// so that the user can set the client's location, game mode, etc.
|
2022-05-17 19:58:43 +10:00
|
|
|
if self.created_tick == current_tick {
|
2022-07-04 08:31:24 +10:00
|
|
|
world
|
|
|
|
.meta
|
|
|
|
.player_list()
|
2022-06-29 11:29:29 +10:00
|
|
|
.initial_packets(|pkt| self.send_packet(pkt));
|
|
|
|
|
2022-07-04 11:45:11 +10:00
|
|
|
let mut dimension_names: Vec<_> = shared
|
|
|
|
.dimensions()
|
|
|
|
.map(|(id, _)| ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
dimension_names.push(ident!("{LIBRARY_NAMESPACE}:dummy_dimension"));
|
|
|
|
|
2022-07-01 07:18:29 +10:00
|
|
|
self.send_packet(Login {
|
2022-07-06 18:12:05 +10:00
|
|
|
entity_id: 0, // EntityId 0 is reserved for clients.
|
|
|
|
is_hardcore: self.flags.hardcore(),
|
2022-04-15 07:55:45 +10:00
|
|
|
gamemode: self.new_game_mode,
|
|
|
|
previous_gamemode: self.old_game_mode,
|
2022-07-04 11:45:11 +10:00
|
|
|
dimension_names,
|
2022-07-11 22:08:02 +10:00
|
|
|
registry_codec: Nbt(make_registry_codec(shared)),
|
2022-06-21 21:55:32 +10:00
|
|
|
dimension_type_name: ident!(
|
|
|
|
"{LIBRARY_NAMESPACE}:dimension_type_{}",
|
2022-07-04 08:31:24 +10:00
|
|
|
world.meta.dimension().0
|
|
|
|
),
|
|
|
|
dimension_name: ident!(
|
|
|
|
"{LIBRARY_NAMESPACE}:dimension_{}",
|
|
|
|
world.meta.dimension().0
|
2022-06-21 21:55:32 +10:00
|
|
|
),
|
2022-04-15 07:55:45 +10:00
|
|
|
hashed_seed: 0,
|
|
|
|
max_players: VarInt(0),
|
|
|
|
view_distance: BoundedInt(VarInt(self.new_max_view_distance as i32)),
|
|
|
|
simulation_distance: VarInt(16),
|
2022-06-29 11:29:29 +10:00
|
|
|
reduced_debug_info: false,
|
2022-06-21 21:55:32 +10:00
|
|
|
enable_respawn_screen: false,
|
2022-06-22 05:26:28 +10:00
|
|
|
is_debug: false,
|
2022-07-04 08:31:24 +10:00
|
|
|
is_flat: world.meta.is_flat(),
|
2022-06-22 07:14:16 +10:00
|
|
|
last_death_location: self
|
|
|
|
.death_location
|
|
|
|
.map(|(id, pos)| (ident!("{LIBRARY_NAMESPACE}:dimension_{}", id.0), pos)),
|
2022-04-15 07:55:45 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
self.teleport(self.position(), self.yaw(), self.pitch());
|
2022-06-28 10:52:23 +10:00
|
|
|
} else {
|
2022-07-05 08:51:28 +10:00
|
|
|
if self.flags.spawn() {
|
2022-07-18 14:29:44 +10:00
|
|
|
self.flags.set_spawn(false);
|
2022-07-04 08:31:24 +10:00
|
|
|
self.loaded_entities.clear();
|
|
|
|
self.loaded_chunks.clear();
|
|
|
|
|
2022-07-04 10:32:05 +10:00
|
|
|
// TODO: clear player list.
|
|
|
|
|
2022-07-04 11:45:11 +10:00
|
|
|
// Client bug workaround: send the client to a dummy dimension first.
|
|
|
|
self.send_packet(Respawn {
|
|
|
|
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: false,
|
|
|
|
copy_metadata: true,
|
|
|
|
last_death_location: None,
|
|
|
|
});
|
2022-07-04 10:32:05 +10:00
|
|
|
|
|
|
|
self.send_packet(Respawn {
|
|
|
|
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: world.meta.is_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());
|
2022-07-04 08:31:24 +10:00
|
|
|
}
|
|
|
|
|
2022-06-30 04:09:00 +10:00
|
|
|
if self.old_game_mode != self.new_game_mode {
|
|
|
|
self.old_game_mode = self.new_game_mode;
|
2022-07-01 07:18:29 +10:00
|
|
|
self.send_packet(GameEvent {
|
|
|
|
reason: GameEventReason::ChangeGameMode,
|
2022-06-30 04:09:00 +10:00
|
|
|
value: self.new_game_mode as i32 as f32,
|
2022-06-28 10:52:23 +10:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-04 08:31:24 +10:00
|
|
|
world
|
|
|
|
.meta
|
|
|
|
.player_list()
|
|
|
|
.diff_packets(|pkt| self.send_packet(pkt));
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-13 13:47:53 +10:00
|
|
|
// Set player attributes
|
|
|
|
if self.flags.attack_speed_modified() {
|
|
|
|
self.flags.set_attack_speed_modified(false);
|
|
|
|
|
|
|
|
self.send_packet(UpdateAttributes {
|
|
|
|
entity_id: VarInt(0),
|
|
|
|
properties: vec![UpdateAttributesProperty {
|
|
|
|
key: ident!("generic.attack_speed"),
|
|
|
|
value: self.attack_speed,
|
|
|
|
modifiers: Vec::new(),
|
|
|
|
}],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.flags.movement_speed_modified() {
|
|
|
|
self.flags.set_movement_speed_modified(false);
|
|
|
|
|
|
|
|
self.send_packet(UpdateAttributes {
|
|
|
|
entity_id: VarInt(0),
|
|
|
|
properties: vec![UpdateAttributesProperty {
|
|
|
|
key: ident!("generic.movement_speed"),
|
|
|
|
value: self.movement_speed,
|
|
|
|
modifiers: Vec::new(),
|
|
|
|
}],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-29 17:48:41 +10:00
|
|
|
// Update the players spawn position (compass position)
|
2022-07-05 08:51:28 +10:00
|
|
|
if self.flags.modified_spawn_position() {
|
|
|
|
self.flags.set_modified_spawn_position(false);
|
2022-04-15 07:55:45 +10:00
|
|
|
|
|
|
|
self.send_packet(SpawnPosition {
|
|
|
|
location: self.spawn_position,
|
|
|
|
angle: self.spawn_position_yaw,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update view distance fog on the client if necessary.
|
2022-06-30 04:09:00 +10:00
|
|
|
if self.old_max_view_distance != self.new_max_view_distance {
|
|
|
|
self.old_max_view_distance = self.new_max_view_distance;
|
|
|
|
if self.created_tick != current_tick {
|
2022-07-01 07:18:29 +10:00
|
|
|
self.send_packet(SetChunkCacheRadius {
|
2022-06-30 04:09:00 +10:00
|
|
|
view_distance: BoundedInt(VarInt(self.new_max_view_distance as i32)),
|
2022-04-15 07:55:45 +10:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it's time to send another keepalive.
|
2022-07-04 08:31:24 +10:00
|
|
|
if current_tick % (shared.tick_rate() * 8) == 0 {
|
2022-07-05 08:51:28 +10:00
|
|
|
if self.flags.got_keepalive() {
|
2022-04-15 07:55:45 +10:00
|
|
|
let id = rand::random();
|
2022-06-25 09:11:15 +10:00
|
|
|
self.send_packet(KeepAlive { id });
|
2022-06-30 04:09:00 +10:00
|
|
|
self.last_keepalive_id = id;
|
2022-07-05 08:51:28 +10:00
|
|
|
self.flags.set_got_keepalive(false);
|
2022-04-15 07:55:45 +10:00
|
|
|
} else {
|
2022-07-03 09:23:58 +10:00
|
|
|
log::warn!(
|
|
|
|
"player {} timed out (no keepalive response)",
|
|
|
|
self.username()
|
|
|
|
);
|
|
|
|
self.disconnect_no_reason();
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 01:40:37 +10:00
|
|
|
let view_dist = self.view_distance();
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-06-25 09:11:15 +10:00
|
|
|
let center = ChunkPos::at(self.new_position.x, self.new_position.z);
|
2022-04-15 07:55:45 +10:00
|
|
|
|
|
|
|
// Send the update view position packet if the client changes the chunk section
|
|
|
|
// they're in.
|
|
|
|
{
|
2022-06-30 04:09:00 +10:00
|
|
|
let old_section = self.old_position.map(|n| (n / 16.0).floor() as i32);
|
|
|
|
let new_section = self.new_position.map(|n| (n / 16.0).floor() as i32);
|
2022-04-15 07:55:45 +10:00
|
|
|
|
|
|
|
if old_section != new_section {
|
2022-07-01 07:18:29 +10:00
|
|
|
self.send_packet(SetChunkCacheCenter {
|
2022-04-15 07:55:45 +10:00
|
|
|
chunk_x: VarInt(new_section.x),
|
|
|
|
chunk_z: VarInt(new_section.z),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 08:31:24 +10:00
|
|
|
let dimension = shared.dimension(world.meta.dimension());
|
2022-06-23 01:06:54 +10:00
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
// Update existing chunks and unload those outside the view distance. Chunks
|
|
|
|
// that have been overwritten also need to be unloaded.
|
2022-06-30 04:09:00 +10:00
|
|
|
self.loaded_chunks.retain(|&pos| {
|
2022-05-16 19:36:14 +10:00
|
|
|
// The cache stops chunk data packets from needing to be sent when a player
|
|
|
|
// moves to an adjacent chunk and back to the original.
|
|
|
|
let cache = 2;
|
|
|
|
|
2022-07-04 08:31:24 +10:00
|
|
|
if let Some(chunk) = world.chunks.get(pos) {
|
2022-05-16 19:36:14 +10:00
|
|
|
if is_chunk_in_view_distance(center, pos, view_dist + cache)
|
2022-05-17 19:58:43 +10:00
|
|
|
&& chunk.created_tick() != current_tick
|
2022-05-16 19:36:14 +10:00
|
|
|
{
|
2022-06-23 01:06:54 +10:00
|
|
|
chunk.block_change_packets(pos, dimension.min_y, |pkt| {
|
2022-06-30 04:09:00 +10:00
|
|
|
send_packet(&mut self.send, pkt)
|
2022-06-23 01:06:54 +10:00
|
|
|
});
|
2022-05-16 19:36:14 +10:00
|
|
|
return true;
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
2022-05-16 19:36:14 +10:00
|
|
|
|
|
|
|
send_packet(
|
2022-06-30 04:09:00 +10:00
|
|
|
&mut self.send,
|
2022-07-01 07:18:29 +10:00
|
|
|
ForgetLevelChunk {
|
2022-05-16 19:36:14 +10:00
|
|
|
chunk_x: pos.x,
|
|
|
|
chunk_z: pos.z,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
false
|
2022-04-15 07:55:45 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
// Load new chunks within the view distance
|
|
|
|
for pos in chunks_in_view_distance(center, view_dist) {
|
2022-07-04 08:31:24 +10:00
|
|
|
if let Some(chunk) = world.chunks.get(pos) {
|
2022-06-30 04:09:00 +10:00
|
|
|
if self.loaded_chunks.insert(pos) {
|
2022-05-16 19:36:14 +10:00
|
|
|
self.send_packet(chunk.chunk_data_packet(pos));
|
2022-06-23 01:06:54 +10:00
|
|
|
chunk.block_change_packets(pos, dimension.min_y, |pkt| self.send_packet(pkt));
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 09:11:15 +10:00
|
|
|
// Acknowledge broken blocks.
|
2022-06-30 04:09:00 +10:00
|
|
|
for seq in self.dug_blocks.drain(..) {
|
2022-06-25 09:11:15 +10:00
|
|
|
send_packet(
|
2022-06-30 04:09:00 +10:00
|
|
|
&mut self.send,
|
2022-07-01 07:18:29 +10:00
|
|
|
BlockChangeAck {
|
2022-06-25 09:11:15 +10:00
|
|
|
sequence: VarInt(seq),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-15 21:21:32 +10:00
|
|
|
// Teleport the player.
|
|
|
|
//
|
2022-04-15 07:55:45 +10:00
|
|
|
// This is done after the chunks are loaded so that the "downloading terrain"
|
|
|
|
// screen is closed at the appropriate time.
|
2022-07-05 08:51:28 +10:00
|
|
|
if self.flags.teleported_this_tick() {
|
|
|
|
self.flags.set_teleported_this_tick(false);
|
2022-05-16 21:09:51 +10:00
|
|
|
|
2022-07-01 07:18:29 +10:00
|
|
|
self.send_packet(PlayerPosition {
|
2022-05-16 21:09:51 +10:00
|
|
|
position: self.new_position,
|
|
|
|
yaw: self.yaw,
|
|
|
|
pitch: self.pitch,
|
2022-07-01 07:18:29 +10:00
|
|
|
flags: PlayerPositionFlags::new(false, false, false, false, false),
|
2022-05-16 21:09:51 +10:00
|
|
|
teleport_id: VarInt((self.teleport_id_counter - 1) as i32),
|
|
|
|
dismount_vehicle: false,
|
|
|
|
});
|
|
|
|
}
|
2022-05-16 19:36:14 +10:00
|
|
|
|
2022-07-18 14:29:44 +10:00
|
|
|
// Set velocity. Do this after teleporting since teleporting sets velocity to
|
|
|
|
// zero.
|
|
|
|
if self.flags.velocity_modified() {
|
|
|
|
self.flags.set_velocity_modified(false);
|
|
|
|
|
|
|
|
self.send_packet(SetEntityMotion {
|
|
|
|
entity_id: VarInt(0),
|
|
|
|
velocity: velocity_to_packet_units(self.velocity),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-15 21:21:32 +10:00
|
|
|
// Send chat messages.
|
2022-07-02 12:18:59 +10:00
|
|
|
for msg in self.msgs_to_send.drain(..) {
|
|
|
|
send_packet(
|
|
|
|
&mut self.send,
|
|
|
|
SystemChat {
|
|
|
|
chat: msg,
|
2022-07-06 12:21:52 +10:00
|
|
|
kind: VarInt(0),
|
2022-07-02 12:18:59 +10:00
|
|
|
},
|
|
|
|
);
|
2022-06-30 14:33:42 +10:00
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
let mut entities_to_unload = Vec::new();
|
|
|
|
|
|
|
|
// Update all entities that are visible and unload entities that are no
|
|
|
|
// longer visible.
|
2022-06-30 04:09:00 +10:00
|
|
|
self.loaded_entities.retain(|&id| {
|
2022-05-16 19:36:14 +10:00
|
|
|
if let Some(entity) = entities.get(id) {
|
2022-07-06 12:21:52 +10:00
|
|
|
debug_assert!(entity.kind() != EntityKind::Marker);
|
2022-07-04 17:24:55 +10:00
|
|
|
if self.new_position.distance(entity.position()) <= view_dist as f64 * 16.0 {
|
2022-05-17 04:53:21 +10:00
|
|
|
if let Some(meta) = entity.updated_metadata_packet(id) {
|
2022-06-30 04:09:00 +10:00
|
|
|
send_packet(&mut self.send, meta);
|
2022-05-17 04:53:21 +10:00
|
|
|
}
|
2022-05-17 19:58:43 +10:00
|
|
|
|
|
|
|
let position_delta = entity.position() - entity.old_position();
|
|
|
|
let needs_teleport = position_delta.map(f64::abs).reduce_partial_max() >= 8.0;
|
|
|
|
let flags = entity.flags();
|
|
|
|
|
|
|
|
if entity.position() != entity.old_position()
|
|
|
|
&& !needs_teleport
|
|
|
|
&& flags.yaw_or_pitch_modified()
|
|
|
|
{
|
|
|
|
send_packet(
|
2022-06-30 04:09:00 +10:00
|
|
|
&mut self.send,
|
2022-07-01 07:18:29 +10:00
|
|
|
MoveEntityPositionAndRotation {
|
2022-05-17 19:58:43 +10:00
|
|
|
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(
|
2022-06-30 04:09:00 +10:00
|
|
|
&mut self.send,
|
2022-07-01 07:18:29 +10:00
|
|
|
MoveEntityPosition {
|
2022-05-17 19:58:43 +10:00
|
|
|
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(
|
2022-06-30 04:09:00 +10:00
|
|
|
&mut self.send,
|
2022-07-01 07:18:29 +10:00
|
|
|
MoveEntityRotation {
|
2022-05-17 19:58:43 +10:00
|
|
|
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(
|
2022-06-30 04:09:00 +10:00
|
|
|
&mut self.send,
|
2022-07-01 07:18:29 +10:00
|
|
|
TeleportEntity {
|
2022-05-17 19:58:43 +10:00
|
|
|
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(
|
2022-06-30 04:09:00 +10:00
|
|
|
&mut self.send,
|
2022-07-01 07:18:29 +10:00
|
|
|
SetEntityMotion {
|
2022-05-17 19:58:43 +10:00
|
|
|
entity_id: VarInt(id.to_network_id()),
|
|
|
|
velocity: velocity_to_packet_units(entity.velocity()),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags.head_yaw_modified() {
|
|
|
|
send_packet(
|
2022-06-30 04:09:00 +10:00
|
|
|
&mut self.send,
|
2022-07-01 07:18:29 +10:00
|
|
|
RotateHead {
|
2022-05-17 19:58:43 +10:00
|
|
|
entity_id: VarInt(id.to_network_id()),
|
|
|
|
head_yaw: ByteAngle::from_degrees(entity.head_yaw()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:08:40 +10:00
|
|
|
send_entity_events(&mut self.send, id, entity);
|
2022-07-04 17:24:55 +10:00
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entities_to_unload.push(VarInt(id.to_network_id()));
|
|
|
|
false
|
|
|
|
});
|
|
|
|
|
|
|
|
if !entities_to_unload.is_empty() {
|
2022-07-01 07:18:29 +10:00
|
|
|
self.send_packet(RemoveEntities {
|
2022-05-16 19:36:14 +10:00
|
|
|
entities: entities_to_unload,
|
2022-04-15 07:55:45 +10:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-30 06:00:41 +10:00
|
|
|
// Update the client's own player metadata.
|
2022-06-30 14:33:42 +10:00
|
|
|
let mut data = Vec::new();
|
2022-07-06 11:08:40 +10:00
|
|
|
self.player_data.updated_metadata(&mut data);
|
2022-06-30 06:00:41 +10:00
|
|
|
|
2022-06-30 14:33:42 +10:00
|
|
|
if !data.is_empty() {
|
|
|
|
data.push(0xff);
|
2022-06-30 06:00:41 +10:00
|
|
|
|
2022-07-01 07:18:29 +10:00
|
|
|
self.send_packet(SetEntityMetadata {
|
2022-06-30 14:33:42 +10:00
|
|
|
entity_id: VarInt(0),
|
|
|
|
metadata: RawBytes(data),
|
|
|
|
});
|
2022-06-30 06:00:41 +10:00
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
// Spawn new entities within the view distance.
|
2022-06-19 18:01:48 +10:00
|
|
|
let pos = self.position();
|
2022-07-04 08:31:24 +10:00
|
|
|
world.spatial_index.query::<_, _, ()>(
|
2022-06-20 01:40:37 +10:00
|
|
|
|bb| bb.projected_point(pos).distance(pos) <= view_dist as f64 * 16.0,
|
2022-06-19 18:01:48 +10:00
|
|
|
|id, _| {
|
2022-07-04 17:48:21 +10:00
|
|
|
let entity = entities
|
|
|
|
.get(id)
|
2022-07-18 14:29:44 +10:00
|
|
|
.expect("entity IDs in spatial index should be valid at this point");
|
2022-07-06 12:21:52 +10:00
|
|
|
if entity.kind() != EntityKind::Marker
|
2022-07-04 17:48:21 +10:00
|
|
|
&& entity.uuid() != self.uuid
|
|
|
|
&& self.loaded_entities.insert(id)
|
|
|
|
{
|
2022-07-04 17:24:55 +10:00
|
|
|
self.send_packet(
|
|
|
|
entity
|
|
|
|
.spawn_packet(id)
|
|
|
|
.expect("should not be a marker entity"),
|
|
|
|
);
|
2022-06-20 01:40:37 +10:00
|
|
|
|
2022-07-04 17:24:55 +10:00
|
|
|
if let Some(meta) = entity.initial_metadata_packet(id) {
|
|
|
|
self.send_packet(meta);
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:08:40 +10:00
|
|
|
send_entity_events(&mut self.send, id, entity);
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
2022-06-19 18:01:48 +10:00
|
|
|
None
|
|
|
|
},
|
|
|
|
);
|
2022-05-16 19:36:14 +10:00
|
|
|
|
2022-07-06 11:08:40 +10:00
|
|
|
for &code in self.player_data.event_codes() {
|
|
|
|
if code <= ENTITY_EVENT_MAX_BOUND as u8 {
|
|
|
|
send_packet(
|
|
|
|
&mut self.send,
|
|
|
|
EntityEvent {
|
|
|
|
entity_id: 0,
|
|
|
|
entity_status: BoundedInt(code),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2022-07-15 21:21:32 +10:00
|
|
|
// Don't bother sending animations for self since it shouldn't have
|
|
|
|
// any effect.
|
2022-07-04 16:17:51 +10:00
|
|
|
}
|
|
|
|
|
2022-07-18 14:29:44 +10:00
|
|
|
self.player_data.clear_modifications();
|
2022-06-30 04:09:00 +10:00
|
|
|
self.old_position = self.new_position;
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:08:40 +10:00
|
|
|
type SendOpt = Option<Sender<S2cPlayPacket>>;
|
|
|
|
|
|
|
|
fn send_packet(send_opt: &mut SendOpt, pkt: impl Into<S2cPlayPacket>) {
|
2022-04-15 07:55:45 +10:00
|
|
|
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(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
fn send_entity_events<C: Config>(send_opt: &mut SendOpt, id: EntityId, entity: &Entity<C>) {
|
|
|
|
for &code in entity.state.event_codes() {
|
2022-07-06 11:08:40 +10:00
|
|
|
if code <= ENTITY_EVENT_MAX_BOUND as u8 {
|
|
|
|
send_packet(
|
|
|
|
send_opt,
|
|
|
|
EntityEvent {
|
|
|
|
entity_id: id.to_network_id(),
|
|
|
|
entity_status: BoundedInt(code),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
send_packet(
|
|
|
|
send_opt,
|
|
|
|
Animate {
|
|
|
|
entity_id: VarInt(id.to_network_id()),
|
|
|
|
animation: BoundedInt(code - ENTITY_EVENT_MAX_BOUND as u8 - 1),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
fn make_registry_codec<C: Config>(shared: &SharedServer<C>) -> RegistryCodec {
|
2022-04-15 07:55:45 +10:00
|
|
|
let mut dims = Vec::new();
|
2022-07-04 08:31:24 +10:00
|
|
|
for (id, dim) in shared.dimensions() {
|
2022-04-15 07:55:45 +10:00
|
|
|
let id = id.0 as i32;
|
|
|
|
dims.push(DimensionTypeRegistryEntry {
|
|
|
|
name: ident!("{LIBRARY_NAMESPACE}:dimension_type_{id}"),
|
|
|
|
id,
|
2022-07-11 22:08:02 +10:00
|
|
|
element: dim.to_dimension_registry_item(),
|
2022-04-15 07:55:45 +10:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
let mut biomes: Vec<_> = shared
|
|
|
|
.biomes()
|
|
|
|
.map(|(id, biome)| biome.to_biome_registry_item(id.0 as i32))
|
|
|
|
.collect();
|
2022-04-15 07:55:45 +10:00
|
|
|
|
|
|
|
// The client needs a biome named "minecraft:plains" in the registry to
|
|
|
|
// connect. This is probably a bug.
|
|
|
|
//
|
|
|
|
// If the issue is resolved, just delete this block.
|
|
|
|
if !biomes.iter().any(|b| b.name == ident!("plains")) {
|
|
|
|
let biome = Biome::default();
|
|
|
|
assert_eq!(biome.name, ident!("plains"));
|
2022-07-11 22:08:02 +10:00
|
|
|
biomes.push(biome.to_biome_registry_item(biomes.len() as i32));
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
2022-07-03 06:41:45 +10:00
|
|
|
|
2022-06-21 21:55:32 +10:00
|
|
|
RegistryCodec {
|
2022-04-15 07:55:45 +10:00
|
|
|
dimension_type_registry: DimensionTypeRegistry {
|
2022-07-06 12:21:52 +10:00
|
|
|
kind: ident!("dimension_type"),
|
2022-04-15 07:55:45 +10:00
|
|
|
value: dims,
|
|
|
|
},
|
|
|
|
biome_registry: BiomeRegistry {
|
2022-07-06 12:21:52 +10:00
|
|
|
kind: ident!("worldgen/biome"),
|
2022-04-15 07:55:45 +10:00
|
|
|
value: biomes,
|
|
|
|
},
|
2022-06-21 21:55:32 +10:00
|
|
|
chat_type_registry: ChatTypeRegistry {
|
2022-07-06 12:21:52 +10:00
|
|
|
kind: ident!("chat_type"),
|
2022-07-02 12:18:59 +10:00
|
|
|
value: vec![ChatTypeRegistryEntry {
|
|
|
|
name: ident!("system"),
|
|
|
|
id: 0,
|
|
|
|
element: ChatType {
|
|
|
|
chat: ChatTypeChat {},
|
|
|
|
narration: ChatTypeNarration {
|
2022-07-11 22:08:02 +10:00
|
|
|
priority: "system".into(),
|
2022-07-02 12:18:59 +10:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}],
|
2022-06-21 21:55:32 +10:00
|
|
|
},
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|