aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/testbot/commands/movement.rs
blob: df3b74181b593315eefff48b1cc381992c86cee9 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::time::Duration;

use azalea::{
    BlockPos, SprintDirection, WalkDirection,
    brigadier::prelude::*,
    pathfinder::goals::{BlockPosGoal, RadiusGoal, XZGoal},
    prelude::*,
};

use super::Ctx;
use crate::commands::Dispatcher;

pub fn register(commands: &mut Dispatcher) {
    commands.register(
        literal("goto")
            .executes(|ctx: &Ctx| {
                let source = ctx.source.lock();
                println!("got goto");
                // look for the sender
                let Some(entity) = source.entity() else {
                    source.reply("I can't see you!");
                    return Ok(0);
                };
                let position = entity.position()?;
                source.reply("ok");
                source
                    .bot
                    .start_goto(BlockPosGoal(BlockPos::from(position.up(0.5))));
                Ok(1)
            })
            .then(literal("xz").then(argument("x", integer()).then(
                argument("z", integer()).executes(|ctx: &Ctx| {
                    let source = ctx.source.lock();
                    let x = get_integer(ctx, "x").unwrap();
                    let z = get_integer(ctx, "z").unwrap();
                    println!("goto xz {x} {z}");
                    source.reply("ok");
                    source.bot.start_goto(XZGoal { x, z });
                    Ok(1)
                }),
            )))
            .then(literal("radius").then(argument("radius", float()).then(
                argument("x", integer()).then(argument("y", integer()).then(
                    argument("z", integer()).executes(|ctx: &Ctx| {
                        let source = ctx.source.lock();
                        let radius = get_float(ctx, "radius").unwrap();
                        let x = get_integer(ctx, "x").unwrap();
                        let y = get_integer(ctx, "y").unwrap();
                        let z = get_integer(ctx, "z").unwrap();
                        println!("goto radius {radius}, position: {x} {y} {z}");
                        source.reply("ok");
                        source.bot.start_goto(RadiusGoal {
                            pos: BlockPos::new(x, y, z).center(),
                            radius,
                        });
                        Ok(1)
                    }),
                )),
            )))
            .then(argument("x", integer()).then(argument("y", integer()).then(
                argument("z", integer()).executes(|ctx: &Ctx| {
                    let source = ctx.source.lock();
                    let x = get_integer(ctx, "x").unwrap();
                    let y = get_integer(ctx, "y").unwrap();
                    let z = get_integer(ctx, "z").unwrap();
                    println!("goto xyz {x} {y} {z}");
                    source.reply("ok");
                    source.bot.start_goto(BlockPosGoal(BlockPos::new(x, y, z)));
                    Ok(1)
                }),
            ))),
    );

    commands.register(literal("follow").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        println!("got follow");
        // look for the sender
        let Some(entity) = source.entity() else {
            source.reply("I can't see you!");
            return Ok(0);
        };
        source.reply("ok");
        *source.state.following_entity.lock() = Some(entity);
        Ok(1)
    }));

    commands.register(literal("down").executes(|ctx: &Ctx| {
        let source = ctx.source.clone();
        let bot = source.lock().bot.clone();
        let position = BlockPos::from(bot.position()?);
        tokio::spawn(async move {
            source.lock().reply("mining...");
            bot.mine(position.down(1)).await;
            source.lock().reply("done");
        });
        Ok(1)
    }));

    commands.register(
        literal("look")
            .executes(|ctx: &Ctx| {
                // look for the sender
                let source = ctx.source.lock();
                let Some(entity) = source.entity() else {
                    source.reply("I can't see you!");
                    return Ok(0);
                };
                let eye_position = entity.eye_position()?;
                source.bot.look_at(eye_position);
                Ok(1)
            })
            .then(argument("x", integer()).then(argument("y", integer()).then(
                argument("z", integer()).executes(|ctx: &Ctx| {
                    let pos = BlockPos::new(
                        get_integer(ctx, "x").unwrap(),
                        get_integer(ctx, "y").unwrap(),
                        get_integer(ctx, "z").unwrap(),
                    );
                    println!("{pos:?}");
                    let source = ctx.source.lock();
                    source.bot.look_at(pos.center());
                    Ok(1)
                }),
            ))),
    );

    commands.register(
        literal("walk").then(argument("seconds", float()).executes(|ctx: &Ctx| {
            let mut seconds = get_float(ctx, "seconds").unwrap();
            let source = ctx.source.lock();
            let bot = source.bot.clone();

            if seconds < 0. {
                bot.walk(WalkDirection::Backward);
                seconds = -seconds;
            } else {
                bot.walk(WalkDirection::Forward);
            }

            tokio::spawn(async move {
                tokio::time::sleep(Duration::from_secs_f32(seconds)).await;
                bot.walk(WalkDirection::None);
            });
            source.reply(format!("ok, walking for {seconds} seconds"));
            Ok(1)
        })),
    );
    commands.register(
        literal("sprint").then(argument("seconds", float()).executes(|ctx: &Ctx| {
            let seconds = get_float(ctx, "seconds").unwrap();
            let source = ctx.source.lock();
            let bot = source.bot.clone();
            bot.sprint(SprintDirection::Forward);
            tokio::spawn(async move {
                tokio::time::sleep(Duration::from_secs_f32(seconds)).await;
                bot.walk(WalkDirection::None);
            });
            source.reply(format!("ok, sprinting for {seconds} seconds"));
            Ok(1)
        })),
    );

    commands.register(literal("north").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        source.bot.set_direction(180., 0.)?;
        source.reply("ok");
        Ok(1)
    }));
    commands.register(literal("south").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        source.bot.set_direction(0., 0.)?;
        source.reply("ok");
        Ok(1)
    }));
    commands.register(literal("east").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        source.bot.set_direction(-90., 0.)?;
        source.reply("ok");
        Ok(1)
    }));
    commands.register(literal("west").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        source.bot.set_direction(90., 0.)?;
        source.reply("ok");
        Ok(1)
    }));
    commands.register(
        literal("jump")
            .executes(|ctx: &Ctx| {
                let source = ctx.source.lock();
                source.bot.jump();
                source.reply("ok");
                Ok(1)
            })
            .then(argument("enabled", bool()).executes(|ctx: &Ctx| {
                let jumping = get_bool(ctx, "enabled").unwrap();
                let source = ctx.source.lock();
                source.bot.set_jumping(jumping)?;
                Ok(1)
            })),
    );

    let sneak = |ctx: &Ctx| {
        let source = ctx.source.lock();
        source.bot.set_crouching(!source.bot.crouching())?;
        source.reply("ok");
        Ok(1)
    };
    let sneak_enabled = argument("enabled", bool()).executes(|ctx: &Ctx| {
        let sneaking = get_bool(ctx, "enabled").unwrap();
        let source = ctx.source.lock();
        source.bot.set_crouching(sneaking)?;
        Ok(1)
    });
    commands.register(literal("sneak").executes(sneak).then(sneak_enabled.clone()));
    commands.register(literal("crouch").executes(sneak).then(sneak_enabled));

    commands.register(literal("stop").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        source.bot.stop_pathfinding();
        source.reply("ok");
        *source.state.following_entity.lock() = None;
        Ok(1)
    }));
    commands.register(literal("forcestop").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        source.bot.force_stop_pathfinding();
        source.reply("ok");
        *source.state.following_entity.lock() = None;
        Ok(1)
    }));
}