1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 23:44:38 +00:00

put the inventories in Client

This commit is contained in:
Ubuntu 2022-12-08 21:01:25 +00:00
commit 48a9221300
6 changed files with 82 additions and 33 deletions

1
Cargo.lock generated
View file

@ -221,6 +221,7 @@ dependencies = [
"azalea-chat",
"azalea-core",
"azalea-crypto",
"azalea-inventory",
"azalea-physics",
"azalea-protocol",
"azalea-world",

View file

@ -11,20 +11,21 @@ version = "0.4.0"
[dependencies]
anyhow = "1.0.59"
async-trait = "0.1.58"
azalea-auth = {path = "../azalea-auth", version = "0.4.0"}
azalea-block = {path = "../azalea-block", version = "0.4.0"}
azalea-chat = {path = "../azalea-chat", version = "0.4.0"}
azalea-core = {path = "../azalea-core", version = "0.4.0"}
azalea-crypto = {path = "../azalea-crypto", version = "0.4.0"}
azalea-physics = {path = "../azalea-physics", version = "0.4.0"}
azalea-protocol = {path = "../azalea-protocol", version = "0.4.0"}
azalea-world = {path = "../azalea-world", version = "0.4.0"}
azalea-auth = { path = "../azalea-auth", version = "0.4.0" }
azalea-block = { path = "../azalea-block", version = "0.4.0" }
azalea-chat = { path = "../azalea-chat", version = "0.4.0" }
azalea-core = { path = "../azalea-core", version = "0.4.0" }
azalea-crypto = { path = "../azalea-crypto", version = "0.4.0" }
azalea-physics = { path = "../azalea-physics", version = "0.4.0" }
azalea-protocol = { path = "../azalea-protocol", version = "0.4.0" }
azalea-inventory = { path = "../azalea-inventory", version = "0.1.0" }
azalea-world = { path = "../azalea-world", version = "0.4.0" }
log = "0.4.17"
nohash-hasher = "0.2.0"
once_cell = "1.16.0"
parking_lot = {version = "^0.12.1", features = ["deadlock_detection"]}
parking_lot = { version = "^0.12.1", features = ["deadlock_detection"] }
regex = "1.7.0"
thiserror = "^1.0.34"
tokio = {version = "^1.21.2", features = ["sync"]}
tokio = { version = "^1.21.2", features = ["sync"] }
typemap_rev = "0.2.0"
uuid = "^1.1.2"

View file

@ -108,6 +108,9 @@ pub struct Client {
/// A map of player uuids to their information in the tab list
pub players: Arc<RwLock<HashMap<Uuid, PlayerInfo>>>,
tasks: Arc<Mutex<Vec<JoinHandle<()>>>>,
pub inventory_menu: Arc<Mutex<azalea_inventory::Player>>,
pub container_menu: Arc<Mutex<Option<azalea_inventory::Menu>>>,
}
#[derive(Default)]
@ -191,6 +194,11 @@ impl Client {
plugins: Arc::new(PluginStates::default()),
players: Arc::new(RwLock::new(HashMap::new())),
tasks: Arc::new(Mutex::new(Vec::new())),
// empty by default
inventory_menu: Arc::new(Mutex::new(azalea_inventory::Player::default())),
// we don't have any container open when we spawn
container_menu: Arc::new(Mutex::new(None)),
}
}

View file

@ -1,18 +1,30 @@
//! Generate the `enum menu` and nothing else. Implementations are in
//! impl_menu.rs
use crate::parse_macro::{DeclareMenus, Menu};
use crate::parse_macro::{DeclareMenus, Field, Menu};
use proc_macro2::TokenStream;
use quote::quote;
pub fn generate(input: &DeclareMenus) -> TokenStream {
let mut variants = quote! {};
let mut player_fields = None;
for menu in &input.menus {
variants.extend(generate_variant_for_menu(menu));
if menu.name.to_string() == "Player" {
player_fields = Some(generate_fields(&menu.fields, true));
} else {
variants.extend(generate_variant_for_menu(menu));
}
}
let player_fields = player_fields.expect("Player variant must be present");
quote! {
#[derive(Clone, Debug, Default)]
pub struct Player {
#player_fields
}
pub enum Menu {
Player(Player),
#variants
}
}
@ -27,21 +39,7 @@ pub fn generate(input: &DeclareMenus) -> TokenStream {
/// },
fn generate_variant_for_menu(menu: &Menu) -> TokenStream {
let name = &menu.name;
let mut fields = quote! {};
for field in &menu.fields {
let field_name = &field.name;
let field_length = field.length;
let field_type = if matches!(field_name.to_string().as_str(), "inventory" | "player") {
quote! { std::sync::Arc<[Slot; #field_length ]> }
} else if field.length == 1 {
quote! { Slot }
} else {
quote! { [Slot; #field_length ] }
};
fields.extend(quote! { #field_name: #field_type, })
}
let fields = generate_fields(&menu.fields, false);
quote! {
#name {
@ -49,3 +47,22 @@ fn generate_variant_for_menu(menu: &Menu) -> TokenStream {
},
}
}
fn generate_fields(fields: &[Field], public: bool) -> TokenStream {
let mut generated_fields = quote! {};
for field in fields {
let field_length = field.length;
let field_type = if field.length == 1 {
quote! { Slot }
} else {
quote! { SlotList<#field_length> }
};
let field_name = &field.name;
if public {
generated_fields.extend(quote! { pub #field_name: #field_type, })
} else {
generated_fields.extend(quote! { #field_name: #field_type, })
}
}
generated_fields
}

View file

@ -61,10 +61,13 @@ pub fn generate_match_variant_for_menu(menu: &Menu) -> TokenStream {
});
}
let matcher = if menu.name.to_string() == "Player" {
quote! { (Player { #menu_field_names }) }
} else {
quote! { { #menu_field_names } }
};
quote! {
Menu::#menu_name {
#menu_field_names
} => {
Menu::#menu_name #matcher => {
match i {
#match_arms
_ => return None,

View file

@ -1,6 +1,26 @@
use std::ops::Deref;
use azalea_core::Slot;
use azalea_inventory_macros::declare_menus;
// TODO: remove this here and in azalea-inventory-macros when rust makes
// Default be implemented for all array sizes (since right now it's only up to
// 32)
#[derive(Debug, Clone)]
pub struct SlotList<const N: usize>([Slot; N]);
impl<const N: usize> Deref for SlotList<N> {
type Target = [Slot; N];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<const N: usize> Default for SlotList<N> {
fn default() -> Self {
SlotList([(); N].map(|_| Slot::Empty))
}
}
// the player inventory part is always the last 36 slots (except in the Player
// menu), so we don't have to explicitly specify it
@ -10,9 +30,8 @@ use azalea_inventory_macros::declare_menus;
// pub inventory: Arc<[Slot; 36]>
// }
// Generate an `enum Menu` and `impl Menu`.
// if the `inventory` field is present, then the `player` field doesn't get
// implicitly added
// Generate a `struct Player`, `enum Menu`, and `impl Menu`.
// a "player" field gets implicitly added with the player inventory
declare_menus! {
Player {
craft_result: 1,