Skip to content

Commit

Permalink
parse Biome enum at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Jan 11, 2025
1 parent 4f37084 commit 6415f69
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 20 deletions.
1 change: 1 addition & 0 deletions assets/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["badlands","bamboo_jungle","basalt_deltas","beach","birch_forest","cherry_grove","cold_ocean","crimson_forest","dark_forest","deep_cold_ocean","deep_dark","deep_frozen_ocean","deep_lukewarm_ocean","deep_ocean","desert","dripstone_caves","end_barrens","end_highlands","end_midlands","eroded_badlands","flower_forest","forest","frozen_ocean","frozen_peaks","frozen_river","grove","ice_spikes","jagged_peaks","jungle","lukewarm_ocean","lush_caves","mangrove_swamp","meadow","mushroom_fields","nether_wastes","ocean","old_growth_birch_forest","old_growth_pine_taiga","old_growth_spruce_taiga","pale_garden","plains","river","savanna","savanna_plateau","small_end_islands","snowy_beach","snowy_plains","snowy_slopes","snowy_taiga","soul_sand_valley","sparse_jungle","stony_peaks","stony_shore","sunflower_plains","swamp","taiga","the_end","the_void","warm_ocean","warped_forest","windswept_forest","windswept_gravelly_hills","windswept_hills","windswept_savanna","wooded_badlands"]
20 changes: 20 additions & 0 deletions pumpkin-data/build/biome.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use proc_macro2::TokenStream;
use quote::quote;

use crate::array_to_tokenstream;

pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=assets/biome.json");

let biomes: Vec<String> = serde_json::from_str(include_str!("../../assets/biome.json"))
.expect("Failed to parse entity_pose.json");
let variants = array_to_tokenstream(biomes);

quote! {
#[derive(Clone, Copy)]
#[repr(u8)]
pub enum Biome {
#variants
}
}
}
2 changes: 2 additions & 0 deletions pumpkin-data/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use heck::ToPascalCase;
use proc_macro2::{Span, TokenStream};
use syn::Ident;

mod biome;
mod chunk_status;
mod entity_pose;
mod entity_type;
Expand All @@ -31,6 +32,7 @@ pub fn main() {
write_generated_file(world_event::build(), "world_event.rs");
write_generated_file(entity_type::build(), "entity_type.rs");
write_generated_file(noise_parmeter::build(), "noise_parmeter.rs");
write_generated_file(biome::build(), "biome.rs");
}

pub fn array_to_tokenstream(array: Vec<String>) -> TokenStream {
Expand Down
7 changes: 3 additions & 4 deletions pumpkin-data/build/entity_pose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use crate::array_to_tokenstream;
pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=assets/entity_pose.json");

let sound_categories: Vec<String> =
serde_json::from_str(include_str!("../../assets/entity_pose.json"))
.expect("Failed to parse entity_pose.json");
let variants = array_to_tokenstream(sound_categories);
let poses: Vec<String> = serde_json::from_str(include_str!("../../assets/entity_pose.json"))
.expect("Failed to parse entity_pose.json");
let variants = array_to_tokenstream(poses);

quote! {
#[derive(Clone, Copy)]
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-data/build/noise_parmeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn build() -> TokenStream {
let mut variants = TokenStream::new();

for (name, paremter) in json.iter() {
let raw_name = name;
let raw_name = format!("minecraft:{name}");
let name = ident(name.to_uppercase());
let first_octave = paremter.first_octave;
let amplitudes = &paremter.amplitudes;
Expand Down
1 change: 1 addition & 0 deletions pumpkin-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod sound {
}

pub mod chunk {
include!(concat!(env!("OUT_DIR"), "/biome.rs"));
include!(concat!(env!("OUT_DIR"), "/noise_parmeter.rs"));
include!(concat!(env!("OUT_DIR"), "/chunk_status.rs"));
}
Expand Down
12 changes: 1 addition & 11 deletions pumpkin-world/src/biome.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use enum_dispatch::enum_dispatch;
use serde::{Deserialize, Serialize};

// TODO make this work with the protocol
// Send by the registry
#[derive(Serialize, Deserialize, Clone, Copy)]
#[non_exhaustive]
pub enum Biome {
Plains,
SnowyTiga,
// TODO list all Biomes
}
use pumpkin_data::chunk::Biome;

#[derive(Clone)]
#[enum_dispatch(BiomeSupplierImpl)]
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/generation/generator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use noise::Perlin;
use pumpkin_data::chunk::Biome;
use pumpkin_util::math::vector2::Vector2;
use pumpkin_util::math::vector3::Vector3;

use crate::biome::Biome;
use crate::block::block_state::BlockState;
use crate::chunk::{ChunkData, Subchunks};
use crate::coordinates::{BlockCoordinates, ChunkRelativeBlockCoordinates, XZBlockCoordinates};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use noise::Perlin;
use pumpkin_data::chunk::Biome;
use pumpkin_macros::block_state;
use pumpkin_util::math::vector2::Vector2;
use rand::Rng;

use crate::{
biome::Biome,
chunk::Subchunks,
coordinates::{BlockCoordinates, ChunkRelativeBlockCoordinates, XZBlockCoordinates},
generation::{
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/generation/implementation/superflat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pumpkin_data::chunk::Biome;
use pumpkin_util::math::vector2::Vector2;

use crate::{
biome::Biome,
block::block_state::BlockState,
coordinates::XZBlockCoordinates,
generation::{
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/generation/implementation/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::{

use dashmap::{DashMap, Entry};
use num_traits::Zero;
use pumpkin_data::chunk::Biome;
use pumpkin_util::math::{vector2::Vector2, vector3::Vector3};

use crate::{
biome::Biome,
block::block_state::BlockState,
chunk::{ChunkData, Subchunks},
coordinates::{
Expand Down

0 comments on commit 6415f69

Please sign in to comment.