-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_totem.rs
68 lines (60 loc) · 1.81 KB
/
auto_totem.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
use azalea::{
app::{App, Plugin},
ecs::prelude::*,
inventory::{
handle_container_click_event,
operations::{ClickOperation, SwapClick},
ContainerClickEvent,
Inventory,
Menu,
},
prelude::*,
registry::Item,
};
use crate::prelude::*;
/// Automatically equip totems of undying.
pub struct AutoTotemPlugin;
impl Plugin for AutoTotemPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
GameTick,
Self::handle_auto_totem
.after(AutoEatPlugin::handle_auto_eat)
.after(GameTickPlugin::handle_game_ticks)
.before(handle_container_click_event),
);
}
}
impl AutoTotemPlugin {
pub fn handle_auto_totem(
mut query: Query<(Entity, &Inventory, &GameTicks)>,
mut container_click_events: EventWriter<ContainerClickEvent>,
) {
for (entity, inventory, game_ticks) in &mut query {
if game_ticks.0 % 2 == 0 {
continue;
}
let Menu::Player(player) = &inventory.inventory_menu else {
continue;
};
if player.offhand.kind() == Item::TotemOfUndying {
continue;
}
let Some(index) = inventory.menu().slots()[8..]
.iter()
.position(|slot| slot.kind() == Item::TotemOfUndying)
.and_then(|index| u16::try_from(index + 8).ok())
else {
continue;
};
container_click_events.send(ContainerClickEvent {
entity,
window_id: inventory.id,
operation: ClickOperation::Swap(SwapClick {
source_slot: index,
target_slot: 40,
}),
});
}
}
}