-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_pearl.rs
197 lines (176 loc) · 6.37 KB
/
auto_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::sync::Arc;
use azalea::{
app::{App, Plugin, Update},
chunks::handle_receive_chunk_events,
core::direction::Direction,
ecs::prelude::*,
interact::handle_swing_arm_event,
inventory::InventorySet,
mining::MiningSet,
packet_handling::game::{handle_send_packet_event, SendPacketEvent},
pathfinder::{
astar::PathfinderTimeout,
goals::RadiusGoal,
goto_listener,
moves::default_move,
GotoEvent,
Pathfinder,
},
physics::PhysicsSet,
prelude::*,
protocol::packets::game::{
s_interact::InteractionHand,
s_use_item_on::BlockHit,
ServerboundGamePacket,
ServerboundUseItemOn,
},
BlockPos,
TabList,
Vec3,
};
use uuid::Uuid;
use crate::prelude::*;
/// Automatically goto and pull player pearls.
pub struct AutoPearlPlugin;
impl Plugin for AutoPearlPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(ResendPearlEvents::default())
.add_event::<PearlGotoEvent>()
.add_event::<PearlPullEvent>()
.add_systems(
Update,
(
Self::handle_resend_pearl_events,
Self::handle_goto_pearl_events
.before(goto_listener)
.after(handle_receive_chunk_events),
Self::handle_pull_pearl_events
.before(handle_send_packet_event)
.before(handle_swing_arm_event)
.after(InventorySet)
.after(PhysicsSet)
.after(MiningSet),
)
.chain(),
);
}
}
#[derive(Clone, Debug)]
pub struct PearlEvent {
pub entity: Entity,
pub idle_goal: IdleGoal,
pub block_pos: BlockPos,
pub owner_uuid: Uuid,
}
#[derive(Clone, Debug, Deref, DerefMut, Event)]
pub struct PearlGotoEvent(pub PearlEvent);
#[derive(Clone, Debug, Deref, DerefMut, Event)]
pub struct PearlPullEvent(pub PearlEvent);
#[derive(Default, Resource)]
pub struct ResendPearlEvents {
goto: Vec<PearlGotoEvent>,
pull: Vec<PearlPullEvent>,
}
impl AutoPearlPlugin {
pub fn handle_resend_pearl_events(
mut pearl_goto_events: EventWriter<PearlGotoEvent>,
mut pearl_pull_events: EventWriter<PearlPullEvent>,
mut pearl_pending_events: ResMut<ResendPearlEvents>,
mut query: Query<&Pathfinder>,
) {
for pathfinder in &mut query {
if let Some(_goal) = &pathfinder.goal {
continue;
}
if let Some(event) = pearl_pending_events.goto.pop() {
debug!("Resending {event:#?}");
pearl_goto_events.send(event);
}
if let Some(event) = pearl_pending_events.pull.pop() {
debug!("Resending {event:#?}");
pearl_pull_events.send(event);
}
}
}
pub fn handle_goto_pearl_events(
mut goto_events: EventWriter<GotoEvent>,
mut pearl_goto_events: EventReader<PearlGotoEvent>,
mut pearl_pull_events: EventWriter<PearlPullEvent>,
mut pearl_pending_events: ResMut<ResendPearlEvents>,
mut query: Query<&Pathfinder>,
) {
for event in pearl_goto_events.read().cloned() {
let Ok(pathfinder) = query.get_mut(event.entity) else {
continue;
};
if let Some(_goal) = &pathfinder.goal {
pearl_pending_events.goto.push(event.clone());
continue;
}
let pos = event.block_pos.to_vec3_floored();
let goal = RadiusGoal { radius: 3.0, pos };
goto_events.send(GotoEvent {
entity: event.entity,
goal: Arc::new(goal),
successors_fn: default_move,
allow_mining: false,
min_timeout: PathfinderTimeout::Time(Duration::from_secs(10)),
max_timeout: PathfinderTimeout::Time(Duration::from_secs(10)),
});
pearl_pull_events.send(PearlPullEvent(event.0));
}
}
pub fn handle_pull_pearl_events(
mut goto_events: EventWriter<GotoEvent>,
mut pearl_pending_events: ResMut<ResendPearlEvents>,
mut pearl_pull_events: EventReader<PearlPullEvent>,
mut send_packet_events: EventWriter<SendPacketEvent>,
mut query: Query<(&Pathfinder, &TabList)>,
) {
for event in pearl_pull_events.read().cloned() {
let Ok((pathfinder, tab_list)) = query.get_mut(event.entity) else {
continue;
};
if let Some(_goal) = &pathfinder.goal {
pearl_pending_events.pull.push(event);
continue;
}
if !tab_list.contains_key(&event.owner_uuid) {
pearl_pending_events.goto.push(PearlGotoEvent(event.0));
continue;
}
let packet = ServerboundGamePacket::UseItemOn(ServerboundUseItemOn {
hand: InteractionHand::MainHand,
block_hit: BlockHit {
block_pos: event.block_pos,
direction: Direction::Down,
location: Vec3 {
x: f64::from(event.block_pos.x) + 0.5,
y: f64::from(event.block_pos.y) + 0.5,
z: f64::from(event.block_pos.z) + 0.5,
},
inside: false,
world_border: false,
},
sequence: 0,
});
send_packet_events.send(SendPacketEvent {
sent_by: event.entity,
packet,
});
if event.idle_goal != IdleGoal::default() {
goto_events.send(GotoEvent {
entity: event.entity,
goal: Arc::new(RadiusGoal {
pos: event.idle_goal.coords,
radius: event.idle_goal.radius + 1.0,
}),
successors_fn: default_move,
allow_mining: false,
min_timeout: PathfinderTimeout::Time(Duration::from_secs(10)),
max_timeout: PathfinderTimeout::Time(Duration::from_secs(10)),
});
}
}
}
}