Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryntet committed Aug 21, 2024
1 parent 2e00110 commit 816215c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 41 deletions.
4 changes: 2 additions & 2 deletions pumpkin-inventory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum WindowType {
impl WindowType {
pub const fn default_title(&self) -> &'static str {
match self {
_ => "WINDOW TITLE"
_ => "WINDOW TITLE",
}
}
}
}
6 changes: 3 additions & 3 deletions pumpkin-inventory/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ impl PlayerInventory {

pub fn slots(&self) -> Vec<Option<&Item>> {
let mut slots = vec![self.crafting_output.as_ref()];
slots.extend(self.crafting.iter().map(|c|c.as_ref()));
slots.extend(self.armor.iter().map(|c|c.as_ref()));
slots.extend(self.items.iter().map(|c|c.as_ref()));
slots.extend(self.crafting.iter().map(|c| c.as_ref()));
slots.extend(self.armor.iter().map(|c| c.as_ref()));
slots.extend(self.items.iter().map(|c| c.as_ref()));
slots.push(self.offhand.as_ref());
slots
}
Expand Down
11 changes: 5 additions & 6 deletions pumpkin-protocol/src/client/play/c_set_container_content.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pumpkin_macros::packet;
use serde::Serialize;
use crate::slot::Slot;
use crate::VarInt;
use pumpkin_macros::packet;
use serde::Serialize;

#[derive(Serialize)]
#[packet(0x13)]
Expand All @@ -10,18 +10,17 @@ pub struct CSetContainerContent<'a> {
state_id: VarInt,
count: VarInt,
slot_data: &'a [Slot],
carried_item: &'a Slot
carried_item: &'a Slot,
}


impl<'a> CSetContainerContent<'a> {
pub fn new(window_id: u8,state_id: VarInt, slots: &'a [Slot], carried_item: &'a Slot) -> Self {
pub fn new(window_id: u8, state_id: VarInt, slots: &'a [Slot], carried_item: &'a Slot) -> Self {
Self {
window_id,
state_id,
count: slots.len().try_into().unwrap(),
slot_data: slots,
carried_item
carried_item,
}
}
}
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod c_player_chat_message;
mod c_player_info_update;
mod c_player_remove;
mod c_remove_entities;
mod c_set_container_content;
mod c_set_held_item;
mod c_set_title;
mod c_spawn_player;
Expand All @@ -33,7 +34,6 @@ mod c_update_entity_pos;
mod c_update_entity_rot;
mod c_worldevent;
mod player_action;
mod c_set_container_content;

pub use c_acknowledge_block::*;
pub use c_actionbar::*;
Expand All @@ -59,6 +59,7 @@ pub use c_player_chat_message::*;
pub use c_player_info_update::*;
pub use c_player_remove::*;
pub use c_remove_entities::*;
pub use c_set_container_content::*;
pub use c_set_held_item::*;
pub use c_set_title::*;
pub use c_spawn_player::*;
Expand All @@ -70,4 +71,3 @@ pub use c_update_entity_pos::*;
pub use c_update_entity_rot::*;
pub use c_worldevent::*;
pub use player_action::*;
pub use c_set_container_content::*;
18 changes: 10 additions & 8 deletions pumpkin-protocol/src/slot.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::VarInt;
use pumpkin_world::item::Item;
use serde::{de::{self, SeqAccess, Visitor}, Deserialize, Serialize, Serializer};
use serde::ser::SerializeSeq;
use serde::{
de::{self, SeqAccess, Visitor},
Deserialize, Serialize, Serializer,
};

#[derive(Debug, Clone)]
#[allow(dead_code)]
Expand Down Expand Up @@ -84,8 +87,8 @@ impl Serialize for Slot {
s.serialize_element(&self.item_count)?;
s.end()
} else {
match (&self.num_components_to_add,&self.num_components_to_remove) {
(Some(to_add),Some(to_remove)) => {
match (&self.num_components_to_add, &self.num_components_to_remove) {
(Some(to_add), Some(to_remove)) => {
let mut s = serializer.serialize_seq(Some(6))?;
s.serialize_element(&self.item_count)?;
s.serialize_element(self.item_id.as_ref().unwrap())?;
Expand All @@ -104,7 +107,7 @@ impl Serialize for Slot {
s.serialize_element(self.components_to_remove.as_ref().unwrap())?;
s.end()
}
(Some(to_add),None) => {
(Some(to_add), None) => {
let mut s = serializer.serialize_seq(Some(5))?;
s.serialize_element(&self.item_count)?;
s.serialize_element(self.item_id.as_ref().unwrap())?;
Expand All @@ -113,7 +116,7 @@ impl Serialize for Slot {
s.serialize_element(self.components_to_add.as_ref().unwrap())?;
s.end()
}
(None,None) => {
(None, None) => {
let mut s = serializer.serialize_seq(Some(4))?;
s.serialize_element(&self.item_count)?;
s.serialize_element(&self.item_id.as_ref().unwrap())?;
Expand All @@ -122,7 +125,6 @@ impl Serialize for Slot {
s.end()
}
}

}
}
}
Expand Down Expand Up @@ -154,10 +156,10 @@ impl From<&Item> for Slot {
item_count: item.item_count.into(),
item_id: Some(item.item_id.into()),
// TODO: add these
num_components_to_add:None,
num_components_to_add: None,
num_components_to_remove: None,
components_to_add: None,
components_to_remove: None,
}
}
}
}
76 changes: 56 additions & 20 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use pumpkin_protocol::{
client::{
config::CConfigDisconnect,
login::CLoginDisconnect,
play::{CGameEvent, CPlayDisconnect, CSyncPlayerPostion, CSystemChatMessge, CSetContainerContent},
play::{
CGameEvent, CPlayDisconnect, CSetContainerContent, CSyncPlayerPostion,
CSystemChatMessge,
},
},
packet_decoder::PacketDecoder,
packet_encoder::PacketEncoder,
Expand All @@ -38,13 +41,13 @@ use pumpkin_protocol::{
};
use pumpkin_text::TextComponent;

use std::io::Read;
use thiserror::Error;
use pumpkin_inventory::WindowType;
use pumpkin_protocol::bytebuf::ByteBuffer;
use pumpkin_protocol::client::play::COpenScreen;
use pumpkin_protocol::slot::Slot;
use pumpkin_world::item::Item;
use std::io::Read;
use thiserror::Error;

pub mod authentication;
mod client_packet;
Expand Down Expand Up @@ -179,30 +182,58 @@ impl Client {
player.gamemode = gamemode;
self.send_packet(&CGameEvent::new(3, gamemode.to_f32().unwrap()));
}

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();

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.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>) {

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|{

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()};
})
.collect()
};

let carried_item = {
if let Some(item) = carried_item {
Expand All @@ -211,7 +242,12 @@ impl Client {
Slot::empty()
}
};
self.send_packet(&CSetContainerContent::new(window_type as u8+1, 0.into(), &slots, &carried_item));
self.send_packet(&CSetContainerContent::new(
window_type as u8 + 1,
0.into(),
&slots,
&carried_item,
));
}

pub async fn process_packets(&mut self, server: &mut Server) {
Expand Down Expand Up @@ -398,7 +434,7 @@ impl Client {
}
}
}

pub fn send_system_message(&mut self, text: TextComponent) {
self.send_packet(&CSystemChatMessge::new(text, false));
}
Expand Down

0 comments on commit 816215c

Please sign in to comment.