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    // note: using MinecraftEntityId for entity ids is acceptable here since
123    // there's no chance of collisions here
124    /// The entity id of the player that owns this partial world. This will
125    /// make `RelativeEntityUpdate` pretend this entity doesn't exist so
126    /// it doesn't get modified from outside sources.
127    pub owner_entity: Option<Entity>,
128    /// A counter for each entity that tracks how many updates we've observed
129    /// for it.
130    ///
131    /// This is used for shared worlds (i.e. swarms), to make sure we don't
132    /// update entities twice on accident.
133    pub updates_received: IntMap<MinecraftEntityId, u32>,
134}
135
136impl PartialEntityInfos {
137    pub fn new(owner_entity: Option<Entity>) -> Self {
138        Self {
139            owner_entity,
140            updates_received: IntMap::default(),
141        }
142    }
143}
144
145/// A world where the chunks are stored as weak pointers. This is used for
146/// shared worlds.
147///
148/// Also see [`PartialInstance`].
149///
150/// This is sometimes interchangably called a "world". However, this type is
151/// called `Instance` to avoid colliding with the `World` type from Bevy ECS.
152#[derive(Default, Debug)]
153pub struct Instance {
154    pub chunks: ChunkStorage,
155
156    /// An index of all the entities we know are in the chunks of the world
157    pub entities_by_chunk: HashMap<ChunkPos, HashSet<Entity>>,
158
159    /// An index of Minecraft entity IDs to Azalea ECS entities.
160    ///
161    /// You should avoid using this (particularly if you're using swarms) and
162    /// instead use `azalea_entity::EntityIdIndex`, since some servers may
163    /// give different entity IDs for the same entities to different
164    /// players.
165    pub entity_by_id: IntMap<MinecraftEntityId, Entity>,
166
167    pub registries: RegistryHolder,
168}
169
170impl Instance {
171    pub fn get_block_state(&self, pos: BlockPos) -> Option<BlockState> {
172        self.chunks.get_block_state(pos)
173    }
174
175    pub fn get_fluid_state(&self, pos: BlockPos) -> Option<FluidState> {
176        self.chunks.get_block_state(pos).map(FluidState::from)
177    }
178
179    /// Get the biome at the given position.
180    ///
181    /// You can then use `Client::with_resolved_registry` to get the name and
182    /// data from the biome.
183    ///
184    /// Note that biomes are internally stored as 4x4x4 blocks, so if you're
185    /// writing code that searches for a specific biome it'll probably be more
186    /// efficient to avoid scanning every single block.
187    pub fn get_biome(&self, pos: BlockPos) -> Option<Biome> {
188        self.chunks.get_biome(pos)
189    }
190
191    pub fn set_block_state(&self, pos: BlockPos, state: BlockState) -> Option<BlockState> {
192        self.chunks.set_block_state(pos, state)
193    }
194}
195
196impl Debug for PartialInstance {
197    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198        f.debug_struct("PartialInstance")
199            .field("chunks", &self.chunks)
200            .field("entity_infos", &self.entity_infos)
201            .finish()
202    }
203}
204
205impl Default for PartialInstance {
206    /// Creates a completely self-contained `PartialInstance`. This is only for
207    /// testing and shouldn't be used in actual code!
208    fn default() -> Self {
209        let chunk_storage = PartialChunkStorage::default();
210        let entity_storage = PartialEntityInfos::default();
211        Self {
212            chunks: chunk_storage,
213            entity_infos: entity_storage,
214        }
215    }
216}
217
218impl From<ChunkStorage> for Instance {
219    /// Make an empty world from this `ChunkStorage`. This is meant to be a
220    /// convenience function for tests.
221    fn from(chunks: ChunkStorage) -> Self {
222        Self {
223            chunks,
224            entities_by_chunk: HashMap::new(),
225            entity_by_id: IntMap::default(),
226            registries: RegistryHolder::default(),
227        }
228    }
229}