#[cfg(feature = "v1_8")]
use ghast_protocol::v1_8::play::{clientbound as cb8, serverbound as sb8};
#[cfg(feature = "v1_12")]
use ghast_protocol::v1_12_2::play::{clientbound as cb12, serverbound as sb12};
#[cfg(feature = "v1_16_5")]
use ghast_protocol::v1_16_5::play::{clientbound as cb16, serverbound as sb16};
#[allow(unused_imports)]
use crate::inventory::{ClickPacket, Inventory, Outgoing};
#[allow(unused_imports)]
use crate::stack::{from_legacy_slot, from_modern_slot, to_legacy_slot, to_modern_slot};
#[allow(unused_imports)]
use crate::window::WindowKind;
impl ClickPacket {
#[cfg(feature = "v1_8")]
#[must_use]
pub fn to_v1_8(&self) -> sb8::WindowClick {
sb8::WindowClick {
window_id: self.window_id,
slot: self.slot,
mouse_button: self.button as i8,
action: self.action,
mode: self.mode as i8,
item: to_legacy_slot(self.item.as_ref()),
}
}
#[cfg(feature = "v1_12")]
#[must_use]
pub fn to_v1_12_2(&self) -> sb12::WindowClick {
sb12::WindowClick {
window_id: self.window_id,
slot: self.slot,
mouse_button: self.button as i8,
action: self.action,
mode: self.mode as i8,
item: to_legacy_slot(self.item.as_ref()),
}
}
#[cfg(feature = "v1_16_5")]
#[must_use]
pub fn to_v1_16_5(&self) -> sb16::WindowClick {
sb16::WindowClick {
window_id: self.window_id,
slot: self.slot,
mouse_button: self.button as i8,
action: self.action,
mode: self.mode as i8,
item: to_modern_slot(self.item.as_ref()),
}
}
}
impl Outgoing {
#[cfg(feature = "v1_8")]
#[must_use]
pub fn to_v1_8(&self) -> Option<sb8::ServerboundPlayPacket> {
Some(match self {
Outgoing::Click(c) => sb8::ServerboundPlayPacket::WindowClick(c.to_v1_8()),
Outgoing::Confirm { window_id, action } => {
sb8::ServerboundPlayPacket::Transaction(sb8::Transaction {
window_id: *window_id,
action: *action,
accepted: true,
})
}
Outgoing::CloseWindow(id) => {
sb8::ServerboundPlayPacket::CloseWindow(sb8::CloseWindow { window_id: *id })
}
Outgoing::OpenInventory => {
sb8::ServerboundPlayPacket::ClientCommand(sb8::ClientCommand {
payload: sb8::ClientCommandPayload(2),
})
}
Outgoing::ContainerClick(_)
| Outgoing::Creative(_)
| Outgoing::RenameItem(_)
| Outgoing::ButtonClick { .. }
| Outgoing::SelectTrade(_) => return None,
})
}
#[cfg(feature = "v1_12")]
#[must_use]
pub fn to_v1_12_2(&self) -> Option<sb12::ServerboundPlayPacket> {
Some(match self {
Outgoing::Click(c) => sb12::ServerboundPlayPacket::WindowClick(c.to_v1_12_2()),
Outgoing::Confirm { window_id, action } => {
sb12::ServerboundPlayPacket::Transaction(sb12::Transaction {
window_id: *window_id,
action: *action,
accepted: true,
})
}
Outgoing::CloseWindow(id) => {
sb12::ServerboundPlayPacket::CloseWindow(sb12::CloseWindow { window_id: *id })
}
Outgoing::OpenInventory
| Outgoing::ContainerClick(_)
| Outgoing::Creative(_)
| Outgoing::RenameItem(_)
| Outgoing::ButtonClick { .. }
| Outgoing::SelectTrade(_) => {
return None;
}
})
}
#[cfg(feature = "v1_16_5")]
#[must_use]
pub fn to_v1_16_5(&self) -> Option<sb16::ServerboundPlayPacket> {
Some(match self {
Outgoing::Click(c) => sb16::ServerboundPlayPacket::WindowClick(c.to_v1_16_5()),
Outgoing::Confirm { window_id, action } => {
sb16::ServerboundPlayPacket::Transaction(sb16::Transaction {
window_id: *window_id,
action: *action,
accepted: true,
})
}
Outgoing::CloseWindow(id) => {
sb16::ServerboundPlayPacket::CloseWindow(sb16::CloseWindow { window_id: *id })
}
Outgoing::OpenInventory
| Outgoing::ContainerClick(_)
| Outgoing::Creative(_)
| Outgoing::RenameItem(_)
| Outgoing::ButtonClick { .. }
| Outgoing::SelectTrade(_) => {
return None;
}
})
}
}
impl Inventory {
#[cfg(feature = "v1_8")]
pub fn apply_v1_8(&mut self, p: &cb8::ClientboundPlayPacket) -> Option<Outgoing> {
use cb8::ClientboundPlayPacket as P;
match p {
P::Login(_) | P::Respawn(_) => self.reset(),
P::OpenWindow(w) => self.on_open_window(
i32::from(w.window_id),
WindowKind::from_v1_8(&w.inventory_type, w.slot_count),
w.window_title.clone(),
),
P::CloseWindow(_) => self.on_close_window(),
P::WindowItems(w) => self.on_window_items_legacy(i32::from(w.window_id), &w.items),
P::SetSlot(s) => {
self.on_set_slot(
i32::from(s.window_id),
s.slot,
from_legacy_slot(s.item.clone()),
);
}
P::Transaction(t) => return self.on_transaction(t.window_id, t.action, t.accepted),
P::HeldItemSlot(h) => self.on_held_slot(h.slot),
P::CraftProgressBar(c) => {
self.on_window_property(i32::from(c.window_id), c.property, c.value);
}
_ => {}
}
None
}
#[cfg(feature = "v1_12")]
pub fn apply_v1_12_2(&mut self, p: &cb12::ClientboundPlayPacket) -> Option<Outgoing> {
use cb12::ClientboundPlayPacket as P;
match p {
P::Login(_) | P::Respawn(_) => self.reset(),
P::OpenWindow(w) => self.on_open_window(
i32::from(w.window_id),
WindowKind::from_v1_12_2(&w.inventory_type, w.slot_count),
w.window_title.clone(),
),
P::CloseWindow(_) => self.on_close_window(),
P::WindowItems(w) => self.on_window_items_legacy(i32::from(w.window_id), &w.items),
P::SetSlot(s) => {
self.on_set_slot(
i32::from(s.window_id),
s.slot,
from_legacy_slot(s.item.clone()),
);
}
P::Transaction(t) => return self.on_transaction(t.window_id, t.action, t.accepted),
P::HeldItemSlot(h) => self.on_held_slot(h.slot),
P::CraftProgressBar(c) => {
self.on_window_property(i32::from(c.window_id), c.property, c.value);
}
_ => {}
}
None
}
#[cfg(feature = "v1_16_5")]
pub fn apply_v1_16_5(&mut self, p: &cb16::ClientboundPlayPacket) -> Option<Outgoing> {
use cb16::ClientboundPlayPacket as P;
match p {
P::Login(_) | P::Respawn(_) => self.reset(),
P::OpenWindow(w) => self.on_open_window(
w.window_id,
WindowKind::from_v1_16_5(w.inventory_type),
w.window_title.clone(),
),
P::OpenHorseWindow(w) => self.on_open_window(
i32::from(w.window_id),
u8::try_from(w.nb_slots)
.ok()
.map(|slots| WindowKind::Horse { slots }),
bytestring::ByteString::new(),
),
P::CloseWindow(_) => self.on_close_window(),
P::WindowItems(w) => self.on_window_items(
i32::from(w.window_id),
w.items.iter().cloned().map(from_modern_slot),
),
P::SetSlot(s) => {
self.on_set_slot(
i32::from(s.window_id),
s.slot,
from_modern_slot(s.item.clone()),
);
}
P::Transaction(t) => return self.on_transaction(t.window_id, t.action, t.accepted),
P::HeldItemSlot(h) => self.on_held_slot(h.slot),
P::CraftProgressBar(c) => {
self.on_window_property(i32::from(c.window_id), c.property, c.value);
}
P::DeclareRecipes(d) => self.on_declare_recipes(crate::RecipeBook::from_v1_16_5(d)),
_ => {}
}
None
}
}
#[cfg(feature = "v26_1")]
mod v26_1 {
use bytes::BytesMut;
use ghast_protocol::codec::{Encode, Reader};
use ghast_protocol::types::slot::v775::ItemStack;
use ghast_protocol::v26_1::play::clientbound::{self as cb, ClientboundPlayPacket};
use ghast_protocol::v26_1::play::serverbound as sb;
use ghast_protocol::{DecodeError, RawPacket};
use crate::inventory::{ContainerClick, CreativePacket, Inventory, Outgoing};
use crate::stack::Stack;
use crate::v26_1::{HashRegistries, hashed_slot};
pub const PACKET_IDS_26_1: &[i32] = &[
cb::id::LOGIN,
cb::id::RESPAWN,
cb::id::OPEN_WINDOW,
cb::id::CLOSE_WINDOW,
cb::id::WINDOW_ITEMS,
cb::id::SET_SLOT,
cb::id::SET_CURSOR_ITEM,
cb::id::SET_PLAYER_INVENTORY,
cb::id::HELD_ITEM_SLOT,
cb::id::CRAFT_PROGRESS_BAR,
cb::id::DECLARE_RECIPES,
cb::id::ABILITIES,
cb::id::EXPERIENCE,
cb::id::TAGS,
cb::id::TRADE_LIST,
];
fn slot(r: &mut Reader<'_>) -> Result<Option<Stack>, DecodeError> {
Ok(ItemStack::read(r)?.map(Stack::from_v775))
}
fn from_generated(s: &ghast_protocol::defs::p775::types::Slot) -> Option<Stack> {
let mut w = BytesMut::new();
s.encode(&mut w).ok()?;
let b = w.freeze();
ItemStack::read(&mut Reader::new(&b))
.ok()?
.map(Stack::from_v775)
}
impl ContainerClick {
#[must_use]
pub fn to_v26_1(&self, reg: &HashRegistries) -> sb::WindowClick {
sb::WindowClick {
window_id: self.window_id,
state_id: self.state_id,
slot: self.slot,
mouse_button: self.button as i8,
mode: i32::from(self.mode),
changed_slots: self
.changed
.iter()
.map(|(location, s)| sb::WindowClickChangedSlots {
location: *location,
item: hashed_slot(s.as_ref(), reg).0,
})
.collect(),
cursor_item: hashed_slot(self.carried.as_ref(), reg).0,
}
}
}
impl CreativePacket {
#[must_use]
pub fn to_v26_1(&self) -> sb::ServerboundPlayPacket {
use ghast_protocol::defs::p775::types::{
SlotComponentType, UntrustedSlot, UntrustedSlotBodyDefault,
UntrustedSlotBodyDefaultRemoveComponents, UntrustedSlotComponent,
};
match self {
CreativePacket::Swing => {
sb::ServerboundPlayPacket::ArmAnimation(sb::ArmAnimation {
hand: sb::ArmAnimationHand(0),
})
}
CreativePacket::SetSlot { slot, item } => {
let item = match item.as_ref().filter(|s| s.count > 0 && s.id != 0) {
None => UntrustedSlot {
item_count: 0,
body: None,
},
Some(s) => {
let mut components: Vec<UntrustedSlotComponent> = Vec::new();
let mut removed: Vec<i32> = Vec::new();
if let Some(p) = s.components() {
for (t, b) in p.iter() {
match components.iter_mut().find(|c| c.r#type.0 == t) {
Some(c) => c.data = b,
None => components.push(UntrustedSlotComponent {
r#type: SlotComponentType(t),
data: b,
}),
}
}
removed.extend(p.removed());
}
components.sort_by_key(|c| c.r#type.0);
removed.sort_unstable();
removed.dedup();
UntrustedSlot {
item_count: s.count,
body: Some(UntrustedSlotBodyDefault {
item_id: s.id,
added_component_count: components.len() as i32,
removed_component_count: removed.len() as i32,
components,
remove_components: removed
.into_iter()
.map(|t| UntrustedSlotBodyDefaultRemoveComponents {
r#type: SlotComponentType(t),
})
.collect(),
}),
}
}
};
sb::ServerboundPlayPacket::SetCreativeSlot(sb::SetCreativeSlot {
slot: *slot,
item,
})
}
}
}
}
impl Outgoing {
#[must_use]
pub fn to_v26_1(&self, reg: &HashRegistries) -> Option<sb::ServerboundPlayPacket> {
Some(match self {
Outgoing::ContainerClick(c) => {
sb::ServerboundPlayPacket::WindowClick(c.to_v26_1(reg))
}
Outgoing::CloseWindow(id) => {
sb::ServerboundPlayPacket::CloseWindow(sb::CloseWindow {
window_id: i32::from(*id),
})
}
Outgoing::RenameItem(n) => sb::ServerboundPlayPacket::NameItem(sb::NameItem {
name: n.as_str().into(),
}),
Outgoing::ButtonClick { window_id, button } => {
sb::ServerboundPlayPacket::EnchantItem(sb::EnchantItem {
window_id: *window_id,
enchantment: *button,
})
}
Outgoing::SelectTrade(i) => {
sb::ServerboundPlayPacket::SelectTrade(sb::SelectTrade { slot: *i })
}
_ => return None,
})
}
}
impl Inventory {
#[must_use]
pub fn encode_v26_1(&self, out: &Outgoing) -> Option<sb::ServerboundPlayPacket> {
out.to_v26_1(self.registries_26_1())
}
pub fn apply_v26_1(&mut self, p: &ClientboundPlayPacket) {
use ClientboundPlayPacket as P;
match p {
P::Login(_) => self.reset(),
P::Respawn(r) => {
let _ = r;
self.reset();
}
P::OpenWindow(w) => self.open_window_26_1(w),
P::CloseWindow(_) => self.on_close_window(),
P::WindowItems(w) => self.on_window_items_26_1(
w.window_id,
w.state_id,
w.items.iter().map(from_generated),
from_generated(&w.carried_item),
),
P::SetSlot(s) => {
self.on_set_slot_26_1(s.window_id, s.state_id, s.slot, from_generated(&s.item))
}
P::SetCursorItem(c) => self.on_set_cursor(from_generated(&c.contents)),
P::SetPlayerInventory(p) => {
self.on_set_player_inventory(p.slot_id, from_generated(&p.contents))
}
P::HeldItemSlot(h) => self.on_held_slot(i8::try_from(h.slot).unwrap_or(-1)),
P::CraftProgressBar(c) => self.on_window_property(c.window_id, c.property, c.value),
P::DeclareRecipes(d) => self.declare_recipes_26_1(d),
P::Abilities(a) => self.set_creative_26_1(a.flags & 0x08 != 0),
P::Experience(e) => self.set_experience_level_26_1(e.level),
_ => {}
}
}
pub(crate) fn ensure_recipes_26_1(&mut self) {
if let Some(body) = self.take_recipes_raw_26_1()
&& let Ok(d) =
RawPacket::new(cb::id::DECLARE_RECIPES, body).decode_as::<cb::DeclareRecipes>()
{
self.declare_recipes_26_1(&d);
}
}
fn declare_recipes_26_1(&mut self, d: &cb::DeclareRecipes) {
use ghast_protocol::types::holder::HolderSet;
let mut inputs = Some(Vec::new());
for r in &d.stone_cutter_recipes {
match (&r.input, &mut inputs) {
(HolderSet::Ids(ids), Some(v)) => v.extend_from_slice(ids),
_ => inputs = None,
}
}
self.on_recipes_26_1(
d.recipes.iter().map(|r| (&*r.name, r.items.as_slice())),
inputs,
);
}
pub fn apply_raw_v26_1(&mut self, raw: &RawPacket) -> Result<(), DecodeError> {
let mut r = Reader::new(&raw.body);
match raw.id {
cb::id::WINDOW_ITEMS => {
let window = r.read_varint()?;
let state = r.read_varint()?;
let n = r.read_varint()?;
let n = r.check_count(i64::from(n), 1)?;
let mut items = Vec::with_capacity(n.min(256));
for _ in 0..n {
items.push(slot(&mut r)?);
}
let carried = slot(&mut r)?;
self.on_window_items_26_1(window, state, items, carried);
}
cb::id::SET_SLOT => {
let window = r.read_varint()?;
let state = r.read_varint()?;
let s = r.read_i16()?;
let item = slot(&mut r)?;
self.on_set_slot_26_1(window, state, s, item);
}
cb::id::SET_CURSOR_ITEM => {
let item = slot(&mut r)?;
self.on_set_cursor(item);
}
cb::id::SET_PLAYER_INVENTORY => {
let k = r.read_varint()?;
let item = slot(&mut r)?;
self.on_set_player_inventory(k, item);
}
cb::id::LOGIN => {
raw.decode_as::<cb::Login>()?;
self.reset();
}
cb::id::RESPAWN => {
raw.decode_as::<cb::Respawn>()?;
self.reset();
}
cb::id::OPEN_WINDOW => self.open_window_26_1(&raw.decode_as::<cb::OpenWindow>()?),
cb::id::CLOSE_WINDOW => {
raw.decode_as::<cb::CloseWindow>()?;
self.on_close_window();
}
cb::id::HELD_ITEM_SLOT => {
let h = raw.decode_as::<cb::HeldItemSlot>()?;
self.on_held_slot(i8::try_from(h.slot).unwrap_or(-1));
}
cb::id::CRAFT_PROGRESS_BAR => {
let c = raw.decode_as::<cb::CraftProgressBar>()?;
self.on_window_property(c.window_id, c.property, c.value);
}
cb::id::DECLARE_RECIPES => {
self.set_recipes_raw_26_1(bytes::Bytes::copy_from_slice(&raw.body));
}
cb::id::TAGS => self.on_update_tags_26_1(bytes::Bytes::copy_from_slice(&raw.body)),
cb::id::TRADE_LIST => self.on_merchant_offers_26_1(&raw.body),
cb::id::ABILITIES => {
let a = raw.decode_as::<cb::Abilities>()?;
self.set_creative_26_1(a.flags & 0x08 != 0);
}
cb::id::EXPERIENCE => {
let e = raw.decode_as::<cb::Experience>()?;
self.set_experience_level_26_1(e.level);
}
_ => {}
}
Ok(())
}
fn open_window_26_1(&mut self, w: &cb::OpenWindow) {
self.on_open_screen_26_1(
w.window_id,
w.inventory_type,
ghast_protocol::chat::nbt_to_plain_text(&w.window_title).into(),
);
}
}
}
#[cfg(feature = "v26_1")]
pub use v26_1::PACKET_IDS_26_1;