This commit is contained in:
Edvin Bryntesson 2024-08-21 19:28:48 +02:00
parent 2e00110d5d
commit 816215c698
No known key found for this signature in database
6 changed files with 78 additions and 41 deletions

View File

@ -45,7 +45,7 @@ pub enum WindowType {
impl WindowType { impl WindowType {
pub const fn default_title(&self) -> &'static str { pub const fn default_title(&self) -> &'static str {
match self { match self {
_ => "WINDOW TITLE" _ => "WINDOW TITLE",
} }
} }
} }

View File

@ -1,7 +1,7 @@
use pumpkin_macros::packet;
use serde::Serialize;
use crate::slot::Slot; use crate::slot::Slot;
use crate::VarInt; use crate::VarInt;
use pumpkin_macros::packet;
use serde::Serialize;
#[derive(Serialize)] #[derive(Serialize)]
#[packet(0x13)] #[packet(0x13)]
@ -10,10 +10,9 @@ pub struct CSetContainerContent<'a> {
state_id: VarInt, state_id: VarInt,
count: VarInt, count: VarInt,
slot_data: &'a [Slot], slot_data: &'a [Slot],
carried_item: &'a Slot carried_item: &'a Slot,
} }
impl<'a> CSetContainerContent<'a> { 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 { Self {
@ -21,7 +20,7 @@ impl<'a> CSetContainerContent<'a> {
state_id, state_id,
count: slots.len().try_into().unwrap(), count: slots.len().try_into().unwrap(),
slot_data: slots, slot_data: slots,
carried_item carried_item,
} }
} }
} }

View File

@ -22,6 +22,7 @@ mod c_player_chat_message;
mod c_player_info_update; mod c_player_info_update;
mod c_player_remove; mod c_player_remove;
mod c_remove_entities; mod c_remove_entities;
mod c_set_container_content;
mod c_set_held_item; mod c_set_held_item;
mod c_set_title; mod c_set_title;
mod c_spawn_player; mod c_spawn_player;
@ -33,7 +34,6 @@ mod c_update_entity_pos;
mod c_update_entity_rot; mod c_update_entity_rot;
mod c_worldevent; mod c_worldevent;
mod player_action; mod player_action;
mod c_set_container_content;
pub use c_acknowledge_block::*; pub use c_acknowledge_block::*;
pub use c_actionbar::*; pub use c_actionbar::*;
@ -59,6 +59,7 @@ pub use c_player_chat_message::*;
pub use c_player_info_update::*; pub use c_player_info_update::*;
pub use c_player_remove::*; pub use c_player_remove::*;
pub use c_remove_entities::*; pub use c_remove_entities::*;
pub use c_set_container_content::*;
pub use c_set_held_item::*; pub use c_set_held_item::*;
pub use c_set_title::*; pub use c_set_title::*;
pub use c_spawn_player::*; pub use c_spawn_player::*;
@ -70,4 +71,3 @@ pub use c_update_entity_pos::*;
pub use c_update_entity_rot::*; pub use c_update_entity_rot::*;
pub use c_worldevent::*; pub use c_worldevent::*;
pub use player_action::*; pub use player_action::*;
pub use c_set_container_content::*;

View File

@ -1,7 +1,10 @@
use crate::VarInt; use crate::VarInt;
use pumpkin_world::item::Item; use pumpkin_world::item::Item;
use serde::{de::{self, SeqAccess, Visitor}, Deserialize, Serialize, Serializer};
use serde::ser::SerializeSeq; use serde::ser::SerializeSeq;
use serde::{
de::{self, SeqAccess, Visitor},
Deserialize, Serialize, Serializer,
};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[allow(dead_code)] #[allow(dead_code)]
@ -122,7 +125,6 @@ impl Serialize for Slot {
s.end() s.end()
} }
} }
} }
} }
} }

View File

@ -18,7 +18,10 @@ use pumpkin_protocol::{
client::{ client::{
config::CConfigDisconnect, config::CConfigDisconnect,
login::CLoginDisconnect, login::CLoginDisconnect,
play::{CGameEvent, CPlayDisconnect, CSyncPlayerPostion, CSystemChatMessge, CSetContainerContent}, play::{
CGameEvent, CPlayDisconnect, CSetContainerContent, CSyncPlayerPostion,
CSystemChatMessge,
},
}, },
packet_decoder::PacketDecoder, packet_decoder::PacketDecoder,
packet_encoder::PacketEncoder, packet_encoder::PacketEncoder,
@ -38,13 +41,13 @@ use pumpkin_protocol::{
}; };
use pumpkin_text::TextComponent; use pumpkin_text::TextComponent;
use std::io::Read;
use thiserror::Error;
use pumpkin_inventory::WindowType; use pumpkin_inventory::WindowType;
use pumpkin_protocol::bytebuf::ByteBuffer; use pumpkin_protocol::bytebuf::ByteBuffer;
use pumpkin_protocol::client::play::COpenScreen; use pumpkin_protocol::client::play::COpenScreen;
use pumpkin_protocol::slot::Slot; use pumpkin_protocol::slot::Slot;
use pumpkin_world::item::Item; use pumpkin_world::item::Item;
use std::io::Read;
use thiserror::Error;
pub mod authentication; pub mod authentication;
mod client_packet; mod client_packet;
@ -180,29 +183,57 @@ impl Client {
self.send_packet(&CGameEvent::new(3, gamemode.to_f32().unwrap())); 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>) { pub fn open_container(
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(); &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())); 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); 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 player = self.player.as_ref().unwrap();
let slots: Vec<Slot> = {if let Some(mut items) = items { let slots: Vec<Slot> = {
if let Some(mut items) = items {
items.extend(player.inventory.slots()); items.extend(player.inventory.slots());
items items
} else { } else {
player.inventory.slots() player.inventory.slots()
}.into_iter() }
.into_iter()
.map(|item| { .map(|item| {
if let Some(item) = item { if let Some(item) = item {
Slot::from(item) Slot::from(item)
} else { } else {
Slot::empty() Slot::empty()
} }
}).collect()}; })
.collect()
};
let carried_item = { let carried_item = {
if let Some(item) = carried_item { if let Some(item) = carried_item {
@ -211,7 +242,12 @@ impl Client {
Slot::empty() 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) { pub async fn process_packets(&mut self, server: &mut Server) {