Add a null state to some IDs

This commit is contained in:
Ryan 2022-07-03 18:02:00 -07:00
parent 622f15e71d
commit 04ce5366c8
4 changed files with 36 additions and 9 deletions

View file

@ -86,8 +86,13 @@ impl Clients {
} }
} }
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct ClientId(Key); pub struct ClientId(Key);
impl ClientId {
pub const NULL: Self = Self(Key::NULL);
}
/// Represents a client connected to the server after logging in. /// Represents a client connected to the server after logging in.
pub struct Client { pub struct Client {
/// Setting this to `None` disconnects the client. /// Setting this to `None` disconnects the client.
@ -98,8 +103,8 @@ pub struct Client {
uuid: Uuid, uuid: Uuid,
username: String, username: String,
textures: Option<SignedPlayerTextures>, textures: Option<SignedPlayerTextures>,
new_world: Option<WorldId>, new_world: WorldId,
old_world: Option<WorldId>, old_world: WorldId,
on_ground: bool, on_ground: bool,
new_position: Vec3<f64>, new_position: Vec3<f64>,
old_position: Vec3<f64>, old_position: Vec3<f64>,
@ -157,8 +162,8 @@ impl Client {
uuid: ncd.uuid, uuid: ncd.uuid,
username: ncd.username, username: ncd.username,
textures: ncd.textures, textures: ncd.textures,
new_world: None, new_world: WorldId::default(),
old_world: None, old_world: WorldId::default(),
on_ground: false, on_ground: false,
new_position: Vec3::default(), new_position: Vec3::default(),
old_position: Vec3::default(), old_position: Vec3::default(),
@ -203,12 +208,12 @@ impl Client {
self.textures.as_ref() self.textures.as_ref()
} }
pub fn world(&self) -> Option<WorldId> { pub fn world(&self) -> WorldId {
self.new_world self.new_world
} }
pub fn set_world(&mut self, world: WorldId) { pub fn set_world(&mut self, world: WorldId) {
self.new_world = Some(world); self.new_world = world;
} }
/// Sends a system message to the player. /// Sends a system message to the player.
@ -573,7 +578,7 @@ impl Client {
return; return;
} }
let world = match self.new_world.and_then(|id| worlds.get(id)) { let world = match worlds.get(self.new_world) {
Some(world) => world, Some(world) => world,
None => { None => {
log::warn!( log::warn!(

View file

@ -164,10 +164,12 @@ impl Entities {
} }
} }
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct EntityId(Key); pub struct EntityId(Key);
impl EntityId { impl EntityId {
pub const NULL: Self = Self(Key::NULL);
pub(crate) fn to_network_id(self) -> i32 { pub(crate) fn to_network_id(self) -> i32 {
self.0.version().get() as i32 self.0.version().get() as i32
} }

View file

@ -23,6 +23,22 @@ pub struct Key {
version: NonZeroU32, version: NonZeroU32,
} }
impl Key {
pub const NULL: Self = Self {
index: u32::MAX,
version: match NonZeroU32::new(u32::MAX) {
Some(n) => n,
None => unreachable!(),
},
};
}
impl Default for Key {
fn default() -> Self {
Self::NULL
}
}
impl Key { impl Key {
pub fn new(index: u32, version: NonZeroU32) -> Self { pub fn new(index: u32, version: NonZeroU32) -> Self {
Self { index, version } Self { index, version }

View file

@ -11,9 +11,13 @@ pub struct Worlds {
server: SharedServer, server: SharedServer,
} }
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct WorldId(Key); pub struct WorldId(Key);
impl WorldId {
pub const NULL: Self = Self(Key::NULL);
}
impl Worlds { impl Worlds {
pub(crate) fn new(server: SharedServer) -> Self { pub(crate) fn new(server: SharedServer) -> Self {
Self { Self {