From 49dfc1881844e6d46a42ac7229f20d750c027f17 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Tue, 20 Aug 2024 13:23:32 +0200 Subject: [PATCH] Fix: Clippy & fmt --- pumpkin-inventory/src/player.rs | 38 +++------- pumpkin-protocol/src/slot.rs | 11 +-- pumpkin-world/src/global_registry.rs | 13 ++-- pumpkin-world/src/item/item_categories.rs | 92 +++++++++-------------- pumpkin-world/src/item/item_registry.rs | 2 +- pumpkin-world/src/item/mod.rs | 2 +- pumpkin/src/client/player_packet.rs | 34 +++++---- 7 files changed, 83 insertions(+), 109 deletions(-) diff --git a/pumpkin-inventory/src/player.rs b/pumpkin-inventory/src/player.rs index 68f4128c5..f7147f181 100644 --- a/pumpkin-inventory/src/player.rs +++ b/pumpkin-inventory/src/player.rs @@ -12,18 +12,6 @@ pub struct PlayerInventory { selected: usize, } -pub struct Hotbar<'a>(&'a mut [Option;9]); - -impl Hotbar<'_> { - fn get_mut(&mut self, index: usize) -> &mut Option { - &mut self.0[index] - } -} - -pub struct Armor<'a>(&'a mut [Option; 4]); - - - impl Default for PlayerInventory { fn default() -> Self { Self::new() @@ -42,15 +30,15 @@ impl PlayerInventory { selected: 0, } } - + /// Set the contents of an item in a slot /// /// ## Slot /// The slot according to https://wiki.vg/Inventory#Player_Inventory - /// + /// /// ## Item /// The optional item to place in the slot - /// + /// /// ## Item allowed override /// An override, which when enabled, makes it so that invalid items, can be placed in slots they normally can't. /// Useful functionality for plugins in the future. @@ -60,17 +48,13 @@ impl PlayerInventory { // TODO: Add crafting check here self.crafting_output = item } - 1..=4 => { - self.crafting[slot-1] = item - } + 1..=4 => self.crafting[slot - 1] = item, 5..=8 => { match item { - None => { - self.armor[slot-4] = None - }, + None => self.armor[slot - 4] = None, Some(item) => { // TODO: Replace asserts with error handling - match slot-5 { + match slot - 5 { 0 => { assert!(item.is_helmet() || item_allowed_override); self.armor[0] = Some(item); @@ -87,18 +71,18 @@ impl PlayerInventory { assert!(item.is_boots() || item_allowed_override); self.armor[3] = Some(item) } - _ => unreachable!() + _ => unreachable!(), } } } } 9..=44 => { - self.items[slot-9] = item; + self.items[slot - 9] = item; } 45 => { self.offhand = item; } - _ => unreachable!() + _ => unreachable!(), } } @@ -106,9 +90,9 @@ impl PlayerInventory { assert!((0..9).contains(&slot)); self.selected = slot; } - + pub fn held_item(&self) -> Option<&Item> { debug_assert!((0..9).contains(&self.selected)); - self.items[self.selected+36-9].as_ref() + self.items[self.selected + 36 - 9].as_ref() } } diff --git a/pumpkin-protocol/src/slot.rs b/pumpkin-protocol/src/slot.rs index 04ac9fe30..22223f3b4 100644 --- a/pumpkin-protocol/src/slot.rs +++ b/pumpkin-protocol/src/slot.rs @@ -1,9 +1,9 @@ +use crate::VarInt; +use pumpkin_world::item::Item; use serde::{ de::{self, SeqAccess, Visitor}, Deserialize, }; -use pumpkin_world::item::Item; -use crate::VarInt; #[derive(Debug, Clone)] #[allow(dead_code)] @@ -16,9 +16,6 @@ pub struct Slot { components_to_remove: Option>, } - - - impl<'de> Deserialize<'de> for Slot { fn deserialize(deserializer: D) -> Result where @@ -91,7 +88,7 @@ impl From for Item { fn from(slot: Slot) -> Self { Item { item_count: slot.item_count.0.try_into().unwrap(), - item_id: slot.item_id.unwrap().0.try_into().unwrap() + item_id: slot.item_id.unwrap().0.try_into().unwrap(), } } -} \ No newline at end of file +} diff --git a/pumpkin-world/src/global_registry.rs b/pumpkin-world/src/global_registry.rs index fd55c3585..6a7aeafa1 100644 --- a/pumpkin-world/src/global_registry.rs +++ b/pumpkin-world/src/global_registry.rs @@ -9,7 +9,7 @@ const REGISTRY_JSON: &str = include_str!("../assets/registries.json"); #[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)] pub struct RegistryElement { default: Option, - pub entries: HashMap>, + pub entries: HashMap>, } lazy_static! { @@ -23,7 +23,7 @@ pub fn get_protocol_id(category: &str, entry: &str) -> u32 { .expect("Invalid Category in registry") .entries .get(entry) - .map(|p|p.get("protocol_id").unwrap()) + .map(|p| p.get("protocol_id").unwrap()) .expect("No Entry found") } @@ -37,9 +37,12 @@ pub fn get_default<'a>(category: &str) -> Option<&'a str> { } pub fn find_minecraft_id(category: &str, protocol_id: u32) -> Option<&str> { - REGISTRY.get(category)? + REGISTRY + .get(category)? .entries .iter() - .find(|(_,other_protocol_id)|*other_protocol_id.get("protocol_id").unwrap()==protocol_id) - .map(|(id,_)|id.as_str()) + .find(|(_, other_protocol_id)| { + *other_protocol_id.get("protocol_id").unwrap() == protocol_id + }) + .map(|(id, _)| id.as_str()) } diff --git a/pumpkin-world/src/item/item_categories.rs b/pumpkin-world/src/item/item_categories.rs index dfd032e33..6543f348a 100644 --- a/pumpkin-world/src/item/item_categories.rs +++ b/pumpkin-world/src/item/item_categories.rs @@ -4,72 +4,54 @@ impl Item { pub fn is_helmet(&self) -> bool { [ // Leather - 856, - // Netherite - 876, - // Turtle helmet - 794, - // Chainmail - 860, - // Diamond - 868, - // Gold - 872, - // Iron - 864 - ].contains(&self.item_id) + 856, // Netherite + 876, // Turtle helmet + 794, // Chainmail + 860, // Diamond + 868, // Gold + 872, // Iron + 864, + ] + .contains(&self.item_id) } - + pub fn is_chestplate(&self) -> bool { [ // Leather - 857, - // Netherite - 877, - // Chainmail - 861, - // Diamond - 869, - // Gold - 873, - // Iron - 865, - // Elytra + 857, // Netherite + 877, // Chainmail + 861, // Diamond + 869, // Gold + 873, // Iron + 865, // Elytra 773, - ].contains(&self.item_id) + ] + .contains(&self.item_id) } - + pub fn is_leggings(&self) -> bool { [ // Leather - 858, - // Netherite - 878, - // Chainmail - 862, - // Diamond - 870, - // Gold - 874, - // Iron - 866 - ].contains(&self.item_id) + 858, // Netherite + 878, // Chainmail + 862, // Diamond + 870, // Gold + 874, // Iron + 866, + ] + .contains(&self.item_id) } - + pub fn is_boots(&self) -> bool { [ // Leather - 859, - // Netherite - 879, - // Chainmail - 863, - // Diamond - 871, - // Gold - 875, - // Iron - 867 - ].contains(&self.item_id) + 859, // Netherite + 879, // Chainmail + 863, // Diamond + 871, // Gold + 875, // Iron + 867, + ] + .contains(&self.item_id) } -} \ No newline at end of file +} diff --git a/pumpkin-world/src/item/item_registry.rs b/pumpkin-world/src/item/item_registry.rs index f391cea9c..81794c063 100644 --- a/pumpkin-world/src/item/item_registry.rs +++ b/pumpkin-world/src/item/item_registry.rs @@ -28,7 +28,7 @@ pub struct ItemElement { } lazy_static! { - pub static ref ITEMS: HashMap = + pub static ref ITEMS: HashMap = serde_json::from_str(ITEMS_JSON).expect("Could not parse items.json registry."); } diff --git a/pumpkin-world/src/item/mod.rs b/pumpkin-world/src/item/mod.rs index 0fa843d57..65c9dbfe8 100644 --- a/pumpkin-world/src/item/mod.rs +++ b/pumpkin-world/src/item/mod.rs @@ -1,5 +1,5 @@ -mod item_registry; mod item_categories; +mod item_registry; pub use item_registry::ITEMS; #[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "lowercase")] diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 05e0bccd1..45d8c67ae 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -1,5 +1,11 @@ use std::f32::consts::PI; +use crate::{ + commands::{handle_command, CommandSender}, + entity::player::{ChatMode, GameMode, Hand}, + server::Server, + util::math::wrap_degrees, +}; use num_traits::FromPrimitive; use pumpkin_entity::EntityId; use pumpkin_protocol::{ @@ -17,12 +23,6 @@ use pumpkin_protocol::{ use pumpkin_text::TextComponent; use pumpkin_world::block::BlockFace; use pumpkin_world::global_registry; -use crate::{ - commands::{handle_command, CommandSender}, - entity::player::{ChatMode, GameMode, Hand}, - server::Server, - util::math::wrap_degrees, -}; use super::{Client, PlayerConfig}; @@ -317,14 +317,24 @@ impl Client { } pub fn handle_player_action(&mut self, _server: &mut Server, _player_action: SPlayerAction) {} - pub fn handle_use_item_on(&mut self, server: &mut Server, use_item_on: SUseItemOn) { + pub fn handle_use_item_on(&mut self, server: &mut Server, use_item_on: SUseItemOn) { let location = use_item_on.location; let face = BlockFace::from_i32(use_item_on.face.0).unwrap(); let location = WorldPosition(location.0 + face.to_offset()); if let Some(item) = self.player.as_ref().unwrap().inventory.held_item() { - let minecraft_id = global_registry::find_minecraft_id(global_registry::ITEM_REGISTRY,item.item_id).expect("All item ids are in the global registry"); - if let Ok(block_state_id) = pumpkin_world::block::block_registry::block_id_and_properties_to_block_state_id(minecraft_id,None) { - server.broadcast_packet(self, &CBlockUpdate::new(location, (block_state_id as i32).into())); + let minecraft_id = + global_registry::find_minecraft_id(global_registry::ITEM_REGISTRY, item.item_id) + .expect("All item ids are in the global registry"); + if let Ok(block_state_id) = + pumpkin_world::block::block_registry::block_id_and_properties_to_block_state_id( + minecraft_id, + None, + ) + { + server.broadcast_packet( + self, + &CBlockUpdate::new(location, (block_state_id as i32).into()), + ); } } } @@ -339,9 +349,7 @@ impl Client { } pub fn handle_set_creative_slot(&mut self, _server: &mut Server, packet: SSetCreativeSlot) { - let inventory = &mut self.player.as_mut() - .unwrap() - .inventory; + let inventory = &mut self.player.as_mut().unwrap().inventory; inventory.set_slot(packet.slot as usize, packet.clicked_item.to_item(), false); }