Support parsing players out of commands and custom error messages
This commit is contained in:
1 parent
406db91f06
commit
5ac45b0ed5
10 files changed
+109
-83
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
use std::f32::consts::PI;
|
use std::{f32::consts::PI, sync::Arc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
commands::CommandSender,
|
commands::CommandSender,
|
||||||
@@ -216,9 +216,13 @@ impl Player {
|
|||||||
world.broadcast_packet_expect(&[self.client.id], &packet);
|
world.broadcast_packet_expect(&[self.client.id], &packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_chat_command(&self, server: &Server, command: SChatCommand) {
|
pub fn handle_chat_command(self: &Arc<Self>, server: &Server, command: SChatCommand) {
|
||||||
let dispatcher = server.command_dispatcher.clone();
|
let dispatcher = server.command_dispatcher.clone();
|
||||||
dispatcher.handle_command(&mut CommandSender::Player(self), server, &command.command);
|
dispatcher.handle_command(
|
||||||
|
&mut CommandSender::Player(self.clone()),
|
||||||
|
server,
|
||||||
|
&command.command,
|
||||||
|
);
|
||||||
if ADVANCED_CONFIG.commands.log_console {
|
if ADVANCED_CONFIG.commands.log_console {
|
||||||
log::info!(
|
log::info!(
|
||||||
"Player ({}): executed command /{}",
|
"Player ({}): executed command /{}",
|
||||||
|
|||||||
@@ -1,39 +1,50 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::commands::dispatcher::InvalidTreeError;
|
use crate::commands::dispatcher::InvalidTreeError;
|
||||||
use crate::commands::dispatcher::InvalidTreeError::InvalidConsumptionError;
|
use crate::commands::dispatcher::InvalidTreeError::InvalidConsumptionError;
|
||||||
use crate::commands::tree::{ConsumedArgs, RawArgs};
|
use crate::commands::tree::{ConsumedArgs, RawArgs};
|
||||||
use crate::commands::CommandSender;
|
use crate::commands::CommandSender;
|
||||||
use crate::commands::CommandSender::Player;
|
|
||||||
use crate::server::Server;
|
use crate::server::Server;
|
||||||
|
|
||||||
/// todo: implement (so far only own name + @s/@p is implemented)
|
/// todo: implement (so far only own name + @s/@p is implemented)
|
||||||
pub fn consume_arg_player(src: &CommandSender, args: &mut RawArgs) -> Option<String> {
|
pub fn consume_arg_player(
|
||||||
let s = args.pop()?;
|
src: &CommandSender,
|
||||||
|
server: &Server,
|
||||||
match s {
|
args: &mut RawArgs,
|
||||||
"@s" if src.is_player() => Some(s.into()),
|
) -> Result<String, Option<String>> {
|
||||||
"@p" if src.is_player() => Some(s.into()),
|
if let Some(arg) = args.pop() {
|
||||||
"@r" => None, // todo: implement random player target selector
|
match arg {
|
||||||
"@a" | "@e" => None, // todo: implement all players target selector
|
"@s" => {
|
||||||
_ => {
|
if src.is_player() {
|
||||||
// todo: implement any other player than sender
|
return Ok(arg.into());
|
||||||
if let Player(player) = src {
|
} else {
|
||||||
let profile = &player.gameprofile;
|
return Err(Some("You are not a Player".into()));
|
||||||
if profile.name == s {
|
}
|
||||||
return Some(s.into());
|
}
|
||||||
};
|
"@p" if src.is_player() => return Ok(arg.into()),
|
||||||
};
|
"@r" => todo!(), // todo: implement random player target selector
|
||||||
None
|
"@a" | "@e" => todo!(), // todo: implement all players target selector
|
||||||
|
name => {
|
||||||
|
// todo: implement any other player than sender
|
||||||
|
for world in &server.worlds {
|
||||||
|
if world.get_player_by_name(name).is_some() {
|
||||||
|
return Ok(name.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err(Some(format!("Player not found: {}", arg)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// todo: implement (so far only own name + @s/@p is implemented)
|
/// todo: implement (so far only own name + @s/@p is implemented)
|
||||||
pub fn parse_arg_player<'a>(
|
pub fn parse_arg_player(
|
||||||
src: &'a mut CommandSender,
|
src: &mut CommandSender,
|
||||||
_server: &Server,
|
server: &Server,
|
||||||
arg_name: &str,
|
arg_name: &str,
|
||||||
consumed_args: &ConsumedArgs,
|
consumed_args: &ConsumedArgs,
|
||||||
) -> Result<&'a crate::entity::player::Player, InvalidTreeError> {
|
) -> Result<Arc<crate::entity::player::Player>, InvalidTreeError> {
|
||||||
let s = consumed_args
|
let s = consumed_args
|
||||||
.get(arg_name)
|
.get(arg_name)
|
||||||
.ok_or(InvalidConsumptionError(None))?
|
.ok_or(InvalidConsumptionError(None))?
|
||||||
@@ -44,14 +55,12 @@ pub fn parse_arg_player<'a>(
|
|||||||
"@p" if src.is_player() => Ok(src.as_mut_player().unwrap()),
|
"@p" if src.is_player() => Ok(src.as_mut_player().unwrap()),
|
||||||
"@r" => Err(InvalidConsumptionError(Some(s.into()))), // todo: implement random player target selector
|
"@r" => Err(InvalidConsumptionError(Some(s.into()))), // todo: implement random player target selector
|
||||||
"@a" | "@e" => Err(InvalidConsumptionError(Some(s.into()))), // todo: implement all players target selector
|
"@a" | "@e" => Err(InvalidConsumptionError(Some(s.into()))), // todo: implement all players target selector
|
||||||
_ => {
|
name => {
|
||||||
// todo: implement any other player than sender
|
for world in &server.worlds {
|
||||||
if let Player(player) = src {
|
if let Some(player) = world.get_player_by_name(name) {
|
||||||
let profile = &player.gameprofile;
|
|
||||||
if profile.name == s {
|
|
||||||
return Ok(player);
|
return Ok(player);
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
Err(InvalidConsumptionError(Some(s.into())))
|
Err(InvalidConsumptionError(Some(s.into())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ use crate::commands::tree::{CommandTree, ConsumedArgs, RawArgs};
|
|||||||
use crate::commands::tree_builder::{argument, require};
|
use crate::commands::tree_builder::{argument, require};
|
||||||
use crate::commands::CommandSender;
|
use crate::commands::CommandSender;
|
||||||
use crate::commands::CommandSender::Player;
|
use crate::commands::CommandSender::Player;
|
||||||
|
use crate::server::Server;
|
||||||
|
|
||||||
const NAMES: [&str; 1] = ["gamemode"];
|
const NAMES: [&str; 1] = ["gamemode"];
|
||||||
|
|
||||||
@@ -22,20 +23,27 @@ const DESCRIPTION: &str = "Change a player's gamemode.";
|
|||||||
const ARG_GAMEMODE: &str = "gamemode";
|
const ARG_GAMEMODE: &str = "gamemode";
|
||||||
const ARG_TARGET: &str = "target";
|
const ARG_TARGET: &str = "target";
|
||||||
|
|
||||||
pub fn consume_arg_gamemode(_src: &CommandSender, args: &mut RawArgs) -> Option<String> {
|
pub fn consume_arg_gamemode(
|
||||||
let s = args.pop()?;
|
_src: &CommandSender,
|
||||||
|
_server: &Server,
|
||||||
if let Ok(id) = s.parse::<u8>() {
|
args: &mut RawArgs,
|
||||||
match GameMode::from_u8(id) {
|
) -> Result<String, Option<String>> {
|
||||||
None | Some(GameMode::Undefined) => {}
|
if let Some(arg) = args.pop() {
|
||||||
Some(_) => return Some(s.into()),
|
if let Ok(id) = arg.parse::<u8>() {
|
||||||
|
match GameMode::from_u8(id) {
|
||||||
|
None | Some(GameMode::Undefined) => {}
|
||||||
|
Some(_) => return Ok(arg.into()),
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
match GameMode::from_str(s) {
|
match GameMode::from_str(arg) {
|
||||||
Err(_) | Ok(GameMode::Undefined) => None,
|
Err(_) | Ok(GameMode::Undefined) => {
|
||||||
Ok(_) => Some(s.into()),
|
return Err(Some(format!("Gamemode not found: {}", arg)))
|
||||||
|
}
|
||||||
|
Ok(_) => return Ok(arg.into()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Err(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_arg_gamemode(consumed_args: &ConsumedArgs) -> Result<GameMode, InvalidTreeError> {
|
pub fn parse_arg_gamemode(consumed_args: &ConsumedArgs) -> Result<GameMode, InvalidTreeError> {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use crate::commands::dispatcher::{CommandDispatcher, InvalidTreeError};
|
|||||||
use crate::commands::tree::{Command, CommandTree, ConsumedArgs, RawArgs};
|
use crate::commands::tree::{Command, CommandTree, ConsumedArgs, RawArgs};
|
||||||
use crate::commands::tree_builder::argument;
|
use crate::commands::tree_builder::argument;
|
||||||
use crate::commands::CommandSender;
|
use crate::commands::CommandSender;
|
||||||
|
use crate::server::Server;
|
||||||
use pumpkin_core::text::TextComponent;
|
use pumpkin_core::text::TextComponent;
|
||||||
|
|
||||||
const NAMES: [&str; 3] = ["help", "h", "?"];
|
const NAMES: [&str; 3] = ["help", "h", "?"];
|
||||||
@@ -11,12 +12,16 @@ const DESCRIPTION: &str = "Print a help message.";
|
|||||||
|
|
||||||
const ARG_COMMAND: &str = "command";
|
const ARG_COMMAND: &str = "command";
|
||||||
|
|
||||||
fn consume_arg_command(_src: &CommandSender, _args: &mut RawArgs) -> Option<String> {
|
fn consume_arg_command(
|
||||||
|
_src: &CommandSender,
|
||||||
|
_server: &Server,
|
||||||
|
_args: &mut RawArgs,
|
||||||
|
) -> Result<String, Option<String>> {
|
||||||
// let s = args.pop()?;
|
// let s = args.pop()?;
|
||||||
|
|
||||||
// dispatcher.get_tree(s).ok().map(|tree| tree.names[0].into())
|
// dispatcher.get_tree(s).ok().map(|tree| tree.names[0].into())
|
||||||
// TODO
|
// TODO
|
||||||
None
|
Err(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_arg_command<'a>(
|
fn parse_arg_command<'a>(
|
||||||
|
|||||||
@@ -1,22 +1,19 @@
|
|||||||
use crate::commands::arg_player::{consume_arg_player, parse_arg_player};
|
use crate::commands::arg_player::parse_arg_player;
|
||||||
use crate::commands::tree::CommandTree;
|
use crate::commands::tree::CommandTree;
|
||||||
use crate::commands::tree::RawArgs;
|
|
||||||
use crate::commands::tree_builder::argument;
|
use crate::commands::tree_builder::argument;
|
||||||
use crate::commands::CommandSender;
|
|
||||||
use pumpkin_core::text::{color::NamedColor, TextComponent};
|
use pumpkin_core::text::{color::NamedColor, TextComponent};
|
||||||
|
|
||||||
|
use super::arg_player::consume_arg_player;
|
||||||
|
|
||||||
const NAMES: [&str; 1] = ["kick"];
|
const NAMES: [&str; 1] = ["kick"];
|
||||||
const DESCRIPTION: &str = "Kicks the target player from the server.";
|
const DESCRIPTION: &str = "Kicks the target player from the server.";
|
||||||
|
|
||||||
const ARG_TARGET: &str = "target";
|
const ARG_TARGET: &str = "target";
|
||||||
|
|
||||||
pub fn consume_arg_target(_src: &CommandSender, args: &mut RawArgs) -> Option<String> {
|
|
||||||
consume_arg_player(_src, args)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_command_tree<'a>() -> CommandTree<'a> {
|
pub fn init_command_tree<'a>() -> CommandTree<'a> {
|
||||||
CommandTree::new(NAMES, DESCRIPTION).with_child(
|
CommandTree::new(NAMES, DESCRIPTION).with_child(
|
||||||
argument(ARG_TARGET, consume_arg_target).execute(&|sender, server, args| {
|
argument(ARG_TARGET, consume_arg_player).execute(&|sender, server, args| {
|
||||||
|
dbg!("aa");
|
||||||
let target = parse_arg_player(sender, server, ARG_TARGET, args)?;
|
let target = parse_arg_player(sender, server, ARG_TARGET, args)?;
|
||||||
target.kick(TextComponent::text("Kicked by an operator"));
|
target.kick(TextComponent::text("Kicked by an operator"));
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
use crate::commands::arg_player::{consume_arg_player, parse_arg_player};
|
use crate::commands::arg_player::{consume_arg_player, parse_arg_player};
|
||||||
use crate::commands::tree::CommandTree;
|
use crate::commands::tree::CommandTree;
|
||||||
use crate::commands::tree::RawArgs;
|
|
||||||
use crate::commands::tree_builder::argument;
|
use crate::commands::tree_builder::argument;
|
||||||
use crate::commands::CommandSender;
|
|
||||||
use pumpkin_core::text::{color::NamedColor, TextComponent};
|
use pumpkin_core::text::{color::NamedColor, TextComponent};
|
||||||
|
|
||||||
const NAMES: [&str; 1] = ["kill"];
|
const NAMES: [&str; 1] = ["kill"];
|
||||||
@@ -10,13 +8,10 @@ const DESCRIPTION: &str = "Kills a target player.";
|
|||||||
|
|
||||||
const ARG_TARGET: &str = "target";
|
const ARG_TARGET: &str = "target";
|
||||||
|
|
||||||
pub fn consume_arg_target(_src: &CommandSender, args: &mut RawArgs) -> Option<String> {
|
|
||||||
consume_arg_player(_src, args)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_command_tree<'a>() -> CommandTree<'a> {
|
pub fn init_command_tree<'a>() -> CommandTree<'a> {
|
||||||
CommandTree::new(NAMES, DESCRIPTION).with_child(
|
CommandTree::new(NAMES, DESCRIPTION).with_child(
|
||||||
argument(ARG_TARGET, consume_arg_target).execute(&|sender, server, args| {
|
argument(ARG_TARGET, consume_arg_player).execute(&|sender, server, args| {
|
||||||
|
// TODO parse entities not only players
|
||||||
let target = parse_arg_player(sender, server, ARG_TARGET, args)?;
|
let target = parse_arg_player(sender, server, ARG_TARGET, args)?;
|
||||||
target.living_entity.kill();
|
target.living_entity.kill();
|
||||||
|
|
||||||
|
|||||||
@@ -58,14 +58,17 @@ impl<'a> CommandDispatcher<'a> {
|
|||||||
println!("Error while parsing command \"{cmd}\": a requirement that was expected was not met.");
|
println!("Error while parsing command \"{cmd}\": a requirement that was expected was not met.");
|
||||||
return Err("Internal Error (See logs for details)".into());
|
return Err("Internal Error (See logs for details)".into());
|
||||||
}
|
}
|
||||||
Ok(is_fitting_path) => {
|
Ok(is_fitting_path) => match is_fitting_path {
|
||||||
if is_fitting_path {
|
Ok(_) => return Ok(()),
|
||||||
return Ok(());
|
Err(error) => {
|
||||||
|
// Custom error message or not ?
|
||||||
|
if let Some(error) = error {
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(format!("Invalid Syntax. Usage: {}", tree))
|
Err(format!("Invalid Syntax. Usage: {}", tree))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +93,7 @@ impl<'a> CommandDispatcher<'a> {
|
|||||||
path: Vec<usize>,
|
path: Vec<usize>,
|
||||||
tree: &CommandTree,
|
tree: &CommandTree,
|
||||||
mut raw_args: RawArgs,
|
mut raw_args: RawArgs,
|
||||||
) -> Result<bool, InvalidTreeError> {
|
) -> Result<Result<(), Option<String>>, InvalidTreeError> {
|
||||||
let mut parsed_args: ConsumedArgs = HashMap::new();
|
let mut parsed_args: ConsumedArgs = HashMap::new();
|
||||||
|
|
||||||
for node in path.iter().map(|&i| &tree.nodes[i]) {
|
for node in path.iter().map(|&i| &tree.nodes[i]) {
|
||||||
@@ -98,36 +101,37 @@ impl<'a> CommandDispatcher<'a> {
|
|||||||
NodeType::ExecuteLeaf { run } => {
|
NodeType::ExecuteLeaf { run } => {
|
||||||
return if raw_args.is_empty() {
|
return if raw_args.is_empty() {
|
||||||
run(src, server, &parsed_args)?;
|
run(src, server, &parsed_args)?;
|
||||||
Ok(true)
|
Ok(Ok(()))
|
||||||
} else {
|
} else {
|
||||||
Ok(false)
|
Ok(Err(None))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
NodeType::Literal { string, .. } => {
|
NodeType::Literal { string, .. } => {
|
||||||
if raw_args.pop() != Some(string) {
|
if raw_args.pop() != Some(string) {
|
||||||
return Ok(false);
|
return Ok(Err(None));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NodeType::Argument {
|
NodeType::Argument {
|
||||||
consumer: consume,
|
consumer: consume,
|
||||||
name,
|
name,
|
||||||
..
|
..
|
||||||
} => {
|
} => match consume(src, server, &mut raw_args) {
|
||||||
if let Some(consumed) = consume(src, &mut raw_args) {
|
Ok(consumed) => {
|
||||||
parsed_args.insert(name, consumed);
|
parsed_args.insert(name, consumed);
|
||||||
} else {
|
|
||||||
return Ok(false);
|
|
||||||
}
|
}
|
||||||
}
|
Err(err) => {
|
||||||
|
return Ok(Err(err));
|
||||||
|
}
|
||||||
|
},
|
||||||
NodeType::Require { predicate, .. } => {
|
NodeType::Require { predicate, .. } => {
|
||||||
if !predicate(src) {
|
if !predicate(src) {
|
||||||
return Ok(false);
|
return Ok(Err(None));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(false)
|
Ok(Err(None))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register a command with the dispatcher.
|
/// Register a command with the dispatcher.
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use dispatcher::InvalidTreeError;
|
use dispatcher::InvalidTreeError;
|
||||||
use pumpkin_core::text::TextComponent;
|
use pumpkin_core::text::TextComponent;
|
||||||
use tree::ConsumedArgs;
|
use tree::ConsumedArgs;
|
||||||
@@ -21,7 +23,7 @@ mod tree_format;
|
|||||||
pub enum CommandSender<'a> {
|
pub enum CommandSender<'a> {
|
||||||
Rcon(&'a mut Vec<String>),
|
Rcon(&'a mut Vec<String>),
|
||||||
Console,
|
Console,
|
||||||
Player(&'a Player),
|
Player(Arc<Player>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CommandSender<'a> {
|
impl<'a> CommandSender<'a> {
|
||||||
@@ -49,9 +51,9 @@ impl<'a> CommandSender<'a> {
|
|||||||
CommandSender::Rcon(_) => true,
|
CommandSender::Rcon(_) => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn as_mut_player(&mut self) -> Option<&Player> {
|
pub fn as_mut_player(&mut self) -> Option<Arc<Player>> {
|
||||||
match self {
|
match self {
|
||||||
CommandSender::Player(player) => Some(player),
|
CommandSender::Player(player) => Some(player.clone()),
|
||||||
CommandSender::Console => None,
|
CommandSender::Console => None,
|
||||||
CommandSender::Rcon(_) => None,
|
CommandSender::Rcon(_) => None,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use super::RunFunctionType;
|
use super::RunFunctionType;
|
||||||
use crate::commands::CommandSender;
|
use crate::{commands::CommandSender, server::Server};
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
|
||||||
/// see [crate::commands::tree_builder::argument]
|
/// see [crate::commands::tree_builder::argument]
|
||||||
@@ -9,7 +9,9 @@ pub type RawArgs<'a> = Vec<&'a str>;
|
|||||||
pub type ConsumedArgs<'a> = HashMap<&'a str, String>;
|
pub type ConsumedArgs<'a> = HashMap<&'a str, String>;
|
||||||
|
|
||||||
/// see [crate::commands::tree_builder::argument]
|
/// see [crate::commands::tree_builder::argument]
|
||||||
pub type ArgumentConsumer<'a> = fn(&CommandSender, &mut RawArgs) -> Option<String>;
|
/// Provide value or an Optional error message, If no Error message provided the default will be used
|
||||||
|
pub type ArgumentConsumer<'a> =
|
||||||
|
fn(&CommandSender, &Server, &mut RawArgs) -> Result<String, Option<String>>;
|
||||||
|
|
||||||
pub struct Node<'a> {
|
pub struct Node<'a> {
|
||||||
pub(crate) children: Vec<usize>,
|
pub(crate) children: Vec<usize>,
|
||||||
|
|||||||
@@ -274,7 +274,7 @@ impl Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
pub async fn process_packets(&self, server: &Arc<Server>) {
|
pub async fn process_packets(self: &Arc<Self>, server: &Arc<Server>) {
|
||||||
let mut packets = self.client.client_packets_queue.lock();
|
let mut packets = self.client.client_packets_queue.lock();
|
||||||
while let Some(mut packet) = packets.pop_back() {
|
while let Some(mut packet) = packets.pop_back() {
|
||||||
match self.handle_play_packet(server, &mut packet).await {
|
match self.handle_play_packet(server, &mut packet).await {
|
||||||
@@ -297,7 +297,7 @@ impl Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_play_packet(
|
pub async fn handle_play_packet(
|
||||||
&self,
|
self: &Arc<Self>,
|
||||||
server: &Arc<Server>,
|
server: &Arc<Server>,
|
||||||
packet: &mut RawPacket,
|
packet: &mut RawPacket,
|
||||||
) -> Result<(), Box<dyn PumpkinError>> {
|
) -> Result<(), Box<dyn PumpkinError>> {
|
||||||
|
|||||||
Reference in new issue
Block a user