minecraft_net/fields/
general.rs1use minecraft_net_proc::Field;
2use crate::{Field, PacketReader};
3use crate::fields::{encode_int, encode_long, encode_string, encode_var_int};
4use crate::fields::types::{Identifier, Int, VarInt};
5
6Field!(BitSet, {
7 data: PrefixedArray<Long>
8});
9impl BitSet {
10 pub fn is_set(&self, i: usize) -> bool {
11 self.data[i / 64] & (1 << (i % 64)) != 0
12 }
13 pub fn set(&mut self, i: usize) {
14 self.extend_to(i);
15 self.set_unchecked(i);
16 }
17 pub fn set_unchecked(&mut self, i: usize) {
18 self.data[i / 64] |= 1 << (i % 64);
19 }
20 pub fn reset(&mut self, i: usize) {
21 self.extend_to(i);
22 self.reset_unchecked(i);
23 }
24 pub fn reset_unchecked(&mut self, i: usize) {
25 self.data[i / 64] &= !(1 << (i % 64));
26 }
27 pub fn extend_to(&mut self, i: usize) {
28 let to_add = (i / 8) + if i % 8 != 0 {1} else {0};
29 self.data.resize(to_add, 0);
30 }
31}
32#[derive(Clone, Debug)]
33pub struct IdOr<T: Field + Clone> {
34 pub id: VarInt,
35 pub value: Option<T>,
36}
37impl<T: Field + Clone> Field for IdOr<T> {
38 fn to_bytes(&self) -> Vec<u8> {
39 let mut res = encode_var_int(self.id);
40 if self.id == 0 {
41 res.append(&mut self.value.clone().expect("invalid IdOr field. Requires either ID to be non-zero or a value").to_bytes());
42 }
43 res
44 }
45 fn from_reader(reader: &mut PacketReader) -> crate::errors::Result<Self> {
46 let id = reader.read_var_int()?;
47 if id == 0 {
48 Ok(Self {
49 id,
50 value: Some(T::from_reader(reader)?),
51 })
52 } else {
53 Ok(Self {
54 id, value: None,
55 })
56 }
57 }
58}
59impl<T: Clone + Field> IdOr<T> {
60 pub fn with_id(id: VarInt) -> Self {
61 Self {id, value: None}
62 }
63 pub fn with_value(value: T) -> Self {
64 Self {id: 0, value: Some(value) }
65 }
66}
67#[derive(Clone, Debug)]
68pub enum IDSet {
69 TagName(Identifier),
70 Ids(Vec<VarInt>),
71}
72impl Field for IDSet {
73 fn to_bytes(&self) -> Vec<u8> {
74 match self {
75 IDSet::TagName(name) => vec![encode_var_int(0), encode_string(name.clone())].iter().flatten().cloned().collect::<Vec<u8>>(),
76 IDSet::Ids(ids) => vec![
77 encode_var_int(ids.len() as i32 + 1),
78 ids.iter().flat_map(|id| encode_var_int(*id)).collect::<Vec<u8>>(),
79 ].iter().flatten().cloned().collect::<Vec<u8>>(),
80 }
81 }
82 fn from_reader(reader: &mut PacketReader) -> crate::errors::Result<Self> {
83 let t = reader.read_var_int()?;
84 if t == 0 {
85 Ok(Self::TagName(reader.read_identifier()?))
86 } else {
87 let mut res: Vec<VarInt> = Vec::with_capacity((t - 1) as usize);
88 for _ in 0..(t-1) {
89 res.push(reader.read_var_int()?);
90 }
91 Ok(Self::Ids(res))
92 }
93 }
94}
95
96#[derive(Debug, Eq, PartialEq, Clone, Copy)]
97pub struct TeleportFlags(Int);
98impl TeleportFlags {
99 pub fn new(relative_x: bool,
100 relative_y: bool,
101 relative_z: bool,
102 relative_yaw: bool,
103 relative_pitch: bool,
104 relative_velocity_x: bool,
105 relative_velocity_y: bool,
106 relative_velocity_z: bool,
107 rotation_first: bool) -> Self {
108 let mut new = Self::default();
109 new.set_relative_x(relative_x);
110 new.set_relative_y(relative_y);
111 new.set_relative_z(relative_z);
112 new.set_relative_yaw(relative_yaw);
113 new.set_relative_pitch(relative_pitch);
114 new.set_relative_velocity_x(relative_velocity_x);
115 new.set_relative_velocity_y(relative_velocity_y);
116 new.set_relative_velocity_z(relative_velocity_z);
117 new.set_rotation_first(rotation_first);
118 new
119 }
120 fn apply_mask(&mut self, index: usize, value: bool) {
121 if value {
122 self.0 |= 1 << index;
123 } else {
124 self.0 &= !(1 << index);
125 }
126 }
127 pub fn set_relative_x(&mut self, new: bool) -> () {
128 self.apply_mask(0, new)
129 }
130 pub fn set_relative_y(&mut self, new: bool) -> () {
131 self.apply_mask(1, new)
132 }
133 pub fn set_relative_z(&mut self, new: bool) -> () {
134 self.apply_mask(2, new)
135 }
136 pub fn set_relative_yaw(&mut self, new: bool) -> () {
137 self.apply_mask(3, new)
138 }
139 pub fn set_relative_pitch(&mut self, new: bool) -> () {
140 self.apply_mask(4, new)
141 }
142 pub fn set_relative_velocity_x(&mut self, new: bool) -> () {
143 self.apply_mask(5, new)
144 }
145 pub fn set_relative_velocity_y(&mut self, new: bool) -> () {
146 self.apply_mask(6, new)
147 }
148 pub fn set_relative_velocity_z(&mut self, new: bool) -> () {
149 self.apply_mask(7, new)
150 }
151 pub fn set_rotation_first(&mut self, new: bool) -> () {
152 self.apply_mask(8, new)
153 }
154}
155impl Field for TeleportFlags {
156 fn to_bytes(&self) -> Vec<u8> {
157 encode_int(self.0)
158 }
159
160 fn from_reader(reader: &mut PacketReader) -> crate::errors::Result<Self> {
161 Ok(Self(reader.read_int()))
162 }
163}
164impl Default for TeleportFlags {
165 fn default() -> Self {
166 Self(0)
167 }
168}
169
170#[derive(Debug, Clone)]
171pub struct Position {
172 x: i32,
173 y: i16,
174 z: i32,
175}
176impl Position {
177 pub fn new(x: i32, y: i16, z: i32) -> Position {
178 Position { x, y, z }
179 }
180}
181impl Field for Position {
182 fn to_bytes(&self) -> Vec<u8> {
183 let val = ((self.x as i64 & 0x3FFFFFF) << 38) | ((self.z as i64 & 0x3FFFFFF) << 12) | (self.y as i64 & 0xFFF);
184 encode_long(val)
185 }
186 fn from_reader(reader: &mut PacketReader) -> crate::errors::Result<Position> {
187 let val = reader.read_long();
188 let mut x = ((val >> 38) & 0x3FFFFFF) as i32;
189 let mut z = ((val >> 12) & 0x3FFFFFF) as i32;
190 let mut y = (val & 0xFFF) as i16;
191 if x >= 1 << 25 { x -= 1 << 26 }
192 if y >= 1 << 11 { y -= 1 << 12 }
193 if z >= 1 << 25 { z -= 1 << 26 }
194 Ok(Position {x, y, z})
195 }
196}