Simplified match, use filter and cargo fmt suggestions

This commit is contained in:
Woeful_Wolf 2024-01-20 20:40:16 +02:00
parent 25e4e09ab4
commit 3cb5132ab9
3 changed files with 41 additions and 64 deletions

View File

@ -2122,53 +2122,31 @@ impl Hud {
})] })]
}, },
BlockInteraction::Mine(mine_tool) => { BlockInteraction::Mine(mine_tool) => {
match &info.active_mine_tool { match (mine_tool, &info.active_mine_tool) {
Some(active_mine_tool) => { (ToolKind::Pick, Some(ToolKind::Pick)) => {
match (mine_tool, active_mine_tool) { vec![(
(ToolKind::Pick, ToolKind::Pick) => { Some(GameInput::Primary),
vec![( i18n.get_msg("hud-mine").to_string(),
Some(GameInput::Primary), )]
i18n.get_msg("hud-mine").to_string(), },
)] (ToolKind::Pick, _) => {
}, vec![(None, i18n.get_msg("hud-mine-needs_pickaxe").to_string())]
(ToolKind::Pick, _) => { },
vec![(None, i18n.get_msg("hud-mine-needs_pickaxe").to_string())] (ToolKind::Shovel, Some(ToolKind::Shovel)) => {
}, vec![(
(ToolKind::Shovel, ToolKind::Shovel) => { Some(GameInput::Primary),
vec![( i18n.get_msg("hud-dig").to_string(),
Some(GameInput::Primary), )]
i18n.get_msg("hud-dig").to_string(), },
)] (ToolKind::Shovel, _) => {
}, vec![(None, i18n.get_msg("hud-mine-needs_shovel").to_string())]
(ToolKind::Shovel, _) => { },
vec![(None, i18n.get_msg("hud-mine-needs_shovel").to_string())] _ => {
}, vec![(
_ => { None,
vec![( i18n.get_msg("hud-mine-needs_unhandled_case").to_string(),
None, )]
i18n.get_msg("hud-mine-needs_unhandled_case").to_string(),
)]
},
}
}, },
None => {
match mine_tool {
ToolKind::Pick => {
vec![(None, i18n.get_msg("hud-mine-needs_pickaxe").to_string())]
},
ToolKind::Shovel => {
vec![(None, i18n.get_msg("hud-mine-needs_shovel").to_string())]
}
// TODO: The required tool for mining something may not always be a
// pickaxe!
_ => {
vec![(
None,
i18n.get_msg("hud-mine-needs_unhandled_case").to_string(),
)]
},
}
}
} }
}, },
BlockInteraction::Mount => { BlockInteraction::Mount => {

View File

@ -601,22 +601,16 @@ impl PlayState for SessionState {
.get(player_entity) .get(player_entity)
.map_or_else(|| false, |cb| cb.enabled); .map_or_else(|| false, |cb| cb.enabled);
let active_mine_tool: Option<ToolKind> = if client.is_wielding() == Some(true) { client let active_mine_tool: Option<ToolKind> = if client.is_wielding() == Some(true) {
.inventories() client
.get(player_entity) .inventories()
.and_then(|inv| inv.equipped(EquipSlot::ActiveMainhand)) .get(player_entity)
.and_then(|item| item.tool_info()) .and_then(|inv| inv.equipped(EquipSlot::ActiveMainhand))
.map_or(None, |tool_kind| { .and_then(|item| item.tool_info())
match tool_kind { .filter(|tool_kind| matches!(tool_kind, ToolKind::Pick | ToolKind::Shovel))
ToolKind::Pick => Some(tool_kind),
ToolKind::Shovel => Some(tool_kind),
_ => None,
}
})
} else { } else {
None None
}; };
// Check to see whether we're aiming at anything // Check to see whether we're aiming at anything
let (build_target, collect_target, entity_target, mine_target, terrain_target) = let (build_target, collect_target, entity_target, mine_target, terrain_target) =
@ -658,17 +652,21 @@ impl PlayState for SessionState {
// Nearest block to consider with GameInput primary or secondary key. // Nearest block to consider with GameInput primary or secondary key.
let nearest_block_dist = find_shortest_distance(&[ let nearest_block_dist = find_shortest_distance(&[
mine_target.filter(|_| active_mine_tool.is_some()).map(|t| t.distance), mine_target
.filter(|_| active_mine_tool.is_some())
.map(|t| t.distance),
build_target.filter(|_| can_build).map(|t| t.distance), build_target.filter(|_| can_build).map(|t| t.distance),
]); ]);
// Nearest block to be highlighted in the scene (self.scene.set_select_pos). // Nearest block to be highlighted in the scene (self.scene.set_select_pos).
let nearest_scene_dist = find_shortest_distance(&[ let nearest_scene_dist = find_shortest_distance(&[
nearest_block_dist, nearest_block_dist,
collect_target.filter(|_| active_mine_tool.is_none()).map(|t| t.distance), collect_target
.filter(|_| active_mine_tool.is_none())
.map(|t| t.distance),
]); ]);
// Set break_block_pos only if mining is closest. // Set break_block_pos only if mining is closest.
self.inputs.break_block_pos = if let Some(mt) = self.inputs.break_block_pos = if let Some(mt) = mine_target
mine_target.filter(|mt| active_mine_tool.is_some() && nearest_scene_dist == Some(mt.distance)) .filter(|mt| active_mine_tool.is_some() && nearest_scene_dist == Some(mt.distance))
{ {
self.scene.set_select_pos(Some(mt.position_int())); self.scene.set_select_pos(Some(mt.position_int()));
Some(mt.position) Some(mt.position)

View File

@ -103,7 +103,8 @@ pub(super) fn targets_under_cursor(
}; };
let (collect_pos, _, collect_cam_ray) = find_pos(|b: Block| b.is_collectible()); let (collect_pos, _, collect_cam_ray) = find_pos(|b: Block| b.is_collectible());
let (mine_pos, _, mine_cam_ray) = active_mine_tool.is_some() let (mine_pos, _, mine_cam_ray) = active_mine_tool
.is_some()
.then(|| find_pos(|b: Block| b.mine_tool().is_some())) .then(|| find_pos(|b: Block| b.mine_tool().is_some()))
.unwrap_or((None, None, None)); .unwrap_or((None, None, None));
let (solid_pos, place_block_pos, solid_cam_ray) = find_pos(|b: Block| b.is_filled()); let (solid_pos, place_block_pos, solid_cam_ray) = find_pos(|b: Block| b.is_filled());