Skip to content

Commit

Permalink
Plugin api events rewrite (#510)
Browse files Browse the repository at this point in the history
* simplify all

* fmt
  • Loading branch information
suprohub authored Jan 28, 2025
1 parent 7b4d28f commit 49c697b
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 514 deletions.
64 changes: 64 additions & 0 deletions pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,72 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse::{Nothing, Parser},
parse_macro_input, Field, Fields, ItemStruct,
};

extern crate proc_macro;

#[proc_macro_attribute]
pub fn event(args: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as ItemStruct);
let name = &input.ident;
let _ = parse_macro_input!(args as Nothing);

quote! {
#input

impl crate::plugin::Event for #name {
fn get_name_static() -> &'static str {
stringify!(#name)
}

fn get_name(&self) -> &'static str {
stringify!(#name)
}

fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}

fn as_any(&self) -> &dyn std::any::Any {
self
}
}
}
.into()
}

#[proc_macro_attribute]
pub fn cancellable(args: TokenStream, input: TokenStream) -> TokenStream {
let mut item_struct = parse_macro_input!(input as ItemStruct);
let name = item_struct.ident.clone();
let _ = parse_macro_input!(args as Nothing);

if let Fields::Named(ref mut fields) = item_struct.fields {
fields.named.push(
Field::parse_named
.parse2(quote! { pub cancelled: bool })
.unwrap(),
);
}

quote! {
#item_struct

impl crate::plugin::Cancellable for #name {
fn cancelled(&self) -> bool {
self.cancelled
}

fn set_cancelled(&mut self, cancelled: bool) {
self.cancelled = cancelled;
}
}
}
.into()
}

#[proc_macro_attribute]
pub fn client_packet(input: TokenStream, item: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(item.clone()).unwrap();
Expand Down
81 changes: 81 additions & 0 deletions pumpkin/src/plugin/api/events/block.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
89 changes: 0 additions & 89 deletions pumpkin/src/plugin/api/events/block/break.rs

This file was deleted.

51 changes: 0 additions & 51 deletions pumpkin/src/plugin/api/events/block/burn.rs

This file was deleted.

69 changes: 0 additions & 69 deletions pumpkin/src/plugin/api/events/block/can_build.rs

This file was deleted.

Loading

0 comments on commit 49c697b

Please sign in to comment.