aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/tests/correct_movement.rs
blob: c20fdb85d33f3d4bc3b2da040c486deb345bf16d (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
use azalea_client::{StartWalkEvent, WalkDirection, test_utils::prelude::*};
use azalea_core::{
    position::{BlockPos, ChunkPos, Vec3},
    resource_location::ResourceLocation,
};
use azalea_entity::LookDirection;
use azalea_protocol::{
    common::movements::{MoveFlags, PositionMoveRotation, RelativeMovements},
    packets::{
        ConnectionProtocol,
        config::{ClientboundFinishConfiguration, ClientboundRegistryData},
        game::{
            ClientboundBlockUpdate, ClientboundPlayerPosition, ClientboundSetChunkCacheCenter,
            ServerboundGamePacket, ServerboundMovePlayerPos,
        },
    },
};
use azalea_registry::{Block, DataRegistry, DimensionType};
use simdnbt::owned::{NbtCompound, NbtTag};

#[test]
fn test_correct_movement() {
    init_tracing();

    let mut simulation = Simulation::new(ConnectionProtocol::Configuration);
    let sent_packets = SentPackets::new(&mut simulation);

    simulation.receive_packet(ClientboundRegistryData {
        registry_id: ResourceLocation::new("minecraft:dimension_type"),
        entries: vec![(
            ResourceLocation::new("minecraft:overworld"),
            Some(NbtCompound::from_values(vec![
                ("height".into(), NbtTag::Int(384)),
                ("min_y".into(), NbtTag::Int(-64)),
            ])),
        )]
        .into_iter()
        .collect(),
    });
    simulation.tick();
    simulation.receive_packet(ClientboundFinishConfiguration);
    simulation.tick();

    simulation.receive_packet(make_basic_login_packet(
        DimensionType::new_raw(0), // overworld
        ResourceLocation::new("minecraft:overworld"),
    ));
    simulation.tick();

    sent_packets.expect_tick_end();
    sent_packets.expect_empty();

    // receive a chunk so the player is "loaded" now
    simulation.receive_packet(ClientboundSetChunkCacheCenter { x: 1, z: 23 });
    simulation.receive_packet(make_basic_empty_chunk(
        ChunkPos::new(1, 23),
        (384 + 64) / 16,
    ));
    simulation.receive_packet(ClientboundBlockUpdate {
        pos: BlockPos::new(31, 63, 370),
        block_state: Block::Stone.into(),
    });
    simulation.receive_packet(ClientboundPlayerPosition {
        id: 1,
        change: PositionMoveRotation {
            pos: Vec3::new(31.5, 64., 370.5),
            delta: Vec3::ZERO,
            look_direction: LookDirection::default(),
        },
        relative: RelativeMovements::all_absolute(),
    });
    simulation.tick();
    simulation.tick();

    // walk for a tick
    simulation.send_event(StartWalkEvent {
        entity: simulation.entity,
        direction: WalkDirection::Forward,
    });
    sent_packets.clear();
    simulation.tick();
    sent_packets.expect("PlayerInput", |p| {
        matches!(p, ServerboundGamePacket::PlayerInput(_))
    });
    sent_packets.expect("MovePlayerPos { pos.z: 370.59800000336764, ... }", |p| {
        matches!(
            p,
            ServerboundGamePacket::MovePlayerPos(ServerboundMovePlayerPos {
                pos: Vec3 {
                    x: 31.5,
                    y: 64.0,
                    z: 370.59800000336764
                },
                flags: MoveFlags {
                    on_ground: true,
                    horizontal_collision: false
                }
            })
        )
    });
    sent_packets.expect_tick_end();
    sent_packets.expect_empty();
}