From 228489dded720cea566dd0a4638b39cba2aff5ec Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 16 Feb 2025 17:10:04 +0000 Subject: [PATCH] use MinecraftEntityId type instead of u32 in az-protocol --- azalea-client/src/attack.rs | 2 +- azalea-client/src/movement.rs | 2 +- azalea-client/src/packet_handling/game.rs | 22 ++++---- azalea-client/tests/simulation.rs | 4 +- .../src/packets/game/c_add_entity.rs | 5 +- .../src/packets/game/c_add_experience_orb.rs | 3 +- azalea-protocol/src/packets/game/c_animate.rs | 3 +- .../src/packets/game/c_block_destruction.rs | 3 +- .../src/packets/game/c_damage_event.rs | 5 +- .../src/packets/game/c_entity_event.rs | 3 +- .../packets/game/c_entity_position_sync.rs | 3 +- .../src/packets/game/c_horse_screen_open.rs | 3 +- .../src/packets/game/c_hurt_animation.rs | 3 +- azalea-protocol/src/packets/game/c_login.rs | 3 +- .../src/packets/game/c_move_entity_pos.rs | 3 +- .../src/packets/game/c_move_entity_pos_rot.rs | 3 +- .../src/packets/game/c_move_entity_rot.rs | 3 +- .../game/c_move_minecart_along_track.rs | 3 +- .../src/packets/game/c_player_combat_kill.rs | 3 +- .../src/packets/game/c_player_position.rs | 1 + .../src/packets/game/c_projectile_power.rs | 4 +- .../src/packets/game/c_remove_entities.rs | 3 +- .../src/packets/game/c_remove_mob_effect.rs | 3 +- .../src/packets/game/c_rotate_head.rs | 3 +- .../src/packets/game/c_set_camera.rs | 3 +- .../src/packets/game/c_set_entity_data.rs | 3 +- .../src/packets/game/c_set_entity_link.rs | 5 +- .../src/packets/game/c_set_entity_motion.rs | 3 +- .../src/packets/game/c_set_equipment.rs | 3 +- .../src/packets/game/c_sound_entity.rs | 3 +- .../src/packets/game/c_take_item_entity.rs | 3 +- .../src/packets/game/c_teleport_entity.rs | 4 +- .../src/packets/game/c_update_attributes.rs | 3 +- .../src/packets/game/c_update_mob_effect.rs | 3 +- .../src/packets/game/s_entity_tag_query.rs | 3 +- .../src/packets/game/s_interact.rs | 3 +- .../packets/game/s_pick_item_from_entity.rs | 3 +- .../src/packets/game/s_player_command.rs | 3 +- azalea-world/src/world.rs | 52 +++++++++++++++++-- 39 files changed, 133 insertions(+), 57 deletions(-) diff --git a/azalea-client/src/attack.rs b/azalea-client/src/attack.rs index b1b36b69..f626483f 100644 --- a/azalea-client/src/attack.rs +++ b/azalea-client/src/attack.rs @@ -86,7 +86,7 @@ pub fn handle_attack_event( send_packet_events.send(SendPacketEvent::new( event.entity, ServerboundInteract { - entity_id: *event.target, + entity_id: event.target, action: s_interact::ActionType::Attack, using_secondary_action: **sneaking, }, diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index 6308002e..3bea9de2 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -303,7 +303,7 @@ fn send_sprinting_if_needed( send_packet_events.send(SendPacketEvent::new( entity, ServerboundPlayerCommand { - id: **minecraft_entity_id, + id: *minecraft_entity_id, action: sprinting_action, data: 0, }, diff --git a/azalea-client/src/packet_handling/game.rs b/azalea-client/src/packet_handling/game.rs index f8e72a4a..207cdd91 100644 --- a/azalea-client/src/packet_handling/game.rs +++ b/azalea-client/src/packet_handling/game.rs @@ -301,7 +301,7 @@ pub fn process_packet_events(ecs: &mut World) { azalea_registry::EntityKind::Player, new_instance_name, ); - let entity_id = MinecraftEntityId(p.player_id); + let entity_id = p.player_id; // insert our components into the ecs :) commands.entity(player_entity).insert(( entity_id, @@ -643,7 +643,7 @@ pub fn process_packet_events(ecs: &mut World) { let (mut entity_id_index, instance_name, tab_list) = query.get_mut(player_entity).unwrap(); - let entity_id = MinecraftEntityId(p.id); + let entity_id = p.id; let Some(instance_name) = instance_name else { warn!("got add player packet but we haven't gotten a login packet yet"); @@ -721,7 +721,7 @@ pub fn process_packet_events(ecs: &mut World) { let (mut commands, mut query, entity_kind_query) = system_state.get_mut(ecs); let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap(); - let entity = entity_id_index.get(MinecraftEntityId(p.id)); + let entity = entity_id_index.get(p.id); let Some(entity) = entity else { // some servers like hypixel trigger this a lot :( @@ -768,7 +768,7 @@ pub fn process_packet_events(ecs: &mut World) { let (mut commands, mut query) = system_state.get_mut(ecs); let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap(); - let Some(entity) = entity_id_index.get(MinecraftEntityId(p.id)) else { + let Some(entity) = entity_id_index.get(p.id) else { // note that this log (and some other ones like the one in RemoveEntities) // sometimes happens when killing mobs. it seems to be a vanilla bug, which is // why it's a debug log instead of a warning @@ -837,7 +837,7 @@ pub fn process_packet_events(ecs: &mut World) { let (mut commands, mut query) = system_state.get_mut(ecs); let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap(); - let Some(entity) = entity_id_index.get(MinecraftEntityId(p.id)) else { + let Some(entity) = entity_id_index.get(p.id) else { warn!("Got teleport entity packet for unknown entity id {}", p.id); continue; }; @@ -883,7 +883,7 @@ pub fn process_packet_events(ecs: &mut World) { debug!("Got move entity pos packet {p:?}"); - let Some(entity) = entity_id_index.get(MinecraftEntityId(p.entity_id)) else { + let Some(entity) = entity_id_index.get(p.entity_id) else { debug!( "Got move entity pos packet for unknown entity id {}", p.entity_id @@ -924,7 +924,7 @@ pub fn process_packet_events(ecs: &mut World) { debug!("Got move entity pos rot packet {p:?}"); - let entity = entity_id_index.get(MinecraftEntityId(p.entity_id)); + let entity = entity_id_index.get(p.entity_id); if let Some(entity) = entity { let new_delta = p.delta.clone(); @@ -977,7 +977,7 @@ pub fn process_packet_events(ecs: &mut World) { let (mut commands, mut query) = system_state.get_mut(ecs); let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap(); - let entity = entity_id_index.get(MinecraftEntityId(p.entity_id)); + let entity = entity_id_index.get(p.entity_id); if let Some(entity) = entity { let new_look_direction = LookDirection { @@ -1040,7 +1040,7 @@ pub fn process_packet_events(ecs: &mut World) { }; for &id in &p.entity_ids { - let Some(entity) = entity_id_index.remove(MinecraftEntityId(id)) else { + let Some(entity) = entity_id_index.remove(id) else { debug!("Tried to remove entity with id {id} but it wasn't in the EntityIdIndex"); continue; }; @@ -1334,7 +1334,7 @@ pub fn process_packet_events(ecs: &mut World) { let (mut commands, mut query, mut death_events) = system_state.get_mut(ecs); let (entity_id, dead) = query.get_mut(player_entity).unwrap(); - if **entity_id == p.player_id && dead.is_none() { + if *entity_id == p.player_id && dead.is_none() { commands.entity(player_entity).insert(Dead); death_events.send(DeathEvent { entity: player_entity, @@ -1475,7 +1475,7 @@ pub fn process_packet_events(ecs: &mut World) { let (mut commands, mut query) = system_state.get_mut(ecs); let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap(); - let Some(entity) = entity_id_index.get(MinecraftEntityId(p.id)) else { + let Some(entity) = entity_id_index.get(p.id) else { debug!("Got teleport entity packet for unknown entity id {}", p.id); continue; }; diff --git a/azalea-client/tests/simulation.rs b/azalea-client/tests/simulation.rs index 257e507e..593622aa 100644 --- a/azalea-client/tests/simulation.rs +++ b/azalea-client/tests/simulation.rs @@ -22,7 +22,7 @@ use azalea_protocol::packets::{ ConnectionProtocol, Packet, ProtocolPacket, }; use azalea_registry::DimensionType; -use azalea_world::Instance; +use azalea_world::{Instance, MinecraftEntityId}; use bevy_app::App; use bevy_app::PluginGroup; use bevy_ecs::{prelude::*, schedule::ExecutorKind}; @@ -67,7 +67,7 @@ fn test_set_health_before_login() { assert_eq!(*simulation.component::(), 15.); simulation.receive_packet(ClientboundLogin { - player_id: 0, + player_id: MinecraftEntityId(0), hardcore: false, levels: vec![], max_players: 20, diff --git a/azalea-protocol/src/packets/game/c_add_entity.rs b/azalea-protocol/src/packets/game/c_add_entity.rs index cf0adeb7..67615fb9 100755 --- a/azalea-protocol/src/packets/game/c_add_entity.rs +++ b/azalea-protocol/src/packets/game/c_add_entity.rs @@ -2,13 +2,14 @@ use azalea_buf::AzBuf; use azalea_core::{position::Vec3, resource_location::ResourceLocation}; use azalea_entity::{metadata::apply_default_metadata, EntityBundle}; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; use uuid::Uuid; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundAddEntity { - /// The id of the entity. + /// The numeric ID of the entity being added to the world. #[var] - pub id: u32, + pub id: MinecraftEntityId, pub uuid: Uuid, pub entity_type: azalea_registry::EntityKind, pub position: Vec3, diff --git a/azalea-protocol/src/packets/game/c_add_experience_orb.rs b/azalea-protocol/src/packets/game/c_add_experience_orb.rs index 195ccc5a..295e1010 100755 --- a/azalea-protocol/src/packets/game/c_add_experience_orb.rs +++ b/azalea-protocol/src/packets/game/c_add_experience_orb.rs @@ -1,11 +1,12 @@ use azalea_buf::AzBuf; use azalea_core::position::Vec3; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundAddExperienceOrb { #[var] - pub id: u32, + pub id: MinecraftEntityId, pub pos: Vec3, pub value: u16, } diff --git a/azalea-protocol/src/packets/game/c_animate.rs b/azalea-protocol/src/packets/game/c_animate.rs index bda0b152..eaaa151d 100755 --- a/azalea-protocol/src/packets/game/c_animate.rs +++ b/azalea-protocol/src/packets/game/c_animate.rs @@ -1,10 +1,11 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundAnimate { #[var] - pub id: u32, + pub id: MinecraftEntityId, pub action: AnimationAction, } diff --git a/azalea-protocol/src/packets/game/c_block_destruction.rs b/azalea-protocol/src/packets/game/c_block_destruction.rs index 50d8085b..4a6e5f69 100755 --- a/azalea-protocol/src/packets/game/c_block_destruction.rs +++ b/azalea-protocol/src/packets/game/c_block_destruction.rs @@ -1,12 +1,13 @@ use azalea_buf::AzBuf; use azalea_core::position::BlockPos; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundBlockDestruction { /// The ID of the entity breaking the block. #[var] - pub id: u32, + pub id: MinecraftEntityId, pub pos: BlockPos, /// 0–9 to set it, any other value to remove it. pub progress: u8, diff --git a/azalea-protocol/src/packets/game/c_damage_event.rs b/azalea-protocol/src/packets/game/c_damage_event.rs index a736f39a..d4393685 100644 --- a/azalea-protocol/src/packets/game/c_damage_event.rs +++ b/azalea-protocol/src/packets/game/c_damage_event.rs @@ -1,13 +1,14 @@ use std::io::{Cursor, Write}; -use azalea_buf::{AzBuf, AzaleaRead, AzaleaReadVar, AzaleaWriteVar, AzaleaWrite}; +use azalea_buf::{AzBuf, AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar}; use azalea_core::position::Vec3; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundDamageEvent { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, #[var] pub source_type_id: u32, pub source_cause_id: OptionalEntityId, diff --git a/azalea-protocol/src/packets/game/c_entity_event.rs b/azalea-protocol/src/packets/game/c_entity_event.rs index 13efbabd..6eb08adc 100755 --- a/azalea-protocol/src/packets/game/c_entity_event.rs +++ b/azalea-protocol/src/packets/game/c_entity_event.rs @@ -1,8 +1,9 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundEntityEvent { - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub event_id: u8, } diff --git a/azalea-protocol/src/packets/game/c_entity_position_sync.rs b/azalea-protocol/src/packets/game/c_entity_position_sync.rs index 0e8bea01..e3ecc3f0 100755 --- a/azalea-protocol/src/packets/game/c_entity_position_sync.rs +++ b/azalea-protocol/src/packets/game/c_entity_position_sync.rs @@ -1,12 +1,13 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; use crate::common::movements::PositionMoveRotation; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundEntityPositionSync { #[var] - pub id: u32, + pub id: MinecraftEntityId, pub values: PositionMoveRotation, pub on_ground: bool, } diff --git a/azalea-protocol/src/packets/game/c_horse_screen_open.rs b/azalea-protocol/src/packets/game/c_horse_screen_open.rs index 10a89725..53730d24 100755 --- a/azalea-protocol/src/packets/game/c_horse_screen_open.rs +++ b/azalea-protocol/src/packets/game/c_horse_screen_open.rs @@ -1,5 +1,6 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundHorseScreenOpen { @@ -7,5 +8,5 @@ pub struct ClientboundHorseScreenOpen { pub container_id: i32, #[var] pub inventory_columns: u32, - pub entity_id: u32, + pub entity_id: MinecraftEntityId, } diff --git a/azalea-protocol/src/packets/game/c_hurt_animation.rs b/azalea-protocol/src/packets/game/c_hurt_animation.rs index 49a32989..df019bd5 100644 --- a/azalea-protocol/src/packets/game/c_hurt_animation.rs +++ b/azalea-protocol/src/packets/game/c_hurt_animation.rs @@ -1,9 +1,10 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundHurtAnimation { #[var] - pub id: u32, + pub id: MinecraftEntityId, pub yaw: f32, } diff --git a/azalea-protocol/src/packets/game/c_login.rs b/azalea-protocol/src/packets/game/c_login.rs index c0616c55..63503697 100755 --- a/azalea-protocol/src/packets/game/c_login.rs +++ b/azalea-protocol/src/packets/game/c_login.rs @@ -1,6 +1,7 @@ use azalea_buf::AzBuf; use azalea_core::resource_location::ResourceLocation; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; use crate::packets::common::CommonPlayerSpawnInfo; @@ -10,7 +11,7 @@ use crate::packets::common::CommonPlayerSpawnInfo; /// world, and the registry. #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundLogin { - pub player_id: u32, + pub player_id: MinecraftEntityId, pub hardcore: bool, pub levels: Vec, #[var] diff --git a/azalea-protocol/src/packets/game/c_move_entity_pos.rs b/azalea-protocol/src/packets/game/c_move_entity_pos.rs index b153bb3c..790a1580 100755 --- a/azalea-protocol/src/packets/game/c_move_entity_pos.rs +++ b/azalea-protocol/src/packets/game/c_move_entity_pos.rs @@ -1,11 +1,12 @@ use azalea_buf::AzBuf; use azalea_core::delta::PositionDelta8; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundMoveEntityPos { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub delta: PositionDelta8, pub on_ground: bool, } diff --git a/azalea-protocol/src/packets/game/c_move_entity_pos_rot.rs b/azalea-protocol/src/packets/game/c_move_entity_pos_rot.rs index 7b22e290..2d76aa14 100755 --- a/azalea-protocol/src/packets/game/c_move_entity_pos_rot.rs +++ b/azalea-protocol/src/packets/game/c_move_entity_pos_rot.rs @@ -1,12 +1,13 @@ use azalea_buf::AzBuf; use azalea_core::delta::PositionDelta8; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; /// This packet is sent by the server when an entity moves less then 8 blocks. #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundMoveEntityPosRot { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub delta: PositionDelta8, pub y_rot: i8, pub x_rot: i8, diff --git a/azalea-protocol/src/packets/game/c_move_entity_rot.rs b/azalea-protocol/src/packets/game/c_move_entity_rot.rs index a8362748..e33a12e9 100755 --- a/azalea-protocol/src/packets/game/c_move_entity_rot.rs +++ b/azalea-protocol/src/packets/game/c_move_entity_rot.rs @@ -1,10 +1,11 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundMoveEntityRot { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub y_rot: i8, pub x_rot: i8, pub on_ground: bool, diff --git a/azalea-protocol/src/packets/game/c_move_minecart_along_track.rs b/azalea-protocol/src/packets/game/c_move_minecart_along_track.rs index 0a205ddd..b6313b6a 100644 --- a/azalea-protocol/src/packets/game/c_move_minecart_along_track.rs +++ b/azalea-protocol/src/packets/game/c_move_minecart_along_track.rs @@ -1,11 +1,12 @@ use azalea_buf::AzBuf; use azalea_core::position::Vec3; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundMoveMinecartAlongTrack { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub lerp_steps: Vec, } diff --git a/azalea-protocol/src/packets/game/c_player_combat_kill.rs b/azalea-protocol/src/packets/game/c_player_combat_kill.rs index fb7285b5..2d6fc3e7 100755 --- a/azalea-protocol/src/packets/game/c_player_combat_kill.rs +++ b/azalea-protocol/src/packets/game/c_player_combat_kill.rs @@ -1,11 +1,12 @@ use azalea_buf::AzBuf; use azalea_chat::FormattedText; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; /// Used to send a respawn screen. #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundPlayerCombatKill { #[var] - pub player_id: u32, + pub player_id: MinecraftEntityId, pub message: FormattedText, } diff --git a/azalea-protocol/src/packets/game/c_player_position.rs b/azalea-protocol/src/packets/game/c_player_position.rs index 8460f428..7b95fef7 100755 --- a/azalea-protocol/src/packets/game/c_player_position.rs +++ b/azalea-protocol/src/packets/game/c_player_position.rs @@ -5,6 +5,7 @@ use crate::common::movements::{PositionMoveRotation, RelativeMovements}; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundPlayerPosition { + /// The teleport ID. #[var] pub id: u32, pub change: PositionMoveRotation, diff --git a/azalea-protocol/src/packets/game/c_projectile_power.rs b/azalea-protocol/src/packets/game/c_projectile_power.rs index 8b453ae2..d7d445a2 100644 --- a/azalea-protocol/src/packets/game/c_projectile_power.rs +++ b/azalea-protocol/src/packets/game/c_projectile_power.rs @@ -1,8 +1,10 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundProjectilePower { - pub id: u32, + #[var] + pub id: MinecraftEntityId, pub acceleration_power: f64, } diff --git a/azalea-protocol/src/packets/game/c_remove_entities.rs b/azalea-protocol/src/packets/game/c_remove_entities.rs index f3eb7139..5424cb3c 100755 --- a/azalea-protocol/src/packets/game/c_remove_entities.rs +++ b/azalea-protocol/src/packets/game/c_remove_entities.rs @@ -1,8 +1,9 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundRemoveEntities { #[var] - pub entity_ids: Vec, + pub entity_ids: Vec, } diff --git a/azalea-protocol/src/packets/game/c_remove_mob_effect.rs b/azalea-protocol/src/packets/game/c_remove_mob_effect.rs index 21500db3..08d9416e 100755 --- a/azalea-protocol/src/packets/game/c_remove_mob_effect.rs +++ b/azalea-protocol/src/packets/game/c_remove_mob_effect.rs @@ -1,9 +1,10 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundRemoveMobEffect { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub effect: azalea_registry::MobEffect, } diff --git a/azalea-protocol/src/packets/game/c_rotate_head.rs b/azalea-protocol/src/packets/game/c_rotate_head.rs index 26e55948..d4dec203 100755 --- a/azalea-protocol/src/packets/game/c_rotate_head.rs +++ b/azalea-protocol/src/packets/game/c_rotate_head.rs @@ -1,9 +1,10 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundRotateHead { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub y_head_rot: i8, } diff --git a/azalea-protocol/src/packets/game/c_set_camera.rs b/azalea-protocol/src/packets/game/c_set_camera.rs index 4ce4547c..277b80a3 100755 --- a/azalea-protocol/src/packets/game/c_set_camera.rs +++ b/azalea-protocol/src/packets/game/c_set_camera.rs @@ -1,8 +1,9 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundSetCamera { #[var] - pub camera_id: u32, + pub camera_id: MinecraftEntityId, } diff --git a/azalea-protocol/src/packets/game/c_set_entity_data.rs b/azalea-protocol/src/packets/game/c_set_entity_data.rs index afa352ab..fb77163b 100755 --- a/azalea-protocol/src/packets/game/c_set_entity_data.rs +++ b/azalea-protocol/src/packets/game/c_set_entity_data.rs @@ -1,11 +1,12 @@ use azalea_buf::AzBuf; use azalea_entity::EntityMetadataItems; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundSetEntityData { #[var] - pub id: u32, + pub id: MinecraftEntityId, pub packed_items: EntityMetadataItems, } diff --git a/azalea-protocol/src/packets/game/c_set_entity_link.rs b/azalea-protocol/src/packets/game/c_set_entity_link.rs index c4ce8bdf..2a38db76 100755 --- a/azalea-protocol/src/packets/game/c_set_entity_link.rs +++ b/azalea-protocol/src/packets/game/c_set_entity_link.rs @@ -1,8 +1,9 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundSetEntityLink { - pub source_id: u32, - pub dest_id: u32, + pub source_id: MinecraftEntityId, + pub dest_id: MinecraftEntityId, } diff --git a/azalea-protocol/src/packets/game/c_set_entity_motion.rs b/azalea-protocol/src/packets/game/c_set_entity_motion.rs index d0dd7698..7a112784 100755 --- a/azalea-protocol/src/packets/game/c_set_entity_motion.rs +++ b/azalea-protocol/src/packets/game/c_set_entity_motion.rs @@ -1,10 +1,11 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundSetEntityMotion { #[var] - pub id: u32, + pub id: MinecraftEntityId, pub xa: i16, pub ya: i16, pub za: i16, diff --git a/azalea-protocol/src/packets/game/c_set_equipment.rs b/azalea-protocol/src/packets/game/c_set_equipment.rs index 637b2b52..ad691d66 100755 --- a/azalea-protocol/src/packets/game/c_set_equipment.rs +++ b/azalea-protocol/src/packets/game/c_set_equipment.rs @@ -4,11 +4,12 @@ use azalea_buf::{AzBuf, BufReadError}; use azalea_buf::{AzaleaRead, AzaleaWrite}; use azalea_inventory::ItemStack; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundSetEquipment { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub slots: EquipmentSlots, } diff --git a/azalea-protocol/src/packets/game/c_sound_entity.rs b/azalea-protocol/src/packets/game/c_sound_entity.rs index fab30b14..37ce1f16 100755 --- a/azalea-protocol/src/packets/game/c_sound_entity.rs +++ b/azalea-protocol/src/packets/game/c_sound_entity.rs @@ -1,6 +1,7 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; use azalea_registry::SoundEvent; +use azalea_world::MinecraftEntityId; use super::c_sound::{CustomSound, SoundSource}; @@ -9,7 +10,7 @@ pub struct ClientboundSoundEntity { pub sound: azalea_registry::Holder, pub source: SoundSource, #[var] - pub id: u32, + pub id: MinecraftEntityId, pub volume: f32, pub pitch: f32, pub seed: u64, diff --git a/azalea-protocol/src/packets/game/c_take_item_entity.rs b/azalea-protocol/src/packets/game/c_take_item_entity.rs index 0dfc8552..cfbe33c2 100755 --- a/azalea-protocol/src/packets/game/c_take_item_entity.rs +++ b/azalea-protocol/src/packets/game/c_take_item_entity.rs @@ -1,12 +1,13 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundTakeItemEntity { #[var] pub item_id: u32, #[var] - pub player_id: u32, + pub player_id: MinecraftEntityId, #[var] pub amount: u32, } diff --git a/azalea-protocol/src/packets/game/c_teleport_entity.rs b/azalea-protocol/src/packets/game/c_teleport_entity.rs index 7073c625..92b8f1eb 100755 --- a/azalea-protocol/src/packets/game/c_teleport_entity.rs +++ b/azalea-protocol/src/packets/game/c_teleport_entity.rs @@ -1,13 +1,13 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; +use azalea_world::MinecraftEntityId; use crate::common::movements::{PositionMoveRotation, RelativeMovements}; - #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundTeleportEntity { #[var] - pub id: u32, + pub id: MinecraftEntityId, pub change: PositionMoveRotation, pub relatives: RelativeMovements, pub on_ground: bool, diff --git a/azalea-protocol/src/packets/game/c_update_attributes.rs b/azalea-protocol/src/packets/game/c_update_attributes.rs index 2b800643..e4b334e3 100755 --- a/azalea-protocol/src/packets/game/c_update_attributes.rs +++ b/azalea-protocol/src/packets/game/c_update_attributes.rs @@ -2,11 +2,12 @@ use azalea_buf::AzBuf; use azalea_entity::attributes::AttributeModifier; use azalea_protocol_macros::ClientboundGamePacket; use azalea_registry::Attribute; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundUpdateAttributes { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub values: Vec, } diff --git a/azalea-protocol/src/packets/game/c_update_mob_effect.rs b/azalea-protocol/src/packets/game/c_update_mob_effect.rs index 6a31ccab..fe8e8f2e 100755 --- a/azalea-protocol/src/packets/game/c_update_mob_effect.rs +++ b/azalea-protocol/src/packets/game/c_update_mob_effect.rs @@ -1,11 +1,12 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ClientboundGamePacket; use azalea_registry::MobEffect; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] pub struct ClientboundUpdateMobEffect { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub mob_effect: MobEffect, #[var] pub effect_amplifier: u32, diff --git a/azalea-protocol/src/packets/game/s_entity_tag_query.rs b/azalea-protocol/src/packets/game/s_entity_tag_query.rs index 619e9e54..4bccee83 100644 --- a/azalea-protocol/src/packets/game/s_entity_tag_query.rs +++ b/azalea-protocol/src/packets/game/s_entity_tag_query.rs @@ -1,10 +1,11 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ServerboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ServerboundGamePacket)] pub struct ServerboundEntityTagQuery { #[var] pub transaction_id: u32, #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, } diff --git a/azalea-protocol/src/packets/game/s_interact.rs b/azalea-protocol/src/packets/game/s_interact.rs index cdd9cba5..89fc896f 100755 --- a/azalea-protocol/src/packets/game/s_interact.rs +++ b/azalea-protocol/src/packets/game/s_interact.rs @@ -3,13 +3,14 @@ use std::io::{Cursor, Write}; use azalea_buf::{AzBuf, AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar}; use azalea_core::position::Vec3; use azalea_protocol_macros::ServerboundGamePacket; +use azalea_world::MinecraftEntityId; use crate::packets::BufReadError; #[derive(Clone, Debug, AzBuf, ServerboundGamePacket)] pub struct ServerboundInteract { #[var] - pub entity_id: u32, + pub entity_id: MinecraftEntityId, pub action: ActionType, /// Whether the player is sneaking pub using_secondary_action: bool, diff --git a/azalea-protocol/src/packets/game/s_pick_item_from_entity.rs b/azalea-protocol/src/packets/game/s_pick_item_from_entity.rs index 42032063..73185c9b 100644 --- a/azalea-protocol/src/packets/game/s_pick_item_from_entity.rs +++ b/azalea-protocol/src/packets/game/s_pick_item_from_entity.rs @@ -1,9 +1,10 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ServerboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ServerboundGamePacket)] pub struct ServerboundPickItemFromEntity { #[var] - pub id: u32, + pub id: MinecraftEntityId, pub include_data: bool, } diff --git a/azalea-protocol/src/packets/game/s_player_command.rs b/azalea-protocol/src/packets/game/s_player_command.rs index 72a92310..f620b847 100755 --- a/azalea-protocol/src/packets/game/s_player_command.rs +++ b/azalea-protocol/src/packets/game/s_player_command.rs @@ -1,10 +1,11 @@ use azalea_buf::AzBuf; use azalea_protocol_macros::ServerboundGamePacket; +use azalea_world::MinecraftEntityId; #[derive(Clone, Debug, AzBuf, ServerboundGamePacket)] pub struct ServerboundPlayerCommand { #[var] - pub id: u32, + pub id: MinecraftEntityId, pub action: Action, #[var] pub data: u32, 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(&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 { + 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 { + 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)]