aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/testbot/commands/debug.rs
blob: ae0808cb00a48240f26e6ffbe63932e51d0899db (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
//! Commands for debugging and getting the current state of the bot.

use azalea::{
    brigadier::prelude::*,
    entity::{LookDirection, Position},
    interact::HitResultComponent,
    world::MinecraftEntityId,
};
use parking_lot::Mutex;

use super::{CommandSource, Ctx};

pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
    commands.register(literal("ping").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        source.reply("pong!");
        1
    }));

    commands.register(literal("whereami").executes(|ctx: &Ctx| {
        let mut source = ctx.source.lock();
        let Some(entity) = source.entity() else {
            source.reply("You aren't in render distance!");
            return 0;
        };
        let position = source.bot.entity_component::<Position>(entity);
        source.reply(&format!(
            "You are at {}, {}, {}",
            position.x, position.y, position.z
        ));
        1
    }));

    commands.register(literal("entityid").executes(|ctx: &Ctx| {
        let mut source = ctx.source.lock();
        let Some(entity) = source.entity() else {
            source.reply("You aren't in render distance!");
            return 0;
        };
        let entity_id = source.bot.entity_component::<MinecraftEntityId>(entity);
        source.reply(&format!(
            "Your Minecraft ID is {} and your ECS id is {entity:?}",
            *entity_id
        ));
        1
    }));

    let whereareyou = |ctx: &Ctx| {
        let source = ctx.source.lock();
        let position = source.bot.position();
        source.reply(&format!(
            "I'm at {}, {}, {}",
            position.x, position.y, position.z
        ));
        1
    };
    commands.register(literal("whereareyou").executes(whereareyou));
    commands.register(literal("pos").executes(whereareyou));

    commands.register(literal("whoareyou").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        source.reply(&format!(
            "I am {} ({})",
            source.bot.username(),
            source.bot.uuid()
        ));
        1
    }));

    commands.register(literal("getdirection").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();
        let direction = source.bot.component::<LookDirection>();
        source.reply(&format!(
            "I'm looking at {}, {}",
            direction.y_rot, direction.x_rot
        ));
        1
    }));

    commands.register(literal("health").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();

        let health = source.bot.health();
        source.reply(&format!("I have {health} health"));
        1
    }));

    commands.register(literal("lookingat").executes(|ctx: &Ctx| {
        let source = ctx.source.lock();

        let hit_result = *source.bot.component::<HitResultComponent>();

        if hit_result.miss {
            source.reply("I'm not looking at anything");
            return 1;
        }

        let block_pos = hit_result.block_pos;
        let block = source.bot.world().read().get_block_state(&block_pos);

        source.reply(&format!("I'm looking at {block:?} at {block_pos:?}"));

        1
    }));
}