-
-
Notifications
You must be signed in to change notification settings - Fork 164
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
feat: add whitelist feature #451
Open
smuu
wants to merge
7
commits into
Pumpkin-MC:master
Choose a base branch
from
smuu:smuu/20250101-add-whitelist-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9c97d50
feat: add whitelist command
smuu 6ca8773
fix
smuu fb2b213
fix: review
smuu 214269f
fix: reload when whitelist enforced dont kick op
smuu 7f8e937
Merge branch 'master' into smuu/20250101-add-whitelist-feature
smuu 97b5713
fix: merge conflict
smuu 9c6d401
Merge branch 'master' into smuu/20250101-add-whitelist-feature
Snowiiii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,221 @@ | ||
use crate::command::tree_builder::literal; | ||
use crate::data::player_profile::PlayerProfile; | ||
use crate::data::whitelist_data::WHITELIST_CONFIG; | ||
use crate::server::Server; | ||
use crate::{ | ||
command::{ | ||
args::{arg_players::PlayersArgumentConsumer, Arg, ConsumedArgs}, | ||
tree::CommandTree, | ||
tree_builder::argument, | ||
CommandError, CommandExecutor, CommandSender, | ||
}, | ||
data::{ReloadJSONConfiguration, SaveJSONConfiguration}, | ||
}; | ||
use async_trait::async_trait; | ||
use pumpkin_config::BASIC_CONFIG; | ||
use pumpkin_core::text::TextComponent; | ||
use pumpkin_core::PermissionLvl; | ||
use CommandError::InvalidConsumption; | ||
|
||
const NAMES: [&str; 1] = ["whitelist"]; | ||
const DESCRIPTION: &str = "Manages the server's whitelist."; | ||
const ARG_TARGET: &str = "player"; | ||
|
||
struct WhitelistAddExecutor; | ||
|
||
#[async_trait] | ||
impl CommandExecutor for WhitelistAddExecutor { | ||
async fn execute<'a>( | ||
&self, | ||
sender: &mut CommandSender<'a>, | ||
_server: &Server, | ||
args: &ConsumedArgs<'a>, | ||
) -> Result<(), CommandError> { | ||
let mut whitelist_config = WHITELIST_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 whitelist_config | ||
.whitelist | ||
.iter() | ||
.any(|p| p.uuid == player.gameprofile.id) | ||
{ | ||
let message = format!("Player {} is already whitelisted.", player.gameprofile.name); | ||
let msg = TextComponent::text(message); | ||
sender.send_message(msg).await; | ||
} else { | ||
let whitelist_entry = | ||
PlayerProfile::new(player.gameprofile.id, player.gameprofile.name.clone()); | ||
|
||
whitelist_config.whitelist.push(whitelist_entry); | ||
whitelist_config.save(); | ||
|
||
let player_name = &player.gameprofile.name; | ||
let message = format!("Added {player_name} to the whitelist."); | ||
let msg = TextComponent::text(message); | ||
sender.send_message(msg).await; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
struct WhitelistRemoveExecutor; | ||
|
||
#[async_trait] | ||
impl CommandExecutor for WhitelistRemoveExecutor { | ||
async fn execute<'a>( | ||
&self, | ||
sender: &mut CommandSender<'a>, | ||
server: &crate::server::Server, | ||
args: &ConsumedArgs<'a>, | ||
) -> Result<(), CommandError> { | ||
let mut whitelist_config = WHITELIST_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(pos) = whitelist_config | ||
.whitelist | ||
.iter() | ||
.position(|p| p.uuid == player.gameprofile.id) | ||
{ | ||
whitelist_config.whitelist.remove(pos); | ||
whitelist_config.save(); | ||
let is_op = player.permission_lvl.load() >= PermissionLvl::Three; | ||
if *server.white_list.read().await && BASIC_CONFIG.enforce_whitelist && !is_op { | ||
let msg = TextComponent::text("You are not whitelisted anymore."); | ||
player.kick(msg).await; | ||
} | ||
let message = format!("Removed {} from the whitelist.", player.gameprofile.name); | ||
let msg = TextComponent::text(message); | ||
sender.send_message(msg).await; | ||
} else { | ||
let message = format!("Player {} is not whitelisted.", player.gameprofile.name); | ||
let msg = TextComponent::text(message); | ||
sender.send_message(msg).await; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
struct WhitelistListExecutor; | ||
|
||
#[async_trait] | ||
impl CommandExecutor for WhitelistListExecutor { | ||
async fn execute<'a>( | ||
&self, | ||
sender: &mut CommandSender<'a>, | ||
_server: &crate::server::Server, | ||
_args: &ConsumedArgs<'a>, | ||
) -> Result<(), CommandError> { | ||
let whitelist_names: Vec<String> = WHITELIST_CONFIG | ||
.read() | ||
.await | ||
.whitelist | ||
.iter() | ||
.map(|p| p.name.clone()) | ||
.collect(); | ||
let message = format!("Whitelisted players: {whitelist_names:?}"); | ||
let msg = TextComponent::text(message); | ||
sender.send_message(msg).await; | ||
Ok(()) | ||
} | ||
} | ||
|
||
struct WhitelistOffExecutor; | ||
|
||
#[async_trait] | ||
impl CommandExecutor for WhitelistOffExecutor { | ||
async fn execute<'a>( | ||
&self, | ||
sender: &mut CommandSender<'a>, | ||
server: &Server, | ||
_args: &ConsumedArgs<'a>, | ||
) -> Result<(), CommandError> { | ||
let mut whitelist = server.white_list.write().await; | ||
*whitelist = false; | ||
let msg = TextComponent::text( | ||
"Whitelist is now off. To persist this change, modify the configuration.toml file.", | ||
); | ||
sender.send_message(msg).await; | ||
Ok(()) | ||
} | ||
} | ||
|
||
struct WhitelistOnExecutor; | ||
|
||
#[async_trait] | ||
impl CommandExecutor for WhitelistOnExecutor { | ||
async fn execute<'a>( | ||
&self, | ||
sender: &mut CommandSender<'a>, | ||
server: &Server, | ||
_args: &ConsumedArgs<'a>, | ||
) -> Result<(), CommandError> { | ||
let mut whitelist = server.white_list.write().await; | ||
*whitelist = true; | ||
let msg = TextComponent::text( | ||
"Whitelist is now on. To persist this change, modify the configuration.toml file.", | ||
); | ||
sender.send_message(msg).await; | ||
Ok(()) | ||
} | ||
} | ||
|
||
struct WhitelistReloadExecutor; | ||
|
||
#[async_trait] | ||
impl CommandExecutor for WhitelistReloadExecutor { | ||
async fn execute<'a>( | ||
&self, | ||
sender: &mut CommandSender<'a>, | ||
server: &Server, | ||
_args: &ConsumedArgs<'a>, | ||
) -> Result<(), CommandError> { | ||
let mut whitelist_config = WHITELIST_CONFIG.write().await; | ||
whitelist_config.reload(); | ||
// kick all players that are not whitelisted or operator if the whitelist is enforced | ||
if *server.white_list.read().await && BASIC_CONFIG.enforce_whitelist { | ||
for player in server.get_all_players().await { | ||
let is_whitelisted = whitelist_config | ||
.whitelist | ||
.iter() | ||
.any(|p| p.uuid == player.gameprofile.id); | ||
let is_op = player.permission_lvl.load() >= PermissionLvl::Three; | ||
if !is_whitelisted && !is_op { | ||
let msg = TextComponent::text("You are not whitelisted anymore."); | ||
player.kick(msg).await; | ||
} | ||
} | ||
} | ||
|
||
let msg = TextComponent::text("Whitelist configuration reloaded."); | ||
sender.send_message(msg).await; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn init_command_tree() -> CommandTree { | ||
CommandTree::new(NAMES, DESCRIPTION) | ||
.with_child(literal("add").with_child( | ||
argument(ARG_TARGET, PlayersArgumentConsumer).execute(WhitelistAddExecutor), | ||
)) | ||
.with_child(literal("remove").with_child( | ||
argument(ARG_TARGET, PlayersArgumentConsumer).execute(WhitelistRemoveExecutor), | ||
)) | ||
.with_child(literal("list").execute(WhitelistListExecutor)) | ||
.with_child(literal("off").execute(WhitelistOffExecutor)) | ||
.with_child(literal("on").execute(WhitelistOnExecutor)) | ||
.with_child(literal("reload").execute(WhitelistReloadExecutor)) | ||
} |
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
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ pub struct Op { | |
} | ||
|
||
impl Op { | ||
#[must_use] | ||
pub fn new( | ||
uuid: Uuid, | ||
name: String, | ||
|
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,15 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Default, Debug)] | ||
pub struct PlayerProfile { | ||
pub uuid: Uuid, | ||
pub name: String, | ||
} | ||
|
||
impl PlayerProfile { | ||
#[must_use] | ||
pub fn new(uuid: Uuid, name: String) -> Self { | ||
Self { uuid, name } | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both variables are used in the vanilla
server.properties
file. You can enable the whitelist but not enforce it. If you enforce it, players will be kicked when you reload the whitelist if they are not whitelisted.From the Minecraft wiki:
white-list
enforce-whitelist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mhh okay, I mean, I'm fine with leaving it then