move RW traits and blank preparing of slot components
This commit is contained in:
parent
79e0f9c6dc
commit
20c6481c86
@ -7,7 +7,7 @@ use serde_with::skip_serializing_none;
|
||||
|
||||
use crate::ServerError;
|
||||
|
||||
use super::ReadWriteNBT;
|
||||
use super::RWNBT;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[skip_serializing_none]
|
||||
@ -192,7 +192,7 @@ impl TextComponentBuilder {
|
||||
}
|
||||
|
||||
// Реализуем читалку-записывалку текст-компонентов для пакета
|
||||
impl ReadWriteNBT<TextComponent> for Packet {
|
||||
impl<'a> RWNBT<'a, TextComponent> for Packet {
|
||||
fn read_nbt(&mut self) -> Result<TextComponent, ServerError> {
|
||||
let mut data = Vec::new();
|
||||
let pos = self.get_ref().position();
|
||||
|
@ -2,6 +2,7 @@ use std::io::Read;
|
||||
|
||||
use craftflow_nbt::DynNBT;
|
||||
use rust_mc_proto::{DataReader, DataWriter, Packet};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::ServerError;
|
||||
|
||||
@ -10,12 +11,12 @@ pub mod slot;
|
||||
pub mod sound;
|
||||
|
||||
// Трейт для чтения NBT-совместимых приколов
|
||||
pub trait ReadWriteNBT<T>: DataReader + DataWriter {
|
||||
pub trait RWNBT<'a, T: Serialize + Deserialize<'a>>: DataReader + DataWriter {
|
||||
fn read_nbt(&mut self) -> Result<T, ServerError>;
|
||||
fn write_nbt(&mut self, val: &T) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl ReadWriteNBT<DynNBT> for Packet {
|
||||
impl<'a> RWNBT<'a, DynNBT> for Packet {
|
||||
fn read_nbt(&mut self) -> Result<DynNBT, ServerError> {
|
||||
let mut data = Vec::new();
|
||||
let pos = self.get_ref().position();
|
||||
@ -36,12 +37,12 @@ impl ReadWriteNBT<DynNBT> for Packet {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ReadWritePosition: DataReader + DataWriter {
|
||||
pub trait RWPosition: DataReader + DataWriter {
|
||||
fn read_position(&mut self) -> Result<(i64, i64, i64), ServerError>;
|
||||
fn write_position(&mut self, x: i64, y: i64, z: i64) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl ReadWritePosition for Packet {
|
||||
impl<T: DataReader + DataWriter> RWPosition for T {
|
||||
fn read_position(&mut self) -> Result<(i64, i64, i64), ServerError> {
|
||||
let val = self.read_long()?;
|
||||
Ok((val >> 38, val << 52 >> 52, val << 26 >> 38))
|
||||
@ -64,6 +65,45 @@ pub enum IdSet {
|
||||
Ids(Vec<u32>),
|
||||
}
|
||||
|
||||
pub trait RWIdSet: DataReader + DataWriter {
|
||||
fn read_id_set(&mut self) -> Result<IdSet, ServerError>;
|
||||
fn write_id_set(&mut self, val: IdSet) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl<T: DataReader + DataWriter> RWIdSet for T {
|
||||
fn read_id_set(&mut self) -> Result<IdSet, ServerError> {
|
||||
let ty = self.read_varint()?;
|
||||
if ty == 0 {
|
||||
Ok(IdSet::Tag(self.read_string()?))
|
||||
} else {
|
||||
let len = ty - 1;
|
||||
let mut ids = Vec::new();
|
||||
|
||||
for _ in 0..len {
|
||||
ids.push(self.read_u32_varint()?);
|
||||
}
|
||||
|
||||
Ok(IdSet::Ids(ids))
|
||||
}
|
||||
}
|
||||
|
||||
fn write_id_set(&mut self, val: IdSet) -> Result<(), ServerError> {
|
||||
match val {
|
||||
IdSet::Tag(tag) => {
|
||||
self.write_varint(0)?;
|
||||
self.write_string(&tag)?;
|
||||
}
|
||||
IdSet::Ids(ids) => {
|
||||
self.write_usize_varint(ids.len() + 1)?;
|
||||
for id in ids {
|
||||
self.write_u32_varint(id)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Property {
|
||||
pub name: String,
|
||||
@ -90,3 +130,36 @@ pub enum DyeColor {
|
||||
Red,
|
||||
Black,
|
||||
}
|
||||
|
||||
pub trait RWDyeColor: DataReader + DataWriter {
|
||||
fn read_dye_color(&mut self) -> Result<DyeColor, ServerError>;
|
||||
fn write_dye_color(&mut self, val: DyeColor) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl<T: DataReader + DataWriter> RWDyeColor for T {
|
||||
fn read_dye_color(&mut self) -> Result<DyeColor, ServerError> {
|
||||
match self.read_usize_varint()? {
|
||||
0 => Ok(DyeColor::White),
|
||||
1 => Ok(DyeColor::Orange),
|
||||
2 => Ok(DyeColor::Magenta),
|
||||
3 => Ok(DyeColor::LightBlue),
|
||||
4 => Ok(DyeColor::Yellow),
|
||||
5 => Ok(DyeColor::Lime),
|
||||
6 => Ok(DyeColor::Pink),
|
||||
7 => Ok(DyeColor::Gray),
|
||||
8 => Ok(DyeColor::LightGray),
|
||||
9 => Ok(DyeColor::Cyan),
|
||||
10 => Ok(DyeColor::Purple),
|
||||
11 => Ok(DyeColor::Blue),
|
||||
12 => Ok(DyeColor::Brown),
|
||||
13 => Ok(DyeColor::Green),
|
||||
14 => Ok(DyeColor::Red),
|
||||
15 => Ok(DyeColor::Black),
|
||||
_ => Err(ServerError::WrongPacket),
|
||||
}
|
||||
}
|
||||
fn write_dye_color(&mut self, val: DyeColor) -> Result<(), ServerError> {
|
||||
self.write_usize_varint(val as usize)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
235
src/data/slot.rs
235
src/data/slot.rs
@ -6,7 +6,10 @@ use uuid::Uuid;
|
||||
|
||||
use crate::ServerError;
|
||||
|
||||
use super::{DyeColor, IdOr, IdSet, Property, component::TextComponent, sound::SoundEvent};
|
||||
use super::{
|
||||
DyeColor, IdOr, IdSet, Property, RWDyeColor, RWIdSet, RWNBT, component::TextComponent,
|
||||
sound::SoundEvent,
|
||||
};
|
||||
|
||||
pub const SLOT_COMPONENT_LENGTH: u16 = 96;
|
||||
|
||||
@ -160,16 +163,61 @@ pub struct HiveBee {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BannerLayer {
|
||||
pub pattern_type: i32,
|
||||
pub asset_id: Option<String>,
|
||||
pub translation_key: Option<String>,
|
||||
pub color: DyeColor,
|
||||
pub enum BannerLayer {
|
||||
Direct {
|
||||
asset_id: String,
|
||||
translation_key: String,
|
||||
color: DyeColor,
|
||||
},
|
||||
Registry {
|
||||
pattern_id: i32,
|
||||
color: DyeColor,
|
||||
},
|
||||
}
|
||||
|
||||
pub trait RWBannerLayer: DataReader + DataWriter {
|
||||
fn read_banner_layer(&mut self) -> Result<BannerLayer, ServerError>;
|
||||
fn write_banner_layer(&mut self, val: BannerLayer) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl<T: DataReader + DataWriter> RWBannerLayer for T {
|
||||
fn read_banner_layer(&mut self) -> Result<BannerLayer, ServerError> {
|
||||
match self.read_varint()? {
|
||||
0 => Ok(BannerLayer::Direct {
|
||||
asset_id: self.read_string()?,
|
||||
translation_key: self.read_string()?,
|
||||
color: self.read_dye_color()?,
|
||||
}),
|
||||
id => Ok(BannerLayer::Registry {
|
||||
pattern_id: id - 1,
|
||||
color: self.read_dye_color()?,
|
||||
}),
|
||||
}
|
||||
}
|
||||
fn write_banner_layer(&mut self, val: BannerLayer) -> Result<(), ServerError> {
|
||||
match val {
|
||||
BannerLayer::Direct {
|
||||
asset_id,
|
||||
translation_key,
|
||||
color,
|
||||
} => {
|
||||
self.write_varint(0)?;
|
||||
self.write_string(&asset_id)?;
|
||||
self.write_string(&translation_key)?;
|
||||
self.write_dye_color(color)?;
|
||||
}
|
||||
BannerLayer::Registry { pattern_id, color } => {
|
||||
self.write_varint(pattern_id + 1)?;
|
||||
self.write_dye_color(color)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AttributeModifier {
|
||||
pub attribute_id: u64,
|
||||
pub attribute_id: u32,
|
||||
pub modifier_id: String,
|
||||
pub value: f64,
|
||||
/// The operation to be applied upon the value. Can be one of the following:
|
||||
@ -192,15 +240,77 @@ pub struct AttributeModifier {
|
||||
pub slot: u8,
|
||||
}
|
||||
|
||||
pub trait RWAttributeModifier: DataReader + DataWriter {
|
||||
fn read_attribute_modifier(&mut self) -> Result<AttributeModifier, ServerError>;
|
||||
fn write_attribute_modifier(&mut self, val: AttributeModifier) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl<T: DataReader + DataWriter> RWAttributeModifier for T {
|
||||
fn read_attribute_modifier(&mut self) -> Result<AttributeModifier, ServerError> {
|
||||
Ok(AttributeModifier {
|
||||
attribute_id: self.read_u32_varint()?,
|
||||
modifier_id: self.read_string()?,
|
||||
value: self.read_double()?,
|
||||
operation: self.read_u8_varint()?,
|
||||
slot: self.read_u8_varint()?,
|
||||
})
|
||||
}
|
||||
fn write_attribute_modifier(&mut self, val: AttributeModifier) -> Result<(), ServerError> {
|
||||
self.write_u32_varint(val.attribute_id)?;
|
||||
self.write_string(&val.modifier_id)?;
|
||||
self.write_double(val.value)?;
|
||||
self.write_u8_varint(val.operation)?;
|
||||
self.write_u8_varint(val.slot)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ToolRule {
|
||||
pub blocks: IdSet,
|
||||
pub has_speed: bool,
|
||||
pub speed: Option<f32>,
|
||||
pub has_correct_drop_for_blocks: bool,
|
||||
pub correct_drop_for_blocks: Option<bool>,
|
||||
}
|
||||
|
||||
pub trait RWToolRule: DataReader + DataWriter {
|
||||
fn read_tool_rule(&mut self) -> Result<ToolRule, ServerError>;
|
||||
fn write_tool_rule(&mut self, val: ToolRule) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl<T: DataReader + DataWriter> RWToolRule for T {
|
||||
fn read_tool_rule(&mut self) -> Result<ToolRule, ServerError> {
|
||||
Ok(ToolRule {
|
||||
blocks: self.read_id_set()?,
|
||||
speed: if self.read_boolean()? {
|
||||
Some(self.read_float()?)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
correct_drop_for_blocks: if self.read_boolean()? {
|
||||
Some(self.read_boolean()?)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
})
|
||||
}
|
||||
fn write_tool_rule(&mut self, val: ToolRule) -> Result<(), ServerError> {
|
||||
self.write_id_set(val.blocks)?;
|
||||
if let Some(speed) = val.speed {
|
||||
self.write_boolean(true)?;
|
||||
self.write_float(speed)?;
|
||||
} else {
|
||||
self.write_boolean(false)?;
|
||||
}
|
||||
if let Some(cdfb) = val.correct_drop_for_blocks {
|
||||
self.write_boolean(true)?;
|
||||
self.write_boolean(cdfb)?;
|
||||
} else {
|
||||
self.write_boolean(false)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DamageReduction {
|
||||
pub horizontal_blocking_angle: f32,
|
||||
@ -209,6 +319,38 @@ pub struct DamageReduction {
|
||||
pub factor: f32,
|
||||
}
|
||||
|
||||
pub trait RWDamageReduction: DataReader + DataWriter {
|
||||
fn read_damage_reduction(&mut self) -> Result<DamageReduction, ServerError>;
|
||||
fn write_damage_reduction(&mut self, val: DamageReduction) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl<T: DataReader + DataWriter> RWDamageReduction for T {
|
||||
fn read_damage_reduction(&mut self) -> Result<DamageReduction, ServerError> {
|
||||
Ok(DamageReduction {
|
||||
horizontal_blocking_angle: self.read_float()?,
|
||||
damage_kind: if self.read_boolean()? {
|
||||
Some(self.read_id_set()?)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
base: self.read_float()?,
|
||||
factor: self.read_float()?,
|
||||
})
|
||||
}
|
||||
fn write_damage_reduction(&mut self, val: DamageReduction) -> Result<(), ServerError> {
|
||||
self.write_float(val.horizontal_blocking_angle)?;
|
||||
if let Some(id_set) = val.damage_kind {
|
||||
self.write_boolean(true)?;
|
||||
self.write_id_set(id_set)?;
|
||||
} else {
|
||||
self.write_boolean(false)?;
|
||||
}
|
||||
self.write_float(val.base)?;
|
||||
self.write_float(val.factor)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// https://minecraft.wiki/w/Java_Edition_protocol/Slot_data#Structured_components
|
||||
#[derive(Clone, EnumIndex)]
|
||||
pub enum StructuredComponent {
|
||||
@ -221,7 +363,7 @@ pub enum StructuredComponent {
|
||||
CustomName(TextComponent), // 5
|
||||
ItemName(TextComponent), // 6
|
||||
ItemModel(String), // 7
|
||||
Lore(TextComponent), // 8
|
||||
Lore(Vec<TextComponent>), // 8
|
||||
/// Can be one of the following:
|
||||
/// - 0 - Common (white)
|
||||
/// - 1 - Uncommon (yellow)
|
||||
@ -230,7 +372,7 @@ pub enum StructuredComponent {
|
||||
Rarity(u8), // 9
|
||||
/// Key: The ID of the enchantment in the enchantment registry. \
|
||||
/// Value: The level of the enchantment.
|
||||
Enchantments(Vec<(u64, i32)>), // 10
|
||||
Enchantments(Vec<(u32, i32)>), // 10
|
||||
CanPlaceOn(Vec<BlockPredicate>), // 11
|
||||
CanBreak(Vec<BlockPredicate>), // 12
|
||||
AttributeModifiers(Vec<AttributeModifier>), // 13
|
||||
@ -438,15 +580,72 @@ pub trait ReadWriteSlotComponent: DataReader + DataWriter {
|
||||
|
||||
impl ReadWriteSlotComponent for Packet {
|
||||
fn read_slot_component(&mut self) -> Result<StructuredComponent, ServerError> {
|
||||
let _ = self.read_u16_varint()?; // id
|
||||
let id = self.read_u16_varint()?; // id
|
||||
|
||||
todo!()
|
||||
match id {
|
||||
0 => Ok(StructuredComponent::CustomData(self.read_nbt()?)),
|
||||
1 => Ok(StructuredComponent::MaxStackSize(self.read_varint()?)),
|
||||
2 => Ok(StructuredComponent::MaxDamage(self.read_varint()?)),
|
||||
3 => Ok(StructuredComponent::Unbreakable),
|
||||
4 => Ok(StructuredComponent::CustomName(self.read_nbt()?)),
|
||||
5 => Ok(StructuredComponent::ItemName(self.read_nbt()?)),
|
||||
6 => Ok(StructuredComponent::ItemModel(self.read_string()?)),
|
||||
7 => {
|
||||
let len: usize = self.read_usize_varint()?;
|
||||
let mut lore = Vec::new();
|
||||
|
||||
for _ in 0..len {
|
||||
lore.push(self.read_nbt()?);
|
||||
}
|
||||
|
||||
Ok(StructuredComponent::Lore(lore))
|
||||
}
|
||||
8 => Ok(StructuredComponent::Rarity(self.read_u8_varint()?)),
|
||||
9 => {
|
||||
let len: usize = self.read_usize_varint()?;
|
||||
let mut enchantments = Vec::new();
|
||||
|
||||
for _ in 0..len {
|
||||
enchantments.push((self.read_u32_varint()?, self.read_varint()?));
|
||||
}
|
||||
|
||||
Ok(StructuredComponent::Enchantments(enchantments))
|
||||
}
|
||||
_ => Err(ServerError::UnexpectedSlotComponent),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_slot_component(&mut self, val: &StructuredComponent) -> Result<(), ServerError> {
|
||||
self.write_usize_varint(val.enum_index())?;
|
||||
|
||||
todo!()
|
||||
match val {
|
||||
StructuredComponent::CustomData(val) => self.write_nbt(val)?,
|
||||
StructuredComponent::MaxStackSize(val) => self.write_varint(*val)?,
|
||||
StructuredComponent::MaxDamage(val) => self.write_varint(*val)?,
|
||||
StructuredComponent::Unbreakable => {}
|
||||
StructuredComponent::CustomName(val) => self.write_nbt(val)?,
|
||||
StructuredComponent::ItemName(val) => self.write_nbt(val)?,
|
||||
StructuredComponent::ItemModel(val) => self.write_string(val)?,
|
||||
StructuredComponent::Lore(val) => {
|
||||
self.write_usize_varint(val.len())?;
|
||||
|
||||
for text in val {
|
||||
self.write_nbt(text)?;
|
||||
}
|
||||
}
|
||||
StructuredComponent::Rarity(val) => self.write_u8_varint(*val)?,
|
||||
StructuredComponent::Enchantments(val) => {
|
||||
self.write_usize_varint(val.len())?;
|
||||
|
||||
for (id, lvl) in val {
|
||||
self.write_u32_varint(*id)?;
|
||||
self.write_varint(*lvl)?;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -457,12 +656,12 @@ pub struct Slot {
|
||||
pub components: Vec<StructuredComponent>,
|
||||
}
|
||||
|
||||
pub trait ReadWriteSlot: DataReader + DataWriter {
|
||||
pub trait RWSlot: DataReader + DataWriter {
|
||||
fn read_slot(&mut self) -> Result<Option<Slot>, ServerError>;
|
||||
fn write_slot(&mut self, val: Option<Slot>) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl ReadWriteSlot for Packet {
|
||||
impl RWSlot for Packet {
|
||||
fn read_slot(&mut self) -> Result<Option<Slot>, ServerError> {
|
||||
let amount = self.read_varint()?;
|
||||
|
||||
@ -517,12 +716,12 @@ pub struct HashedSlot {
|
||||
pub components: Vec<(u16, i32)>,
|
||||
}
|
||||
|
||||
pub trait ReadWriteHashedSlot: DataReader + DataWriter {
|
||||
pub trait RWHashedSlot: DataReader + DataWriter {
|
||||
fn read_hashed_slot(&mut self) -> Result<Option<HashedSlot>, ServerError>;
|
||||
fn write_hashed_slot(&mut self, val: Option<HashedSlot>) -> Result<(), ServerError>;
|
||||
}
|
||||
|
||||
impl ReadWriteHashedSlot for Packet {
|
||||
impl<T: DataReader + DataWriter> RWHashedSlot for T {
|
||||
fn read_hashed_slot(&mut self) -> Result<Option<HashedSlot>, ServerError> {
|
||||
let amount = self.read_varint()?;
|
||||
|
||||
|
@ -26,6 +26,7 @@ pub enum ServerError {
|
||||
DeTextComponent, // Ошибка при десериализации текст-компонента
|
||||
SerNbt, // Ошибка при сериализации nbt
|
||||
DeNbt, // Ошибка при десериализации nbt
|
||||
UnexpectedSlotComponent,
|
||||
UnexpectedState, // Указывает на то что этот пакет не может быть отправлен в данном режиме (в основном через ProtocolHelper)
|
||||
Other(String), // Другая ошибка, либо очень специфичная, либо хз, лучше не использовать и создавать новое поле ошибки
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ use rust_mc_proto::{DataReader, DataWriter, Packet};
|
||||
|
||||
use crate::{
|
||||
ServerError,
|
||||
data::{ReadWriteNBT, component::TextComponent},
|
||||
data::{RWNBT, component::TextComponent},
|
||||
player::context::ClientContext,
|
||||
protocol::packet_id::{clientbound, serverbound},
|
||||
};
|
||||
|
@ -8,7 +8,7 @@ use rust_mc_proto::{DataReader, DataWriter, Packet};
|
||||
|
||||
use crate::{
|
||||
ServerError,
|
||||
data::{ReadWriteNBT, component::TextComponent},
|
||||
data::{RWNBT, component::TextComponent},
|
||||
protocol::{
|
||||
packet_id::{clientbound, serverbound},
|
||||
*,
|
||||
|
Loading…
x
Reference in New Issue
Block a user