forked from Quozul/PicoLimbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.rs
More file actions
164 lines (156 loc) · 5.85 KB
/
commands.rs
File metadata and controls
164 lines (156 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate::handlers::play::set_player_position_and_rotation::teleport_player_to_spawn;
use crate::server::batch::Batch;
use crate::server::client_state::ClientState;
use crate::server::packet_handler::{PacketHandler, PacketHandlerError};
use crate::server::packet_registry::PacketRegistry;
use crate::server_state::{ServerCommand, ServerCommands, ServerState};
use minecraft_packets::play::chat_command_packet::ChatCommandPacket;
use minecraft_packets::play::chat_message_packet::ChatMessagePacket;
use minecraft_packets::play::client_bound_player_abilities_packet::ClientBoundPlayerAbilitiesPacket;
use minecraft_packets::play::transfer_packet::TransferPacket;
use minecraft_protocol::prelude::{ProtocolVersion, VarInt};
use thiserror::Error;
use tracing::{info, warn};
impl PacketHandler for ChatCommandPacket {
fn handle(
&self,
client_state: &mut ClientState,
server_state: &ServerState,
) -> Result<Batch<PacketRegistry>, PacketHandlerError> {
let mut batch = Batch::new();
run_command(client_state, server_state, self.get_command(), &mut batch);
Ok(batch)
}
}
impl PacketHandler for ChatMessagePacket {
fn handle(
&self,
client_state: &mut ClientState,
server_state: &ServerState,
) -> Result<Batch<PacketRegistry>, PacketHandlerError> {
let mut batch = Batch::new();
if let Some(command) = self.get_command() {
run_command(client_state, server_state, command, &mut batch);
} else {
info!("<{}> {}", client_state.get_username(), self.get_message());
}
Ok(batch)
}
}
fn run_command(
client_state: &mut ClientState,
server_state: &ServerState,
command: &str,
batch: &mut Batch<PacketRegistry>,
) {
info!(
"{} issued server command: /{}",
client_state.get_username(),
command
);
if let Ok(parsed_command) = Command::parse(server_state.server_commands(), command) {
match parsed_command {
Command::Spawn => {
teleport_player_to_spawn(client_state, server_state, batch);
}
Command::Fly => {
let allow_flying = !client_state.is_flight_allowed();
let flying = allow_flying && client_state.is_flying();
let packet = ClientBoundPlayerAbilitiesPacket::builder()
.allow_flying(allow_flying)
.flying(flying)
.flying_speed(client_state.get_flying_speed())
.build();
batch.queue(|| PacketRegistry::ClientBoundPlayerAbilities(packet));
client_state.set_is_flight_allowed(allow_flying);
client_state.set_is_flying(allow_flying);
}
Command::FlySpeed(speed) => {
let packet = ClientBoundPlayerAbilitiesPacket::builder()
.allow_flying(client_state.is_flight_allowed())
.flying(client_state.is_flying())
.flying_speed(speed)
.build();
batch.queue(|| PacketRegistry::ClientBoundPlayerAbilities(packet));
client_state.set_flying_speed(speed);
}
Command::Transfer(host, port) => {
if client_state
.protocol_version()
.is_after_inclusive(ProtocolVersion::V1_20_5)
{
info!(
"Transferring {} to {}:{}",
client_state.get_username(),
host,
port
);
let packet = TransferPacket {
host,
port: VarInt::from(port),
};
batch.queue(|| PacketRegistry::Transfer(packet));
} else {
warn!(
"{} tried to transfer servers on unsupported version {}",
client_state.get_username(),
client_state.protocol_version().humanize()
)
}
}
}
}
}
#[derive(Debug, Error)]
pub enum ParseCommandError {
#[error("empty command")]
Empty,
#[error("unknown command")]
Unknown,
#[error("invalid speed value")]
InvalidSpeed(#[from] std::num::ParseFloatError),
#[error("invalid hostname")]
InvalidHost,
#[error("invalid port")]
InvalidPort(#[from] std::num::ParseIntError),
}
enum Command {
Spawn,
Fly,
FlySpeed(f32),
Transfer(String, i32),
}
impl Command {
pub fn parse(server_commands: &ServerCommands, input: &str) -> Result<Self, ParseCommandError> {
let mut parts = input.split_whitespace();
let cmd = parts.next().ok_or(ParseCommandError::Empty)?;
if Self::is_command(server_commands.spawn(), cmd) {
Ok(Self::Spawn)
} else if Self::is_command(server_commands.fly(), cmd) {
Ok(Self::Fly)
} else if Self::is_command(server_commands.fly_speed(), cmd) {
let speed_str = parts.next().unwrap_or("0.05");
let speed = speed_str.parse::<f32>()?.clamp(0.0, 1.0);
Ok(Self::FlySpeed(speed))
} else if Self::is_command(server_commands.transfer(), cmd) {
let host = parts
.next()
.ok_or(ParseCommandError::InvalidHost)?
.to_string();
let port_str = parts.next().unwrap_or("25565");
let port = port_str.parse::<i32>()?;
Ok(Self::Transfer(host, port))
} else {
Err(ParseCommandError::Unknown)
}
}
fn is_command(server_command: ServerCommand, command: &str) -> bool {
if let ServerCommand::Enabled { alias } = server_command
&& command == alias
{
true
} else {
false
}
}
}