-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_pearl.rs
211 lines (188 loc) · 6.04 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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::{
goals::{RadiusGoal, ReachBlockPosGoal},
goto_listener,
moves::default_move,
GotoEvent,
Pathfinder,
},
physics::PhysicsSet,
prelude::*,
protocol::packets::game::{
serverbound_interact_packet::InteractionHand,
serverbound_use_item_on_packet::{BlockHit, ServerboundUseItemOnPacket},
ServerboundGamePacket,
},
BlockPos,
InstanceHolder,
TabList,
Vec3,
};
use uuid::Uuid;
use crate::{settings::IdleGoal, Settings};
/// Automatically pull stasis chamber pearls
pub struct AutoPearlPlugin;
impl Plugin for AutoPearlPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(PendingPearlEvents::default())
.add_event::<PearlGotoEvent>()
.add_event::<PearlPullEvent>()
.add_systems(
Update,
(
handle_pearl_goto_event
.before(goto_listener)
.after(handle_receive_chunk_events),
handle_pearl_pull_event
.before(handle_send_packet_event)
.before(handle_swing_arm_event)
.after(InventorySet)
.after(PhysicsSet)
.after(MiningSet),
process_pending_pearl_events,
)
.chain(),
);
}
}
#[derive(Clone, Event)]
pub struct PearlGotoEvent {
pub entity: Entity,
pub block_pos: BlockPos,
pub owner_uuid: Uuid,
}
pub fn handle_pearl_goto_event(
mut goto_events: EventWriter<GotoEvent>,
mut pearl_goto_events: EventReader<PearlGotoEvent>,
mut pearl_pull_events: EventWriter<PearlPullEvent>,
mut pearl_pending_events: ResMut<PendingPearlEvents>,
mut query: Query<(&Pathfinder, &InstanceHolder)>,
) {
for event in pearl_goto_events.read() {
let Ok((pathfinder, holder)) = query.get_mut(event.entity) else {
continue;
};
if let Some(_goal) = &pathfinder.goal {
pearl_pending_events.goto.push(event.clone());
continue;
}
let goal = ReachBlockPosGoal {
chunk_storage: holder.instance.read().chunks.clone(),
pos: event.block_pos,
};
goto_events.send(GotoEvent {
entity: event.entity,
goal: Arc::new(goal),
successors_fn: default_move,
allow_mining: false,
});
pearl_pull_events.send(event.into());
}
}
#[derive(Clone, Event)]
pub struct PearlPullEvent {
pub entity: Entity,
pub block_pos: BlockPos,
pub owner_uuid: Uuid,
}
pub fn handle_pearl_pull_event(
mut goto_events: EventWriter<GotoEvent>,
mut pearl_pending_events: ResMut<PendingPearlEvents>,
mut pearl_pull_events: EventReader<PearlPullEvent>,
mut send_packet_events: EventWriter<SendPacketEvent>,
mut query: Query<(&Pathfinder, &TabList)>,
settings: Res<Settings>,
) {
for event in pearl_pull_events.read() {
let Ok((pathfinder, tab_list)) = query.get_mut(event.entity) else {
continue;
};
if let Some(_goal) = &pathfinder.goal {
pearl_pending_events.pull.push(event.clone());
continue;
}
if !tab_list.contains_key(&event.owner_uuid) {
pearl_pending_events.goto.push(event.into());
continue;
}
let packet = ServerboundGamePacket::UseItemOn(ServerboundUseItemOnPacket {
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,
},
sequence: 0,
});
send_packet_events.send(SendPacketEvent {
entity: event.entity,
packet,
});
if settings.idle != IdleGoal::default() {
goto_events.send(GotoEvent {
entity: event.entity,
allow_mining: false,
successors_fn: default_move,
goal: Arc::new(RadiusGoal {
pos: settings.idle.pos,
radius: settings.idle.radius + 1.0,
}),
});
}
}
}
#[derive(Default, Resource)]
pub struct PendingPearlEvents {
goto: Vec<PearlGotoEvent>,
pull: Vec<PearlPullEvent>,
}
pub fn process_pending_pearl_events(
mut pearl_goto_events: EventWriter<PearlGotoEvent>,
mut pearl_pull_events: EventWriter<PearlPullEvent>,
mut pearl_pending_events: ResMut<PendingPearlEvents>,
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() {
pearl_goto_events.send(event);
}
if let Some(event) = pearl_pending_events.pull.pop() {
pearl_pull_events.send(event);
}
}
}
impl From<&PearlGotoEvent> for PearlPullEvent {
fn from(event: &PearlGotoEvent) -> Self {
Self {
entity: event.entity,
block_pos: event.block_pos,
owner_uuid: event.owner_uuid,
}
}
}
impl From<&PearlPullEvent> for PearlGotoEvent {
fn from(event: &PearlPullEvent) -> Self {
Self {
entity: event.entity,
block_pos: event.block_pos,
owner_uuid: event.owner_uuid,
}
}
}