Skip to content

Commit

Permalink
general attempt at getting item dropping to work again after 1.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryntet committed Oct 23, 2024
1 parent 4723459 commit 0e43d63
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 72 deletions.
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/client/play/c_pickup_item.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::ClientboundPlayPackets;
use crate::VarInt;
use pumpkin_macros::packet;
use pumpkin_macros::client_packet;
use serde::Serialize;

#[packet(0x6F)]
#[client_packet(ClientboundPlayPackets::CollectItem as i32)]
#[derive(Serialize)]
pub struct CPickupItem {
collected_entity_id: VarInt,
Expand Down
10 changes: 10 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,13 @@ pub enum ClientboundPlayPackets {
CustomReportDetails,
ServerLinks,
}

#[cfg(test)]
mod test {
use crate::client::play::ClientboundPlayPackets;

#[test]
fn check() {
assert_eq!(ClientboundPlayPackets::CollectItem as i32, 0x6F)
}
}
41 changes: 10 additions & 31 deletions pumpkin/src/entity/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,14 @@ impl ItemEntity {
});
}

let empty = Vector3 {
x: 0.,
y: 0.,
z: 0.,
};

let entity = Arc::new(Entity {
entity_id: server.new_entity_id(),
uuid: Uuid::new_v4(),
entity_type: EntityType::Item,
let entity = Arc::new(Entity::new(
server.new_entity_id(),
Uuid::new_v4(),
world,
pos: AtomicCell::new(empty),
block_pos: AtomicCell::new(WorldPosition::default()),
chunk_pos: AtomicCell::new(Vector2::default()),
sneaking: false.into(),
sprinting: false.into(),
fall_flying: false.into(),
velocity: AtomicCell::new(velocity),
on_ground: false.into(),
in_ground: false.into(),
yaw: 0.0.into(),
head_yaw: 0.0.into(),
pitch: 0.0.into(),
standing_eye_height: 0.0,
pose: EntityPose::Standing.into(),
});
EntityType::Item,
0.,
));
entity.velocity.store(velocity);
entity.set_pos(pos.x, pos.y, pos.z);

let item_entity = Self {
Expand All @@ -77,18 +59,15 @@ impl ItemEntity {
};

server
.broadcast_packet_all(&CSpawnEntity::from(entity.as_ref()))
.broadcast_packet_all(&entity.get_spawn_entity_packet(None))
.await;
{
let server = server.clone();
tokio::spawn(async move { item_entity.drop_loop(server).await });
}
let metadata = Metadata::new(8, 7.into(), Slot::from(&item_stack));
server
.broadcast_packet_all(&CSetEntityMetadata {
entity_id: entity.entity_id.into(),
metadata: Metadata::new(8, 7.into(), Slot::from(&item_stack)),
end: 255,
})
.broadcast_packet_all(&CSetEntityMetadata::new(entity.entity_id.into(), metadata))
.await;
}
pub async fn spawn_from_player(
Expand Down
30 changes: 26 additions & 4 deletions pumpkin/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use pumpkin_core::math::{
get_section_cord, position::WorldPosition, vector2::Vector2, vector3::Vector3,
};
use pumpkin_entity::{entity_type::EntityType, pose::EntityPose, EntityId};
use pumpkin_protocol::client::play::{CEntityVelocity, CUpdateEntityPos};
use pumpkin_protocol::client::play::{CEntityVelocity, CSpawnEntity, CUpdateEntityPos};
use pumpkin_protocol::{
client::play::{CSetEntityMetadata, Metadata},
VarInt,
Expand All @@ -22,7 +22,6 @@ use uuid::Uuid;
pub mod item;
pub mod living;
pub mod player;
mod to_packet;

/// Represents a not living Entity (e.g. Item, Egg, Snowball...)
pub struct Entity {
Expand Down Expand Up @@ -168,9 +167,12 @@ impl Entity {
}
}

let chunks = self.world.get_chunks(chunks).await;
if chunks.is_empty() {
return;
}
let mut chunks = self.world.receive_chunks(chunks);

for chunk in chunks {
while let Some(chunk) = chunks.recv().await {
let chunk = chunk.read().await;
for future_position in &future_positions {
let (section_x, section_z) = (
Expand Down Expand Up @@ -376,6 +378,26 @@ impl Entity {
velocity.y -= self.entity_type.gravity();
self.velocity.store(velocity);
}

pub fn get_spawn_entity_packet(&self, data: Option<i32>) -> CSpawnEntity {
let pos = self.pos.load();
let velocity = self.velocity.load();
CSpawnEntity::new(
self.entity_id.into(),
self.uuid,
(self.entity_type as i32).into(),
pos.x,
pos.y,
pos.z,
self.pitch.load(),
self.yaw.load(),
self.head_yaw.load(),
data.unwrap_or(0).into(),
velocity.x,
velocity.y,
velocity.z,
)
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, FromPrimitive, ToPrimitive)]
Expand Down
28 changes: 0 additions & 28 deletions pumpkin/src/entity/to_packet.rs

This file was deleted.

18 changes: 12 additions & 6 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,23 @@ impl World {
self.broadcast_packet_expect(
&[player.gameprofile.id],
// TODO: add velo
&CSpawnEntity::from(&player.living_entity.entity),
&player.living_entity.entity.get_spawn_entity_packet(None),
)
.await;
// spawn players for our client
let id = player.gameprofile.id;
for (_, existing_player) in self.current_players.lock().await.iter().filter(|c| c.0 != &id) {
for (_, existing_player) in self
.current_players
.lock()
.await
.iter()
.filter(|c| c.0 != &id)
{
let entity = &existing_player.living_entity.entity;
player.client.send_packet(&CSpawnEntity::from(entity)).await;
player
.client
.send_packet(&entity.get_spawn_entity_packet(None))
.await;
}
// entity meta data
// set skin parts
Expand Down Expand Up @@ -350,9 +359,6 @@ impl World {

// Stream the chunks (don't collect them and then do stuff with them)
pub fn receive_chunks(&self, chunks: Vec<Vector2<i32>>) -> Receiver<Arc<RwLock<ChunkData>>> {
if chunks.is_empty() {
return vec![];
}
let (sender, receive) = mpsc::channel(chunks.len());
{
let level = self.level.clone();
Expand Down

0 comments on commit 0e43d63

Please sign in to comment.