Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
user622628252416 committed Aug 20, 2024
1 parent 413f88d commit 92e6d26
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 170 deletions.
25 changes: 15 additions & 10 deletions pumpkin/src/commands/arg_player.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use crate::client::Client;
use crate::commands::CommandSender;
use crate::commands::CommandSender::Player;
use crate::commands::dispatcher::InvalidTreeError;
use crate::commands::dispatcher::InvalidTreeError::InvalidConsumptionError;
use crate::commands::tree::{ConsumedArgs, RawArgs};
use crate::commands::CommandSender;
use crate::commands::CommandSender::Player;

/// todo: implement (so far only own name + @s/@p is implemented)
pub fn consume_arg_player(src: &CommandSender, args: &mut RawArgs) -> Option<String> {
let s = args.pop()?;
match s {

match s {
"@s" if src.is_player() => Some(s.into()),
"@p" if src.is_player() => Some(s.into()),
"@r" => None, // todo: implement random player target selector
"@r" => None, // todo: implement random player target selector
"@a" | "@e" => None, // todo: implement all players target selector
_ => {
// todo: implement any other player than sender
if let Player(client) = src {
if let Some(profile) = &client.gameprofile {
if profile.name == s {
return Some(s.into())
return Some(s.into());
};
};
};
Expand All @@ -29,8 +29,13 @@ pub fn consume_arg_player(src: &CommandSender, args: &mut RawArgs) -> Option<Str
}

/// todo: implement (so far only own name + @s/@p is implemented)
pub fn parse_arg_player<'a>(src: &'a mut CommandSender, arg_name: &str, consumed_args: &ConsumedArgs) -> Result<&'a mut Client, InvalidTreeError> {
let s = consumed_args.get(arg_name)
pub fn parse_arg_player<'a>(
src: &'a mut CommandSender,
arg_name: &str,
consumed_args: &ConsumedArgs,
) -> Result<&'a mut Client, InvalidTreeError> {
let s = consumed_args
.get(arg_name)
.ok_or(InvalidConsumptionError(None))?
.as_str();

Expand All @@ -44,11 +49,11 @@ pub fn parse_arg_player<'a>(src: &'a mut CommandSender, arg_name: &str, consumed
if let Player(client) = src {
if let Some(profile) = &client.gameprofile {
if profile.name == s {
return Ok(client)
return Ok(client);
};
};
};
Err(InvalidConsumptionError(Some(s.into())))
}
}
}
}
70 changes: 33 additions & 37 deletions pumpkin/src/commands/cmd_gamemode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use pumpkin_text::TextComponent;

use crate::commands::arg_player::{consume_arg_player, parse_arg_player};

use crate::commands::CommandSender;
use crate::commands::CommandSender::Player;
use crate::commands::dispatcher::InvalidTreeError;
use crate::commands::dispatcher::InvalidTreeError::{InvalidConsumptionError, InvalidRequirementError};
use crate::commands::dispatcher::InvalidTreeError::{
InvalidConsumptionError, InvalidRequirementError,
};
use crate::commands::tree::{CommandTree, ConsumedArgs, RawArgs};
use crate::commands::tree_builder::{argument, require};
use crate::commands::CommandSender;
use crate::commands::CommandSender::Player;
use crate::entity::player::GameMode;

pub(crate) const NAME: &str = "gamemode";
Expand All @@ -25,76 +27,70 @@ pub fn consume_arg_gamemode(_src: &CommandSender, args: &mut RawArgs) -> Option<

if let Ok(id) = s.parse::<u8>() {
match GameMode::from_u8(id) {
None | Some(GameMode::Undefined) => {},
Some(_) => return Some(s.into())
None | Some(GameMode::Undefined) => {}
Some(_) => return Some(s.into()),
};
};

match GameMode::from_str(s) {
Err(_) | Ok(GameMode::Undefined) => None,
Ok(_) => Some(s.into())
Ok(_) => Some(s.into()),
}
}

pub fn parse_arg_gamemode(consumed_args: &ConsumedArgs) -> Result<GameMode, InvalidTreeError> {
let s = consumed_args.get(ARG_GAMEMODE)
let s = consumed_args
.get(ARG_GAMEMODE)
.ok_or(InvalidConsumptionError(None))?;

if let Ok(id) = s.parse::<u8>() {
match GameMode::from_u8(id) {
None | Some(GameMode::Undefined) => {},
Some(gamemode) => return Ok(gamemode)
None | Some(GameMode::Undefined) => {}
Some(gamemode) => return Ok(gamemode),
};
};

match GameMode::from_str(s) {
Err(_) | Ok(GameMode::Undefined) => Err(InvalidConsumptionError(Some(s.into()))),
Ok(gamemode) => Ok(gamemode)
Ok(gamemode) => Ok(gamemode),
}
}

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(DESCRIPTION).with_child(

require(&|sender| {
sender.permission_lvl() >= 2
}).with_child(

argument(ARG_GAMEMODE, consume_arg_gamemode).with_child(

require(&|sender| sender.is_player())

.execute(&|sender, args| {
require(&|sender| sender.permission_lvl() >= 2).with_child(
argument(ARG_GAMEMODE, consume_arg_gamemode)
.with_child(
require(&|sender| sender.is_player()).execute(&|sender, args| {
let gamemode = parse_arg_gamemode(args)?;

return if let Player(target) = sender {
target.set_gamemode(gamemode);
target.send_system_message(TextComponent::text(
&format!("Game mode was set to {:?}", gamemode)
));
target.send_system_message(TextComponent::text(&format!(
"Game mode was set to {:?}",
gamemode
)));

Ok(())
} else {
Err(InvalidRequirementError)
}
})

).with_child(

argument(ARG_TARGET, consume_arg_player)

.execute(&|sender, args| {
};
}),
)
.with_child(
argument(ARG_TARGET, consume_arg_player).execute(&|sender, args| {
let gamemode = parse_arg_gamemode(args)?;
let target = parse_arg_player(sender, ARG_TARGET, args)?;

target.set_gamemode(gamemode);
target.send_system_message(TextComponent::text(
&format!("Set own game mode to {:?}", gamemode)
));
target.send_system_message(TextComponent::text(&format!(
"Set own game mode to {:?}",
gamemode
)));

Ok(())
})
)
)
}),
),
),
)
}
75 changes: 43 additions & 32 deletions pumpkin/src/commands/cmd_help.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use pumpkin_text::TextComponent;
use crate::commands::{CommandSender, DISPATCHER, dispatcher_init};
use crate::commands::dispatcher::{CommandDispatcher, InvalidTreeError};
use crate::commands::dispatcher::InvalidTreeError::InvalidConsumptionError;
use crate::commands::dispatcher::{CommandDispatcher, InvalidTreeError};
use crate::commands::tree::{CommandTree, ConsumedArgs, RawArgs};
use crate::commands::tree_builder::argument;
use crate::commands::{dispatcher_init, CommandSender, DISPATCHER};
use pumpkin_text::TextComponent;

pub(crate) const NAME: &str = "help";
pub(crate) const ALIAS: &str = "?";
Expand All @@ -17,12 +17,19 @@ fn consume_arg_command(_src: &CommandSender, args: &mut RawArgs) -> Option<Strin

let dispatcher = DISPATCHER.get_or_init(dispatcher_init);

if dispatcher.commands.contains_key(s) { Some(s.into()) }
else { None }
if dispatcher.commands.contains_key(s) {
Some(s.into())
} else {
None
}
}

fn parse_arg_command<'a>(consumed_args: &'a ConsumedArgs, dispatcher: &'a CommandDispatcher) -> Result<(&'a str, &'a CommandTree<'a>), InvalidTreeError> {
let command_name = consumed_args.get(ARG_COMMAND)
fn parse_arg_command<'a>(
consumed_args: &'a ConsumedArgs,
dispatcher: &'a CommandDispatcher,
) -> Result<(&'a str, &'a CommandTree<'a>), InvalidTreeError> {
let command_name = consumed_args
.get(ARG_COMMAND)
.ok_or(InvalidConsumptionError(None))?;

if let Some(tree) = dispatcher.commands.get::<&str>(&command_name.as_str()) {
Expand All @@ -33,31 +40,35 @@ fn parse_arg_command<'a>(consumed_args: &'a ConsumedArgs, dispatcher: &'a Comman
}

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(DESCRIPTION).with_child(
argument(ARG_COMMAND, consume_arg_command).execute(&|sender, args| {
CommandTree::new(DESCRIPTION)
.with_child(
argument(ARG_COMMAND, consume_arg_command).execute(&|sender, args| {
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);

let (name, tree) = parse_arg_command(args, dispatcher)?;

sender.send_message(TextComponent::text(&format!(
"{} - {} Usage:{}",
name,
tree.description,
tree.paths_formatted(name)
)));

Ok(())
}),
)
.execute(&|sender, _args| {
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);

let (name, tree) = parse_arg_command(args, dispatcher)?;

sender.send_message(
TextComponent::text(
&format!("{} - {} Usage:{}", name, tree.description, tree.paths_formatted(name))
)
);


for (name, tree) in &dispatcher.commands {
sender.send_message(TextComponent::text(&format!(
"{} - {} Usage:{}",
name,
tree.description,
tree.paths_formatted(name)
)));
}

Ok(())
})
).execute(&|sender, _args| {
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);

for (name, tree) in &dispatcher.commands {
sender.send_message(
TextComponent::text(
&format!("{} - {} Usage:{}", name, tree.description, tree.paths_formatted(name))
)
);
};

Ok(())
})
}
}
6 changes: 3 additions & 3 deletions pumpkin/src/commands/cmd_pumpkin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pumpkin_text::{color::NamedColor, TextComponent};
use pumpkin_protocol::CURRENT_MC_PROTOCOL;
use crate::server::CURRENT_MC_VERSION;
use pumpkin_protocol::CURRENT_MC_PROTOCOL;
use pumpkin_text::{color::NamedColor, TextComponent};

use crate::commands::tree::CommandTree;

Expand All @@ -12,7 +12,7 @@ pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(DESCRIPTION).execute(&|sender, _| {
let version = env!("CARGO_PKG_VERSION");
let description = env!("CARGO_PKG_DESCRIPTION");

sender.send_message(TextComponent::text(
&format!("Pumpkin {version}, {description} (Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL})")
).color_named(NamedColor::Green));
Expand Down
8 changes: 3 additions & 5 deletions pumpkin/src/commands/cmd_stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const DESCRIPTION: &str = "Stop the server.";

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(DESCRIPTION).with_child(
require(&|sender| { sender.permission_lvl() >= 4 })
.execute(&|_sender, _args| {
std::process::exit(0)
})
require(&|sender| sender.permission_lvl() >= 4)
.execute(&|_sender, _args| std::process::exit(0)),
)
}
}
Loading

0 comments on commit 92e6d26

Please sign in to comment.