azalea_world/
world.rs

1use std::{
2    collections::{HashMap, HashSet},
3    fmt::{self, Debug, Display},
4    hash::{Hash, Hasher},
5    io::{self, Cursor},
6};
7
8use azalea_block::{BlockState, fluid_state::FluidState};
9use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError};
10use azalea_core::{
11    position::{BlockPos, ChunkPos},
12    registry_holder::RegistryHolder,
13};
14use azalea_registry::Biome;
15use bevy_ecs::{component::Component, entity::Entity};
16use derive_more::{Deref, DerefMut};
17use nohash_hasher::IntMap;
18#[cfg(feature = "serde")]
19use serde::{Deserialize, Serialize};
20
21use crate::{ChunkStorage, PartialChunkStorage};
22
23/// PartialInstances are usually owned by clients, and hold strong references to
24/// chunks and entities in [`Instance`]s.
25///
26/// Basically, they hold the chunks and entities that are within render
27/// distance but can still access chunks and entities owned by other
28/// `PartialInstance`s that have the same `Instance`.
29///
30/// This is primarily useful for having multiple clients in the same Instance.
31pub struct PartialInstance {
32    pub chunks: PartialChunkStorage,
33    /// Some metadata about entities, like what entities are in certain chunks.
34    /// This does not contain the entity data itself, that's in the ECS.
35    pub entity_infos: PartialEntityInfos,
36}
37
38impl PartialInstance {
39    pub fn new(chunk_radius: u32, owner_entity: Option<Entity>) -> Self {
40        PartialInstance {
41            chunks: PartialChunkStorage::new(chunk_radius),
42            entity_infos: PartialEntityInfos::new(owner_entity),
43        }
44    }
45
46    /// Clears the internal references to chunks in the PartialInstance and
47    /// resets the view center.
48    pub fn reset(&mut self) {
49        self.chunks = PartialChunkStorage::new(self.chunks.chunk_radius);
50    }
51}
52
53/// An entity ID used by Minecraft.
54///
55/// These IDs are picked by the server. Some server softwares (like Bungeecord)
56/// may pick entity IDs per-player, so you should avoid relying on them for
57/// identifying IDs (especially if you're using a shared world -- i.e. a swarm).
58///
59/// You might find [`Entity`] more useful, since that's an ID decided by us that
60/// is likely to be correct across shared worlds. You could also use the
61/// `EntityUuid` from `azalea_entity`, that one is unlikely to change even
62/// across server restarts.
63///
64/// This serializes as a i32. Usually it's a VarInt in the protocol, but not
65/// always. If you do need it to serialize as a VarInt, make sure to use use the
66/// `#[var]` attribute.
67///
68/// [`Entity`]: bevy_ecs::entity::Entity
69#[derive(Component, Copy, Clone, Debug, Default, PartialEq, Eq, Deref, DerefMut)]
70#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
71pub struct MinecraftEntityId(pub i32);
72
73impl Hash for MinecraftEntityId {
74    fn hash<H: Hasher>(&self, hasher: &mut H) {
75        hasher.write_i32(self.0);
76    }
77}
78impl nohash_hasher::IsEnabled for MinecraftEntityId {}
79
80// we can't have the default be #[var] because mojang doesn't use varints for
81// entities sometimes :(
82impl AzaleaRead for MinecraftEntityId {
83    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
84        i32::azalea_read(buf).map(MinecraftEntityId)
85    }
86}
87impl AzaleaWrite for MinecraftEntityId {
88    fn azalea_write(&self, buf: &mut impl io::Write) -> io::Result<()> {
89        i32::azalea_write(&self.0, buf)
90    }
91}
92impl AzaleaReadVar for MinecraftEntityId {
93    fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
94        i32::azalea_read_var(buf).map(MinecraftEntityId)
95    }
96}
97impl AzaleaWriteVar for MinecraftEntityId {
98    fn azalea_write_var(&self, buf: &mut impl io::Write) -> io::Result<()> {
99        i32::azalea_write_var(&self.0, buf)
100    }
101}
102impl Display for MinecraftEntityId {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        write!(f, "eid({})", self.0)
105    }
106}
107impl From<i32> for MinecraftEntityId {
108    fn from(id: i32) -> Self {
109        Self(id)
110    }
111}
112impl From<u32> for MinecraftEntityId {
113    fn from(id: u32) -> Self {
114        Self(id as i32)
115    }
116}
117
118/// Keep track of certain metadatas that are only relevant for this partial
119/// world.
120#[derive(Debug, Default)]
121pub struct PartialEntityInfos {
122    /// The entity ID of the player that owns this partial world.
123    ///
124    /// This will make `RelativeEntityUpdate` pretend this entity doesn't exist
125    /// so it doesn't get modified from outside sources.
126    pub owner_entity: Option<Entity>,
127    /// A counter for each entity that tracks how many updates we've observed
128    /// for it.
129    ///
130    /// This is used for shared worlds (i.e. swarms), to make sure we don't
131    /// update entities twice on accident.
132    pub updates_received: IntMap<MinecraftEntityId, u32>,
133    // ^ note: using MinecraftEntityId for entity ids is acceptable here since
134    // there's no chance of collisions
135}
136
137impl PartialEntityInfos {
138    pub fn new(owner_entity: Option<Entity>) -> Self {
139        Self {
140            owner_entity,
141            updates_received: IntMap::default(),
142        }
143    }
144}
145
146/// A world where the chunks are stored as weak pointers. This is used for
147/// shared worlds.
148///
149/// Also see [`PartialInstance`].
150///
151/// This is sometimes interchangeably called a "world". However, this type is
152/// called `Instance` to avoid colliding with the `World` type from Bevy ECS.
153#[derive(Default, Debug)]
154pub struct Instance {
155    pub chunks: ChunkStorage,
156
157    /// An index of all the entities we know are in the chunks of the world
158    pub entities_by_chunk: HashMap<ChunkPos, HashSet<Entity>>,
159
160    /// An index of Minecraft entity IDs to Azalea ECS entities.
161    ///
162    /// You should avoid using this (particularly if you're using swarms) and
163    /// instead use `azalea_entity::EntityIdIndex`, since some servers may
164    /// give different entity IDs for the same entities to different
165    /// players.
166    pub entity_by_id: IntMap<MinecraftEntityId, Entity>,
167
168    pub registries: RegistryHolder,
169}
170
171impl Instance {
172    pub fn get_block_state(&self, pos: BlockPos) -> Option<BlockState> {
173        self.chunks.get_block_state(pos)
174    }
175
176    pub fn get_fluid_state(&self, pos: BlockPos) -> Option<FluidState> {
177        self.chunks.get_block_state(pos).map(FluidState::from)
178    }
179
180    /// Get the biome at the given position.
181    ///
182    /// You can then use `Client::with_resolved_registry` to get the name and
183    /// data from the biome.
184    ///
185    /// Note that biomes are internally stored as 4x4x4 blocks, so if you're
186    /// writing code that searches for a specific biome it'll probably be more
187    /// efficient to avoid scanning every single block.
188    pub fn get_biome(&self, pos: BlockPos) -> Option<Biome> {
189        self.chunks.get_biome(pos)
190    }
191
192    pub fn set_block_state(&self, pos: BlockPos, state: BlockState) -> Option<BlockState> {
193        self.chunks.set_block_state(pos, state)
194    }
195}
196
197impl Debug for PartialInstance {
198    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199        f.debug_struct("PartialInstance")
200            .field("chunks", &self.chunks)
201            .field("entity_infos", &self.entity_infos)
202            .finish()
203    }
204}
205
206impl Default for PartialInstance {
207    /// Creates a completely self-contained `PartialInstance`. This is only for
208    /// testing and shouldn't be used in actual code!
209    fn default() -> Self {
210        let chunk_storage = PartialChunkStorage::default();
211        let entity_storage = PartialEntityInfos::default();
212        Self {
213            chunks: chunk_storage,
214            entity_infos: entity_storage,
215        }
216    }
217}
218
219impl From<ChunkStorage> for Instance {
220    /// Make an empty world from this `ChunkStorage`. This is meant to be a
221    /// convenience function for tests.
222    fn from(chunks: ChunkStorage) -> Self {
223        Self {
224            chunks,
225            entities_by_chunk: HashMap::new(),
226            entity_by_id: IntMap::default(),
227            registries: RegistryHolder::default(),
228        }
229    }
230}