Skip to content

Commit 9e9f1b5

Browse files
authored
refactor: use BlockId in more places (#3090)
* compare BlockId instead of (str) name * compare named BlockIds in Block::properties() and Block::from_properties() * compare named BlockIds instead of u16 literals for `impl BlockProperties`
1 parent 70c0f43 commit 9e9f1b5

8 files changed

Lines changed: 2845 additions & 7643 deletions

File tree

crates/pumpkin-data/src/generated/block.rs

Lines changed: 2810 additions & 7598 deletions
Large diffs are not rendered by default.

crates/pumpkin-world/src/generation/feature/features/geode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl GeodeFeature {
325325

326326
// Only place if the target block is replaceable (air/water)
327327
let is_air = place_state.is_air();
328-
let is_water = place_raw.to_block().name == "water";
328+
let is_water = place_raw.to_block_id() == BlockId::WATER;
329329

330330
if is_air || is_water {
331331
let mut final_codec = base_codec.clone();

crates/pumpkin/src/block/blocks/command.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ impl CommandBlock {
3030
) -> Option<(BlockPos, CommandBlockLikeProperties)> {
3131
let target_pos = pos.offset(dir.to_block_direction().to_offset());
3232
let (block, state_id) = world.get_block_and_state_id(&target_pos);
33-
34-
let allowed_blocks = [
35-
Block::COMMAND_BLOCK.name,
36-
Block::CHAIN_COMMAND_BLOCK.name,
37-
Block::REPEATING_COMMAND_BLOCK.name,
38-
];
39-
if !allowed_blocks.contains(&block.name) {
33+
if !matches!(
34+
block.id,
35+
BlockId::COMMAND_BLOCK
36+
| BlockId::CHAIN_COMMAND_BLOCK
37+
| BlockId::REPEATING_COMMAND_BLOCK
38+
) {
4039
return None;
4140
}
4241

crates/pumpkin/src/block/blocks/creaking_heart.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use pumpkin_data::block_properties::{
66
use pumpkin_data::item::Item;
77
use pumpkin_data::item_stack::ItemStack;
88
use pumpkin_data::sound::{Sound, SoundCategory};
9-
use pumpkin_data::{Block, BlockStateId};
9+
use pumpkin_data::{BlockId, BlockStateId};
1010
use pumpkin_macros::pumpkin_block;
1111
use pumpkin_world::world::BlockFlags;
1212

@@ -17,8 +17,8 @@ use crate::block::{BlockBehaviour, BrokenArgs, OnNeighborUpdateArgs, OnPlaceArgs
1717
pub struct CreakingHeartBlock;
1818

1919
impl CreakingHeartBlock {
20-
fn is_pale_oak_log(block: &Block) -> bool {
21-
block.name == "pale_oak_log" || block.name == "stripped_pale_oak_log"
20+
const fn is_pale_oak_log(id: BlockId) -> bool {
21+
matches!(id, BlockId::PALE_OAK_LOG | BlockId::STRIPPED_PALE_OAK_LOG)
2222
}
2323

2424
fn check_active_logs(
@@ -32,11 +32,8 @@ impl CreakingHeartBlock {
3232
Axis::Z => (pos.north(), pos.south()),
3333
};
3434

35-
let state_a = world.get_block_state(&pos_a);
36-
let state_b = world.get_block_state(&pos_b);
37-
38-
let block_a = Block::from_state_id(state_a.id);
39-
let block_b = Block::from_state_id(state_b.id);
35+
let block_a = world.get_block_state_id(&pos_a).to_block_id();
36+
let block_b = world.get_block_state_id(&pos_b).to_block_id();
4037

4138
Self::is_pale_oak_log(block_a) && Self::is_pale_oak_log(block_b)
4239
}

crates/pumpkin/src/block/blocks/fire/fire.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl FireBlock {
104104

105105
for dir in BlockDirection::all() {
106106
let neighbor_block = world.get_block(&pos.offset(dir.to_offset()));
107-
if world.get_fluid(&pos.offset(dir.to_offset())).name != Fluid::EMPTY.name {
107+
if *world.get_fluid(&pos.offset(dir.to_offset())) != Fluid::EMPTY {
108108
continue; // Skip if there is a fluid
109109
}
110110
if let Some(flammable) = &neighbor_block.flammable {

crates/pumpkin/src/block/blocks/sniffer_egg.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use pumpkin_data::block_properties::{BlockProperties, SnifferEggLikeProperties};
22
use pumpkin_data::item::Item;
33
use pumpkin_data::item_stack::ItemStack;
44
use pumpkin_data::sound::{Sound, SoundCategory};
5-
use pumpkin_data::{Block, BlockStateId};
5+
use pumpkin_data::{BlockId, BlockStateId};
66
use pumpkin_macros::pumpkin_block;
77
use pumpkin_world::tick::TickPriority;
88
use pumpkin_world::world::BlockFlags;
@@ -19,8 +19,8 @@ impl SnifferEggBlock {
1919
) -> bool {
2020
let below_pos = pos.down();
2121
let state = world.get_block_state(&below_pos);
22-
let block = Block::from_state_id(state.id);
23-
block.name == "moss_block"
22+
let block = BlockId::from_state_id(state.id);
23+
block == BlockId::MOSS_BLOCK
2424
}
2525

2626
const fn get_hatch_delay(on_moss: bool) -> u8 {

crates/pumpkin/src/item/items/ignite/ignition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Ignition {
2222
where
2323
F: FnOnce(Arc<World>, BlockPos, BlockStateId),
2424
{
25-
if world.get_fluid(&location).name != Fluid::EMPTY.name {
25+
if *world.get_fluid(&location) != Fluid::EMPTY {
2626
return false;
2727
}
2828
let fire_block = FireBlockBase::get_fire_type(world, &fire_pos);

tools/pumpkin-codegen/src/block.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,9 @@ impl ToTokens for BlockPropertyStruct {
222222
}
223223
});
224224

225-
let block_ids = self
226-
.data
227-
.blocks
228-
.iter()
229-
.map(|(_, id)| *id)
230-
.collect::<Vec<_>>();
225+
let block_ids = self.data.blocks.iter().map(|(name, _)| {
226+
Ident::new(&const_block_name_from_block_name(name), Span::call_site())
227+
});
231228

232229
let to_index_logic = self.data.variant_mappings.iter().rev().map(|entry| {
233230
let field = Ident::new_raw(&entry.original_name, Span::call_site());
@@ -389,7 +386,7 @@ impl ToTokens for BlockPropertyStruct {
389386
#[inline]
390387
#[allow(clippy::manual_range_patterns)]
391388
fn handles_block_id(block_id: BlockId) -> bool where Self: Sized {
392-
matches!(block_id.as_u16(), #(#block_ids)|*)
389+
matches!(block_id, #(BlockId::#block_ids)|*)
393390
}
394391

395392
fn to_state_id(&self, block: &Block) -> BlockStateId {
@@ -433,7 +430,7 @@ impl ToTokens for BlockPropertyStruct {
433430
#[allow(clippy::manual_range_patterns)]
434431
fn from_props(props: &[(&str, &str)], block: &Block) -> Self {
435432
#[cfg(debug_assertions)]
436-
if !matches!(block.id.as_u16(), #(#block_ids)|*) {
433+
if !Self::handles_block_id(block.id) {
437434
panic!("{} is not a valid block for {}", block.name, #struct_name);
438435
}
439436
let mut block_props = Self::default(block);
@@ -1072,21 +1069,18 @@ pub fn build() -> TokenStream {
10721069
Span::call_site(),
10731070
);
10741071

1075-
for (block_name, id) in &property_group.blocks {
1076-
let const_block_name = Ident::new(
1077-
&const_block_name_from_block_name(block_name),
1078-
Span::call_site(),
1079-
);
1080-
let id_lit = LitInt::new(&id.to_string(), Span::call_site());
1081-
1082-
block_properties_from_state_and_block_id_arms.push(quote! {
1083-
#id_lit => Box::new(#property_name::from_state_id(state_id, &Block::#const_block_name)),
1084-
});
1072+
let idents: Box<_> = property_group
1073+
.blocks
1074+
.iter()
1075+
.map(|(name, _)| Ident::new(&const_block_name_from_block_name(name), Span::call_site()))
1076+
.collect();
10851077

1086-
block_properties_from_props_and_name_arms.push(quote! {
1087-
#id_lit => Box::new(#property_name::from_props(props, &Block::#const_block_name)),
1088-
});
1089-
}
1078+
block_properties_from_state_and_block_id_arms.push(quote! {
1079+
#(BlockId::#idents)|* => Box::new(#property_name::from_state_id(state_id, self)),
1080+
});
1081+
block_properties_from_props_and_name_arms.push(quote! {
1082+
#(BlockId::#idents)|* => Box::new(#property_name::from_props(props, self)),
1083+
});
10901084

10911085
block_properties.push(BlockPropertyStruct {
10921086
data: property_group,
@@ -1351,7 +1345,7 @@ pub fn build() -> TokenStream {
13511345
#[track_caller]
13521346
#[doc = r" Get the properties of the block."]
13531347
pub fn properties(&self, state_id: BlockStateId) -> Option<Box<dyn BlockProperties>> {
1354-
Some(match self.id.as_u16() {
1348+
Some(match self.id {
13551349
#(#block_properties_from_state_and_block_id_arms)*
13561350
_ => return None,
13571351
})
@@ -1360,7 +1354,7 @@ pub fn build() -> TokenStream {
13601354
#[track_caller]
13611355
#[doc = r" Get the properties of the block."]
13621356
pub fn from_properties(&self, props: &[(&str, &str)]) -> Box<dyn BlockProperties> {
1363-
match self.id.as_u16() {
1357+
match self.id {
13641358
#(#block_properties_from_props_and_name_arms)*
13651359
_ => panic!("Invalid props")
13661360
}

0 commit comments

Comments
 (0)