aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/tests/simulation/enchantments.rs
blob: f9834230c73c1dbf15a79373301b2376f78422f5 (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
use azalea_client::test_utils::prelude::*;
use azalea_entity::Attributes;
use azalea_inventory::{ItemStack, components::Enchantments};
use azalea_protocol::packets::{
    ConnectionProtocol,
    config::{ClientboundFinishConfiguration, ClientboundRegistryData},
    game::ClientboundContainerSetSlot,
};
use azalea_registry::{Registry, builtin::ItemKind, data::Enchantment, identifier::Identifier};
use simdnbt::owned::{NbtCompound, NbtTag};

#[test]
fn test_enchantments() {
    let _lock = init();

    let mut s = Simulation::new(ConnectionProtocol::Configuration);
    s.receive_packet(ClientboundRegistryData {
        registry_id: Identifier::new("minecraft:dimension_type"),
        entries: vec![(
            Identifier::new("minecraft:overworld"),
            Some(NbtCompound::from_values(vec![
                ("height".into(), NbtTag::Int(384)),
                ("min_y".into(), NbtTag::Int(-64)),
            ])),
        )]
        .into_iter()
        .collect(),
    });
    // actual registry data copied from vanilla
    s.receive_packet(ClientboundRegistryData {
        registry_id: Identifier::new("minecraft:enchantment"),
        entries: vec![(
            Identifier::new("minecraft:efficiency"),
            Some(NbtCompound::from([
                (
                    "description",
                    [("translate", "enchantment.minecraft.efficiency".into())].into(),
                ),
                ("anvil_cost", 1.into()),
                (
                    "max_cost",
                    [("base", 51.into()), ("per_level_above_first", 10.into())].into(),
                ),
                (
                    "min_cost",
                    [("base", 1.into()), ("per_level_above_first", 10.into())].into(),
                ),
                (
                    "effects",
                    [(
                        "minecraft:attributes",
                        [
                            ("operation", "add_value".into()),
                            ("attribute", "minecraft:mining_efficiency".into()),
                            (
                                "amount",
                                [
                                    ("type", "minecraft:levels_squared".into()),
                                    ("added", 1.0f32.into()),
                                ]
                                .into(),
                            ),
                            ("id", "minecraft:enchantment.efficiency".into()),
                        ]
                        .into(),
                    )]
                    .into(),
                ),
                ("max_level", 5.into()),
                ("weight", 10.into()),
                ("slots", ["mainhand"].into()),
                ("supported_items", "#minecraft:enchantable/mining".into()),
            ])),
        )]
        .into_iter()
        .collect(),
    });
    s.tick();
    s.receive_packet(ClientboundFinishConfiguration);
    s.tick();
    s.receive_packet(default_login_packet());
    s.tick();

    fn efficiency(simulation: &mut Simulation) -> f64 {
        simulation.query_self::<&Attributes, _>(|c| c.mining_efficiency.calculate())
    }

    assert_eq!(efficiency(&mut s), 0.);

    s.receive_packet(ClientboundContainerSetSlot {
        container_id: 0,
        state_id: 1,
        slot: *azalea_inventory::Player::HOTBAR_SLOTS.start() as u16,
        item_stack: ItemKind::DiamondPickaxe.into(),
    });
    s.tick();

    // still 0 efficiency
    assert_eq!(efficiency(&mut s), 0.);

    s.receive_packet(ClientboundContainerSetSlot {
        container_id: 0,
        state_id: 2,
        slot: *azalea_inventory::Player::HOTBAR_SLOTS.start() as u16,
        item_stack: ItemStack::from(ItemKind::DiamondPickaxe).with_component(Enchantments {
            levels: [(Enchantment::from_u32(0).unwrap(), 1)].into(),
        }),
    });
    s.tick();

    // level 1 gives us value 2
    assert_eq!(efficiency(&mut s), 2.);

    s.receive_packet(ClientboundContainerSetSlot {
        container_id: 0,
        state_id: 1,
        slot: *azalea_inventory::Player::HOTBAR_SLOTS.start() as u16,
        item_stack: ItemKind::DiamondPickaxe.into(),
    });
    s.tick();

    // enchantment is cleared, so back to 0
    assert_eq!(efficiency(&mut s), 0.);
}