diff options
| author | mat <git@matdoes.dev> | 2025-02-16 17:10:04 +0000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-02-16 17:10:04 +0000 |
| commit | 228489dded720cea566dd0a4638b39cba2aff5ec (patch) | |
| tree | ae4631e3ed42cd13b02bdcd32e81761ca79f4d0c /azalea-world | |
| parent | 4fb6b077464e14f816e4c4fe54ff648e8c3d0ede (diff) | |
| download | azalea-drasl-228489dded720cea566dd0a4638b39cba2aff5ec.tar.xz | |
use MinecraftEntityId type instead of u32 in az-protocol
Diffstat (limited to 'azalea-world')
| -rw-r--r-- | azalea-world/src/world.rs | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/azalea-world/src/world.rs b/azalea-world/src/world.rs index 298bd598..ffd60026 100644 --- a/azalea-world/src/world.rs +++ b/azalea-world/src/world.rs @@ -1,5 +1,6 @@ -use std::fmt::Formatter; +use std::fmt::{self, Display, Formatter}; use std::hash::{Hash, Hasher}; +use std::io::{self, Cursor}; use std::{ collections::{HashMap, HashSet}, fmt::Debug, @@ -7,6 +8,7 @@ use std::{ use azalea_block::fluid_state::FluidState; use azalea_block::BlockState; +use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; use azalea_core::position::{BlockPos, ChunkPos}; use azalea_core::registry_holder::RegistryHolder; use bevy_ecs::{component::Component, entity::Entity}; @@ -39,20 +41,60 @@ impl PartialInstance { } } -/// An entity ID used by Minecraft. These are not guaranteed to be unique in -/// shared worlds, that's what [`Entity`] is for. +/// An entity ID used by Minecraft. +/// +/// These IDs are picked by the server. Some server softwares (like Bungeecord) +/// may pick entity IDs per-player, so you should avoid relying on them for +/// identifying IDs (especially if you're using a shared world -- i.e. a swarm). +/// +/// You might find [`Entity`] more useful, since that's an ID decided by us that +/// is likely to be correct across shared worlds. You could also use the +/// `EntityUuid` from `azalea_entity`, that one is unlikely to change even +/// across server restarts. +/// +/// This serializes as a i32. Usually it's a VarInt in the protocol, but not +/// always. If you do need it to serialize as a VarInt, make sure to use use the +/// `#[var]` attribute. /// /// [`Entity`]: bevy_ecs::entity::Entity #[derive(Component, Copy, Clone, Debug, PartialEq, Eq, Deref, DerefMut)] -pub struct MinecraftEntityId(pub u32); +pub struct MinecraftEntityId(pub i32); impl Hash for MinecraftEntityId { fn hash<H: Hasher>(&self, hasher: &mut H) { - hasher.write_u32(self.0); + hasher.write_i32(self.0); } } impl nohash_hasher::IsEnabled for MinecraftEntityId {} +// we can't have the default be #[var] because mojang doesn't use varints for +// entities sometimes :( +impl AzaleaRead for MinecraftEntityId { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + i32::azalea_read(buf).map(MinecraftEntityId) + } +} +impl AzaleaWrite for MinecraftEntityId { + fn azalea_write(&self, buf: &mut impl io::Write) -> Result<(), io::Error> { + i32::azalea_write(&self.0, buf) + } +} +impl AzaleaReadVar for MinecraftEntityId { + fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + i32::azalea_read_var(buf).map(MinecraftEntityId) + } +} +impl AzaleaWriteVar for MinecraftEntityId { + fn azalea_write_var(&self, buf: &mut impl io::Write) -> Result<(), io::Error> { + i32::azalea_write_var(&self.0, buf) + } +} +impl Display for MinecraftEntityId { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "eid({})", self.0) + } +} + /// Keep track of certain metadatas that are only relevant for this partial /// world. #[derive(Debug, Default)] |
