From 50dcbfeb6dc4f856d9145456a80f34ec0e9dabd1 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Fri, 9 Aug 2024 22:38:00 +0200 Subject: [PATCH] Add inventory support --- Cargo.lock | 9 ++++ Cargo.toml | 2 +- README.md | 1 + pumpkin-inventory/Cargo.toml | 8 ++++ pumpkin-inventory/src/lib.rs | 41 +++++++++++++++++++ .../src/client/play/c_open_screen.rs | 23 +++++++++++ pumpkin-protocol/src/client/play/mod.rs | 2 + pumpkin-registry/src/chat_type.rs | 6 +++ pumpkin-text/src/lib.rs | 2 +- pumpkin/Cargo.toml | 1 + pumpkin/src/client/client_packet.rs | 1 + pumpkin/src/client/player_packet.rs | 15 +++++-- 12 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 pumpkin-inventory/Cargo.toml create mode 100644 pumpkin-inventory/src/lib.rs create mode 100644 pumpkin-protocol/src/client/play/c_open_screen.rs diff --git a/Cargo.lock b/Cargo.lock index 9e11ea864..65dfa29a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1163,6 +1163,7 @@ dependencies = [ "num-derive", "num-traits", "pumpkin-entity", + "pumpkin-inventory", "pumpkin-protocol", "pumpkin-registry", "pumpkin-text", @@ -1189,6 +1190,14 @@ dependencies = [ "num-traits", ] +[[package]] +name = "pumpkin-inventory" +version = "0.1.0" +dependencies = [ + "num-derive", + "num-traits", +] + [[package]] name = "pumpkin-macros" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 7d819891f..c1b1034be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = [ "pumpkin-entity", "pumpkin-macros/", "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-text", "pumpkin-world", "pumpkin/" ] +members = [ "pumpkin-entity", "pumpkin-inventory", "pumpkin-macros/", "pumpkin-protocol/", "pumpkin-registry/", "pumpkin-world", "pumpkin/" ] [workspace.package] version = "0.1.0" diff --git a/README.md b/README.md index ba2b50a25..7ca197778 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Pumpkin is currently under heavy development. - [ ] Player Inventory - [ ] Player Attack - Server + - [x] Inventories - [x] Chat - [x] Commands diff --git a/pumpkin-inventory/Cargo.toml b/pumpkin-inventory/Cargo.toml new file mode 100644 index 000000000..f9f1b10d1 --- /dev/null +++ b/pumpkin-inventory/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "pumpkin-inventory" +version.workspace = true +edition.workspace = true + +[dependencies] +num-traits = "0.2" +num-derive = "0.4" \ No newline at end of file diff --git a/pumpkin-inventory/src/lib.rs b/pumpkin-inventory/src/lib.rs new file mode 100644 index 000000000..32a322a63 --- /dev/null +++ b/pumpkin-inventory/src/lib.rs @@ -0,0 +1,41 @@ +use num_derive::ToPrimitive; + +/// https://wiki.vg/Inventory +#[derive(Debug, ToPrimitive)] +pub enum WindowType { + // not used + Generic9x1, + // not used + Generic9x2, + // General-purpose 3-row inventory. Used by Chest, minecart with chest, ender chest, and barrel + Generic9x3, + // not used + Generic9x4, + // not used + Generic9x5, + // Used by large chests + Generic9x6, + // General-purpose 3-by-3 square inventory, used by Dispenser and Dropper + Generic3x3, + // General-purpose 3-by-3 square inventory, used by the Crafter + Craft3x3, + Anvil, + Beacon, + BlastFurance, + BrewingStand, + CraftingTable, + EnchantmentTable, + Furnace, + Grindstone, + // Hopper or minecart with hopper + Hopper, + Lectern, + Loom, + // Villager, Wandering Trader + Merchant, + ShulkerBox, + SmithingTable, + Smoker, + CartographyTable, + Stonecutter, +} diff --git a/pumpkin-protocol/src/client/play/c_open_screen.rs b/pumpkin-protocol/src/client/play/c_open_screen.rs new file mode 100644 index 000000000..0e0480584 --- /dev/null +++ b/pumpkin-protocol/src/client/play/c_open_screen.rs @@ -0,0 +1,23 @@ +use pumpkin_macros::packet; +use pumpkin_text::TextComponent; +use serde::Serialize; + +use crate::VarInt; + +#[derive(Serialize, Clone)] +#[packet(0x33)] +pub struct COpenScreen { + window_id: VarInt, + window_type: VarInt, + window_title: TextComponent, +} + +impl COpenScreen { + pub fn new(window_id: VarInt, window_type: VarInt, window_title: TextComponent) -> Self { + Self { + window_id, + window_type, + window_title, + } + } +} diff --git a/pumpkin-protocol/src/client/play/mod.rs b/pumpkin-protocol/src/client/play/mod.rs index c120926ee..664d22b40 100644 --- a/pumpkin-protocol/src/client/play/mod.rs +++ b/pumpkin-protocol/src/client/play/mod.rs @@ -6,6 +6,7 @@ mod c_entity_metadata; mod c_game_event; mod c_head_rot; mod c_login; +mod c_open_screen; mod c_play_disconnect; mod c_player_abilities; mod c_player_chat_message; @@ -28,6 +29,7 @@ pub use c_entity_metadata::*; pub use c_game_event::*; pub use c_head_rot::*; pub use c_login::*; +pub use c_open_screen::*; pub use c_play_disconnect::*; pub use c_player_abilities::*; pub use c_player_chat_message::*; diff --git a/pumpkin-registry/src/chat_type.rs b/pumpkin-registry/src/chat_type.rs index 8b1378917..aa70c24bb 100644 --- a/pumpkin-registry/src/chat_type.rs +++ b/pumpkin-registry/src/chat_type.rs @@ -1 +1,7 @@ +pub struct ChatType {} +pub struct Decoration { + translation_key: String, + // style: Option, + parameters: Vec, +} diff --git a/pumpkin-text/src/lib.rs b/pumpkin-text/src/lib.rs index f18c04188..1a9512bdc 100644 --- a/pumpkin-text/src/lib.rs +++ b/pumpkin-text/src/lib.rs @@ -231,4 +231,4 @@ impl Default for TextContent { fn default() -> Self { Self::Text { text: "".into() } } -} +} \ No newline at end of file diff --git a/pumpkin/Cargo.toml b/pumpkin/Cargo.toml index e4b726028..4288e8fef 100644 --- a/pumpkin/Cargo.toml +++ b/pumpkin/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] pumpkin-text = { path = "../pumpkin-text" } +pumpkin-inventory = { path = "../pumpkin-inventory"} pumpkin-world = { path = "../pumpkin-world"} pumpkin-entity = { path = "../pumpkin-entity"} pumpkin-protocol = { path = "../pumpkin-protocol"} diff --git a/pumpkin/src/client/client_packet.rs b/pumpkin/src/client/client_packet.rs index c8cefdf28..fd87d5711 100644 --- a/pumpkin/src/client/client_packet.rs +++ b/pumpkin/src/client/client_packet.rs @@ -134,6 +134,7 @@ impl Client { } } for ele in self.gameprofile.as_ref().unwrap().properties.clone() { + // todo, use this unpack_textures(ele, &server.advanced_config.authentication.textures); } diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 9163561a4..95d429338 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -1,14 +1,15 @@ use num_traits::FromPrimitive; +use pumpkin_inventory::WindowType; use pumpkin_protocol::{ client::play::{ - Animation, CEntityAnimation, CHeadRot, CSystemChatMessge, CUpdateEntityPos, + Animation, CEntityAnimation, CHeadRot, COpenScreen, CSystemChatMessge, CUpdateEntityPos, CUpdateEntityPosRot, CUpdateEntityRot, }, server::play::{ SChatCommand, SChatMessage, SConfirmTeleport, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation, SPlayerRotation, SSwingArm, - }, -}; + }, VarInt, + }; use pumpkin_text::TextComponent; use crate::{ @@ -189,6 +190,14 @@ impl Client { pub fn handle_chat_message(&mut self, server: &mut Server, chat_message: SChatMessage) { let message = chat_message.message; + server.broadcast_packet( + self, + COpenScreen::new( + VarInt(0), + VarInt(WindowType::CraftingTable as i32), + TextComponent::from("Test Crafter"), + ), + ); // TODO: filter message & validation let gameprofile = self.gameprofile.as_ref().unwrap(); dbg!("got message");