forked from Pumpkin-MC/Pumpkin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add deop command and refactor op command (Pumpkin-MC#440)
* fix: command permission handling Signed-off-by: Smuu <[email protected]> * fix: formatting Signed-off-by: Smuu <[email protected]> * feat: add deop command Signed-off-by: Smuu <[email protected]> * refactor: op command Signed-off-by: Smuu <[email protected]> * fix: fmt and clippy Signed-off-by: Smuu <[email protected]> * fix: merge conflict Signed-off-by: Smuu <[email protected]> * fix: cargo fmt Signed-off-by: Smuu <[email protected]> --------- Signed-off-by: Smuu <[email protected]>
- Loading branch information
Showing
5 changed files
with
106 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::{ | ||
command::{ | ||
args::{arg_players::PlayersArgumentConsumer, Arg, ConsumedArgs}, | ||
tree::CommandTree, | ||
tree_builder::argument, | ||
CommandError, CommandExecutor, CommandSender, | ||
}, | ||
data::{op_data::OPERATOR_CONFIG, SaveJSONConfiguration}, | ||
}; | ||
use async_trait::async_trait; | ||
use pumpkin_core::text::TextComponent; | ||
use CommandError::InvalidConsumption; | ||
|
||
const NAMES: [&str; 1] = ["deop"]; | ||
const DESCRIPTION: &str = "Revokes operator status from a player."; | ||
const ARG_TARGET: &str = "player"; | ||
|
||
struct DeopExecutor; | ||
|
||
#[async_trait] | ||
impl CommandExecutor for DeopExecutor { | ||
async fn execute<'a>( | ||
&self, | ||
sender: &mut CommandSender<'a>, | ||
server: &crate::server::Server, | ||
args: &ConsumedArgs<'a>, | ||
) -> Result<(), CommandError> { | ||
let mut config = OPERATOR_CONFIG.write().await; | ||
|
||
let Some(Arg::Players(targets)) = args.get(&ARG_TARGET) else { | ||
return Err(InvalidConsumption(Some(ARG_TARGET.into()))); | ||
}; | ||
|
||
// from the command tree, the command can only be executed with one player | ||
let player = &targets[0]; | ||
|
||
if let Some(op_index) = config | ||
.ops | ||
.iter() | ||
.position(|o| o.uuid == player.gameprofile.id) | ||
{ | ||
config.ops.remove(op_index); | ||
} | ||
config.save(); | ||
|
||
player | ||
.set_permission_lvl( | ||
pumpkin_core::PermissionLvl::Zero, | ||
&server.command_dispatcher, | ||
) | ||
.await; | ||
|
||
let player_name = &player.gameprofile.name; | ||
let message = format!("Revoked {player_name}'s server operator status."); | ||
let msg = TextComponent::text(message); | ||
sender.send_message(msg).await; | ||
player | ||
.send_system_message(&TextComponent::text("You are no longer a server operator.")) | ||
.await; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn init_command_tree() -> CommandTree { | ||
CommandTree::new(NAMES, DESCRIPTION) | ||
.with_child(argument(ARG_TARGET, PlayersArgumentConsumer).execute(DeopExecutor)) | ||
} |
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