forked from Pumpkin-MC/Pumpkin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add pumpkin-api crate for API definitions * Add proc-macro definitions for pumpkin-api * Add pumpkin-api and pumpkin-api-macros to workspace * Add basic PluginManager implementation plugin loading * Update .gitignore to include specific plugin file types * Add example plugin * Cargo fmt * Fix clippy issues * Refactoring to prevent a circular import * Add base impl for plugin hooks * Move plugin manager to server * Make metadata have static lifetime * Add default implementations to events * Add hooks to proc macro * Update example * Fix formatting * Fix clippy warnings * Load metadata from Cargo.toml * Mark plugins as an implemented feature :D * Implement new event handling * Create a static global reference to the plugin manager * Emit player join and leave events * Update macro generation * Update example * Fix formatting * Fix clippy issue * Simplify event handling * Add plugin command to list plugins * Make handlers async * Update macros * Update example * Fix formatting and clippy issues * Better styling on plugins command * Fix clippy issues * Cargo fmt * Disable doctest for lib target on pumpkin crate * New API for plugins * Update api macros * Update plugin example * A bit of clean up * Some QoL (and performance) improvements * Cargo fmt and clippy fixes * refactoring, better event handling, new context functions * Async on_load and on_unload * Fix mutex lock never going out of scope * Async plugin loading * Add plugin management command * Fix clippy issues * Fix import issues * Move TcpConnection out of client * Move packet encoding out of client * Allow plugins to register commands * Fix fmt and clippy * Implement plugin list in query * Make arguments public so that plugins can use them * Update proc_macros to handle runtime * Make tree_builder public for use in plugins * Make FindArg trait public for use in plugins * Update example plugin * Fix merge related issues * Fix cargo fmt * Post-merge fixes (also 69th commit, nice) * New event system * cargo fmt * Impl block break event * cargo fmt and clippy * Reduced plugin size * Reduce dependency count * Old code cleanup * cargo fmt * Add event priority and blocking options to event registration and handling * Refactor event handling to support blocking execution and improve event registration * Fix post-merge errors * Update example plugin * increase mpsc channel capacity and handle write errors gracefully * Use Arc<Player> instead of &Player * add missing import * Update example plugin with playing sounds * Remove example plugin * Create plugins dir if doesnt exist * Remove unused vars * Remove unnecessary clones * Fix impl * cargo fmt * Make server pub in context
- Loading branch information
Showing
48 changed files
with
1,588 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "pumpkin-api-macros" | ||
version.workspace = true | ||
edition.workspace = true | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = { version = "2.0.89", features = ["full"] } | ||
quote = "1.0.37" | ||
proc-macro2 = "1.0.92" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use std::sync::LazyLock; | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use std::collections::HashMap; | ||
use std::sync::Mutex; | ||
use syn::{parse_macro_input, parse_quote, ImplItem, ItemFn, ItemImpl, ItemStruct}; | ||
|
||
static PLUGIN_METHODS: LazyLock<Mutex<HashMap<String, Vec<String>>>> = | ||
LazyLock::new(|| Mutex::new(HashMap::new())); | ||
|
||
#[proc_macro_attribute] | ||
pub fn plugin_method(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let input_fn = parse_macro_input!(item as ItemFn); | ||
let fn_name = &input_fn.sig.ident; | ||
let fn_inputs = &input_fn.sig.inputs; | ||
let fn_output = &input_fn.sig.output; | ||
let fn_body = &input_fn.block; | ||
|
||
let struct_name = if attr.is_empty() { | ||
"MyPlugin".to_string() | ||
} else { | ||
attr.to_string().trim().to_string() | ||
}; | ||
|
||
let method = quote! { | ||
#[allow(unused_mut)] | ||
async fn #fn_name(#fn_inputs) #fn_output { | ||
crate::GLOBAL_RUNTIME.block_on(async move { | ||
#fn_body | ||
}) | ||
} | ||
} | ||
.to_string(); | ||
|
||
PLUGIN_METHODS | ||
.lock() | ||
.unwrap() | ||
.entry(struct_name) | ||
.or_default() | ||
.push(method); | ||
|
||
TokenStream::new() | ||
} | ||
|
||
#[proc_macro_attribute] | ||
pub fn plugin_impl(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
// Parse the input struct | ||
let input_struct = parse_macro_input!(item as ItemStruct); | ||
let struct_ident = &input_struct.ident; | ||
|
||
// Get the custom name from attribute or use the struct's name | ||
let struct_name = if attr.is_empty() { | ||
struct_ident.clone() | ||
} else { | ||
let attr_str = attr.to_string(); | ||
quote::format_ident!("{}", attr_str.trim()) | ||
}; | ||
|
||
let methods = PLUGIN_METHODS | ||
.lock() | ||
.unwrap() | ||
.remove(&struct_name.to_string()) | ||
.unwrap_or_default(); | ||
|
||
let methods: Vec<proc_macro2::TokenStream> = methods | ||
.iter() | ||
.filter_map(|method_str| method_str.parse().ok()) | ||
.collect(); | ||
|
||
// Combine the original struct definition with the impl block and plugin() function | ||
let expanded = quote! { | ||
pub static GLOBAL_RUNTIME: std::sync::LazyLock<std::sync::Arc<tokio::runtime::Runtime>> = | ||
std::sync::LazyLock::new(|| std::sync::Arc::new(tokio::runtime::Runtime::new().unwrap())); | ||
|
||
#[no_mangle] | ||
pub static METADATA: pumpkin::plugin::PluginMetadata = pumpkin::plugin::PluginMetadata { | ||
name: env!("CARGO_PKG_NAME"), | ||
version: env!("CARGO_PKG_VERSION"), | ||
authors: env!("CARGO_PKG_AUTHORS"), | ||
description: env!("CARGO_PKG_DESCRIPTION"), | ||
}; | ||
|
||
#input_struct | ||
|
||
#[async_trait::async_trait] | ||
impl pumpkin::plugin::Plugin for #struct_ident { | ||
#(#methods)* | ||
} | ||
|
||
#[no_mangle] | ||
pub fn plugin() -> Box<dyn pumpkin::plugin::Plugin> { | ||
Box::new(#struct_ident::new()) | ||
} | ||
}; | ||
|
||
TokenStream::from(expanded) | ||
} | ||
|
||
#[proc_macro_attribute] | ||
pub fn with_runtime(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let mut input = parse_macro_input!(item as ItemImpl); | ||
|
||
let use_global = attr.to_string() == "global"; | ||
|
||
for item in &mut input.items { | ||
if let ImplItem::Fn(method) = item { | ||
let original_body = &method.block; | ||
|
||
method.block = if use_global { | ||
parse_quote!({ | ||
GLOBAL_RUNTIME.block_on(async move { | ||
#original_body | ||
}) | ||
}) | ||
} else { | ||
parse_quote!({ | ||
tokio::runtime::Runtime::new() | ||
.unwrap() | ||
.block_on(async move { | ||
#original_body | ||
}) | ||
}) | ||
}; | ||
} | ||
} | ||
|
||
TokenStream::from(quote!(#input)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.