use bevy::prelude::Event;
use fmc_protocol_derive::{ClientBound, ServerBound};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(ClientBound, Event, Serialize, Deserialize, Debug, Clone)]
pub struct InterfaceVisibilityUpdate {
pub interface_path: String,
pub visible: bool,
}
#[derive(ClientBound, Event, Serialize, Deserialize, Debug, Clone, Default)]
pub struct InterfaceNodeVisibilityUpdate {
pub updates: Vec<(String, bool)>,
}
impl InterfaceNodeVisibilityUpdate {
pub fn set_hidden(&mut self, interface_path: String) {
self.updates.push((interface_path, false));
}
pub fn set_visible(&mut self, interface_path: String) {
self.updates.push((interface_path, true));
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ItemBox {
pub index: u32,
pub item_stack: ItemStack,
}
#[derive(Default, Serialize, Deserialize, Clone, Debug)]
pub struct ItemStack {
pub item_id: Option<u32>,
pub quantity: u32,
pub durability: Option<u32>,
pub description: Option<String>,
}
#[derive(ClientBound, Event, Serialize, Deserialize, Debug, Clone, Default)]
pub struct InterfaceItemBoxUpdate {
pub replace: bool,
pub updates: HashMap<String, Vec<ItemBox>>,
}
impl InterfaceItemBoxUpdate {
pub fn add_itembox(
&mut self,
name: &str,
item_box_id: u32,
item_id: u32,
quantity: u32,
durability: Option<u32>,
description: Option<&str>,
) {
if !self.updates.contains_key(name) {
self.updates.insert(name.to_owned(), Vec::new());
}
self.updates.get_mut(name).unwrap().push(ItemBox {
index: item_box_id,
item_stack: ItemStack {
item_id: Some(item_id),
quantity,
durability,
description: description.map(|x| x.to_owned()),
},
})
}
pub fn add_empty_itembox(&mut self, name: &str, item_box_id: u32) {
if !self.updates.contains_key(name) {
self.updates.insert(name.to_owned(), Vec::new());
}
self.updates.get_mut(name).unwrap().push(ItemBox {
index: item_box_id,
item_stack: ItemStack {
item_id: None,
quantity: 0,
durability: None,
description: None,
},
})
}
pub fn combine(&mut self, other: InterfaceItemBoxUpdate) {
for (interface_path, mut updates) in other.updates.into_iter() {
if self.updates.contains_key(&interface_path) {
self.updates
.get_mut(&interface_path)
.unwrap()
.append(&mut updates);
} else {
self.updates.insert(interface_path, updates);
}
}
}
}
#[derive(ServerBound, Serialize, Deserialize, Debug, Clone)]
pub enum InterfaceInteraction {
TakeItem {
interface_path: String,
index: u32,
quantity: u32,
},
PlaceItem {
interface_path: String,
index: u32,
quantity: u32,
},
Button {
interface_path: String,
},
}
#[derive(ServerBound, Serialize, Deserialize, Debug, Clone)]
pub struct InterfaceEquipItem {
pub interface_path: String,
pub index: u32,
}
#[derive(ClientBound, Event, Serialize, Deserialize, Debug, Clone, Default)]
pub struct InterfaceTextUpdate {
pub interface_path: String,
pub index: i32,
pub text: String,
pub font_size: f32,
pub color: String,
}
#[derive(ServerBound, Serialize, Deserialize, Debug, Clone, Default)]
pub struct InterfaceTextInput {
pub interface_path: String,
pub text: String,
}