-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pearl.rs
141 lines (123 loc) · 4.48 KB
/
pearl.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
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
use azalea::{
app::{App, Plugin, Update},
ecs::prelude::*,
entity::Position,
BlockPos,
TabList,
};
use handlers::prelude::*;
use crate::{
commands::{prelude::*, Commands},
plugins::prelude::*,
trapdoors::Trapdoors,
Settings,
};
/// Pearl Stasis Command
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PearlCommandPlugin;
impl Command for PearlCommandPlugin {
fn aliases(&self) -> Vec<&'static str> {
vec!["pearl", "tp", "teleport", "warp", "home"]
}
}
impl Plugin for PearlCommandPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
handle_pearl_command_event
.ambiguous_with_all()
.before(handle_pearl_goto_event)
.before(handle_discord_whisper_event)
.before(handle_minecraft_whisper_event)
.after(handle_chat_received_event),
);
}
}
pub fn handle_pearl_command_event(
mut command_events: EventReader<CommandEvent>,
mut pearl_events: EventWriter<PearlGotoEvent>,
mut whisper_events: EventWriter<WhisperEvent>,
query: Query<(&TabList, &Position)>,
settings: Res<Settings>,
trapdoors: Res<Trapdoors>,
) {
for mut event in command_events.read().cloned() {
let Commands::Pearl(_plugin) = event.command else {
continue;
};
let Ok((tab_list, position)) = query.get(event.entity) else {
continue;
};
let mut whisper_event = WhisperEvent {
entity: event.entity,
source: event.source,
sender: event.sender,
content: String::new(),
};
let uuid = match event.sender {
CommandSender::Minecraft(username) => username,
CommandSender::Discord(user_id) => {
let Some(username) = event.args.pop_front() else {
whisper_event.content = String::from("[404] Missing username");
whisper_events.send(whisper_event);
continue;
};
let Some((uuid, _info)) = tab_list
.iter()
.find(|(_, info)| info.profile.name == username)
else {
whisper_event.content = format!("[404] {username} is not online");
whisper_events.send(whisper_event);
continue;
};
if !settings.whitelisted.is_empty() {
let Some(whitelist) = settings.whitelisted.get(uuid) else {
continue;
};
let Some(discord_id) = whitelist else {
whisper_event.content = String::from("[404] Link not found");
whisper_events.send(whisper_event);
continue;
};
if discord_id != &user_id.to_string() {
whisper_event.content = String::from("[403] Not your account");
whisper_events.send(whisper_event);
continue;
}
}
*uuid
}
};
let Some(trapdoor) = trapdoors
.0
.clone()
.into_values()
.filter(|trapdoor| trapdoor.owner_uuid == uuid)
.min_by_key(|trapdoor| {
let shared_count = trapdoors
.0
.values()
.filter(|td| td.block_pos == trapdoor.block_pos)
.filter(|td| td.owner_uuid != trapdoor.owner_uuid)
.count();
let client_pos = BlockPos::from(position);
let distance = (client_pos.x - trapdoor.block_pos.x).abs()
+ (client_pos.y - trapdoor.block_pos.y).abs()
+ (client_pos.z - trapdoor.block_pos.z).abs();
// First compare by shared count, then by distance
(shared_count, distance)
})
else {
whisper_event.content = String::from("[404] Pearl not found");
whisper_events.send(whisper_event);
continue;
};
whisper_event.content = String::from("[202] I'm on my way");
whisper_events.send(whisper_event);
pearl_events.send(PearlGotoEvent {
entity: event.entity,
block_pos: trapdoor.block_pos,
owner_uuid: trapdoor.owner_uuid,
});
}
}