You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.9 KiB
72 lines
1.9 KiB
mod debug;
|
|
mod gamemode;
|
|
|
|
use valence::{
|
|
client::event_loop::PacketEvent, packet::c2s::play::CommandExecutionC2s, prelude::*,
|
|
};
|
|
|
|
use crate::CurrentGameState;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct CommandExecution {
|
|
pub client: Entity,
|
|
pub command: String,
|
|
pub timestamp: u64,
|
|
}
|
|
|
|
pub fn handle_command_execution(
|
|
mut packets: EventReader<PacketEvent>,
|
|
mut events: EventWriter<CommandExecution>,
|
|
) {
|
|
for packet in packets.iter() {
|
|
if let Some(pkt) = packet.decode::<CommandExecutionC2s>() {
|
|
events.send(CommandExecution {
|
|
client: packet.client,
|
|
command: pkt.command.to_string(),
|
|
timestamp: pkt.timestamp,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct Command {
|
|
pub name: String,
|
|
pub args: Option<Vec<String>>,
|
|
}
|
|
|
|
impl Command {
|
|
pub fn parse(command: String) -> Self {
|
|
let command = command
|
|
.split(" ")
|
|
.map(|e| e.to_string())
|
|
.collect::<Vec<String>>();
|
|
|
|
let name = command[0].clone();
|
|
let args = if command.len() > 1 {
|
|
Some(command.as_slice()[1..].to_vec())
|
|
} else {
|
|
None
|
|
};
|
|
|
|
Self { name, args }
|
|
}
|
|
}
|
|
|
|
pub fn command_executor(
|
|
mut clients: Query<(&mut Client, &mut GameMode)>,
|
|
mut game_state: ResMut<CurrentGameState>,
|
|
mut events: EventReader<CommandExecution>,
|
|
) {
|
|
for event in events.iter() {
|
|
let cmd = Command::parse(event.command.to_string());
|
|
let (mut client, mut gamemode) = clients.get_mut(event.client).unwrap();
|
|
|
|
match cmd.name.as_str() {
|
|
"gamemode" => gamemode::run(cmd, &mut client, &mut gamemode),
|
|
"advance_game_state" => debug::advance_game_state(&mut game_state),
|
|
"game_state" => debug::game_state(&mut client, &game_state),
|
|
_ => client.send_message(format!("Command {} not found!", cmd.name)),
|
|
}
|
|
}
|
|
}
|