From 73d3fe037356cb597cc018d5a39ee06ef60c2c25 Mon Sep 17 00:00:00 2001 From: Thomas Tartrau <145434513+ThomasTartrau@users.noreply.github.com> Date: Wed, 12 Feb 2025 16:37:23 +0100 Subject: [PATCH] Add new plugin events / change plugin organization (#536) * refactor: make file per event * feat: edit plugins organization --- pumpkin/src/plugin/api/events/block.rs | 136 ------------------ .../plugin/api/events/block/block_break.rs | 56 ++++++++ .../src/plugin/api/events/block/block_burn.rs | 23 +++ .../api/events/block/block_can_build.rs | 33 +++++ .../plugin/api/events/block/block_place.rs | 33 +++++ pumpkin/src/plugin/api/events/block/mod.rs | 17 +++ pumpkin/src/plugin/api/events/player.rs | 90 ------------ pumpkin/src/plugin/api/events/player/mod.rs | 17 +++ .../plugin/api/events/player/player_join.rs | 44 ++++++ .../plugin/api/events/player/player_leave.rs | 44 ++++++ pumpkin/src/plugin/api/events/world.rs | 44 ------ .../src/plugin/api/events/world/chunk_load.rs | 18 +++ .../src/plugin/api/events/world/chunk_save.rs | 18 +++ .../src/plugin/api/events/world/chunk_send.rs | 18 +++ pumpkin/src/plugin/api/events/world/mod.rs | 3 + pumpkin/src/world/mod.rs | 6 +- 16 files changed, 327 insertions(+), 273 deletions(-) delete mode 100644 pumpkin/src/plugin/api/events/block.rs create mode 100644 pumpkin/src/plugin/api/events/block/block_break.rs create mode 100644 pumpkin/src/plugin/api/events/block/block_burn.rs create mode 100644 pumpkin/src/plugin/api/events/block/block_can_build.rs create mode 100644 pumpkin/src/plugin/api/events/block/block_place.rs create mode 100644 pumpkin/src/plugin/api/events/block/mod.rs delete mode 100644 pumpkin/src/plugin/api/events/player.rs create mode 100644 pumpkin/src/plugin/api/events/player/mod.rs create mode 100644 pumpkin/src/plugin/api/events/player/player_join.rs create mode 100644 pumpkin/src/plugin/api/events/player/player_leave.rs delete mode 100644 pumpkin/src/plugin/api/events/world.rs create mode 100644 pumpkin/src/plugin/api/events/world/chunk_load.rs create mode 100644 pumpkin/src/plugin/api/events/world/chunk_save.rs create mode 100644 pumpkin/src/plugin/api/events/world/chunk_send.rs create mode 100644 pumpkin/src/plugin/api/events/world/mod.rs diff --git a/pumpkin/src/plugin/api/events/block.rs b/pumpkin/src/plugin/api/events/block.rs deleted file mode 100644 index 2f8187534..000000000 --- a/pumpkin/src/plugin/api/events/block.rs +++ /dev/null @@ -1,136 +0,0 @@ -use pumpkin_macros::{cancellable, Event}; -use pumpkin_world::block::registry::Block; -use std::sync::Arc; - -use crate::entity::player::Player; - -/// A trait representing events related to blocks. -/// -/// This trait provides a method to retrieve the block associated with the event. -pub trait BlockEvent: Send + Sync { - /// Retrieves a reference to the block associated with the event. - /// - /// # Returns - /// A reference to the `Block` involved in the event. - fn get_block(&self) -> &Block; -} - -/// An event that occurs when a block is broken. -/// -/// This event contains information about the player breaking the block, the block itself, -/// the experience gained, and whether the block should drop items. -#[cancellable] -#[derive(Event, Clone)] -pub struct BlockBreakEvent { - /// The player who is breaking the block, if applicable. - pub player: Option>, - - /// The block that is being broken. - pub block: Block, - - /// The amount of experience gained from breaking the block. - pub exp: u32, - - /// A boolean indicating whether the block should drop items. - pub drop: bool, -} - -impl BlockBreakEvent { - /// Creates a new instance of `BlockBreakEvent`. - /// - /// # Arguments - /// - `player`: An optional reference to the player breaking the block. - /// - `block`: The block that is being broken. - /// - `exp`: The amount of experience gained from breaking the block. - /// - `drop`: A boolean indicating whether the block should drop items. - /// - /// # Returns - /// A new instance of `BlockBreakEvent`. - #[must_use] - pub fn new(player: Option>, block: Block, exp: u32, drop: bool) -> Self { - Self { - player, - block, - exp, - drop, - cancelled: false, - } - } -} - -impl BlockEvent for BlockBreakEvent { - fn get_block(&self) -> &Block { - &self.block - } -} - -/// An event that occurs when a block is burned. -/// -/// This event contains information about the block that ignited the fire and the block that is burning. -#[cancellable] -#[derive(Event, Clone)] -pub struct BlockBurnEvent { - /// The block that is igniting the fire. - pub igniting_block: Block, - - /// The block that is burning. - pub block: Block, -} - -impl BlockEvent for BlockBurnEvent { - fn get_block(&self) -> &Block { - &self.block - } -} - -/// An event that occurs when a player attempts to build on a block. -/// -/// This event contains information about the block to build, whether building is allowed, -/// the player attempting to build, and the block being built upon. -#[cancellable] -#[derive(Event, Clone)] -pub struct BlockCanBuildEvent { - /// The block that the player is attempting to build. - pub block_to_build: Block, - - /// A boolean indicating whether building is allowed. - pub buildable: bool, - - /// The player attempting to build. - pub player: Arc, - - /// The block being built upon. - pub block: Block, -} - -impl BlockEvent for BlockCanBuildEvent { - fn get_block(&self) -> &Block { - &self.block - } -} - -/// An event that occurs when a block is placed. -/// -/// This event contains information about the player placing the block, the block being placed, -/// the block being placed against, and whether the player can build. -#[cancellable] -#[derive(Event, Clone)] -pub struct BlockPlaceEvent { - /// The player placing the block. - pub player: Arc, - - /// The block that is being placed. - pub block_placed: Block, - - /// The block that the new block is being placed against. - pub block_placed_against: Block, - - /// A boolean indicating whether the player can build. - pub can_build: bool, -} - -impl BlockEvent for BlockPlaceEvent { - fn get_block(&self) -> &Block { - &self.block_placed - } -} diff --git a/pumpkin/src/plugin/api/events/block/block_break.rs b/pumpkin/src/plugin/api/events/block/block_break.rs new file mode 100644 index 000000000..fbbc63958 --- /dev/null +++ b/pumpkin/src/plugin/api/events/block/block_break.rs @@ -0,0 +1,56 @@ +use pumpkin_macros::{cancellable, Event}; +use pumpkin_world::block::registry::Block; +use std::sync::Arc; + +use crate::entity::player::Player; + +use super::BlockEvent; + +/// An event that occurs when a block is broken. +/// +/// This event contains information about the player breaking the block, the block itself, +/// the experience gained, and whether the block should drop items. +#[cancellable] +#[derive(Event, Clone)] +pub struct BlockBreakEvent { + /// The player who is breaking the block, if applicable. + pub player: Option>, + + /// The block that is being broken. + pub block: Block, + + /// The amount of experience gained from breaking the block. + pub exp: u32, + + /// A boolean indicating whether the block should drop items. + pub drop: bool, +} + +impl BlockBreakEvent { + /// Creates a new instance of `BlockBreakEvent`. + /// + /// # Arguments + /// - `player`: An optional reference to the player breaking the block. + /// - `block`: The block that is being broken. + /// - `exp`: The amount of experience gained from breaking the block. + /// - `drop`: A boolean indicating whether the block should drop items. + /// + /// # Returns + /// A new instance of `BlockBreakEvent`. + #[must_use] + pub fn new(player: Option>, block: Block, exp: u32, drop: bool) -> Self { + Self { + player, + block, + exp, + drop, + cancelled: false, + } + } +} + +impl BlockEvent for BlockBreakEvent { + fn get_block(&self) -> &Block { + &self.block + } +} diff --git a/pumpkin/src/plugin/api/events/block/block_burn.rs b/pumpkin/src/plugin/api/events/block/block_burn.rs new file mode 100644 index 000000000..eb73bb1b9 --- /dev/null +++ b/pumpkin/src/plugin/api/events/block/block_burn.rs @@ -0,0 +1,23 @@ +use pumpkin_macros::{cancellable, Event}; +use pumpkin_world::block::registry::Block; + +use super::BlockEvent; + +/// An event that occurs when a block is burned. +/// +/// This event contains information about the block that ignited the fire and the block that is burning. +#[cancellable] +#[derive(Event, Clone)] +pub struct BlockBurnEvent { + /// The block that is igniting the fire. + pub igniting_block: Block, + + /// The block that is burning. + pub block: Block, +} + +impl BlockEvent for BlockBurnEvent { + fn get_block(&self) -> &Block { + &self.block + } +} diff --git a/pumpkin/src/plugin/api/events/block/block_can_build.rs b/pumpkin/src/plugin/api/events/block/block_can_build.rs new file mode 100644 index 000000000..798dc920c --- /dev/null +++ b/pumpkin/src/plugin/api/events/block/block_can_build.rs @@ -0,0 +1,33 @@ +use pumpkin_macros::{cancellable, Event}; +use pumpkin_world::block::registry::Block; +use std::sync::Arc; + +use crate::entity::player::Player; + +use super::BlockEvent; + +/// An event that occurs when a player attempts to build on a block. +/// +/// This event contains information about the block to build, whether building is allowed, +/// the player attempting to build, and the block being built upon. +#[cancellable] +#[derive(Event, Clone)] +pub struct BlockCanBuildEvent { + /// The block that the player is attempting to build. + pub block_to_build: Block, + + /// A boolean indicating whether building is allowed. + pub buildable: bool, + + /// The player attempting to build. + pub player: Arc, + + /// The block being built upon. + pub block: Block, +} + +impl BlockEvent for BlockCanBuildEvent { + fn get_block(&self) -> &Block { + &self.block + } +} diff --git a/pumpkin/src/plugin/api/events/block/block_place.rs b/pumpkin/src/plugin/api/events/block/block_place.rs new file mode 100644 index 000000000..a750b4be4 --- /dev/null +++ b/pumpkin/src/plugin/api/events/block/block_place.rs @@ -0,0 +1,33 @@ +use pumpkin_macros::{cancellable, Event}; +use pumpkin_world::block::registry::Block; +use std::sync::Arc; + +use crate::entity::player::Player; + +use super::BlockEvent; + +/// An event that occurs when a block is placed. +/// +/// This event contains information about the player placing the block, the block being placed, +/// the block being placed against, and whether the player can build. +#[cancellable] +#[derive(Event, Clone)] +pub struct BlockPlaceEvent { + /// The player placing the block. + pub player: Arc, + + /// The block that is being placed. + pub block_placed: Block, + + /// The block that the new block is being placed against. + pub block_placed_against: Block, + + /// A boolean indicating whether the player can build. + pub can_build: bool, +} + +impl BlockEvent for BlockPlaceEvent { + fn get_block(&self) -> &Block { + &self.block_placed + } +} diff --git a/pumpkin/src/plugin/api/events/block/mod.rs b/pumpkin/src/plugin/api/events/block/mod.rs new file mode 100644 index 000000000..b1f47605a --- /dev/null +++ b/pumpkin/src/plugin/api/events/block/mod.rs @@ -0,0 +1,17 @@ +pub mod block_break; +pub mod block_burn; +pub mod block_can_build; +pub mod block_place; + +use pumpkin_world::block::registry::Block; + +/// A trait representing events related to blocks. +/// +/// This trait provides a method to retrieve the block associated with the event. +pub trait BlockEvent: Send + Sync { + /// Retrieves a reference to the block associated with the event. + /// + /// # Returns + /// A reference to the `Block` involved in the event. + fn get_block(&self) -> &Block; +} diff --git a/pumpkin/src/plugin/api/events/player.rs b/pumpkin/src/plugin/api/events/player.rs deleted file mode 100644 index 21ad75967..000000000 --- a/pumpkin/src/plugin/api/events/player.rs +++ /dev/null @@ -1,90 +0,0 @@ -use pumpkin_macros::{cancellable, Event}; -use pumpkin_util::text::TextComponent; -use std::sync::Arc; - -use crate::entity::player::Player; - -/// A trait representing events related to players. -/// -/// This trait provides a method to retrieve the player associated with the event. -pub trait PlayerEvent: Send + Sync { - /// Retrieves a reference to the player associated with the event. - /// - /// # Returns - /// A reference to the `Arc` involved in the event. - fn get_player(&self) -> &Arc; -} - -/// An event that occurs when a player joins the game. -/// -/// This event contains information about the player joining and a message to display upon joining. -#[cancellable] -#[derive(Event, Clone)] -pub struct PlayerJoinEvent { - /// The player who is joining the game. - pub player: Arc, - - /// The message to display when the player joins. - pub join_message: TextComponent, -} - -impl PlayerJoinEvent { - /// Creates a new instance of `PlayerJoinEvent`. - /// - /// # Arguments - /// - `player`: A reference to the player joining the game. - /// - `join_message`: The message to display upon joining. - /// - /// # Returns - /// A new instance of `PlayerJoinEvent`. - pub fn new(player: Arc, join_message: TextComponent) -> Self { - Self { - player, - join_message, - cancelled: false, - } - } -} - -impl PlayerEvent for PlayerJoinEvent { - fn get_player(&self) -> &Arc { - &self.player - } -} - -/// An event that occurs when a player leaves the game. -/// -/// This event contains information about the player leaving and a message to display upon leaving. -#[cancellable] -#[derive(Event, Clone)] -pub struct PlayerLeaveEvent { - /// The player who is leaving the game. - pub player: Arc, - - /// The message to display when the player leaves. - pub leave_message: TextComponent, -} - -impl PlayerLeaveEvent { - /// Creates a new instance of `PlayerLeaveEvent`. - /// - /// # Arguments - /// - `player`: A reference to the player leaving the game. - /// - `leave_message`: The message to display upon leaving. - /// - /// # Returns - /// A new instance of `PlayerLeaveEvent`. - pub fn new(player: Arc, leave_message: TextComponent) -> Self { - Self { - player, - leave_message, - cancelled: false, - } - } -} - -impl PlayerEvent for PlayerLeaveEvent { - fn get_player(&self) -> &Arc { - &self.player - } -} diff --git a/pumpkin/src/plugin/api/events/player/mod.rs b/pumpkin/src/plugin/api/events/player/mod.rs new file mode 100644 index 000000000..6d3725fa7 --- /dev/null +++ b/pumpkin/src/plugin/api/events/player/mod.rs @@ -0,0 +1,17 @@ +pub mod player_join; +pub mod player_leave; + +use std::sync::Arc; + +use crate::entity::player::Player; + +/// A trait representing events related to players. +/// +/// This trait provides a method to retrieve the player associated with the event. +pub trait PlayerEvent: Send + Sync { + /// Retrieves a reference to the player associated with the event. + /// + /// # Returns + /// A reference to the `Arc` involved in the event. + fn get_player(&self) -> &Arc; +} diff --git a/pumpkin/src/plugin/api/events/player/player_join.rs b/pumpkin/src/plugin/api/events/player/player_join.rs new file mode 100644 index 000000000..79a877701 --- /dev/null +++ b/pumpkin/src/plugin/api/events/player/player_join.rs @@ -0,0 +1,44 @@ +use pumpkin_macros::{cancellable, Event}; +use pumpkin_util::text::TextComponent; +use std::sync::Arc; + +use crate::entity::player::Player; + +use super::PlayerEvent; + +/// An event that occurs when a player joins the game. +/// +/// This event contains information about the player joining and a message to display upon joining. +#[cancellable] +#[derive(Event, Clone)] +pub struct PlayerJoinEvent { + /// The player who is joining the game. + pub player: Arc, + + /// The message to display when the player joins. + pub join_message: TextComponent, +} + +impl PlayerJoinEvent { + /// Creates a new instance of `PlayerJoinEvent`. + /// + /// # Arguments + /// - `player`: A reference to the player joining the game. + /// - `join_message`: The message to display upon joining. + /// + /// # Returns + /// A new instance of `PlayerJoinEvent`. + pub fn new(player: Arc, join_message: TextComponent) -> Self { + Self { + player, + join_message, + cancelled: false, + } + } +} + +impl PlayerEvent for PlayerJoinEvent { + fn get_player(&self) -> &Arc { + &self.player + } +} diff --git a/pumpkin/src/plugin/api/events/player/player_leave.rs b/pumpkin/src/plugin/api/events/player/player_leave.rs new file mode 100644 index 000000000..5e7126479 --- /dev/null +++ b/pumpkin/src/plugin/api/events/player/player_leave.rs @@ -0,0 +1,44 @@ +use pumpkin_macros::{cancellable, Event}; +use pumpkin_util::text::TextComponent; +use std::sync::Arc; + +use crate::entity::player::Player; + +use super::PlayerEvent; + +/// An event that occurs when a player leaves the game. +/// +/// This event contains information about the player leaving and a message to display upon leaving. +#[cancellable] +#[derive(Event, Clone)] +pub struct PlayerLeaveEvent { + /// The player who is leaving the game. + pub player: Arc, + + /// The message to display when the player leaves. + pub leave_message: TextComponent, +} + +impl PlayerLeaveEvent { + /// Creates a new instance of `PlayerLeaveEvent`. + /// + /// # Arguments + /// - `player`: A reference to the player leaving the game. + /// - `leave_message`: The message to display upon leaving. + /// + /// # Returns + /// A new instance of `PlayerLeaveEvent`. + pub fn new(player: Arc, leave_message: TextComponent) -> Self { + Self { + player, + leave_message, + cancelled: false, + } + } +} + +impl PlayerEvent for PlayerLeaveEvent { + fn get_player(&self) -> &Arc { + &self.player + } +} diff --git a/pumpkin/src/plugin/api/events/world.rs b/pumpkin/src/plugin/api/events/world.rs deleted file mode 100644 index 1789faad2..000000000 --- a/pumpkin/src/plugin/api/events/world.rs +++ /dev/null @@ -1,44 +0,0 @@ -use crate::world::World; -use pumpkin_macros::{cancellable, Event}; -use pumpkin_world::chunk::ChunkData; -use std::sync::Arc; -use tokio::sync::RwLock; - -/// An event that occurs when a chunk is loaded in a world. -/// -/// This event contains information about the world and the chunk being loaded. -#[cancellable] -#[derive(Event, Clone)] -pub struct ChunkLoad { - /// The world in which the chunk is being loaded. - pub world: Arc, - - /// The chunk data being loaded, wrapped in a read-write lock for safe concurrent access. - pub chunk: Arc>, -} - -/// An event that occurs when a chunk is sent to a client. -/// -/// This event contains information about the world and the chunk being sent. -#[cancellable] -#[derive(Event, Clone)] -pub struct ChunkSend { - /// The world from which the chunk is being sent. - pub world: Arc, - - /// The chunk data being sent, wrapped in a read-write lock for safe concurrent access. - pub chunk: Arc>, -} - -/// An event that occurs when a chunk is saved in a world. -/// -/// This event contains information about the world and the chunk being saved. -#[cancellable] -#[derive(Event, Clone)] -pub struct ChunkSave { - /// The world in which the chunk is being saved. - pub world: Arc, - - /// The chunk data being saved, wrapped in a read-write lock for safe concurrent access. - pub chunk: Arc>, -} diff --git a/pumpkin/src/plugin/api/events/world/chunk_load.rs b/pumpkin/src/plugin/api/events/world/chunk_load.rs new file mode 100644 index 000000000..fdfc3a123 --- /dev/null +++ b/pumpkin/src/plugin/api/events/world/chunk_load.rs @@ -0,0 +1,18 @@ +use crate::world::World; +use pumpkin_macros::{cancellable, Event}; +use pumpkin_world::chunk::ChunkData; +use std::sync::Arc; +use tokio::sync::RwLock; + +/// An event that occurs when a chunk is loaded in a world. +/// +/// This event contains information about the world and the chunk being loaded. +#[cancellable] +#[derive(Event, Clone)] +pub struct ChunkLoad { + /// The world in which the chunk is being loaded. + pub world: Arc, + + /// The chunk data being loaded, wrapped in a read-write lock for safe concurrent access. + pub chunk: Arc>, +} diff --git a/pumpkin/src/plugin/api/events/world/chunk_save.rs b/pumpkin/src/plugin/api/events/world/chunk_save.rs new file mode 100644 index 000000000..fda4b4c2c --- /dev/null +++ b/pumpkin/src/plugin/api/events/world/chunk_save.rs @@ -0,0 +1,18 @@ +use crate::world::World; +use pumpkin_macros::{cancellable, Event}; +use pumpkin_world::chunk::ChunkData; +use std::sync::Arc; +use tokio::sync::RwLock; + +/// An event that occurs when a chunk is saved in a world. +/// +/// This event contains information about the world and the chunk being saved. +#[cancellable] +#[derive(Event, Clone)] +pub struct ChunkSave { + /// The world in which the chunk is being saved. + pub world: Arc, + + /// The chunk data being saved, wrapped in a read-write lock for safe concurrent access. + pub chunk: Arc>, +} diff --git a/pumpkin/src/plugin/api/events/world/chunk_send.rs b/pumpkin/src/plugin/api/events/world/chunk_send.rs new file mode 100644 index 000000000..02645a098 --- /dev/null +++ b/pumpkin/src/plugin/api/events/world/chunk_send.rs @@ -0,0 +1,18 @@ +use crate::world::World; +use pumpkin_macros::{cancellable, Event}; +use pumpkin_world::chunk::ChunkData; +use std::sync::Arc; +use tokio::sync::RwLock; + +/// An event that occurs when a chunk is sent to a client. +/// +/// This event contains information about the world and the chunk being sent. +#[cancellable] +#[derive(Event, Clone)] +pub struct ChunkSend { + /// The world from which the chunk is being sent. + pub world: Arc, + + /// The chunk data being sent, wrapped in a read-write lock for safe concurrent access. + pub chunk: Arc>, +} diff --git a/pumpkin/src/plugin/api/events/world/mod.rs b/pumpkin/src/plugin/api/events/world/mod.rs new file mode 100644 index 000000000..54ec97a2a --- /dev/null +++ b/pumpkin/src/plugin/api/events/world/mod.rs @@ -0,0 +1,3 @@ +pub mod chunk_load; +pub mod chunk_save; +pub mod chunk_send; diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index 904bfbc11..a790ce0e6 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -12,9 +12,9 @@ use crate::{ entity::{player::Player, Entity, EntityBase, EntityId}, error::PumpkinError, plugin::{ - block::BlockBreakEvent, - player::{PlayerJoinEvent, PlayerLeaveEvent}, - world::{ChunkLoad, ChunkSave, ChunkSend}, + block::block_break::BlockBreakEvent, + player::{player_join::PlayerJoinEvent, player_leave::PlayerLeaveEvent}, + world::{chunk_load::ChunkLoad, chunk_save::ChunkSave, chunk_send::ChunkSend}, }, server::Server, PLUGIN_MANAGER,