1use std::{
2 fmt::{self, Display},
3 hash::{Hash, Hasher},
4 io::{self, Cursor},
5};
6
7use azalea_buf::{AzBuf, AzBufVar, BufReadError};
8use derive_more::{Deref, DerefMut};
9
10#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
31#[cfg_attr(feature = "bevy_ecs", derive(bevy_ecs::component::Component))]
32#[derive(Clone, Copy, Debug, Default, Deref, DerefMut, Eq, PartialEq)]
33pub struct MinecraftEntityId(pub i32);
34
35impl Hash for MinecraftEntityId {
36 fn hash<H: Hasher>(&self, hasher: &mut H) {
37 hasher.write_i32(self.0);
38 }
39}
40impl nohash_hasher::IsEnabled for MinecraftEntityId {}
41
42impl AzBuf for MinecraftEntityId {
45 fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
46 i32::azalea_read(buf).map(MinecraftEntityId)
47 }
48 fn azalea_write(&self, buf: &mut impl io::Write) -> io::Result<()> {
49 i32::azalea_write(&self.0, buf)
50 }
51}
52impl AzBufVar for MinecraftEntityId {
53 fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
54 i32::azalea_read_var(buf).map(MinecraftEntityId)
55 }
56 fn azalea_write_var(&self, buf: &mut impl io::Write) -> io::Result<()> {
57 i32::azalea_write_var(&self.0, buf)
58 }
59}
60impl Display for MinecraftEntityId {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 write!(f, "eid({})", self.0)
63 }
64}
65impl From<i32> for MinecraftEntityId {
66 fn from(id: i32) -> Self {
67 Self(id)
68 }
69}
70impl From<u32> for MinecraftEntityId {
71 fn from(id: u32) -> Self {
72 Self(id as i32)
73 }
74}