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::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 } => {

View file

@ -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<C: Config> Client<C> {
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
/// has no effect if the client is already disconnected.
///