Skip to content

Commit c7f60e4

Browse files
authored
use pomme's own click operation types (#997)
* use pomme's own click operation types and make the inventory fork a test-only dependency of the client * say who still needs the inventory fork and check a shift-click's and a double click's numbers * drop a leftover rest pattern from a double click test
1 parent b262c8a commit c7f60e4

11 files changed

Lines changed: 228 additions & 65 deletions

File tree

‎Cargo.toml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ tracing-subscriber = "0.3"
5858
# (azalea-rs/azalea#332) from git, since that branch was deleted.
5959
# azalea-block is replaced by the pomme-block shim (pomme owns block data),
6060
# azalea-registry by pomme-registry under that name (its `azalea-compat`
61-
# crate), and azalea-inventory by the pomme-inventory fork; the second patch
62-
# table substitutes them under the azalea git workspace's own path
61+
# crate), and azalea-inventory by the pomme-inventory fork, which only
62+
# azalea-entity and the test-only azalea-protocol still need; the second
63+
# patch table substitutes them under the azalea git workspace's own path
6364
# dependencies too.
6465
[patch.crates-io]
6566
azalea-buf = { git = "https://github.com/azalea-rs/azalea", rev = "ffedf17c9b6ff9fbafbf7e0d14c8d2366b2d0a32" }

‎THIRD_PARTY_LICENSES.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ License: MIT.
137137
`pomme-block` is a data-free shim patched in place of azalea-block, keeping its
138138
public types so the rest of azalea compiles while block semantics come from
139139
Pomme's own per-version tables. `pomme-inventory` is a fork of
140-
azalea-inventory, patched in the same way. Other azalea crates remain
140+
azalea-inventory, patched in the same way for the azalea crates that depend on
141+
it; the client itself no longer uses it. Other azalea crates remain
141142
dependencies.
142143

143144
Source: <https://github.com/azalea-rs/azalea>

‎pomme-client/Cargo.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ azalea-core = { version = "0.16", features = ["bevy_ecs", "serde"] }
8484
pomme-item = { path = "../pomme-item" }
8585
pomme-registry = { path = "../pomme-registry" }
8686
pomme-text = { path = "../pomme-text" }
87-
azalea-inventory = "0.16"
8887
uuid = { version = "1", features = ["v4"] }
8988
md-5 = "0.11"
9089
crossbeam-channel = "0.5"
@@ -139,3 +138,5 @@ pomme-protocol = { path = "../pomme-protocol", features = ["vanilla-fixtures"] }
139138
# server end of the join test. `connecting` and `online-mode` only gate
140139
# azalea's own connection type.
141140
azalea-protocol = { version = "0.16", default-features = false }
141+
# The item types azalea's packets hold, for the same cross-checks.
142+
azalea-inventory = "0.16"

‎pomme-client/src/app/phases/in_game.rs‎

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::time::Instant;
55

66
use glam::FloatExt as _;
77
use pomme_item::components::EquipmentSlot;
8-
use pomme_protocol::serverbound::{ContainerClick, ContainerInput, GamePacket};
8+
use pomme_protocol::serverbound::{ContainerClick, GamePacket};
99
use pomme_registry::builtin::{BlockEntityKind, EntityKind};
1010

1111
use crate::app::core::{AppCore, PlayerInputState};
@@ -246,7 +246,7 @@ pub struct GameState {
246246
/// detect the close transition and send a container-close packet.
247247
pub container_was_open: Option<i32>,
248248
/// Active survival click-drag (button + slots covered), if any.
249-
pub inv_drag: Option<(azalea_inventory::operations::QuickCraftKind, Vec<u16>)>,
249+
pub inv_drag: Option<(crate::player::click::QuickCraftKind, Vec<u16>)>,
250250
/// Last survival left click (slot, time) for double-click detection.
251251
pub inv_last_click: Option<(u16, Instant)>,
252252
/// The container screen's hand cursor is up.
@@ -2627,13 +2627,11 @@ pub(crate) fn send_container_clicks(
26272627
game: &mut GameState,
26282628
connection: &ConnectionHandle,
26292629
audio: &crate::audio::AudioEngine,
2630-
ops: Vec<azalea_inventory::operations::ClickOperation>,
2630+
ops: Vec<crate::player::click::ClickOperation>,
26312631
) {
2632-
use azalea_inventory::operations::{
2633-
ClickOperation, QuickCraftClick, QuickCraftKind, QuickCraftStatus,
2634-
};
26352632
use pomme_item::ItemStack;
26362633

2634+
use crate::player::click::{ClickOperation, QuickCraftClick, QuickCraftKind, QuickCraftStatus};
26372635
use crate::player::menu_click;
26382636

26392637
let (container_id, state_id) = match &game.open_container {
@@ -2657,7 +2655,7 @@ pub(crate) fn send_container_clicks(
26572655
status,
26582656
}) => match status {
26592657
QuickCraftStatus::Start => {
2660-
drag_kind = qc_kind.clone();
2658+
drag_kind = *qc_kind;
26612659
drag_slots.clear();
26622660
(Vec::new(), game.cursor_item.clone())
26632661
}
@@ -2714,7 +2712,7 @@ pub(crate) fn send_container_clicks(
27142712
state_id: state_id as i32,
27152713
slot: op.slot_num().map(|s| s as i16).unwrap_or(-999),
27162714
button: op.button_num() as i8,
2717-
input: container_input(op.click_type()),
2715+
input: op.input(),
27182716
changed_slots: menu_click::in_sent_order(changed)
27192717
.iter()
27202718
.map(|(s, item)| (*s as i16, hashed(item)))
@@ -2724,20 +2722,6 @@ pub(crate) fn send_container_clicks(
27242722
}
27252723
}
27262724

2727-
/// The wire's `ContainerInput` for a click.
2728-
fn container_input(click: azalea_inventory::operations::ClickType) -> ContainerInput {
2729-
use azalea_inventory::operations::ClickType;
2730-
match click {
2731-
ClickType::Pickup => ContainerInput::Pickup,
2732-
ClickType::QuickMove => ContainerInput::QuickMove,
2733-
ClickType::Swap => ContainerInput::Swap,
2734-
ClickType::Clone => ContainerInput::Clone,
2735-
ClickType::Throw => ContainerInput::Throw,
2736-
ClickType::QuickCraft => ContainerInput::QuickCraft,
2737-
ClickType::PickupAll => ContainerInput::PickupAll,
2738-
}
2739-
}
2740-
27412725
/// Vanilla `CrafterScreen.slotClicked`, run before the click itself: a pickup
27422726
/// on an empty grid slot enables it when disabled or, with nothing carried,
27432727
/// disables it; a number-key swap of a held item into a disabled slot enables
@@ -2746,10 +2730,9 @@ fn crafter_slot_clicked(
27462730
game: &mut GameState,
27472731
connection: &ConnectionHandle,
27482732
audio: &crate::audio::AudioEngine,
2749-
op: &azalea_inventory::operations::ClickOperation,
2733+
op: &crate::player::click::ClickOperation,
27502734
) {
2751-
use azalea_inventory::operations::{ClickOperation, PickupClick};
2752-
2735+
use crate::player::click::{ClickOperation, PickupClick};
27532736
use crate::ui::crafter::{GRID_SLOTS, slot_disabled};
27542737

27552738
let Some(c) = &mut game.open_container else {
@@ -4631,7 +4614,7 @@ pub fn update_game(
46314614
// Vanilla `onMouseClickAction`: a shift-click or number-key swap on a
46324615
// bundle unselects it before the click goes out.
46334616
for op in &ops {
4634-
use azalea_inventory::operations::{ClickOperation, QuickMoveClick};
4617+
use crate::player::click::{ClickOperation, QuickMoveClick};
46354618
let slot = match op {
46364619
ClickOperation::QuickMove(
46374620
QuickMoveClick::Left { slot } | QuickMoveClick::Right { slot },

‎pomme-client/src/harness/protocol.rs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,22 +251,19 @@ pub enum ClickKind {
251251
}
252252

253253
impl Click {
254-
pub fn operation(self) -> azalea_inventory::operations::ClickOperation {
255-
use azalea_inventory::operations::{
256-
ClickOperation, PickupClick, QuickMoveClick, SwapClick,
257-
};
254+
pub fn operation(self) -> crate::player::click::ClickOperation {
255+
use crate::player::click::{ClickOperation, PickupClick, QuickMoveClick, SwapClick};
258256
let slot = self.slot;
259257
match self.op {
260258
ClickKind::PickupLeft => ClickOperation::Pickup(PickupClick::Left { slot: Some(slot) }),
261259
ClickKind::PickupRight => {
262260
ClickOperation::Pickup(PickupClick::Right { slot: Some(slot) })
263261
}
264-
ClickKind::QuickMove => QuickMoveClick::Left { slot }.into(),
265-
ClickKind::Swap { hotbar } => SwapClick {
262+
ClickKind::QuickMove => ClickOperation::QuickMove(QuickMoveClick::Left { slot }),
263+
ClickKind::Swap { hotbar } => ClickOperation::Swap(SwapClick {
266264
source_slot: slot,
267265
target_slot: hotbar,
268-
}
269-
.into(),
266+
}),
270267
}
271268
}
272269
}

‎pomme-client/src/player/click.rs‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
//! A click in a container screen, as vanilla `AbstractContainerMenu.clicked`
2+
//! takes it: a slot, a button and a `ContainerInput`.
3+
4+
use pomme_protocol::serverbound::ContainerInput;
5+
6+
/// One click, by what it does.
7+
#[derive(Clone, Debug)]
8+
pub enum ClickOperation {
9+
Pickup(PickupClick),
10+
QuickMove(QuickMoveClick),
11+
Swap(SwapClick),
12+
Clone(CloneClick),
13+
Throw(ThrowClick),
14+
QuickCraft(QuickCraftClick),
15+
PickupAll(PickupAllClick),
16+
}
17+
18+
/// A left or right click; with no slot it is outside the menu and drops
19+
/// what the cursor carries, which the packet sends as slot -999.
20+
#[derive(Clone, Debug)]
21+
pub enum PickupClick {
22+
Left { slot: Option<u16> },
23+
Right { slot: Option<u16> },
24+
}
25+
26+
/// A shift-click, with either button.
27+
#[derive(Clone, Debug)]
28+
pub enum QuickMoveClick {
29+
Left { slot: u16 },
30+
Right { slot: u16 },
31+
}
32+
33+
/// A number key or the swap-hands key over a slot.
34+
#[derive(Clone, Debug)]
35+
pub struct SwapClick {
36+
pub source_slot: u16,
37+
/// The hotbar slot, 0 to 8, or 40 for the offhand.
38+
pub target_slot: u8,
39+
}
40+
41+
/// A middle click, which copies a stack in creative.
42+
#[derive(Clone, Debug)]
43+
pub struct CloneClick {
44+
pub slot: u16,
45+
}
46+
47+
/// The drop key over a slot: one item, or the whole stack with control.
48+
#[derive(Clone, Debug)]
49+
pub enum ThrowClick {
50+
Single { slot: u16 },
51+
All { slot: u16 },
52+
}
53+
54+
/// One step of a drag across slots.
55+
#[derive(Clone, Debug, Eq, PartialEq)]
56+
pub struct QuickCraftClick {
57+
pub kind: QuickCraftKind,
58+
pub status: QuickCraftStatus,
59+
}
60+
61+
/// The button a drag is made with, in `getQuickcraftType`'s order.
62+
// TODO: creative's middle-button drag (type 2), which fills each slot, is
63+
// not started anywhere yet.
64+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
65+
pub enum QuickCraftKind {
66+
Left,
67+
Right,
68+
}
69+
70+
/// Where in a drag a step is, in `getQuickcraftHeader`'s order.
71+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
72+
pub enum QuickCraftStatus {
73+
Start,
74+
Add { slot: u16 },
75+
End,
76+
}
77+
78+
/// A double click, which gathers what matches the carried stack.
79+
#[derive(Clone, Debug)]
80+
pub struct PickupAllClick {
81+
pub slot: u16,
82+
}
83+
84+
impl ClickOperation {
85+
/// The slot the click acts on, if it has one.
86+
pub fn slot_num(&self) -> Option<u16> {
87+
match self {
88+
Self::Pickup(PickupClick::Left { slot } | PickupClick::Right { slot }) => *slot,
89+
Self::QuickMove(QuickMoveClick::Left { slot } | QuickMoveClick::Right { slot })
90+
| Self::Throw(ThrowClick::Single { slot } | ThrowClick::All { slot })
91+
| Self::Clone(CloneClick { slot })
92+
| Self::PickupAll(PickupAllClick { slot }) => Some(*slot),
93+
Self::Swap(swap) => Some(swap.source_slot),
94+
Self::QuickCraft(drag) => match drag.status {
95+
QuickCraftStatus::Add { slot } => Some(slot),
96+
QuickCraftStatus::Start | QuickCraftStatus::End => None,
97+
},
98+
}
99+
}
100+
101+
/// The packet's button: the mouse button, a swap's target, or a drag's
102+
/// `getQuickcraftMask`.
103+
pub fn button_num(&self) -> u8 {
104+
match self {
105+
Self::Pickup(PickupClick::Left { .. })
106+
| Self::QuickMove(QuickMoveClick::Left { .. })
107+
| Self::Throw(ThrowClick::Single { .. })
108+
| Self::PickupAll(_) => 0,
109+
Self::Pickup(PickupClick::Right { .. })
110+
| Self::QuickMove(QuickMoveClick::Right { .. })
111+
| Self::Throw(ThrowClick::All { .. }) => 1,
112+
Self::Clone(_) => 2,
113+
Self::Swap(swap) => swap.target_slot,
114+
Self::QuickCraft(drag) => {
115+
let header = match drag.status {
116+
QuickCraftStatus::Start => 0,
117+
QuickCraftStatus::Add { .. } => 1,
118+
QuickCraftStatus::End => 2,
119+
};
120+
header | (drag.kind as u8) << 2
121+
}
122+
}
123+
}
124+
125+
pub fn input(&self) -> ContainerInput {
126+
match self {
127+
Self::Pickup(_) => ContainerInput::Pickup,
128+
Self::QuickMove(_) => ContainerInput::QuickMove,
129+
Self::Swap(_) => ContainerInput::Swap,
130+
Self::Clone(_) => ContainerInput::Clone,
131+
Self::Throw(_) => ContainerInput::Throw,
132+
Self::QuickCraft(_) => ContainerInput::QuickCraft,
133+
Self::PickupAll(_) => ContainerInput::PickupAll,
134+
}
135+
}
136+
}
137+
138+
#[cfg(test)]
139+
mod tests {
140+
use super::*;
141+
142+
#[test]
143+
fn a_drags_button_packs_its_step_and_its_mouse_button() {
144+
let button = |kind, status| {
145+
ClickOperation::QuickCraft(QuickCraftClick { kind, status }).button_num()
146+
};
147+
let add = QuickCraftStatus::Add { slot: 3 };
148+
assert_eq!(button(QuickCraftKind::Left, QuickCraftStatus::Start), 0);
149+
assert_eq!(button(QuickCraftKind::Left, add), 1);
150+
assert_eq!(button(QuickCraftKind::Left, QuickCraftStatus::End), 2);
151+
assert_eq!(button(QuickCraftKind::Right, QuickCraftStatus::Start), 4);
152+
assert_eq!(button(QuickCraftKind::Right, add), 5);
153+
assert_eq!(button(QuickCraftKind::Right, QuickCraftStatus::End), 6);
154+
}
155+
156+
#[test]
157+
fn a_click_names_its_slot_button_and_input() {
158+
let parts = |op: ClickOperation| (op.slot_num(), op.button_num(), op.input());
159+
assert_eq!(
160+
parts(ClickOperation::Pickup(PickupClick::Right { slot: None })),
161+
(None, 1, ContainerInput::Pickup)
162+
);
163+
assert_eq!(
164+
parts(ClickOperation::Swap(SwapClick {
165+
source_slot: 12,
166+
target_slot: 40,
167+
})),
168+
(Some(12), 40, ContainerInput::Swap)
169+
);
170+
assert_eq!(
171+
parts(ClickOperation::Throw(ThrowClick::All { slot: 7 })),
172+
(Some(7), 1, ContainerInput::Throw)
173+
);
174+
assert_eq!(
175+
parts(ClickOperation::Clone(CloneClick { slot: 5 })),
176+
(Some(5), 2, ContainerInput::Clone)
177+
);
178+
assert_eq!(
179+
parts(ClickOperation::QuickMove(QuickMoveClick::Right { slot: 9 })),
180+
(Some(9), 1, ContainerInput::QuickMove)
181+
);
182+
assert_eq!(
183+
parts(ClickOperation::PickupAll(PickupAllClick { slot: 4 })),
184+
(Some(4), 0, ContainerInput::PickupAll)
185+
);
186+
}
187+
}

0 commit comments

Comments
 (0)