Plugin api events rewrite (#510)

* simplify all

* fmt
This commit is contained in:
suprohub
2025-01-28 23:30:34 +04:00
committed by GitHub
parent 7b4d28fbea
commit 49c697b431
13 changed files with 220 additions and 514 deletions

View File

@@ -0,0 +1,81 @@
use std::sync::Arc;
use pumpkin_macros::{cancellable, event};
use pumpkin_world::block::block_registry::Block;
use crate::entity::player::Player;
pub trait BlockEvent: Send + Sync {
fn get_block(&self) -> &Block;
}
#[event]
#[cancellable]
pub struct BlockBreakEvent {
pub player: Option<Arc<Player>>,
pub block: Block,
pub exp: u32,
pub drop: bool,
}
impl 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
}
}
#[event]
#[cancellable]
pub struct BlockBurnEvent {
pub igniting_block: Block,
pub block: Block,
}
impl BlockEvent for BlockBurnEvent {
fn get_block(&self) -> &Block {
&self.block
}
}
#[event]
#[cancellable]
pub struct BlockCanBuildEvent {
pub block_to_build: Block,
pub buildable: bool,
pub player: Arc<Player>,
pub block: Block,
}
impl BlockEvent for BlockCanBuildEvent {
fn get_block(&self) -> &Block {
&self.block
}
}
#[event]
#[cancellable]
pub struct BlockPlaceEvent {
pub player: Arc<Player>,
pub block_placed: Block,
pub block_placed_against: Block,
pub can_build: bool,
}
impl BlockEvent for BlockPlaceEvent {
fn get_block(&self) -> &Block {
&self.block_placed
}
}

View File

@@ -1,89 +0,0 @@
use std::sync::Arc;
use pumpkin_world::block::block_registry::Block;
use crate::{
entity::player::Player,
plugin::{CancellableEvent, Event},
};
use super::{BlockBreakEvent, BlockEvent, BlockExpEvent};
pub struct BlockBreakEventImpl {
player: Option<Arc<Player>>,
block: Block,
exp: u32,
drop: bool,
is_cancelled: bool,
}
impl BlockBreakEventImpl {
#[must_use]
pub fn new(player: Option<Arc<Player>>, block: Block, exp: u32, drop: bool) -> Self {
Self {
player,
block,
exp,
drop,
is_cancelled: false,
}
}
}
impl BlockBreakEvent for BlockBreakEventImpl {
fn get_player(&self) -> Option<Arc<Player>> {
self.player.clone()
}
fn will_drop(&self) -> bool {
self.drop
}
fn set_drop(&mut self, drop: bool) {
self.drop = drop;
}
}
impl BlockExpEvent for BlockBreakEventImpl {
fn get_exp_to_drop(&self) -> u32 {
self.exp
}
fn set_exp_to_drop(&mut self, exp: u32) {
self.exp = exp;
}
}
impl BlockEvent for BlockBreakEventImpl {
fn get_block(&self) -> &Block {
&self.block
}
}
impl CancellableEvent for BlockBreakEventImpl {
fn is_cancelled(&self) -> bool {
self.is_cancelled
}
fn set_cancelled(&mut self, cancelled: bool) {
self.is_cancelled = cancelled;
}
}
impl Event for BlockBreakEventImpl {
fn get_name_static() -> &'static str {
"BlockBreakEvent"
}
fn get_name(&self) -> &'static str {
"BlockBreakEvent"
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}

View File

@@ -1,51 +0,0 @@
use pumpkin_world::block::block_registry::Block;
use crate::plugin::{CancellableEvent, Event};
use super::{BlockBurnEvent, BlockEvent};
pub struct BlockBurnEventImpl {
igniting_block: Block,
block: Block,
is_cancelled: bool,
}
impl BlockBurnEvent for BlockBurnEventImpl {
fn get_igniting_block(&self) -> &Block {
&self.igniting_block
}
}
impl BlockEvent for BlockBurnEventImpl {
fn get_block(&self) -> &Block {
&self.block
}
}
impl CancellableEvent for BlockBurnEventImpl {
fn is_cancelled(&self) -> bool {
self.is_cancelled
}
fn set_cancelled(&mut self, cancelled: bool) {
self.is_cancelled = cancelled;
}
}
impl Event for BlockBurnEventImpl {
fn get_name_static() -> &'static str {
"BlockBurnEvent"
}
fn get_name(&self) -> &'static str {
"BlockBurnEvent"
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}

View File

@@ -1,69 +0,0 @@
use pumpkin_world::block::block_registry::Block;
use std::sync::Arc;
use crate::{
entity::player::Player,
plugin::{CancellableEvent, Event},
};
use super::{BlockCanBuildEvent, BlockEvent};
pub struct BlockCanBuildEventImpl {
block_to_build: Block,
buildable: bool,
player: Arc<Player>,
block: Block,
is_cancelled: bool,
}
impl BlockCanBuildEvent for BlockCanBuildEventImpl {
fn get_block_to_build(&self) -> &Block {
&self.block_to_build
}
fn is_buildable(&self) -> bool {
self.buildable
}
fn set_buildable(&mut self, buildable: bool) {
self.buildable = buildable;
}
fn get_player(&self) -> Option<Arc<Player>> {
Some(self.player.clone())
}
}
impl BlockEvent for BlockCanBuildEventImpl {
fn get_block(&self) -> &Block {
&self.block
}
}
impl CancellableEvent for BlockCanBuildEventImpl {
fn is_cancelled(&self) -> bool {
self.is_cancelled
}
fn set_cancelled(&mut self, cancelled: bool) {
self.is_cancelled = cancelled;
}
}
impl Event for BlockCanBuildEventImpl {
fn get_name_static() -> &'static str {
"BlockCanBuildEvent"
}
fn get_name(&self) -> &'static str {
"BlockCanBuildEvent"
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}

View File

@@ -1,46 +0,0 @@
use std::sync::Arc;
use pumpkin_world::block::block_registry::Block;
use crate::entity::player::Player;
use super::CancellableEvent;
pub mod r#break;
pub mod burn;
pub mod can_build;
pub mod place;
pub trait BlockEvent: CancellableEvent {
fn get_block(&self) -> &Block;
}
pub trait BlockExpEvent: BlockEvent {
fn get_exp_to_drop(&self) -> u32;
fn set_exp_to_drop(&mut self, exp: u32);
}
pub trait BlockBreakEvent: BlockExpEvent {
fn get_player(&self) -> Option<Arc<Player>>;
fn will_drop(&self) -> bool;
fn set_drop(&mut self, drop: bool);
}
pub trait BlockBurnEvent: BlockEvent {
fn get_igniting_block(&self) -> &Block;
}
pub trait BlockCanBuildEvent: BlockEvent {
fn get_block_to_build(&self) -> &Block;
fn is_buildable(&self) -> bool;
fn set_buildable(&mut self, buildable: bool);
fn get_player(&self) -> Option<Arc<Player>>;
}
pub trait BlockPlaceEvent: BlockEvent {
fn get_player(&self) -> Option<Arc<Player>>;
fn can_build(&self) -> bool;
fn set_build(&mut self, build: bool);
fn get_block_placed_against(&self) -> &Block;
fn get_block_placed(&self) -> &Block;
}

View File

@@ -1,73 +0,0 @@
use pumpkin_world::block::block_registry::Block;
use std::sync::Arc;
use crate::{
entity::player::Player,
plugin::{CancellableEvent, Event},
};
use super::{BlockEvent, BlockPlaceEvent};
pub struct BlockPlaceEventImpl {
player: Arc<Player>,
block_placed: Block,
block_placed_against: Block,
can_build: bool,
is_cancelled: bool,
}
impl BlockPlaceEvent for BlockPlaceEventImpl {
fn get_player(&self) -> Option<Arc<Player>> {
Some(self.player.clone())
}
fn can_build(&self) -> bool {
self.can_build
}
fn set_build(&mut self, build: bool) {
self.can_build = build;
}
fn get_block_placed_against(&self) -> &Block {
&self.block_placed_against
}
fn get_block_placed(&self) -> &Block {
&self.block_placed
}
}
impl BlockEvent for BlockPlaceEventImpl {
fn get_block(&self) -> &Block {
&self.block_placed
}
}
impl CancellableEvent for BlockPlaceEventImpl {
fn is_cancelled(&self) -> bool {
self.is_cancelled
}
fn set_cancelled(&mut self, cancelled: bool) {
self.is_cancelled = cancelled;
}
}
impl Event for BlockPlaceEventImpl {
fn get_name_static() -> &'static str {
"BlockPlaceEvent"
}
fn get_name(&self) -> &'static str {
"BlockPlaceEvent"
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}

View File

@@ -3,7 +3,7 @@ use std::any::Any;
pub mod block;
pub mod player;
pub trait Event: Any + Send + Sync {
pub trait Event: Send + Sync {
fn get_name_static() -> &'static str
where
Self: Sized;
@@ -12,13 +12,13 @@ pub trait Event: Any + Send + Sync {
fn as_any(&self) -> &dyn Any;
}
pub trait CancellableEvent: Event {
fn is_cancelled(&self) -> bool;
pub trait Cancellable: Send + Sync {
fn cancelled(&self) -> bool;
fn set_cancelled(&mut self, cancelled: bool);
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
// Lowest priority events are executed first, so that higher priority events can override their changes
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
pub enum EventPriority {
Highest,
High,

View File

@@ -0,0 +1,56 @@
use std::sync::Arc;
use pumpkin_macros::{cancellable, event};
use pumpkin_util::text::TextComponent;
use crate::entity::player::Player;
pub trait PlayerEvent: Send + Sync {
fn get_player(&self) -> &Arc<Player>;
}
#[event]
#[cancellable]
pub struct PlayerJoinEvent {
pub player: Arc<Player>,
pub join_message: TextComponent,
}
impl 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
}
}
#[event]
#[cancellable]
pub struct PlayerLeaveEvent {
pub player: Arc<Player>,
pub leave_message: TextComponent,
}
impl 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,70 +0,0 @@
use std::sync::Arc;
use pumpkin_util::text::TextComponent;
use crate::{
entity::player::Player,
plugin::{CancellableEvent, Event},
};
use super::{PlayerEvent, PlayerJoinEvent};
pub struct PlayerJoinEventImpl {
player: Arc<Player>,
join_message: TextComponent,
is_cancelled: bool,
}
impl PlayerJoinEventImpl {
pub fn new(player: Arc<Player>, join_message: TextComponent) -> Self {
Self {
player,
join_message,
is_cancelled: false,
}
}
}
impl PlayerJoinEvent for PlayerJoinEventImpl {
fn get_join_message(&self) -> &TextComponent {
&self.join_message
}
fn set_join_message(&mut self, message: TextComponent) {
self.join_message = message;
}
}
impl PlayerEvent for PlayerJoinEventImpl {
fn get_player(&self) -> Arc<Player> {
self.player.clone()
}
}
impl CancellableEvent for PlayerJoinEventImpl {
fn is_cancelled(&self) -> bool {
self.is_cancelled
}
fn set_cancelled(&mut self, cancelled: bool) {
self.is_cancelled = cancelled;
}
}
impl Event for PlayerJoinEventImpl {
fn get_name_static() -> &'static str {
"PlayerJoinEvent"
}
fn get_name(&self) -> &'static str {
"PlayerJoinEvent"
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}

View File

@@ -1,70 +0,0 @@
use std::sync::Arc;
use pumpkin_util::text::TextComponent;
use crate::{
entity::player::Player,
plugin::{CancellableEvent, Event},
};
use super::{PlayerEvent, PlayerLeaveEvent};
pub struct PlayerLeaveEventImpl {
player: Arc<Player>,
leave_message: TextComponent,
is_cancelled: bool,
}
impl PlayerLeaveEventImpl {
pub fn new(player: Arc<Player>, leave_message: TextComponent) -> Self {
Self {
player,
leave_message,
is_cancelled: false,
}
}
}
impl PlayerLeaveEvent for PlayerLeaveEventImpl {
fn get_leave_message(&self) -> &TextComponent {
&self.leave_message
}
fn set_leave_message(&mut self, message: TextComponent) {
self.leave_message = message;
}
}
impl PlayerEvent for PlayerLeaveEventImpl {
fn get_player(&self) -> Arc<Player> {
self.player.clone()
}
}
impl CancellableEvent for PlayerLeaveEventImpl {
fn is_cancelled(&self) -> bool {
self.is_cancelled
}
fn set_cancelled(&mut self, cancelled: bool) {
self.is_cancelled = cancelled;
}
}
impl Event for PlayerLeaveEventImpl {
fn get_name_static() -> &'static str {
"PlayerLeaveEvent"
}
fn get_name(&self) -> &'static str {
"PlayerLeaveEvent"
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}

View File

@@ -1,23 +0,0 @@
use pumpkin_util::text::TextComponent;
use std::sync::Arc;
use crate::entity::player::Player;
use super::CancellableEvent;
pub mod join;
pub mod leave;
pub trait PlayerEvent: CancellableEvent {
fn get_player(&self) -> Arc<Player>;
}
pub trait PlayerJoinEvent: PlayerEvent {
fn get_join_message(&self) -> &TextComponent;
fn set_join_message(&mut self, message: TextComponent);
}
pub trait PlayerLeaveEvent: PlayerEvent {
fn get_leave_message(&self) -> &TextComponent;
fn set_leave_message(&mut self, message: TextComponent);
}