Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dupe, doubble click and shift click #577

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions pumpkin-inventory/src/container_click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::InventoryError;
use pumpkin_protocol::server::play::SlotActionType;
use pumpkin_world::item::ItemStack;

#[derive(Debug)]
pub struct Click {
pub slot: Slot,
pub click_type: ClickType,
Expand All @@ -18,7 +19,7 @@ impl Click {
click_type: ClickType::CreativePickItem,
slot: Slot::Normal(slot.try_into().or(Err(InventoryError::InvalidSlot))?),
}),
SlotActionType::Throw => Self::new_drop_item(button),
SlotActionType::Throw => Self::new_drop_item(button, slot),
SlotActionType::QuickCraft => Self::new_drag_item(button, slot),
SlotActionType::PickupAll => Ok(Self {
click_type: ClickType::DoubleClick,
Expand Down Expand Up @@ -66,15 +67,22 @@ impl Click {
})
}

fn new_drop_item(button: i8) -> Result<Self, InventoryError> {
fn new_drop_item(button: i8, slot: i16) -> Result<Self, InventoryError> {
let drop_type = match button {
0 => DropType::SingleItem,
1 => DropType::FullStack,
_ => Err(InventoryError::InvalidPacket)?,
};
let slot = match slot {
-999 => Slot::OutsideInventory,
Comment on lines 72 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create constants for these magic numbers? like left click, right click, outside

_ => {
let slot = slot.try_into().unwrap_or(0);
Slot::Normal(slot)
}
};
Ok(Self {
click_type: ClickType::DropType(drop_type),
slot: Slot::OutsideInventory,
slot,
})
}

Expand Down Expand Up @@ -120,7 +128,7 @@ pub enum KeyClick {
Slot(u8),
Offhand,
}
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum Slot {
Normal(usize),
OutsideInventory,
Expand Down
4 changes: 4 additions & 0 deletions pumpkin-inventory/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ impl PlayerInventory {
self.selected + 36
}

pub fn increment_state_id(&mut self) {
self.state_id = self.state_id % 100 + 1;
}

pub fn held_item(&self) -> Option<&ItemStack> {
debug_assert!((0..9).contains(&self.selected));
self.items[self.selected as usize + 36 - 9].as_ref()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also make constants for the inventory locations? Like MAX_SLOT, HOTBAR_SLOTS = (0..9), etc.

Expand Down
3 changes: 2 additions & 1 deletion pumpkin-protocol/src/server/play/click_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::de::SeqAccess;
use serde::{Deserialize, de};

#[server_packet(PLAY_CONTAINER_CLICK)]
#[derive(Debug)]
pub struct SClickContainer {
pub window_id: VarInt,
pub state_id: VarInt,
Expand Down Expand Up @@ -86,7 +87,7 @@ impl<'de> Deserialize<'de> for SClickContainer {
}
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
pub enum SlotActionType {
/// Performs a normal slot click. This can pickup or place items in the slot, possibly merging the cursor stack into the slot, or swapping the slot stack with the cursor stack if they can't be merged.
Pickup,
Expand Down
66 changes: 34 additions & 32 deletions pumpkin-world/src/item/categories.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,70 @@
use pumpkin_data::item::Item;

use crate::item::ItemStack;

impl ItemStack {
pub fn is_sword(&self) -> bool {
[
818, // Wooden
823, // Stone
828, // Gold
833, // Iron
838, // Diamond
843, // Netherite
Item::WOODEN_SWORD.id, // Wooden
Item::STONE_SWORD.id, // Stone
Item::GOLDEN_SWORD.id, // Gold
Item::IRON_SWORD.id, // Iron
Item::DIAMOND_SWORD.id, // Diamond
Item::NETHERITE_SWORD.id, // Netherite
Comment on lines -6 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use our item tags for this instead of explicitly writing them out. See #566 (Ideally with my recommendations to @Jayryn) See also https://minecraft.wiki/w/Item_tag_(Java_Edition)

]
.contains(&self.item.id)
}

pub fn is_helmet(&self) -> bool {
[
// Leather
856, // Netherite
876, // Turtle helmet
794, // Chainmail
860, // Diamond
868, // Gold
872, // Iron
864,
Item::NETHERITE_HELMET.id, // Netherite
Item::TURTLE_HELMET.id, // Turtle helmet
Item::CHAINMAIL_HELMET.id, // Chainmail
Item::DIAMOND_HELMET.id, // Diamond
Item::GOLDEN_HELMET.id, // Gold
Item::IRON_HELMET.id, // Iron
Item::LEATHER_HELMET.id,
]
.contains(&self.item.id)
}

pub fn is_chestplate(&self) -> bool {
[
// Leather
857, // Netherite
877, // Chainmail
861, // Diamond
869, // Gold
873, // Iron
865, // Elytra
773,
Item::NETHERITE_CHESTPLATE.id, // Netherite
Item::CHAINMAIL_CHESTPLATE.id, // Chainmail
Item::DIAMOND_CHESTPLATE.id, // Diamond
Item::GOLDEN_CHESTPLATE.id, // Gold
Item::IRON_CHESTPLATE.id, // Iron
Item::ELYTRA.id, // Elytra
Item::LEATHER_CHESTPLATE.id,
]
.contains(&self.item.id)
}

pub fn is_leggings(&self) -> bool {
[
// Leather
858, // Netherite
878, // Chainmail
862, // Diamond
870, // Gold
874, // Iron
866,
Item::NETHERITE_LEGGINGS.id, // Netherite
Item::CHAINMAIL_LEGGINGS.id, // Chainmail
Item::DIAMOND_LEGGINGS.id, // Diamond
Item::GOLDEN_LEGGINGS.id, // Gold
Item::IRON_LEGGINGS.id, // Iron
Item::LEATHER_LEGGINGS.id,
]
.contains(&self.item.id)
}

pub fn is_boots(&self) -> bool {
[
// Leather
859, // Netherite
879, // Chainmail
863, // Diamond
871, // Gold
875, // Iron
867,
Item::NETHERITE_BOOTS.id, // Netherite
Item::CHAINMAIL_BOOTS.id, // Chainmail
Item::DIAMOND_BOOTS.id, // Diamond
Item::GOLDEN_BOOTS.id, // Gold
Item::IRON_BOOTS.id, // Iron
Item::LEATHER_BOOTS.id,
]
.contains(&self.item.id)
}
Expand Down
Loading