From 609f894c56c0e51b0cb20ad8c8156a368c282a16 Mon Sep 17 00:00:00 2001 From: Bjorn Beishline <75190918+BjornTheProgrammer@users.noreply.github.com> Date: Sat, 31 Jan 2026 05:39:25 -0700 Subject: [PATCH] Clean up codebase --- rust/src/java/jvm/command_executor.rs | 10 +- rust/src/java/jvm/worker.rs | 7 +- rust/src/java/native_callbacks/abilities.rs | 84 +++++++++++++ rust/src/java/native_callbacks/events.rs | 9 ++ rust/src/java/native_callbacks/message.rs | 25 ++++ rust/src/java/native_callbacks/mod.rs | 127 ++------------------ rust/src/lib.rs | 11 +- rust/src/plugin/manager.rs | 4 +- 8 files changed, 141 insertions(+), 136 deletions(-) create mode 100644 rust/src/java/native_callbacks/abilities.rs create mode 100644 rust/src/java/native_callbacks/events.rs create mode 100644 rust/src/java/native_callbacks/message.rs diff --git a/rust/src/java/jvm/command_executor.rs b/rust/src/java/jvm/command_executor.rs index dacf99d..9bee99a 100644 --- a/rust/src/java/jvm/command_executor.rs +++ b/rust/src/java/jvm/command_executor.rs @@ -23,18 +23,18 @@ impl CommandExecutor for JavaCommandExecutor { fn execute<'a>( &'a self, sender: &'a pumpkin::command::CommandSender, - server: &'a pumpkin::server::Server, - args: &'a pumpkin::command::args::ConsumedArgs<'a>, + _server: &'a pumpkin::server::Server, + _args: &'a pumpkin::command::args::ConsumedArgs<'a>, ) -> pumpkin::command::CommandResult<'a> { Box::pin(async move { - let (tx, rx) = oneshot::channel(); + let (tx, _rx) = oneshot::channel(); let sender = match sender { - pumpkin::command::CommandSender::Rcon(mutex) => todo!(), + pumpkin::command::CommandSender::Rcon(_mutex) => todo!(), pumpkin::command::CommandSender::Console => SimpleCommandSender::Console, pumpkin::command::CommandSender::Player(player) => { SimpleCommandSender::Player(player.gameprofile.id.to_string()) } - pumpkin::command::CommandSender::CommandBlock(block_entity, world) => todo!(), + pumpkin::command::CommandSender::CommandBlock(_block_entity, _world) => todo!(), }; self.command_tx .send(JvmCommand::TriggerCommand { diff --git a/rust/src/java/jvm/worker.rs b/rust/src/java/jvm/worker.rs index fc15909..6c9388c 100644 --- a/rust/src/java/jvm/worker.rs +++ b/rust/src/java/jvm/worker.rs @@ -16,7 +16,6 @@ use crate::{ pub struct JvmWorker { command_rx: mpsc::Receiver, - command_tx: mpsc::Sender, pub plugin_manager: PluginManager, pub event_manager: EventManager, jvm: Option, @@ -24,13 +23,9 @@ pub struct JvmWorker { } impl JvmWorker { - pub fn new( - command_tx: mpsc::Sender, - command_rx: mpsc::Receiver, - ) -> Self { + pub fn new(command_rx: mpsc::Receiver) -> Self { Self { command_rx, - command_tx, plugin_manager: PluginManager::new(), event_manager: EventManager::new(), jvm: None, diff --git a/rust/src/java/native_callbacks/abilities.rs b/rust/src/java/native_callbacks/abilities.rs new file mode 100644 index 0000000..c63d184 --- /dev/null +++ b/rust/src/java/native_callbacks/abilities.rs @@ -0,0 +1,84 @@ +use std::ffi::{CStr, c_char}; + +use pumpkin::entity::player::Abilities; +use tokio::sync::MutexGuard; + +use crate::java::native_callbacks::CALLBACK_CONTEXT; + +#[repr(C)] +pub struct AbilitiesFFI { + pub invulnerable: bool, + pub flying: bool, + pub allow_flying: bool, + pub creative: bool, + pub allow_modify_world: bool, + pub fly_speed: f32, + pub walk_speed: f32, +} + +impl AbilitiesFFI { + fn new(abilities: MutexGuard<'_, Abilities>) -> Self { + Self { + invulnerable: abilities.invulnerable, + flying: abilities.flying, + allow_flying: abilities.allow_flying, + creative: abilities.creative, + allow_modify_world: abilities.allow_modify_world, + fly_speed: abilities.fly_speed, + walk_speed: abilities.walk_speed, + } + } +} + +pub extern "C" fn rust_set_abilities( + uuid_ptr: *const c_char, + abilities: *mut AbilitiesFFI, +) -> bool { + let uuid_str = unsafe { CStr::from_ptr(uuid_ptr).to_string_lossy().into_owned() }; + if let Some(ctx) = CALLBACK_CONTEXT.get() { + let uuid = uuid::Uuid::parse_str(&uuid_str).unwrap(); + let player = ctx.plugin_context.server.get_player_by_uuid(uuid); + if let Some(player) = player { + tokio::task::block_in_place(|| { + ctx.runtime.block_on(async { + let mut server_abilities = player.abilities.lock().await; + unsafe { + server_abilities.allow_flying = (*abilities).allow_flying; + server_abilities.allow_modify_world = (*abilities).allow_modify_world; + server_abilities.creative = (*abilities).creative; + server_abilities.fly_speed = (*abilities).fly_speed; + server_abilities.flying = (*abilities).flying; + server_abilities.invulnerable = (*abilities).invulnerable; + server_abilities.walk_speed = (*abilities).walk_speed; + } + }) + }); + + return true; + } + } + + false +} + +pub extern "C" fn rust_get_abilities(uuid_ptr: *const c_char, out: *mut AbilitiesFFI) -> bool { + let uuid_str = unsafe { CStr::from_ptr(uuid_ptr).to_string_lossy().into_owned() }; + if let Some(ctx) = CALLBACK_CONTEXT.get() { + let uuid = uuid::Uuid::parse_str(&uuid_str).unwrap(); + let player = ctx.plugin_context.server.get_player_by_uuid(uuid); + if let Some(player) = player { + let abilities = tokio::task::block_in_place(|| { + ctx.runtime + .block_on(async { AbilitiesFFI::new(player.abilities.lock().await) }) + }); + + unsafe { + *out = abilities; + } + + return true; + } + } + + false +} diff --git a/rust/src/java/native_callbacks/events.rs b/rust/src/java/native_callbacks/events.rs new file mode 100644 index 0000000..494aa98 --- /dev/null +++ b/rust/src/java/native_callbacks/events.rs @@ -0,0 +1,9 @@ +use std::ffi::c_void; + +#[unsafe(no_mangle)] +pub extern "C" fn rust_register_event( + _listener_ptr: *const c_void, // opaque pointer to Java object + _plugin_ptr: *const c_void, +) { + // Handle event registration +} diff --git a/rust/src/java/native_callbacks/message.rs b/rust/src/java/native_callbacks/message.rs new file mode 100644 index 0000000..45643aa --- /dev/null +++ b/rust/src/java/native_callbacks/message.rs @@ -0,0 +1,25 @@ +use std::ffi::{CStr, c_char}; + +use pumpkin_util::text::TextComponent; + +use crate::java::native_callbacks::CALLBACK_CONTEXT; + +#[unsafe(no_mangle)] +pub extern "C" fn rust_send_message(uuid_ptr: *const c_char, message_ptr: *const c_char) { + // This into_owned might not seem to make any sense, but it's necessary to ensure that the string is owned by the Rust code and can move into the runtime. + let uuid_str = unsafe { CStr::from_ptr(uuid_ptr).to_string_lossy().into_owned() }; + let message = unsafe { CStr::from_ptr(message_ptr).to_string_lossy().into_owned() }; + + if let Some(ctx) = CALLBACK_CONTEXT.get() { + let uuid = uuid::Uuid::parse_str(&uuid_str).unwrap(); + + ctx.runtime.spawn(async move { + let player = ctx.plugin_context.server.get_player_by_uuid(uuid); + if let Some(player) = player { + player + .send_system_message(&TextComponent::from_legacy_string(&message)) + .await; + } + }); + } +} diff --git a/rust/src/java/native_callbacks/mod.rs b/rust/src/java/native_callbacks/mod.rs index 9c0fd16..4d9d30c 100644 --- a/rust/src/java/native_callbacks/mod.rs +++ b/rust/src/java/native_callbacks/mod.rs @@ -1,13 +1,12 @@ -use std::{ - ffi::{CStr, c_char, c_void}, - sync::{Arc, OnceLock}, -}; +use std::sync::{Arc, OnceLock}; use anyhow::Result; use j4rs::{InvocationArg, Jvm}; -use pumpkin::{entity::player::Abilities, plugin::Context}; -use pumpkin_util::text::TextComponent; -use tokio::sync::MutexGuard; +use pumpkin::plugin::Context; + +pub mod abilities; +pub mod events; +pub mod message; static CALLBACK_CONTEXT: OnceLock = OnceLock::new(); @@ -32,10 +31,10 @@ pub fn init_callback_context( } pub fn initialize_callbacks(jvm: &Jvm) -> Result<()> { - let send_message_addr = rust_send_message as *const () as i64; - let register_event_addr = rust_register_event as *const () as i64; - let get_abilities_addr = rust_get_abilities as *const () as i64; - let set_abilities_addr = rust_set_abilities as *const () as i64; + let send_message_addr = message::rust_send_message as *const () as i64; + let register_event_addr = events::rust_register_event as *const () as i64; + let get_abilities_addr = abilities::rust_get_abilities as *const () as i64; + let set_abilities_addr = abilities::rust_set_abilities as *const () as i64; jvm.invoke_static( "org.patchbukkit.bridge.NativePatchBukkit", @@ -50,109 +49,3 @@ pub fn initialize_callbacks(jvm: &Jvm) -> Result<()> { Ok(()) } - -#[unsafe(no_mangle)] -pub extern "C" fn rust_send_message(uuid_ptr: *const c_char, message_ptr: *const c_char) { - // This into_owned might not seem to make any sense, but it's necessary to ensure that the string is owned by the Rust code and can move into the runtime. - let uuid_str = unsafe { CStr::from_ptr(uuid_ptr).to_string_lossy().into_owned() }; - let message = unsafe { CStr::from_ptr(message_ptr).to_string_lossy().into_owned() }; - - if let Some(ctx) = CALLBACK_CONTEXT.get() { - let uuid = uuid::Uuid::parse_str(&uuid_str).unwrap(); - - ctx.runtime.spawn(async move { - let player = ctx.plugin_context.server.get_player_by_uuid(uuid); - if let Some(player) = player { - player - .send_system_message(&TextComponent::from_legacy_string(&message)) - .await; - } - }); - } -} - -#[repr(C)] -pub struct AbilitiesFFI { - pub invulnerable: bool, - pub flying: bool, - pub allow_flying: bool, - pub creative: bool, - pub allow_modify_world: bool, - pub fly_speed: f32, - pub walk_speed: f32, -} - -impl AbilitiesFFI { - fn new(abilities: MutexGuard<'_, Abilities>) -> Self { - Self { - invulnerable: abilities.invulnerable, - flying: abilities.flying, - allow_flying: abilities.allow_flying, - creative: abilities.creative, - allow_modify_world: abilities.allow_modify_world, - fly_speed: abilities.fly_speed, - walk_speed: abilities.walk_speed, - } - } -} - -pub extern "C" fn rust_set_abilities( - uuid_ptr: *const c_char, - abilities: *mut AbilitiesFFI, -) -> bool { - let uuid_str = unsafe { CStr::from_ptr(uuid_ptr).to_string_lossy().into_owned() }; - if let Some(ctx) = CALLBACK_CONTEXT.get() { - let uuid = uuid::Uuid::parse_str(&uuid_str).unwrap(); - let player = ctx.plugin_context.server.get_player_by_uuid(uuid); - if let Some(player) = player { - tokio::task::block_in_place(|| { - ctx.runtime.block_on(async { - let mut server_abilities = player.abilities.lock().await; - unsafe { - server_abilities.allow_flying = (*abilities).allow_flying; - server_abilities.allow_modify_world = (*abilities).allow_modify_world; - server_abilities.creative = (*abilities).creative; - server_abilities.fly_speed = (*abilities).fly_speed; - server_abilities.flying = (*abilities).flying; - server_abilities.invulnerable = (*abilities).invulnerable; - server_abilities.walk_speed = (*abilities).walk_speed; - } - }) - }); - - return true; - } - } - - false -} - -pub extern "C" fn rust_get_abilities(uuid_ptr: *const c_char, out: *mut AbilitiesFFI) -> bool { - let uuid_str = unsafe { CStr::from_ptr(uuid_ptr).to_string_lossy().into_owned() }; - if let Some(ctx) = CALLBACK_CONTEXT.get() { - let uuid = uuid::Uuid::parse_str(&uuid_str).unwrap(); - let player = ctx.plugin_context.server.get_player_by_uuid(uuid); - if let Some(player) = player { - let abilities = tokio::task::block_in_place(|| { - ctx.runtime - .block_on(async { AbilitiesFFI::new(player.abilities.lock().await) }) - }); - - unsafe { - *out = abilities; - } - - return true; - } - } - - false -} - -#[unsafe(no_mangle)] -pub extern "C" fn rust_register_event( - listener_ptr: *const c_void, // opaque pointer to Java object - plugin_ptr: *const c_void, -) { - // Handle event registration -} diff --git a/rust/src/lib.rs b/rust/src/lib.rs index fb8576e..3ff0d81 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use pumpkin::{plugin::Context, server::Server}; +use pumpkin::plugin::Context; use pumpkin_api_macros::{plugin_impl, plugin_method}; pub mod config; @@ -15,7 +15,7 @@ use java::{ resources::{cleanup_stale_files, sync_embedded_resources}, }; use tokio::sync::{ - mpsc::{self, Receiver, Sender}, + mpsc::{self, Receiver}, oneshot, }; @@ -200,17 +200,16 @@ pub struct PatchBukkitPlugin { impl PatchBukkitPlugin { pub fn new() -> Self { let (tx, rx) = mpsc::channel(100); - let command_tx = tx.clone(); #[tokio::main] - pub async fn jvm_thread_task(command_tx: Sender, rx: Receiver) { - JvmWorker::new(command_tx, rx).attach_thread().await; + pub async fn jvm_thread_task(rx: Receiver) { + JvmWorker::new(rx).attach_thread().await; } std::thread::Builder::new() .name("patchbukkit-jvm-worker".to_string()) .spawn(move || { - jvm_thread_task(command_tx.clone(), rx); + jvm_thread_task(rx); }) .unwrap(); PatchBukkitPlugin { command_tx: tx } diff --git a/rust/src/plugin/manager.rs b/rust/src/plugin/manager.rs index 0b69a08..e4fc5c0 100644 --- a/rust/src/plugin/manager.rs +++ b/rust/src/plugin/manager.rs @@ -223,9 +223,9 @@ impl PluginManager { &self, jvm: &Jvm, cmd_name: &str, - command: Arc>, + _command: Arc>, sender: SimpleCommandSender, - args: Vec, + _args: Vec, ) -> Result<(), String> { let j_sender = match sender { SimpleCommandSender::Console => jvm