Skip to content

Commit

Permalink
feat: add deop command and refactor op command (Pumpkin-MC#440)
Browse files Browse the repository at this point in the history
* 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
smuu authored Jan 1, 2025
1 parent c95345f commit 4bfbff7
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 29 deletions.
8 changes: 7 additions & 1 deletion pumpkin-core/src/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ pub enum PermissionLvl {

impl PartialOrd for PermissionLvl {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
(*self as u8).partial_cmp(&(*other as u8))
Some((*self as u8).cmp(&(*other as u8)))
}
}

impl Ord for PermissionLvl {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(*self as u8).cmp(&(*other as u8))
}
}

Expand Down
68 changes: 68 additions & 0 deletions pumpkin/src/command/commands/cmd_deop.rs
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))
}
52 changes: 27 additions & 25 deletions pumpkin/src/command/commands/cmd_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,42 @@ impl CommandExecutor for OpExecutor {
return Err(InvalidConsumption(Some(ARG_TARGET.into())));
};

// log each player to the console.
for player in targets {
let new_level = if BASIC_CONFIG.op_permission_level > sender.permission_lvl() {
sender.permission_lvl()
} else {
BASIC_CONFIG.op_permission_level
};
// from the command tree, the command can only be executed with one player
let player = &targets[0];

let new_level = BASIC_CONFIG
.op_permission_level
.min(sender.permission_lvl());

if let Some(op) = config
.ops
.iter_mut()
.find(|o| o.uuid == player.gameprofile.id)
{
op.level = new_level;
} else {
let op_entry = Op::new(
player.gameprofile.id,
player.gameprofile.name.clone(),
new_level,
false,
);
if let Some(op) = config
.ops
.iter_mut()
.find(|o| o.uuid == player.gameprofile.id)
{
op.level = new_level;
} else {
config.ops.push(op_entry);
}
config.save();
config.ops.push(op_entry);
}

player
.set_permission_lvl(new_level, &server.command_dispatcher)
.await;
config.save();

let player_name = player.gameprofile.name.clone();
let message = format!("Made {player_name} a server operator.");
let msg = TextComponent::text(message);
sender.send_message(msg).await;
}
player
.set_permission_lvl(new_level, &server.command_dispatcher)
.await;

let player_name = &player.gameprofile.name;
let message = format!("Made {player_name} a server operator.");
let msg = TextComponent::text(message);
sender.send_message(msg).await;
player
.send_system_message(&TextComponent::text("You are now a server operator."))
.await;

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions pumpkin/src/command/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod cmd_bossbar;
pub mod cmd_clear;
pub mod cmd_deop;
pub mod cmd_fill;
pub mod cmd_gamemode;
pub mod cmd_give;
Expand Down
6 changes: 3 additions & 3 deletions pumpkin/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use crate::server::Server;
use crate::world::World;
use args::ConsumedArgs;
use async_trait::async_trait;
use commands::cmd_op;
use commands::{
cmd_clear, cmd_fill, cmd_gamemode, cmd_give, cmd_help, cmd_kick, cmd_kill, cmd_list,
cmd_pumpkin, cmd_say, cmd_setblock, cmd_stop, cmd_teleport, cmd_time, cmd_worldborder,
cmd_clear, cmd_deop, cmd_fill, cmd_gamemode, cmd_give, cmd_help, cmd_kick, cmd_kill, cmd_list,
cmd_op, cmd_pumpkin, cmd_say, cmd_setblock, cmd_stop, cmd_teleport, cmd_time, cmd_worldborder,
};
use dispatcher::CommandError;
use pumpkin_core::math::vector3::Vector3;
Expand Down Expand Up @@ -131,6 +130,7 @@ pub fn default_dispatcher() -> CommandDispatcher {
dispatcher.register(cmd_transfer::init_command_tree(), PermissionLvl::Zero);
dispatcher.register(cmd_fill::init_command_tree(), PermissionLvl::Two);
dispatcher.register(cmd_op::init_command_tree(), PermissionLvl::Three);
dispatcher.register(cmd_deop::init_command_tree(), PermissionLvl::Three);

dispatcher
}
Expand Down

0 comments on commit 4bfbff7

Please sign in to comment.