Add new plugin events / change plugin organization (#536)

* refactor: make file per event

* feat: edit plugins organization
This commit is contained in:
Thomas Tartrau
2025-02-12 16:37:23 +01:00
committed by GitHub
parent e2dff9d55a
commit 73d3fe0373
16 changed files with 327 additions and 273 deletions

View File

@@ -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<Arc<Player>>,
/// 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<Arc<Player>>, 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<Player>,
/// 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<Player>,
/// 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
}
}

View File

@@ -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<Arc<Player>>,
/// 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<Arc<Player>>, 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
}
}

View File

@@ -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
}
}

View File

@@ -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<Player>,
/// The block being built upon.
pub block: Block,
}
impl BlockEvent for BlockCanBuildEvent {
fn get_block(&self) -> &Block {
&self.block
}
}

View File

@@ -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<Player>,
/// 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
}
}

View File

@@ -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;
}

View File

@@ -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<Player>` involved in the event.
fn get_player(&self) -> &Arc<Player>;
}
/// 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<Player>,
/// 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<Player>, join_message: TextComponent) -> Self {
Self {
player,
join_message,
cancelled: false,
}
}
}
impl PlayerEvent for PlayerJoinEvent {
fn get_player(&self) -> &Arc<Player> {
&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<Player>,
/// 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<Player>, leave_message: TextComponent) -> Self {
Self {
player,
leave_message,
cancelled: false,
}
}
}
impl PlayerEvent for PlayerLeaveEvent {
fn get_player(&self) -> &Arc<Player> {
&self.player
}
}

View File

@@ -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<Player>` involved in the event.
fn get_player(&self) -> &Arc<Player>;
}

View File

@@ -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<Player>,
/// 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<Player>, join_message: TextComponent) -> Self {
Self {
player,
join_message,
cancelled: false,
}
}
}
impl PlayerEvent for PlayerJoinEvent {
fn get_player(&self) -> &Arc<Player> {
&self.player
}
}

View File

@@ -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<Player>,
/// 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<Player>, leave_message: TextComponent) -> Self {
Self {
player,
leave_message,
cancelled: false,
}
}
}
impl PlayerEvent for PlayerLeaveEvent {
fn get_player(&self) -> &Arc<Player> {
&self.player
}
}

View File

@@ -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<World>,
/// The chunk data being loaded, wrapped in a read-write lock for safe concurrent access.
pub chunk: Arc<RwLock<ChunkData>>,
}
/// 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<World>,
/// The chunk data being sent, wrapped in a read-write lock for safe concurrent access.
pub chunk: Arc<RwLock<ChunkData>>,
}
/// 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<World>,
/// The chunk data being saved, wrapped in a read-write lock for safe concurrent access.
pub chunk: Arc<RwLock<ChunkData>>,
}

View File

@@ -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<World>,
/// The chunk data being loaded, wrapped in a read-write lock for safe concurrent access.
pub chunk: Arc<RwLock<ChunkData>>,
}

View File

@@ -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<World>,
/// The chunk data being saved, wrapped in a read-write lock for safe concurrent access.
pub chunk: Arc<RwLock<ChunkData>>,
}

View File

@@ -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<World>,
/// The chunk data being sent, wrapped in a read-write lock for safe concurrent access.
pub chunk: Arc<RwLock<ChunkData>>,
}

View File

@@ -0,0 +1,3 @@
pub mod chunk_load;
pub mod chunk_save;
pub mod chunk_send;