add open_inventory to clients (#119)

This commit is contained in:
Carson McManus 2022-10-16 17:12:36 -04:00 committed by GitHub
parent 0e58bfbb77
commit 9faac7a0fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 18 deletions

View file

@ -10,12 +10,9 @@ use valence::client::{handle_event_default, ClientEvent, Hand};
use valence::config::{Config, ServerListPing}; use valence::config::{Config, ServerListPing};
use valence::dimension::{Dimension, DimensionId}; use valence::dimension::{Dimension, DimensionId};
use valence::entity::{EntityId, EntityKind}; use valence::entity::{EntityId, EntityKind};
use valence::inventory::{ use valence::inventory::{ConfigurableInventory, Inventory, InventoryId, PlayerInventory};
ConfigurableInventory, Inventory, InventoryId, PlayerInventory, WindowInventory,
};
use valence::item::{ItemKind, ItemStack}; use valence::item::{ItemKind, ItemStack};
use valence::player_list::PlayerListId; use valence::player_list::PlayerListId;
use valence::protocol::packets::s2c::play::OpenScreen;
use valence::protocol::{SlotId, VarInt}; use valence::protocol::{SlotId, VarInt};
use valence::server::{Server, SharedServer, ShutdownResult}; use valence::server::{Server, SharedServer, ShutdownResult};
use valence::text::{Color, TextFormat}; use valence::text::{Color, TextFormat};
@ -208,15 +205,13 @@ impl Config for Game {
&& world.chunks.block_state(location) == Some(BlockState::CHEST) && world.chunks.block_state(location) == Some(BlockState::CHEST)
{ {
client.send_message("Opening chest!"); client.send_message("Opening chest!");
let window = WindowInventory::new(1, server.state.chest); client.open_inventory(
client.send_packet(OpenScreen { &server.inventories,
window_id: VarInt(window.window_id.into()), server.state.chest,
window_type: VarInt(2), "Extra".italic()
window_title: "Extra".italic()
+ " Chesty".not_italic().bold().color(Color::RED) + " Chesty".not_italic().bold().color(Color::RED)
+ " Chest".not_italic(), + " Chest".not_italic(),
}); );
client.open_inventory = Some(window);
} }
} }
ClientEvent::CloseScreen { window_id } => { ClientEvent::CloseScreen { window_id } => {

View file

@ -22,7 +22,7 @@ use crate::entity::{
}; };
use crate::ident::Ident; use crate::ident::Ident;
use crate::inventory::{ use crate::inventory::{
Inventories, Inventory, InventoryDirtyable, PlayerInventory, WindowInventory, Inventories, Inventory, InventoryDirtyable, InventoryId, PlayerInventory, WindowInventory,
}; };
use crate::item::ItemStack; use crate::item::ItemStack;
use crate::player_list::{PlayerListId, PlayerLists}; use crate::player_list::{PlayerListId, PlayerLists};
@ -34,12 +34,12 @@ pub use crate::protocol::packets::s2c::play::SetTitleAnimationTimes;
use crate::protocol::packets::s2c::play::{ use crate::protocol::packets::s2c::play::{
AcknowledgeBlockChange, ClearTitles, CombatDeath, CustomSoundEffect, DisconnectPlay, AcknowledgeBlockChange, ClearTitles, CombatDeath, CustomSoundEffect, DisconnectPlay,
EntityAnimationS2c, EntityAttributesProperty, EntityEvent, GameEvent, GameStateChangeReason, EntityAnimationS2c, EntityAttributesProperty, EntityEvent, GameEvent, GameStateChangeReason,
KeepAliveS2c, LoginPlay, PlayerPositionLookFlags, RemoveEntities, ResourcePackS2c, Respawn, KeepAliveS2c, LoginPlay, OpenScreen, PlayerPositionLookFlags, RemoveEntities, ResourcePackS2c,
S2cPlayPacket, SetActionBarText, SetCenterChunk, SetContainerContent, SetDefaultSpawnPosition, Respawn, S2cPlayPacket, SetActionBarText, SetCenterChunk, SetContainerContent,
SetEntityMetadata, SetEntityVelocity, SetExperience, SetHeadRotation, SetHealth, SetDefaultSpawnPosition, SetEntityMetadata, SetEntityVelocity, SetExperience, SetHeadRotation,
SetRenderDistance, SetSubtitleText, SetTitleText, SoundCategory, SynchronizePlayerPosition, SetHealth, SetRenderDistance, SetSubtitleText, SetTitleText, SoundCategory,
SystemChatMessage, TeleportEntity, UnloadChunk, UpdateAttributes, UpdateEntityPosition, SynchronizePlayerPosition, SystemChatMessage, TeleportEntity, UnloadChunk, UpdateAttributes,
UpdateEntityPositionAndRotation, UpdateEntityRotation, UpdateTime, UpdateEntityPosition, UpdateEntityPositionAndRotation, UpdateEntityRotation, UpdateTime,
}; };
use crate::protocol::{BoundedInt, BoundedString, ByteAngle, RawBytes, SlotId, VarInt}; use crate::protocol::{BoundedInt, BoundedString, ByteAngle, RawBytes, SlotId, VarInt};
use crate::server::{C2sPacketChannels, NewClientData, S2cPlayMessage, SharedServer}; use crate::server::{C2sPacketChannels, NewClientData, S2cPlayMessage, SharedServer};
@ -786,6 +786,24 @@ impl<C: Config> Client<C> {
self.inventory.consume_one(self.selected_hotbar_slot); 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<Text>,
) {
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 /// Disconnects this client from the server with the provided reason. This
/// has no effect if the client is already disconnected. /// has no effect if the client is already disconnected.
/// ///