feat(wasm-api): port player egg throw event (#1899)

This commit is contained in:
yunuservices
2026-03-27 14:06:20 +03:00
committed by GitHub
parent a6efe7ec7a
commit 0855dbd163
6 changed files with 88 additions and 6 deletions

View File

@@ -0,0 +1,25 @@
use crate::wit::pumpkin::plugin::event::{Event, EventType, PlayerEggThrowEventData};
use super::super::FromIntoEvent;
/// An event that occurs when a thrown egg resolves.
///
/// The associated [`PlayerEggThrowEventData`] contains the player, the egg entity UUID,
/// whether the egg hatched, how many entities hatched, and the entity type to hatch.
/// This event is cancellable.
pub struct PlayerEggThrowEvent;
impl FromIntoEvent for PlayerEggThrowEvent {
const EVENT_TYPE: EventType = EventType::PlayerEggThrowEvent;
type Data = PlayerEggThrowEventData;
fn data_from_event(event: Event) -> Self::Data {
match event {
Event::PlayerEggThrowEvent(data) => data,
_ => panic!("unexpected event"),
}
}
fn data_into_event(data: Self::Data) -> Event {
Event::PlayerEggThrowEvent(data)
}
}

View File

@@ -1,4 +1,5 @@
pub mod changed_main_hand;
pub mod egg_throw;
pub mod exp_change;
pub mod fish;
pub mod item_held;
@@ -15,6 +16,7 @@ pub mod player_permission_check;
pub mod player_teleport;
pub use changed_main_hand::*;
pub use egg_throw::*;
pub use exp_change::*;
pub use fish::*;
pub use item_held::*;

View File

@@ -124,6 +124,15 @@ interface event {
cancelled: bool
}
record player-egg-throw-event-data {
player: player,
egg-uuid: string,
hatching: bool,
num-hatches: u8,
hatching-type: string,
cancelled: bool
}
record block-redstone-event-data {
target-world: %world,
state-id: u16,
@@ -211,6 +220,7 @@ interface event {
player-gamemode-change-event,
player-custom-payload-event,
player-fish-event,
player-egg-throw-event,
block-redstone-event,
block-break-event,
block-burn-event,
@@ -238,6 +248,7 @@ interface event {
player-gamemode-change-event(player-gamemode-change-event-data),
player-custom-payload-event(player-custom-payload-event-data),
player-fish-event(player-fish-event-data),
player-egg-throw-event(player-egg-throw-event-data),
block-redstone-event(block-redstone-event-data),
block-break-event(block-break-event-data),
block-burn-event(block-burn-event-data),

View File

@@ -45,8 +45,8 @@ async fn register_player_event(
event_type: EventType,
) {
use crate::plugin::player::{
changed_main_hand::PlayerChangedMainHandEvent, exp_change::PlayerExpChangeEvent,
fish::PlayerFishEvent, item_held::PlayerItemHeldEvent,
changed_main_hand::PlayerChangedMainHandEvent, egg_throw::PlayerEggThrowEvent,
exp_change::PlayerExpChangeEvent, fish::PlayerFishEvent, item_held::PlayerItemHeldEvent,
player_change_world::PlayerChangeWorldEvent, player_chat::PlayerChatEvent,
player_command_send::PlayerCommandSendEvent,
player_custom_payload::PlayerCustomPayloadEvent,
@@ -117,6 +117,10 @@ async fn register_player_event(
EventType::PlayerFishEvent => {
register_typed_event::<PlayerFishEvent>(resource, handler, priority, blocking).await;
}
EventType::PlayerEggThrowEvent => {
register_typed_event::<PlayerEggThrowEvent>(resource, handler, priority, blocking)
.await;
}
_ => unreachable!("non-player event should not be routed to register_player_event"),
}
}

View File

@@ -1,6 +1,6 @@
use std::sync::Arc;
use pumpkin_data::Block;
use pumpkin_data::{Block, entity::EntityType};
use pumpkin_util::{
GameMode, Hand,
math::{position::BlockPos, vector3::Vector3},
@@ -76,6 +76,14 @@ pub(super) fn from_wasm_block_name(block_name: &str) -> &'static Block {
.expect("invalid block name")
}
pub(super) fn to_wasm_entity_type(entity_type: &'static EntityType) -> String {
format!("minecraft:{}", entity_type.resource_name)
}
pub(super) fn from_wasm_entity_type(entity_type: &str) -> &'static EntityType {
EntityType::from_name(entity_type).expect("invalid entity type")
}
pub(super) const fn to_wasm_hand(hand: Hand) -> pumpkin::plugin::common::Hand {
match hand {
Hand::Left => pumpkin::plugin::common::Hand::Left,

View File

@@ -4,13 +4,13 @@ use crate::plugin::{
wit::v0_1_0::{
events::{
ToFromV0_1_0WasmEvent, consume_player, consume_text_component, consume_world,
from_wasm_game_mode, from_wasm_hand, from_wasm_position, to_wasm_game_mode,
to_wasm_hand, to_wasm_position,
from_wasm_entity_type, from_wasm_game_mode, from_wasm_hand, from_wasm_position,
to_wasm_entity_type, to_wasm_game_mode, to_wasm_hand, to_wasm_position,
},
pumpkin::plugin::event::{
Event, PlayerChangeWorldEventData, PlayerChangedMainHandEventData,
PlayerChatEventData, PlayerCommandSendEventData, PlayerCustomPayloadEventData,
PlayerExpChangeEventData, PlayerFishEventData,
PlayerEggThrowEventData, PlayerExpChangeEventData, PlayerFishEventData,
PlayerFishState as WasmPlayerFishState, PlayerGamemodeChangeEventData,
PlayerItemHeldEventData, PlayerJoinEventData, PlayerLeaveEventData,
PlayerLoginEventData, PlayerMoveEventData, PlayerPermissionCheckEventData,
@@ -20,6 +20,7 @@ use crate::plugin::{
},
player::{
changed_main_hand::PlayerChangedMainHandEvent,
egg_throw::PlayerEggThrowEvent,
exp_change::PlayerExpChangeEvent,
fish::{PlayerFishEvent, PlayerFishState},
item_held::PlayerItemHeldEvent,
@@ -491,3 +492,34 @@ impl ToFromV0_1_0WasmEvent for PlayerFishEvent {
}
}
}
impl ToFromV0_1_0WasmEvent for PlayerEggThrowEvent {
fn to_v0_1_0_wasm_event(&self, state: &mut PluginHostState) -> Event {
let player = state
.add_player(self.player.clone())
.expect("failed to add player resource");
Event::PlayerEggThrowEvent(PlayerEggThrowEventData {
player,
egg_uuid: self.egg_uuid.to_string(),
hatching: self.hatching,
num_hatches: self.num_hatches,
hatching_type: to_wasm_entity_type(self.hatching_type),
cancelled: self.cancelled,
})
}
fn from_v0_1_0_wasm_event(event: Event, state: &mut PluginHostState) -> Self {
match event {
Event::PlayerEggThrowEvent(data) => Self {
player: consume_player(state, &data.player),
egg_uuid: uuid::Uuid::parse_str(&data.egg_uuid).expect("invalid egg UUID"),
hatching: data.hatching,
num_hatches: data.num_hatches,
hatching_type: from_wasm_entity_type(&data.hatching_type),
cancelled: data.cancelled,
},
_ => panic!("unexpected event type"),
}
}
}