Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added potion effects #551

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9144729
Added effect extractor & effect to `valence_generated`
SelfMadeSystem Oct 9, 2023
fd5f516
Moved `toPascalCase` to ValenceUtils.java
SelfMadeSystem Oct 9, 2023
53405be
copilot momento
SelfMadeSystem Oct 9, 2023
5707080
Rename `Effect` to `StatusEffect`
SelfMadeSystem Oct 9, 2023
3575691
Started adding component
SelfMadeSystem Oct 9, 2023
fc9cef7
fmt fix /shrug
SelfMadeSystem Oct 9, 2023
c7fc6e5
Added StatusEffectPlugin. Haven't tested it yet.
SelfMadeSystem Oct 10, 2023
b912f82
Added ActiveStatusEffects to ClientBundle.
SelfMadeSystem Oct 10, 2023
932f5ba
fmt
SelfMadeSystem Oct 10, 2023
6224f82
fmt*
SelfMadeSystem Oct 10, 2023
acf3563
Improved & fixed stuff
SelfMadeSystem Oct 10, 2023
369ab4c
Changed active & new from vec to HashMap, changed amplifier from u8 t…
SelfMadeSystem Oct 10, 2023
3d8ce9e
Improved the status effect queries so that it doesn't do too much stu…
SelfMadeSystem Oct 10, 2023
22e71c8
My vscode todo extension didn't recognize TODO in block comments that…
SelfMadeSystem Oct 10, 2023
a77c77c
fmt
SelfMadeSystem Oct 10, 2023
56fc839
clippy
SelfMadeSystem Oct 10, 2023
130570c
Refactor: move `StatusEffect` and `ActiveStatusEffect/s` to `valence_…
SelfMadeSystem Oct 10, 2023
348e7bf
Update depgraph.svg
SelfMadeSystem Oct 10, 2023
e5020df
Merge branch 'valence-rs:main' into main
SelfMadeSystem Oct 10, 2023
586f9e8
Rename some stuff kuz bad naming & clippy didn't liek
SelfMadeSystem Oct 10, 2023
68bac26
I did the suggestions :)
SelfMadeSystem Oct 10, 2023
b05c3e3
Added warning to `active_effects_mut`
SelfMadeSystem Oct 10, 2023
6a67ac1
Added notes to `ActiveStatusEffects.add` and `ActiveStatusEffects.rem…
SelfMadeSystem Oct 10, 2023
dfad5f9
Hopefully fixed docs
SelfMadeSystem Oct 10, 2023
b565086
Used diff link
SelfMadeSystem Oct 11, 2023
e58c3e5
Added swirls
SelfMadeSystem Oct 11, 2023
491c689
dis complex
SelfMadeSystem Oct 11, 2023
711b6e2
Remove swirl color when effect expires
SelfMadeSystem Oct 11, 2023
ed193b9
Slightly changed the ActiveStatusEffects API again and added color mi…
SelfMadeSystem Oct 11, 2023
7853764
fmt
SelfMadeSystem Oct 11, 2023
a976dd4
Merge branch 'valence-rs:main' into main
SelfMadeSystem Oct 11, 2023
50aec02
Changed up some stuff to remove unnecessary packets
SelfMadeSystem Oct 11, 2023
f14c8b4
Merge branch 'valence-rs:main' into main
SelfMadeSystem Oct 11, 2023
ca2897e
oops
SelfMadeSystem Oct 11, 2023
4840f74
I keep redoing stuff. Hopefully this time it works
SelfMadeSystem Oct 11, 2023
0cfecc6
fixed canine manure logic lmao
SelfMadeSystem Oct 11, 2023
231125e
missed a println!()
SelfMadeSystem Oct 12, 2023
7cb6264
Slight changes to accommodate regen, hunger, saturation, poison, and …
SelfMadeSystem Oct 12, 2023
1082428
Added example :)
SelfMadeSystem Oct 12, 2023
a0afc39
hecc off clippy thx
SelfMadeSystem Oct 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/valence_generated/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ mod chunk_view;
mod item;
mod packet_id;
mod sound;
mod status_effect;

pub fn main() -> anyhow::Result<()> {
write_generated_file(block::build()?, "block.rs")?;
write_generated_file(status_effect::build()?, "effect.rs")?;
write_generated_file(item::build()?, "item.rs")?;
write_generated_file(sound::build()?, "sound.rs")?;
write_generated_file(packet_id::build()?, "packet_id.rs")?;
Expand Down
227 changes: 227 additions & 0 deletions crates/valence_generated/build/status_effect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::quote;
use serde::Deserialize;
use valence_build_utils::{ident, rerun_if_changed};

#[derive(Deserialize, Debug)]
pub enum StatusEffectCategory {
Beneficial,
Harmful,
Neutral,
}

#[derive(Deserialize, Debug)]
pub struct StatusEffect {
id: u16,
name: String,
translation_key: String,
category: StatusEffectCategory,
color: u32,
instant: bool,
}

pub fn build() -> anyhow::Result<TokenStream> {
rerun_if_changed(["extracted/effects.json"]);

let effects =
serde_json::from_str::<Vec<StatusEffect>>(include_str!("../extracted/effects.json"))?;

let effect_count = effects.len();

let effect_from_raw_id_arms = effects
.iter()
.map(|effect| {
let id = &effect.id;
let name = ident(effect.name.to_pascal_case());

quote! {
#id => Some(Self::#name),
}
})
.collect::<TokenStream>();

let effect_to_raw_id_arms = effects
.iter()
.map(|effect| {
let id = &effect.id;
let name = ident(effect.name.to_pascal_case());

quote! {
Self::#name => #id,
}
})
.collect::<TokenStream>();

let effect_from_ident_arms = effects
.iter()
.map(|effect| {
// TODO: put the full resource identifier in the extracted JSON.
let path_name = &effect.name;
let ident_name = format!("minecraft:{}", &effect.name);

let name = ident(path_name.to_pascal_case());
quote! {
#ident_name => Some(Self::#name),
}
})
.collect::<TokenStream>();

let effect_to_ident_arms = effects
.iter()
.map(|effect| {
let str_name = &effect.name;
let name = ident(str_name.to_pascal_case());
quote! {
Self::#name => ident!(#str_name),
}
})
.collect::<TokenStream>();

let effect_to_translation_key_arms = effects
.iter()
.map(|effect| {
let str_name = &effect.translation_key;
let name = ident(effect.name.to_pascal_case());
quote! {
Self::#name => #str_name,
}
})
.collect::<TokenStream>();

let effect_to_category_arms = effects
.iter()
.map(|effect| {
let category = match &effect.category {
StatusEffectCategory::Beneficial => quote! { StatusEffectCategory::Beneficial },
StatusEffectCategory::Harmful => quote! { StatusEffectCategory::Harmful },
StatusEffectCategory::Neutral => quote! { StatusEffectCategory::Neutral },
};

let name = ident(effect.name.to_pascal_case());
quote! {
Self::#name => #category,
}
})
.collect::<TokenStream>();

let effect_to_color_arms = effects
.iter()
.map(|effect| {
let color = &effect.color;
let name = ident(effect.name.to_pascal_case());
quote! {
Self::#name => #color,
}
})
.collect::<TokenStream>();

let effect_to_instant_arms = effects
.iter()
.map(|effect| {
let instant = &effect.instant;
let name = ident(effect.name.to_pascal_case());
quote! {
Self::#name => #instant,
}
})
.collect::<TokenStream>();

let effect_variants = effects
.iter()
.map(|effect| ident(effect.name.to_pascal_case()))
.collect::<Vec<_>>();

Ok(quote! {
use valence_ident::{Ident, ident};

/// Represents a status effect category
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum StatusEffectCategory {
Beneficial,
Harmful,
Neutral,
}

/// Represents a status effect from the game
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum StatusEffect {
#(#effect_variants,)*
}

impl StatusEffect {
/// Constructs a effect from a raw item ID.
///
/// If the given ID is invalid, `None` is returned.
pub const fn from_raw(id: u16) -> Option<Self> {
match id {
#effect_from_raw_id_arms
_ => None
}
}

/// Gets the raw effect ID from the effect
pub const fn to_raw(self) -> u16 {
match self {
#effect_to_raw_id_arms
}
}

/// Construct a effect from its snake_case name.
///
/// Returns `None` if the name is invalid.
pub fn from_ident(id: Ident<&str>) -> Option<Self> {
match id.as_str() {
#effect_from_ident_arms
_ => None
}
}

/// Gets the identifier of this effect.
pub const fn to_ident(self) -> Ident<&'static str> {
match self {
#effect_to_ident_arms
}
}

/// Gets the name of this effect.
/// Same as [`StatusEffect::to_ident`], but doesn't take ownership.
pub const fn name(&self) -> Ident<&'static str> {
match self {
#effect_to_ident_arms
}
}

/// Gets the translation key of this effect.
pub const fn translation_key(&self) -> &'static str {
match self {
#effect_to_translation_key_arms
}
}

/// Gets the category of this effect.
pub const fn category(&self) -> StatusEffectCategory {
match self {
#effect_to_category_arms
}
}

/// Gets the color of this effect.
pub const fn color(&self) -> u32 {
match self {
#effect_to_color_arms
}
}

/// Gets whether this effect is instant.
pub const fn instant(&self) -> bool {
match self {
#effect_to_instant_arms
}
}

/// An array of all effects.
pub const ALL: [Self; #effect_count] = [#(Self::#effect_variants,)*];
}
})
}
Loading
Loading