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

Added native operator permission management #348

Merged
merged 15 commits into from
Dec 27, 2024
Merged
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ services:
pumpkin:
build: .
ports:
- 25565:25565
- "25565:25565"
volumes:
- ./data:/pumpkin
stdin_open: true
tty: true
KairuDeibisu marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 31 additions & 2 deletions pumpkin-config/src/op.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use uuid::Uuid;

#[derive(Serialize, Deserialize, Clone, Copy)]
#[derive(Clone, Copy)]
#[repr(u8)]
pub enum OpLevel {
None = 0,
KairuDeibisu marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -11,6 +11,35 @@ pub enum OpLevel {
Owner = 4,
}

impl Serialize for OpLevel {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
serializer.serialize_u8(*self as u8)
}
}

impl<'de> Deserialize<'de> for OpLevel {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = u8::deserialize(deserializer)?;
match value {
0 => Ok(OpLevel::None),
1 => Ok(OpLevel::Basic),
2 => Ok(OpLevel::Moderator),
3 => Ok(OpLevel::Admin),
4 => Ok(OpLevel::Owner),
_ => Err(serde::de::Error::custom(format!(
"Invalid value for OpLevel: {}",
value
))),
}
}
}

#[derive(Serialize, Deserialize, Clone)]
KairuDeibisu marked this conversation as resolved.
Show resolved Hide resolved
pub struct Op {
pub uuid: Uuid,
Expand Down
8 changes: 5 additions & 3 deletions pumpkin/src/command/commands/cmd_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl CommandExecutor for OpExecutor {
async fn execute<'a>(
&self,
sender: &mut CommandSender<'a>,
_server: &crate::server::Server,
server: &crate::server::Server,
args: &ConsumedArgs<'a>,
) -> Result<(), CommandError> {
let mut config = OPERATOR_CONFIG.write().await;
Expand Down Expand Up @@ -52,9 +52,11 @@ impl CommandExecutor for OpExecutor {
}
config.save();

// TODO: can't fully implement until require can accept async closures
player
.set_permission_lvl(BASIC_CONFIG.op_permission_level.into())
.set_permission_lvl(
BASIC_CONFIG.op_permission_level.into(),
&server.command_dispatcher,
)
.await;

let player_name = player.gameprofile.name.clone();
Expand Down
8 changes: 7 additions & 1 deletion pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use crate::{
combat::{self, player_attack_sound, AttackType},
Client, PlayerConfig,
},
command::{client_cmd_suggestions, dispatcher::CommandDispatcher},
server::Server,
world::World,
};
Expand Down Expand Up @@ -523,13 +524,18 @@ impl Player {
}

/// sets the players permission level and syncs it with the client
pub async fn set_permission_lvl(&self, lvl: PermissionLvl) {
pub async fn set_permission_lvl(
self: &Arc<Self>,
lvl: PermissionLvl,
command_dispatcher: &Arc<CommandDispatcher<'static>>,
) {
{
let mut level = self.permission_lvl.lock();
*level = lvl;
}

self.send_permission_lvl_update().await;
client_cmd_suggestions::send_c_commands_packet(self, command_dispatcher).await;
}

/// get the players permission level
Expand Down
Loading