-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from Bryntet/master
Add CSetContainerSlot and refactor container logic to own file
- Loading branch information
Showing
5 changed files
with
118 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::slot::Slot; | ||
use crate::VarInt; | ||
use pumpkin_macros::packet; | ||
use serde::Serialize; | ||
#[derive(Serialize)] | ||
#[packet(0x15)] | ||
pub struct CSetContainerSlot<'a> { | ||
window_id: i8, | ||
state_id: VarInt, | ||
slot: i16, | ||
slot_data: &'a Slot, | ||
} | ||
|
||
impl<'a> CSetContainerSlot<'a> { | ||
pub fn new(window_id: i8, state_id: i32, slot: usize, slot_data: &'a Slot) -> Self { | ||
Self { | ||
window_id, | ||
state_id: state_id.into(), | ||
slot: slot.try_into().unwrap(), | ||
slot_data, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use pumpkin_core::text::TextComponent; | ||
use pumpkin_inventory::WindowType; | ||
use pumpkin_protocol::client::play::{COpenScreen, CSetContainerContent, CSetContainerSlot}; | ||
use pumpkin_protocol::slot::Slot; | ||
use pumpkin_world::item::Item; | ||
|
||
impl super::Client { | ||
pub fn open_container( | ||
&mut self, | ||
window_type: WindowType, | ||
minecraft_menu_id: &str, | ||
window_title: Option<&str>, | ||
items: Option<Vec<Option<&Item>>>, | ||
carried_item: Option<&Item>, | ||
) { | ||
let menu_protocol_id = (*pumpkin_world::global_registry::REGISTRY | ||
.get("minecraft:menu") | ||
.unwrap() | ||
.entries | ||
.get(minecraft_menu_id) | ||
.expect("Should be a valid menu id") | ||
.get("protocol_id") | ||
.unwrap()) | ||
.into(); | ||
let title = TextComponent::text(window_title.unwrap_or(window_type.default_title())); | ||
self.send_packet(&COpenScreen::new( | ||
(window_type.clone() as u8 + 1).into(), | ||
menu_protocol_id, | ||
title, | ||
)); | ||
self.set_container_content(window_type, items, carried_item); | ||
} | ||
|
||
pub fn set_container_content<'a>( | ||
&mut self, | ||
window_type: WindowType, | ||
items: Option<Vec<Option<&'a Item>>>, | ||
carried_item: Option<&'a Item>, | ||
) { | ||
let player = self.player.as_ref().unwrap(); | ||
|
||
let slots: Vec<Slot> = { | ||
if let Some(mut items) = items { | ||
items.extend(player.inventory.slots()); | ||
items | ||
} else { | ||
player.inventory.slots() | ||
} | ||
.into_iter() | ||
.map(|item| { | ||
if let Some(item) = item { | ||
Slot::from(item) | ||
} else { | ||
Slot::empty() | ||
} | ||
}) | ||
.collect() | ||
}; | ||
|
||
let carried_item = { | ||
if let Some(item) = carried_item { | ||
item.into() | ||
} else { | ||
Slot::empty() | ||
} | ||
}; | ||
let packet = | ||
CSetContainerContent::new(window_type as u8 + 1, 0.into(), &slots, &carried_item); | ||
self.send_packet(&packet); | ||
} | ||
|
||
pub fn set_container_slot( | ||
&mut self, | ||
window_type: WindowType, | ||
slot: usize, | ||
item: Option<&Item>, | ||
) { | ||
self.send_packet(&CSetContainerSlot::new( | ||
window_type as i8, | ||
0, | ||
slot, | ||
&item.into(), | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters