diff --git a/examples/chest.rs b/examples/chest.rs index a60a590..fb8f810 100644 --- a/examples/chest.rs +++ b/examples/chest.rs @@ -10,12 +10,9 @@ use valence::client::{handle_event_default, ClientEvent, Hand}; use valence::config::{Config, ServerListPing}; use valence::dimension::{Dimension, DimensionId}; use valence::entity::{EntityId, EntityKind}; -use valence::inventory::{ - ConfigurableInventory, Inventory, InventoryId, PlayerInventory, WindowInventory, -}; +use valence::inventory::{ConfigurableInventory, Inventory, InventoryId, PlayerInventory}; use valence::item::{ItemKind, ItemStack}; use valence::player_list::PlayerListId; -use valence::protocol::packets::s2c::play::OpenScreen; use valence::protocol::{SlotId, VarInt}; use valence::server::{Server, SharedServer, ShutdownResult}; use valence::text::{Color, TextFormat}; @@ -208,15 +205,13 @@ impl Config for Game { && world.chunks.block_state(location) == Some(BlockState::CHEST) { client.send_message("Opening chest!"); - let window = WindowInventory::new(1, server.state.chest); - client.send_packet(OpenScreen { - window_id: VarInt(window.window_id.into()), - window_type: VarInt(2), - window_title: "Extra".italic() + client.open_inventory( + &server.inventories, + server.state.chest, + "Extra".italic() + " Chesty".not_italic().bold().color(Color::RED) + " Chest".not_italic(), - }); - client.open_inventory = Some(window); + ); } } ClientEvent::CloseScreen { window_id } => { diff --git a/src/client.rs b/src/client.rs index 151bf7b..1ec2494 100644 --- a/src/client.rs +++ b/src/client.rs @@ -22,7 +22,7 @@ use crate::entity::{ }; use crate::ident::Ident; use crate::inventory::{ - Inventories, Inventory, InventoryDirtyable, PlayerInventory, WindowInventory, + Inventories, Inventory, InventoryDirtyable, InventoryId, PlayerInventory, WindowInventory, }; use crate::item::ItemStack; use crate::player_list::{PlayerListId, PlayerLists}; @@ -34,12 +34,12 @@ pub use crate::protocol::packets::s2c::play::SetTitleAnimationTimes; use crate::protocol::packets::s2c::play::{ AcknowledgeBlockChange, ClearTitles, CombatDeath, CustomSoundEffect, DisconnectPlay, EntityAnimationS2c, EntityAttributesProperty, EntityEvent, GameEvent, GameStateChangeReason, - KeepAliveS2c, LoginPlay, PlayerPositionLookFlags, RemoveEntities, ResourcePackS2c, Respawn, - S2cPlayPacket, SetActionBarText, SetCenterChunk, SetContainerContent, SetDefaultSpawnPosition, - SetEntityMetadata, SetEntityVelocity, SetExperience, SetHeadRotation, SetHealth, - SetRenderDistance, SetSubtitleText, SetTitleText, SoundCategory, SynchronizePlayerPosition, - SystemChatMessage, TeleportEntity, UnloadChunk, UpdateAttributes, UpdateEntityPosition, - UpdateEntityPositionAndRotation, UpdateEntityRotation, UpdateTime, + KeepAliveS2c, LoginPlay, OpenScreen, PlayerPositionLookFlags, RemoveEntities, ResourcePackS2c, + Respawn, S2cPlayPacket, SetActionBarText, SetCenterChunk, SetContainerContent, + SetDefaultSpawnPosition, SetEntityMetadata, SetEntityVelocity, SetExperience, SetHeadRotation, + SetHealth, SetRenderDistance, SetSubtitleText, SetTitleText, SoundCategory, + SynchronizePlayerPosition, SystemChatMessage, TeleportEntity, UnloadChunk, UpdateAttributes, + UpdateEntityPosition, UpdateEntityPositionAndRotation, UpdateEntityRotation, UpdateTime, }; use crate::protocol::{BoundedInt, BoundedString, ByteAngle, RawBytes, SlotId, VarInt}; use crate::server::{C2sPacketChannels, NewClientData, S2cPlayMessage, SharedServer}; @@ -786,6 +786,24 @@ impl Client { self.inventory.consume_one(self.selected_hotbar_slot); } + /// Makes the client open a window displaying the given inventory. + pub fn open_inventory( + &mut self, + inventories: &Inventories, + id: InventoryId, + window_title: impl Into, + ) { + if let Some(inv) = inventories.get(id) { + let window = WindowInventory::new(1, id); + self.send_packet(OpenScreen { + window_id: VarInt(window.window_id.into()), + window_type: inv.window_type, + window_title: window_title.into(), + }); + self.open_inventory = Some(window); + } + } + /// Disconnects this client from the server with the provided reason. This /// has no effect if the client is already disconnected. ///