Skip to content

Commit cb42582

Browse files
committed
Fixed the block macro i think
1 parent 449551a commit cb42582

File tree

9 files changed

+51
-147
lines changed

9 files changed

+51
-147
lines changed

src/bin/src/packet_handlers/play_packets/place_block.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use tracing::{debug, error, trace};
1414

1515
use ferrumc_inventories::hotbar::Hotbar;
1616
use ferrumc_inventories::inventory::Inventory;
17+
use ferrumc_world::block_id::BlockId;
1718
use once_cell::sync::Lazy;
1819
use std::collections::HashMap;
1920
use std::str::FromStr;
@@ -128,9 +129,12 @@ pub fn handle(
128129
continue 'ev_loop;
129130
}
130131

131-
if let Err(err) =
132-
chunk.set_block(x & 0xF, y as i32, z & 0xF, VarInt::new(*mapped_block_id))
133-
{
132+
if let Err(err) = chunk.set_block(
133+
x & 0xF,
134+
y as i32,
135+
z & 0xF,
136+
BlockId(*mapped_block_id as u32),
137+
) {
134138
error!("Failed to set block: {:?}", err);
135139
continue 'ev_loop;
136140
}

src/bin/src/packet_handlers/play_packets/player_action.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn handle(
4242
event.location.y as i32,
4343
event.location.z.abs() % 16,
4444
);
45-
chunk.set_block(relative_x, relative_y, relative_z, BlockData::default())?;
45+
chunk.set_block(relative_x, relative_y, relative_z, BlockId::default())?;
4646
// Save the chunk to disk
4747
state.0.world.save_chunk(Arc::new(chunk))?;
4848
for (eid, conn) in query {

src/lib/derive_macros/src/block/mod.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use proc_macro::{quote, TokenStream};
1+
use proc_macro::TokenStream;
2+
use quote::quote;
23
use simd_json::prelude::{ValueAsObject, ValueAsScalar, ValueObjectAccess};
34
use syn::parse::{Parse, ParseStream};
45
use syn::punctuated::Punctuated;
@@ -179,7 +180,7 @@ pub fn block(input: TokenStream) -> TokenStream {
179180
}
180181
} else {
181182
let res = matched[0];
182-
return quote! { ferrumc_world::block_id::BlockId($res) };
183+
quote! { BlockId(#res) }.into()
183184
}
184185
} else if filtered_names.len() > 1 {
185186
syn::Error::new_spanned(
@@ -200,7 +201,7 @@ pub fn block(input: TokenStream) -> TokenStream {
200201
.into()
201202
} else {
202203
let first = filtered_names[0].0;
203-
return quote! { ferrumc_world::block_id::BlockId($first) };
204+
quote! { BlockId(#first) }.into()
204205
}
205206
}
206207

@@ -274,26 +275,3 @@ fn pretty_print_given_props(props: Opts) -> String {
274275
}
275276
s.trim_end_matches(", ").to_string()
276277
}
277-
278-
fn get_name_from_id(id: u32) -> Option<String> {
279-
let mut buf = JSON_FILE.to_vec();
280-
let v = simd_json::to_owned_value(&mut buf).unwrap();
281-
let filtered_names = v
282-
.as_object()
283-
.unwrap()
284-
.iter()
285-
.filter(|v| {
286-
let id_in_json = v.0.parse::<u32>().unwrap();
287-
id_in_json == id
288-
})
289-
.map(|v| v.1)
290-
.collect::<Vec<_>>();
291-
if let Some(first) = filtered_names.first() {
292-
if let Some(obj) = first.as_object() {
293-
if let Some(name) = obj.get("name").and_then(|n| n.as_str()) {
294-
return Some(name.to_string());
295-
}
296-
}
297-
}
298-
None
299-
}

src/lib/derive_macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn lookup_packet(input: TokenStream) -> TokenStream {
7070
///
7171
/// Usage example:
7272
///
73-
/// ```
73+
/// ```ignore
7474
/// #[command("hello")]
7575
/// fn command(#[sender] sender: Sender) {
7676
/// sender.send_message(TextComponent::from("Hello, world!"), false);

src/lib/world/src/benches/edit_bench.rs

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use criterion::{Criterion, Throughput};
2+
use ferrumc_macros::block;
3+
use ferrumc_world::block_id::BlockId;
24
use ferrumc_world::chunk_format::Chunk;
3-
use ferrumc_world::vanilla_chunk_format::BlockData;
45
use rand::Rng;
56
use std::hint::black_box;
67

@@ -44,32 +45,14 @@ pub(crate) fn bench_edits(c: &mut Criterion) {
4445
write_group.bench_with_input("Write 0,0,0", &chunk, |b, chunk| {
4546
b.iter(|| {
4647
let mut chunk = chunk.clone();
47-
black_box(chunk.set_block(
48-
0,
49-
0,
50-
0,
51-
BlockData {
52-
name: "minecraft:bricks".to_string(),
53-
properties: None,
54-
},
55-
))
56-
.unwrap();
48+
black_box(chunk.set_block(0, 0, 0, block!("bricks"))).unwrap();
5749
});
5850
});
5951

6052
write_group.bench_with_input("Write 8,8,150", &chunk, |b, chunk| {
6153
b.iter(|| {
6254
let mut chunk = chunk.clone();
63-
black_box(chunk.set_block(
64-
8,
65-
8,
66-
150,
67-
BlockData {
68-
name: "minecraft:bricks".to_string(),
69-
properties: None,
70-
},
71-
))
72-
.unwrap();
55+
black_box(chunk.set_block(8, 8, 150, block!("bricks"))).unwrap();
7356
});
7457
});
7558

@@ -80,10 +63,7 @@ pub(crate) fn bench_edits(c: &mut Criterion) {
8063
get_rand_in_range(0, 15),
8164
get_rand_in_range(0, 15),
8265
get_rand_in_range(0, 255),
83-
BlockData {
84-
name: "minecraft:bricks".to_string(),
85-
properties: None,
86-
},
66+
block!("bricks"),
8767
))
8868
.unwrap();
8969
});
@@ -94,11 +74,7 @@ pub(crate) fn bench_edits(c: &mut Criterion) {
9474
write_group.bench_with_input("Fill", &chunk, |b, chunk| {
9575
b.iter(|| {
9676
let mut chunk = chunk.clone();
97-
black_box(chunk.fill(BlockData {
98-
name: "minecraft:bricks".to_string(),
99-
properties: None,
100-
}))
101-
.unwrap();
77+
black_box(chunk.fill(block!("bricks"))).unwrap();
10278
});
10379
});
10480

@@ -108,16 +84,7 @@ pub(crate) fn bench_edits(c: &mut Criterion) {
10884
for x in 0..16 {
10985
for y in 0..256 {
11086
for z in 0..16 {
111-
black_box(chunk.set_block(
112-
x,
113-
y,
114-
z,
115-
BlockData {
116-
name: "minecraft:bricks".to_string(),
117-
properties: None,
118-
},
119-
))
120-
.unwrap();
87+
black_box(chunk.set_block(x, y, z, block!("bricks"))).unwrap();
12188
}
12289
}
12390
}
@@ -131,15 +98,7 @@ pub(crate) fn bench_edits(c: &mut Criterion) {
13198
for x in 0..16 {
13299
for y in 0..256 {
133100
for z in 0..16 {
134-
batch.set_block(
135-
x,
136-
y,
137-
z,
138-
black_box(BlockData {
139-
name: "minecraft:bricks".to_string(),
140-
properties: None,
141-
}),
142-
);
101+
batch.set_block(x, y, z, black_box(block!("bricks")));
143102
}
144103
}
145104
}
@@ -155,19 +114,11 @@ pub(crate) fn bench_edits(c: &mut Criterion) {
155114
for y in 0..256 {
156115
for z in 0..16 {
157116
let block = if (x + y + z) % 2 == 0 {
158-
"minecraft:bricks"
117+
block!("bricks")
159118
} else {
160-
"minecraft:stone"
119+
block!("stone")
161120
};
162-
batch.set_block(
163-
x,
164-
y,
165-
z,
166-
black_box(BlockData {
167-
name: block.to_string(),
168-
properties: None,
169-
}),
170-
);
121+
batch.set_block(x, y, z, black_box(block));
171122
}
172123
}
173124
}

src/lib/world/src/chunk_format.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -266,30 +266,25 @@ impl Chunk {
266266
#[cfg(test)]
267267
mod tests {
268268
use super::*;
269+
use crate::block_id::BlockId;
270+
use ferrumc_macros::block;
269271

270272
#[test]
271273
fn test_chunk_set_block() {
272274
let mut chunk = Chunk::new(0, 0, "overworld".to_string());
273-
let block = BlockData {
274-
name: "minecraft:stone".to_string(),
275-
properties: None,
276-
}
277-
.to_block_id();
275+
let block = block!("stone");
278276
chunk.set_block(0, 0, 0, block).unwrap();
279277
assert_eq!(chunk.get_block(0, 0, 0).unwrap(), block);
280278
}
281279

282280
#[test]
283281
fn test_chunk_fill() {
284282
let mut chunk = Chunk::new(0, 0, "overworld".to_string());
285-
let stone_block = BlockData {
286-
name: "minecraft:stone".to_string(),
287-
properties: None,
288-
};
289-
chunk.fill(stone_block.clone()).unwrap();
283+
let stone_block = block!("stone");
284+
chunk.fill(stone_block).unwrap();
290285
for section in &chunk.sections {
291286
for (block, count) in &section.block_states.block_counts {
292-
assert_eq!(*block, stone_block.to_block_id());
287+
assert_eq!(*block, stone_block);
293288
assert_eq!(count, &4096);
294289
}
295290
}
@@ -312,21 +307,14 @@ mod tests {
312307
block_light: vec![255; 2048],
313308
sky_light: vec![255; 2048],
314309
};
315-
let stone_block = BlockData {
316-
name: "minecraft:stone".to_string(),
317-
properties: None,
318-
};
319-
section.fill(stone_block.clone()).unwrap();
310+
let stone_block = block!("stone");
311+
section.fill(stone_block).unwrap();
320312
assert_eq!(
321313
section.block_states.block_data,
322314
PaletteType::Single(VarInt::from(1))
323315
);
324316
assert_eq!(
325-
section
326-
.block_states
327-
.block_counts
328-
.get(&stone_block.to_block_id())
329-
.unwrap(),
317+
section.block_states.block_counts.get(&stone_block).unwrap(),
330318
&4096
331319
);
332320
}
@@ -346,12 +334,9 @@ mod tests {
346334
#[test]
347335
fn test_doesnt_fail() {
348336
let mut chunk = Chunk::new(0, 0, "overworld".to_string());
349-
let block = BlockData {
350-
name: "minecraft:stone".to_string(),
351-
properties: None,
352-
};
353-
assert!(chunk.set_block(0, 0, 0, block.clone()).is_ok());
354-
assert!(chunk.set_block(0, 0, 0, block.clone()).is_ok());
337+
let block = block!("stone");
338+
assert!(chunk.set_block(0, 0, 0, block).is_ok());
339+
assert!(chunk.set_block(0, 0, 0, block).is_ok());
355340
assert!(chunk.get_block(0, 0, 0).is_ok());
356341
}
357342
}

src/lib/world/src/edit_batch.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ use std::hash::{Hash, Hasher};
1515
///
1616
/// # Example
1717
/// ```
18+
/// # use ferrumc_macros::block;
19+
/// # use ferrumc_world::block_id::BlockId;
1820
/// # use ferrumc_world::chunk_format::Chunk;
1921
/// # use ferrumc_world::edit_batch::EditBatch;
2022
/// # use ferrumc_world::vanilla_chunk_format::BlockData;
2123
/// # let mut chunk = Chunk::new(0, 0, "overworld".to_string());
2224
/// let mut batch = EditBatch::new(&mut chunk);
23-
/// batch.set_block(1, 64, 1, BlockData { name: "minecraft:stone".to_string(), properties: None });
24-
/// batch.set_block(2, 64, 1, BlockData { name: "minecraft:bricks".to_string(), properties: None });
25+
/// batch.set_block(1, 64, 1, block!("stone"));
26+
/// batch.set_block(2, 64, 1, block!("stone"));
2527
/// batch.apply().unwrap();
2628
/// ```
2729
///
@@ -47,13 +49,9 @@ pub(crate) struct Edit {
4749
}
4850

4951
fn get_palette_hash(palette: &[VarInt]) -> i32 {
50-
let mut rolling = 0;
5152
let mut hasher = AHasher::default();
52-
for block in palette.iter() {
53-
(rolling + block.0).hash(&mut hasher);
54-
rolling = hasher.finish() as i32;
55-
}
56-
rolling
53+
palette.hash(&mut hasher);
54+
hasher.finish() as i32
5755
}
5856

5957
impl<'a> EditBatch<'a> {
@@ -75,13 +73,8 @@ impl<'a> EditBatch<'a> {
7573
/// Sets a block at the given chunk-relative coordinates.
7674
///
7775
/// This won't have any effect until `apply()` is called.
78-
pub fn set_block(&mut self, x: i32, y: i32, z: i32, block: impl Into<BlockId>) {
79-
self.edits.push(Edit {
80-
x,
81-
y,
82-
z,
83-
block: block.into(),
84-
});
76+
pub fn set_block(&mut self, x: i32, y: i32, z: i32, block: BlockId) {
77+
self.edits.push(Edit { x, y, z, block });
8578
}
8679

8780
/// Applies all edits in the batch to the chunk.

0 commit comments

Comments
 (0)