-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_kill.rs
133 lines (115 loc) · 4.18 KB
/
auto_kill.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
use std::{cmp::Ordering, sync::LazyLock};
use azalea::{
app::{App, Plugin},
attack::AttackEvent,
ecs::prelude::*,
entity::{metadata::AbstractMonster, EyeHeight, Position},
inventory::{
operations::{ClickOperation, SwapClick},
ContainerClickEvent,
Inventory,
},
nearest_entity::EntityFinder,
pathfinder::Pathfinder,
physics::PhysicsSet,
prelude::*,
registry::Item,
world::MinecraftEntityId,
LookAtEvent,
};
use crate::prelude::*;
/// Automatically swap and attack nearby monsters
pub struct AutoKillPlugin;
impl Plugin for AutoKillPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
GameTick,
Self::handle_auto_kill
.after(AutoLookPlugin::handle_auto_look)
.before(GameTickPlugin::handle_game_ticks)
.before(PhysicsSet),
);
}
}
impl AutoKillPlugin {
/// # Panics
/// Will panic if ?
pub fn handle_auto_kill(
mut query: Query<(Entity, &LocalSettings, &GameTicks, &Inventory, &Pathfinder)>,
entities: EntityFinder<With<AbstractMonster>>,
targets: Query<(&MinecraftEntityId, &Position, Option<&EyeHeight>)>,
mut container_click_events: EventWriter<ContainerClickEvent>,
mut look_at_events: EventWriter<LookAtEvent>,
mut attack_events: EventWriter<AttackEvent>,
) {
for (entity, local_settings, game_ticks, inventory, pathfinder) in &mut query {
if !local_settings.auto_kill.enabled {
continue;
}
if let Some(_goal) = &pathfinder.goal {
continue;
}
let Some(target) = entities.nearest_to_entity(entity, 3.2) else {
continue;
};
let Ok((target_id, target_pos, target_eye_height)) = targets.get(target) else {
continue;
};
let mut position = **target_pos;
if let Some(eye_height) = target_eye_height {
position.y += f64::from(**eye_height);
}
look_at_events.send(LookAtEvent { entity, position });
if game_ticks.0 % local_settings.auto_kill.delay_ticks != 0 {
continue;
}
let held_kind = inventory.held_item().kind();
if local_settings.auto_kill.auto_weapon && !WEAPON_ITEMS.contains_key(&held_kind) {
let mut weapon_slots = Vec::new();
for slot in inventory.inventory_menu.player_slots_range() {
let Some(item) = inventory.inventory_menu.slot(slot) else {
continue;
};
if let Some(damage) = WEAPON_ITEMS.get(&item.kind()) {
weapon_slots.push((slot, *damage));
}
}
weapon_slots.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));
if let Some((slot, _)) = weapon_slots.first() {
debug!(
"Swapping Weapon from {slot} to {}",
inventory.selected_hotbar_slot
);
container_click_events.send(ContainerClickEvent {
entity,
window_id: inventory.id,
operation: ClickOperation::Swap(SwapClick {
source_slot: u16::try_from(*slot).unwrap(),
target_slot: inventory.selected_hotbar_slot,
}),
});
}
}
attack_events.send(AttackEvent {
entity,
target: *target_id,
});
}
}
}
pub static WEAPON_ITEMS: LazyLock<HashMap<Item, i32>> = LazyLock::new(|| {
HashMap::from([
(Item::DiamondAxe, 9),
(Item::DiamondSword, 7),
(Item::GoldenAxe, 7),
(Item::GoldenSword, 4),
(Item::IronAxe, 9),
(Item::IronSword, 6),
(Item::NetheriteAxe, 10),
(Item::NetheriteSword, 8),
(Item::StoneAxe, 9),
(Item::StoneSword, 5),
(Item::WoodenAxe, 7),
(Item::WoodenSword, 4),
])
});