-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleave.rs
83 lines (71 loc) · 2.51 KB
/
leave.rs
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
use azalea::{
app::{App, Plugin, Update},
disconnect::DisconnectEvent,
ecs::prelude::*,
GameProfileComponent,
};
use crate::prelude::*;
pub const LEAVE_PREFIX: &str = "Leave Command: ";
/// Disconnect a bot from the Minecraft server.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct LeaveCommandPlugin;
impl ChatCmd for LeaveCommandPlugin {
fn aliases(&self) -> Vec<&'static str> {
vec!["leave", "disconnect", "dc"]
}
}
impl Plugin for LeaveCommandPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
Self::handle_leave_command_events
.ambiguous_with_all()
.before(MinecraftChatPlugin::handle_send_whisper_events)
.after(MinecraftChatPlugin::handle_chat_received_events),
);
}
}
impl LeaveCommandPlugin {
pub fn handle_leave_command_events(
mut command_events: EventReader<CommandEvent>,
mut whisper_events: EventWriter<WhisperEvent>,
mut disconnect_events: EventWriter<DisconnectEvent>,
query: Query<&GameProfileComponent>,
) {
for event in command_events.read().cloned() {
let ChatCmds::Leave(_plugin) = event.command else {
return;
};
let Ok(profile) = query.get(event.entity) else {
continue;
};
let mut whisper_event = WhisperEvent {
content: String::new(),
entity: event.entity,
sender: event.sender,
source: event.source.clone(),
status: 200,
};
let Some(bot_name) = event.args.iter().next().cloned() else {
whisper_event.content = str!("Missing bot name");
whisper_event.status = 404;
whisper_events.send(whisper_event);
continue;
};
let bot_name = bot_name.to_lowercase();
if profile.name.to_lowercase() != bot_name {
if event.message {
whisper_event.content = str!("Invalid bot name");
whisper_event.status = 406;
whisper_events.send(whisper_event);
}
continue; /* Not this account */
}
disconnect_events.send(DisconnectEvent {
entity: event.entity,
reason: Some(format!("{LEAVE_PREFIX}{:?}", event.sender).into()),
});
}
command_events.clear();
}
}