Skip to content

Commit

Permalink
Add Potion Effects Registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Oery committed Oct 24, 2024
1 parent 314ed24 commit 63ef62d
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
79 changes: 79 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ serde_json = "1.0.132"
# TODO: Make this easier to use
kagami_macro = { path = "../../Code/kagami_macro" }
uuid = { version = "1.11.0", features = ["v4"] }
num_enum = "0.7.3"
1 change: 1 addition & 0 deletions src/minecraft/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
};

pub mod packets;
pub mod registry;

pub trait Packet: Serialize + Deserialize + Debug + Any + Send + Sync {
fn deserialize_packet(bytes: &[u8]) -> io::Result<Self>
Expand Down
3 changes: 3 additions & 0 deletions src/minecraft/registry/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod potion_effects;

pub use potion_effects::PotionEffects;
56 changes: 56 additions & 0 deletions src/minecraft/registry/potion_effects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use num_enum::{IntoPrimitive, TryFromPrimitive};

#[derive(Debug, Eq, PartialEq, TryFromPrimitive, IntoPrimitive, Copy, Clone)]
#[repr(u8)]
pub enum PotionEffects {
Speed = 1,
Slowness = 2,
Haste = 3,
MiningFatigue = 4,
Strength = 5,
InstantHealth = 6,
InstantDamage = 7,
JumpBoost = 8,
Nausea = 9,
Regeneration = 10,
Resistance = 11,
FireResistance = 12,
WaterBreathing = 13,
Invisibility = 14,
Blindness = 15,
NightVision = 16,
Hunger = 17,
Weakness = 18,
Poison = 19,
Wither = 20,
HealthBoost = 21,
Absorption = 22,
Saturation = 23,
}

use crate::serialization::Deserialize;
use byteorder::ReadBytesExt;
use std::io;

impl Deserialize for PotionEffects {
fn deserialize<R: io::Read>(reader: &mut R) -> io::Result<Self> {
let byte = reader.read_u8()?;
match PotionEffects::try_from(byte) {
Ok(potion) => Ok(potion),
Err(_) => Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid potion effect",
)),
}
}
}

use crate::serialization::Serialize;
use byteorder::WriteBytesExt;

impl Serialize for PotionEffects {
fn serialize(&self, buf: &mut dyn std::io::Write) -> std::io::Result<()> {
let byte = u8::from(*self);
buf.write_u8(byte)
}
}

0 comments on commit 63ef62d

Please sign in to comment.