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