Skip to content

Commit

Permalink
Allow plugins to unregister commands (#522)
Browse files Browse the repository at this point in the history
* Add unregister function

* Add unregister_command to plugin context

* Send command suggestions packet to all players

* cargo clippy

* cargo clippy again
  • Loading branch information
vyPal authored Jan 31, 2025
1 parent 7ef2770 commit 8bb47b6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
19 changes: 19 additions & 0 deletions pumpkin/src/command/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,25 @@ impl CommandDispatcher {
self.commands
.insert(primary_name.to_string(), Command::Tree(tree));
}

/// Remove a command from the dispatcher by its primary name.
pub(crate) fn unregister(&mut self, name: &str) {
let mut to_remove = Vec::new();
for (key, value) in &self.commands {
if key == name {
to_remove.push(key.clone());
} else if let Command::Alias(target) = value {
if target == name {
to_remove.push(key.clone());
}
}
}

for key in to_remove {
self.commands.remove(&key);
self.permissions.remove(&key);
}
}
}

#[cfg(test)]
Expand Down
34 changes: 32 additions & 2 deletions pumpkin/src/plugin/api/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fs, path::Path, sync::Arc};

use crate::command::client_cmd_suggestions;
use pumpkin_util::PermissionLvl;
use tokio::sync::RwLock;

Expand Down Expand Up @@ -48,8 +49,37 @@ impl Context {
tree: crate::command::tree::CommandTree,
permission: PermissionLvl,
) {
let mut dispatcher_lock = self.server.command_dispatcher.write().await;
dispatcher_lock.register(tree, permission);
{
let mut dispatcher_lock = self.server.command_dispatcher.write().await;
dispatcher_lock.register(tree, permission);
};

for world in self.server.worlds.read().await.iter() {
for player in world.current_players.lock().await.values() {
client_cmd_suggestions::send_c_commands_packet(
player,
&self.server.command_dispatcher,
)
.await;
}
}
}

pub async fn unregister_command(&self, name: &str) {
{
let mut dispatcher_lock = self.server.command_dispatcher.write().await;
dispatcher_lock.unregister(name);
};

for world in self.server.worlds.read().await.iter() {
for player in world.current_players.lock().await.values() {
client_cmd_suggestions::send_c_commands_packet(
player,
&self.server.command_dispatcher,
)
.await;
}
}
}

pub async fn register_event<E: Event + 'static, H>(
Expand Down

0 comments on commit 8bb47b6

Please sign in to comment.