Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: command permission handling #428

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pumpkin/src/command/client_cmd_suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ pub async fn send_c_commands_packet(player: &Arc<Player>, dispatcher: &RwLock<Co
continue;
};

let Some(permission) = dispatcher.get_permission_lvl(key) else {
continue;
};

if !cmd_src.has_permission_lvl(permission) {
continue;
}

let (is_executable, child_nodes) =
nodes_to_proto_node_builders(&cmd_src, &tree.nodes, &tree.children);

Expand Down
26 changes: 11 additions & 15 deletions pumpkin/src/command/commands/cmd_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use crate::command::args::arg_block::BlockArgumentConsumer;
use crate::command::args::arg_position_block::BlockPosArgumentConsumer;
use crate::command::args::{ConsumedArgs, FindArg};
use crate::command::tree::CommandTree;
use crate::command::tree_builder::{argument, literal, require};
use crate::command::tree_builder::{argument, literal};
use crate::command::{CommandError, CommandExecutor, CommandSender};

use async_trait::async_trait;
use pumpkin_core::math::position::WorldPosition;
use pumpkin_core::math::vector3::Vector3;
use pumpkin_core::permission::PermissionLvl;
use pumpkin_core::text::TextComponent;

const NAMES: [&str; 1] = ["fill"];
Expand Down Expand Up @@ -157,19 +156,16 @@ impl CommandExecutor for SetblockExecutor {

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(|sender| sender.has_permission_lvl(PermissionLvl::Two) && sender.world().is_some())
.with_child(
argument(ARG_FROM, BlockPosArgumentConsumer).with_child(
argument(ARG_TO, BlockPosArgumentConsumer).with_child(
argument(ARG_BLOCK, BlockArgumentConsumer)
.with_child(literal("destroy").execute(SetblockExecutor(Mode::Destroy)))
.with_child(literal("hollow").execute(SetblockExecutor(Mode::Hollow)))
.with_child(literal("keep").execute(SetblockExecutor(Mode::Keep)))
.with_child(literal("outline").execute(SetblockExecutor(Mode::Outline)))
.with_child(literal("replace").execute(SetblockExecutor(Mode::Replace)))
.execute(SetblockExecutor(Mode::Replace)),
),
),
argument(ARG_FROM, BlockPosArgumentConsumer).with_child(
argument(ARG_TO, BlockPosArgumentConsumer).with_child(
argument(ARG_BLOCK, BlockArgumentConsumer)
.with_child(literal("destroy").execute(SetblockExecutor(Mode::Destroy)))
.with_child(literal("hollow").execute(SetblockExecutor(Mode::Hollow)))
.with_child(literal("keep").execute(SetblockExecutor(Mode::Keep)))
.with_child(literal("outline").execute(SetblockExecutor(Mode::Outline)))
.with_child(literal("replace").execute(SetblockExecutor(Mode::Replace)))
.execute(SetblockExecutor(Mode::Replace)),
),
),
)
}
13 changes: 5 additions & 8 deletions pumpkin/src/command/commands/cmd_gamemode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::command::args::arg_gamemode::GamemodeArgumentConsumer;
use crate::command::args::GetCloned;

use crate::TextComponent;
use pumpkin_core::permission::PermissionLvl;

use crate::command::args::arg_players::PlayersArgumentConsumer;

Expand Down Expand Up @@ -109,12 +108,10 @@ impl CommandExecutor for GamemodeTargetPlayer {
#[allow(clippy::redundant_closure_for_method_calls)]
pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(|sender| sender.has_permission_lvl(PermissionLvl::Two)).with_child(
argument(ARG_GAMEMODE, GamemodeArgumentConsumer)
.with_child(require(|sender| sender.is_player()).execute(GamemodeTargetSelf))
.with_child(
argument(ARG_TARGET, PlayersArgumentConsumer).execute(GamemodeTargetPlayer),
),
),
argument(ARG_GAMEMODE, GamemodeArgumentConsumer)
.with_child(require(|sender| sender.is_player()).execute(GamemodeTargetSelf))
.with_child(
argument(ARG_TARGET, PlayersArgumentConsumer).execute(GamemodeTargetPlayer),
),
)
}
13 changes: 5 additions & 8 deletions pumpkin/src/command/commands/cmd_give.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use crate::command::args::arg_item::ItemArgumentConsumer;
use crate::command::args::arg_players::PlayersArgumentConsumer;
use crate::command::args::{ConsumedArgs, FindArg, FindArgDefaultName};
use crate::command::tree::CommandTree;
use crate::command::tree_builder::{argument, argument_default_name, require};
use crate::command::tree_builder::{argument, argument_default_name};
use crate::command::{CommandError, CommandExecutor, CommandSender};
use pumpkin_core::permission::PermissionLvl;

const NAMES: [&str; 1] = ["give"];

Expand Down Expand Up @@ -76,12 +75,10 @@ impl CommandExecutor for GiveExecutor {

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(|sender| sender.has_permission_lvl(PermissionLvl::Two)).with_child(
argument_default_name(PlayersArgumentConsumer).with_child(
argument(ARG_ITEM, ItemArgumentConsumer)
.execute(GiveExecutor)
.with_child(argument_default_name(item_count_consumer()).execute(GiveExecutor)),
),
argument_default_name(PlayersArgumentConsumer).with_child(
argument(ARG_ITEM, ItemArgumentConsumer)
.execute(GiveExecutor)
.with_child(argument_default_name(item_count_consumer()).execute(GiveExecutor)),
),
)
}
9 changes: 3 additions & 6 deletions pumpkin/src/command/commands/cmd_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use crate::{
command::{
args::{arg_players::PlayersArgumentConsumer, Arg, ConsumedArgs},
tree::CommandTree,
tree_builder::{argument, require},
tree_builder::argument,
CommandError, CommandExecutor, CommandSender,
},
data::{op_data::OPERATOR_CONFIG, SaveJSONConfiguration},
};
use async_trait::async_trait;
use pumpkin_config::{op::Op, BASIC_CONFIG};
use pumpkin_core::permission::PermissionLvl;
use pumpkin_core::text::TextComponent;
use CommandError::InvalidConsumption;

Expand Down Expand Up @@ -73,8 +72,6 @@ impl CommandExecutor for OpExecutor {
}

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(|sender| sender.has_permission_lvl(PermissionLvl::Three))
.with_child(argument(ARG_TARGET, PlayersArgumentConsumer).execute(OpExecutor)),
)
CommandTree::new(NAMES, DESCRIPTION)
.with_child(argument(ARG_TARGET, PlayersArgumentConsumer).execute(OpExecutor))
}
9 changes: 3 additions & 6 deletions pumpkin/src/command/commands/cmd_say.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use pumpkin_protocol::client::play::CSystemChatMessage;
use crate::command::{
args::{arg_message::MsgArgConsumer, Arg, ConsumedArgs},
tree::CommandTree,
tree_builder::{argument, require},
tree_builder::argument,
CommandError, CommandExecutor, CommandSender,
};
use pumpkin_core::permission::PermissionLvl;
use CommandError::InvalidConsumption;

const NAMES: [&str; 1] = ["say"];
Expand Down Expand Up @@ -42,8 +41,6 @@ impl CommandExecutor for SayExecutor {
}

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(|sender| sender.has_permission_lvl(PermissionLvl::Two))
.with_child(argument(ARG_MESSAGE, MsgArgConsumer).execute(SayExecutor)),
)
CommandTree::new(NAMES, DESCRIPTION)
.with_child(argument(ARG_MESSAGE, MsgArgConsumer).execute(SayExecutor))
}
8 changes: 1 addition & 7 deletions pumpkin/src/command/commands/cmd_seed.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::command::tree_builder::require;
use crate::command::{
args::ConsumedArgs, tree::CommandTree, CommandError, CommandExecutor, CommandSender,
};
use async_trait::async_trait;
use pumpkin_core::permission::PermissionLvl;
use pumpkin_core::text::click::ClickEvent;
use pumpkin_core::text::hover::HoverEvent;
use pumpkin_core::text::{color::NamedColor, TextComponent};
Expand Down Expand Up @@ -55,9 +53,5 @@ impl CommandExecutor for PumpkinExecutor {
}

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION)
.with_child(require(|sender| {
sender.has_permission_lvl(PermissionLvl::Two)
}))
.execute(PumpkinExecutor)
CommandTree::new(NAMES, DESCRIPTION).execute(PumpkinExecutor)
}
20 changes: 8 additions & 12 deletions pumpkin/src/command/commands/cmd_setblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use crate::command::args::arg_block::BlockArgumentConsumer;
use crate::command::args::arg_position_block::BlockPosArgumentConsumer;
use crate::command::args::{ConsumedArgs, FindArg};
use crate::command::tree::CommandTree;
use crate::command::tree_builder::{argument, literal, require};
use crate::command::tree_builder::{argument, literal};
use crate::command::{CommandError, CommandExecutor, CommandSender};
use pumpkin_core::permission::PermissionLvl;

const NAMES: [&str; 1] = ["setblock"];

Expand Down Expand Up @@ -81,15 +80,12 @@ impl CommandExecutor for SetblockExecutor {

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(|sender| sender.has_permission_lvl(PermissionLvl::Two) && sender.world().is_some())
.with_child(
argument(ARG_BLOCK_POS, BlockPosArgumentConsumer).with_child(
argument(ARG_BLOCK, BlockArgumentConsumer)
.with_child(literal("replace").execute(SetblockExecutor(Mode::Replace)))
.with_child(literal("destroy").execute(SetblockExecutor(Mode::Destroy)))
.with_child(literal("keep").execute(SetblockExecutor(Mode::Keep)))
.execute(SetblockExecutor(Mode::Replace)),
),
),
argument(ARG_BLOCK_POS, BlockPosArgumentConsumer).with_child(
argument(ARG_BLOCK, BlockArgumentConsumer)
.with_child(literal("replace").execute(SetblockExecutor(Mode::Replace)))
.with_child(literal("destroy").execute(SetblockExecutor(Mode::Destroy)))
.with_child(literal("keep").execute(SetblockExecutor(Mode::Keep)))
.execute(SetblockExecutor(Mode::Replace)),
),
)
}
6 changes: 1 addition & 5 deletions pumpkin/src/command/commands/cmd_stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use pumpkin_core::text::TextComponent;

use crate::command::args::ConsumedArgs;
use crate::command::tree::CommandTree;
use crate::command::tree_builder::require;
use crate::command::{CommandError, CommandExecutor, CommandSender};
use pumpkin_core::permission::PermissionLvl;

const NAMES: [&str; 1] = ["stop"];

Expand Down Expand Up @@ -38,7 +36,5 @@ impl CommandExecutor for StopExecutor {
}

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(|sender| sender.has_permission_lvl(PermissionLvl::Four)).execute(StopExecutor),
)
CommandTree::new(NAMES, DESCRIPTION).execute(StopExecutor)
}
71 changes: 33 additions & 38 deletions pumpkin/src/command/commands/cmd_teleport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use crate::command::args::arg_rotation::RotationArgumentConsumer;
use crate::command::args::ConsumedArgs;
use crate::command::args::FindArg;
use crate::command::tree::CommandTree;
use crate::command::tree_builder::{argument, literal, require};
use crate::command::tree_builder::{argument, literal};
use crate::command::CommandError;
use crate::command::{CommandExecutor, CommandSender};
use pumpkin_core::permission::PermissionLvl;

const NAMES: [&str; 2] = ["teleport", "tp"];
const DESCRIPTION: &str = "Teleports entities, including players."; // todo
Expand Down Expand Up @@ -238,41 +237,37 @@ impl CommandExecutor for TpSelfToPosExecutor {
}

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(|sender| sender.has_permission_lvl(PermissionLvl::Two))
.with_child(
argument(ARG_LOCATION, Position3DArgumentConsumer).execute(TpSelfToPosExecutor),
)
.with_child(
argument(ARG_DESTINATION, EntityArgumentConsumer).execute(TpSelfToEntityExecutor),
)
.with_child(
argument(ARG_TARGETS, EntitiesArgumentConsumer)
.with_child(
argument(ARG_LOCATION, Position3DArgumentConsumer)
.execute(TpEntitiesToPosExecutor)
.with_child(
argument(ARG_ROTATION, RotationArgumentConsumer)
.execute(TpEntitiesToPosWithRotationExecutor),
)
.with_child(
literal("facing")
.with_child(
literal("entity").with_child(
argument(ARG_FACING_ENTITY, EntityArgumentConsumer)
.execute(TpEntitiesToPosFacingEntityExecutor),
),
)
.with_child(
argument(ARG_FACING_LOCATION, Position3DArgumentConsumer)
.execute(TpEntitiesToPosFacingPosExecutor),
CommandTree::new(NAMES, DESCRIPTION)
.with_child(argument(ARG_LOCATION, Position3DArgumentConsumer).execute(TpSelfToPosExecutor))
.with_child(
argument(ARG_DESTINATION, EntityArgumentConsumer).execute(TpSelfToEntityExecutor),
)
.with_child(
argument(ARG_TARGETS, EntitiesArgumentConsumer)
.with_child(
argument(ARG_LOCATION, Position3DArgumentConsumer)
.execute(TpEntitiesToPosExecutor)
.with_child(
argument(ARG_ROTATION, RotationArgumentConsumer)
.execute(TpEntitiesToPosWithRotationExecutor),
)
.with_child(
literal("facing")
.with_child(
literal("entity").with_child(
argument(ARG_FACING_ENTITY, EntityArgumentConsumer)
.execute(TpEntitiesToPosFacingEntityExecutor),
),
),
)
.with_child(
argument(ARG_DESTINATION, EntityArgumentConsumer)
.execute(TpEntitiesToEntityExecutor),
),
),
)
)
.with_child(
argument(ARG_FACING_LOCATION, Position3DArgumentConsumer)
.execute(TpEntitiesToPosFacingPosExecutor),
),
),
)
.with_child(
argument(ARG_DESTINATION, EntityArgumentConsumer)
.execute(TpEntitiesToEntityExecutor),
),
)
}
32 changes: 16 additions & 16 deletions pumpkin/src/command/commands/cmd_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ use crate::command::args::arg_bounded_num::BoundedNumArgumentConsumer;
use crate::command::args::FindArgDefaultName;
use crate::command::tree_builder::{argument_default_name, literal};
use crate::command::{
tree::CommandTree, tree_builder::require, CommandError, CommandExecutor, CommandSender,
ConsumedArgs,
tree::CommandTree, CommandError, CommandExecutor, CommandSender, ConsumedArgs,
};
use pumpkin_core::permission::PermissionLvl;

const NAMES: [&str; 1] = ["time"];

Expand Down Expand Up @@ -125,19 +123,21 @@ impl CommandExecutor for TimeChangeExecutor {
}

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(|sender| sender.has_permission_lvl(PermissionLvl::Two))
.with_child(literal("add").with_child(
CommandTree::new(NAMES, DESCRIPTION)
.with_child(
literal("add").with_child(
argument_default_name(arg_number()).execute(TimeChangeExecutor(Mode::Add)),
))
.with_child(
literal("query")
.with_child(literal("daytime").execute(TimeQueryExecutor(QueryMode::DayTime)))
.with_child(literal("gametime").execute(TimeQueryExecutor(QueryMode::GameTime)))
.with_child(literal("day").execute(TimeQueryExecutor(QueryMode::Day))),
)
.with_child(literal("set").with_child(
),
)
.with_child(
literal("query")
.with_child(literal("daytime").execute(TimeQueryExecutor(QueryMode::DayTime)))
.with_child(literal("gametime").execute(TimeQueryExecutor(QueryMode::GameTime)))
.with_child(literal("day").execute(TimeQueryExecutor(QueryMode::Day))),
)
.with_child(
literal("set").with_child(
argument_default_name(arg_number()).execute(TimeChangeExecutor(Mode::Set)),
)),
)
),
)
}
Loading
Loading