diff --git a/pumpkin-data/build/fluid.rs b/pumpkin-data/build/fluid.rs index 16fd57f09..55a56b879 100644 --- a/pumpkin-data/build/fluid.rs +++ b/pumpkin-data/build/fluid.rs @@ -442,15 +442,15 @@ pub(crate) fn build() -> TokenStream { )); } fluid_from_state_id.extend(quote! { - #state_id_start..=#state_id_end => Some(Fluid::#const_ident), + #state_id_start..=#state_id_end => Some(&Fluid::#const_ident), }); type_from_name.extend(quote! { - #id_name => Some(Self::#const_ident), + #id_name => Some(&Self::#const_ident), }); type_from_raw_id_arms.extend(quote! { - #id_lit => Some(Self::#const_ident), + #id_lit => Some(&Self::#const_ident), }); let fluid_states = fluid.states.iter().map(|state| { @@ -595,6 +595,7 @@ pub(crate) fn build() -> TokenStream { quote! { use crate::tag::{Tagable, RegistryKey}; + use pumpkin_util::resource_location::{FromResourceLocation, ResourceLocation, ToResourceLocation}; #[derive(Clone, Debug)] pub struct PartialFluidState { @@ -673,21 +674,21 @@ pub(crate) fn build() -> TokenStream { impl Fluid { #constants - pub fn from_registry_key(name: &str) -> Option { + pub fn from_registry_key(name: &str) -> Option<&'static Self> { match name { #type_from_name _ => None } } - pub const fn from_id(id: u16) -> Option { + pub const fn from_id(id: u16) -> Option<&'static Self> { match id { #type_from_raw_id_arms _ => None } } #[allow(unreachable_patterns)] - pub const fn from_state_id(id: u16) -> Option { + pub const fn from_state_id(id: u16) -> Option<&'static Self> { match id { #fluid_from_state_id _ => None @@ -740,6 +741,18 @@ pub(crate) fn build() -> TokenStream { } } + impl ToResourceLocation for &'static Fluid { + fn to_resource_location(&self) -> ResourceLocation { + ResourceLocation::vanilla(self.name) + } + } + + impl FromResourceLocation for &'static Fluid { + fn from_resource_location(resource_location: &ResourceLocation) -> Option { + Fluid::from_registry_key(&resource_location.path) + } + } + impl FluidStateRef { pub fn get_state(&self) -> FluidState { let partial_state = &FLUID_STATES[self.state_idx as usize]; diff --git a/pumpkin-data/src/blocks.rs b/pumpkin-data/src/blocks.rs index f13877b73..2cb8e0a3b 100644 --- a/pumpkin-data/src/blocks.rs +++ b/pumpkin-data/src/blocks.rs @@ -3,7 +3,11 @@ use crate::{ block_properties::get_state_by_state_id, tag::{RegistryKey, Tagable}, }; -use pumpkin_util::{loot_table::LootTable, math::experience::Experience}; +use pumpkin_util::{ + loot_table::LootTable, + math::experience::Experience, + resource_location::{FromResourceLocation, ResourceLocation, ToResourceLocation}, +}; #[derive(Debug)] pub struct Block { @@ -41,6 +45,18 @@ impl Tagable for Block { } } +impl ToResourceLocation for &'static Block { + fn to_resource_location(&self) -> ResourceLocation { + ResourceLocation::vanilla(self.name) + } +} + +impl FromResourceLocation for &'static Block { + fn from_resource_location(resource_location: &ResourceLocation) -> Option { + Block::from_registry_key(&resource_location.path) + } +} + impl Block { pub fn is_waterlogged(&self, state_id: u16) -> bool { self.properties(state_id).is_some_and(|properties| { diff --git a/pumpkin-util/src/resource_location.rs b/pumpkin-util/src/resource_location.rs index b9956e90c..555e3629c 100644 --- a/pumpkin-util/src/resource_location.rs +++ b/pumpkin-util/src/resource_location.rs @@ -79,3 +79,11 @@ impl<'de> Deserialize<'de> for ResourceLocation { deserializer.deserialize_str(ResourceLocationVisitor) } } + +pub trait ToResourceLocation: Sized { + fn to_resource_location(&self) -> ResourceLocation; +} + +pub trait FromResourceLocation: Sized { + fn from_resource_location(resource_location: &ResourceLocation) -> Option; +} diff --git a/pumpkin/src/entity/mod.rs b/pumpkin/src/entity/mod.rs index ba4c656d6..f7c3e19c9 100644 --- a/pumpkin/src/entity/mod.rs +++ b/pumpkin/src/entity/mod.rs @@ -735,7 +735,7 @@ impl Entity { let fluid = world.get_fluid(&pos).await; world .block_registry - .on_entity_collision_fluid(&fluid, entity) + .on_entity_collision_fluid(fluid, entity) .await; continue; } @@ -749,7 +749,7 @@ impl Entity { let fluid = world.get_fluid(&pos).await; world .block_registry - .on_entity_collision_fluid(&fluid, entity) + .on_entity_collision_fluid(fluid, entity) .await; break; } @@ -762,7 +762,7 @@ impl Entity { let fluid = world.get_fluid(&pos).await; world .block_registry - .on_entity_collision_fluid(&fluid, entity) + .on_entity_collision_fluid(fluid, entity) .await; } } diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index b9a1ac8ca..e42a64b0e 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -626,9 +626,9 @@ impl World { for scheduled_tick in fluids_to_tick { let fluid = self.get_fluid(&scheduled_tick.block_pos).await; - if let Some(pumpkin_fluid) = self.block_registry.get_pumpkin_fluid(&fluid) { + if let Some(pumpkin_fluid) = self.block_registry.get_pumpkin_fluid(fluid) { pumpkin_fluid - .on_scheduled_tick(self, &fluid, &scheduled_tick.block_pos) + .on_scheduled_tick(self, fluid, &scheduled_tick.block_pos) .await; } } @@ -1838,7 +1838,7 @@ impl World { self.block_registry .on_placed_fluid( self, - &new_fluid, + new_fluid, block_state_id, position, replaced_block_state_id, @@ -2017,9 +2017,9 @@ impl World { get_block_by_state_id(id).unwrap_or(&Block::AIR) } - pub async fn get_fluid(&self, position: &BlockPos) -> pumpkin_data::fluid::Fluid { + pub async fn get_fluid(&self, position: &BlockPos) -> &'static pumpkin_data::fluid::Fluid { let id = self.get_block_state_id(position).await; - let fluid = Fluid::from_state_id(id).ok_or(Fluid::EMPTY); + let fluid = Fluid::from_state_id(id).ok_or(&Fluid::EMPTY); if let Ok(fluid) = fluid { return fluid; } @@ -2033,13 +2033,13 @@ impl World { .find(|p| p.0 == "waterlogged") .map(|(_, value)| { if value == true.to_string() { - Fluid::FLOWING_WATER + &Fluid::FLOWING_WATER } else { - Fluid::EMPTY + &Fluid::EMPTY } }) }) - .unwrap_or(Fluid::EMPTY) + .unwrap_or(&Fluid::EMPTY) } pub async fn get_block_state_id(&self, position: &BlockPos) -> BlockStateId { @@ -2095,10 +2095,10 @@ impl World { } if let Some(neighbor_pumpkin_fluid) = - self.block_registry.get_pumpkin_fluid(&neighbor_fluid) + self.block_registry.get_pumpkin_fluid(neighbor_fluid) { neighbor_pumpkin_fluid - .on_neighbor_update(self, &neighbor_fluid, &neighbor_pos, false) + .on_neighbor_update(self, neighbor_fluid, &neighbor_pos, false) .await; } }