replace to static Fluid / impl ToResourceLocation and FromResourceLocation traits (#1023)

This commit is contained in:
とぴ。(おーけ
2025-07-10 01:00:52 +09:00
committed by GitHub
parent 90fe277dbe
commit 1c97edb360
5 changed files with 57 additions and 20 deletions

View File

@@ -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<Self> {
pub fn from_registry_key(name: &str) -> Option<&'static Self> {
match name {
#type_from_name
_ => None
}
}
pub const fn from_id(id: u16) -> Option<Self> {
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<Self> {
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<Self> {
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];

View File

@@ -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<Self> {
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| {

View File

@@ -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<Self>;
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}