Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add noteblocks and instruments #133

Merged
merged 9 commits into from
Apr 7, 2024
140 changes: 140 additions & 0 deletions crates/blocks/src/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ noop_block_transform!(
ButtonFace,
LeverFace,
ComparatorMode,
Instrument,
);

impl BlockTransform for BlockDirection {
Expand Down Expand Up @@ -1191,6 +1192,145 @@ blocks! {
},
get_name: "iron_trapdoor",
},
NoteBlock {
props: {
instrument: Instrument,
note: u32,
powered: bool
},
get_id: {
instrument.get_id() * 50
+ note * 2
+ !powered as u32
+ 281
},
from_id_offset: 281,
from_id(id): 281..=1080 => {
instrument: Instrument::from_id((id >> 1) / 25),
note: (id >> 1) % 25,
powered: (id & 1) == 0
},
from_names(_name): {
"note_block" => {
instrument: Instrument::Harp,
note: 0,
powered: false
}
},
get_name: "note_block",
solid: true,
cube: true,
},
Clay {
props: {},
get_id: 4016,
from_id(_id): 4016 => {},
from_names(_name): {
"clay" => {}
},
get_name: "clay",
solid: true,
cube: true,
},
GoldBlock {
props: {},
get_id: 1483,
from_id(_id): 1483 => {},
from_names(_name): {
"gold_block" => {}
},
get_name: "gold_block",
solid: true,
cube: true,
},
PackedIce {
props: {},
get_id: 8134,
from_id(_id): 8134 => {},
from_names(_name): {
"packed_ice" => {}
},
get_name: "packed_ice",
solid: true,
cube: true,
},
BoneBlock {
props: {},
get_id: 9507,
from_id(_id): 9506..=9508 => {},
from_names(_name): {
"bone_block" => {}
},
get_name: "bone_block",
solid: true,
cube: true,
},
IronBlock {
props: {},
get_id: 1484,
from_id(_id): 1484 => {},
from_names(_name): {
"iron_block" => {}
},
get_name: "iron_block",
solid: true,
cube: true,
},
SoulSand {
props: {},
get_id: 4069,
from_id(_id): 4069 => {},
from_names(_name): {
"soul_sand" => {}
},
get_name: "soul_sand",
solid: true,
cube: true,
},
Pumpkin {
props: {},
get_id: 4067,
from_id(_id): 4067 => {},
from_names(_name): {
"pumpkin" => {}
},
get_name: "pumpkin",
solid: true,
cube: true,
},
EmeraldBlock {
props: {},
get_id: 5609,
from_id(_id): 5609 => {},
from_names(_name): {
"emerald_block" => {}
},
get_name: "emerald_block",
solid: true,
cube: true,
},
HayBlock {
props: {},
get_id: 8114,
from_id(_id): 8113..=8115 => {},
from_names(_name): {
"hay_block" => {}
},
get_name: "hay_block",
solid: true,
cube: true,
},
Sand {
props: {},
get_id: 66,
from_id(_id): 66 => {},
from_names(_name): {
"sand" => {}
},
get_name: "sand",
solid: true,
cube: true,
},
Unknown {
props: {
id: u32
Expand Down
155 changes: 154 additions & 1 deletion crates/blocks/src/blocks/props.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{BlockDirection, BlockProperty, BlockTransform, FlipDirection};
use super::{Block, BlockDirection, BlockProperty, BlockTransform, FlipDirection};
use std::str::FromStr;

#[derive(Copy, Clone, Debug, PartialEq, Eq, BlockProperty, BlockTransform)]
Expand Down Expand Up @@ -390,3 +390,156 @@ impl FromStr for TrapdoorHalf {
})
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Instrument {
Harp,
Basedrum,
Snare,
Hat,
Bass,
Flute,
Bell,
Guitar,
Chime,
Xylophone,
IronXylophone,
CowBell,
Didgeridoo,
Bit,
Banjo,
Pling,
}

impl Instrument {
pub fn get_id(self) -> u32 {
self as u32
}

pub fn from_id(id: u32) -> Self {
match id {
0 => Instrument::Harp,
1 => Instrument::Basedrum,
2 => Instrument::Snare,
3 => Instrument::Hat,
4 => Instrument::Bass,
5 => Instrument::Flute,
6 => Instrument::Bell,
7 => Instrument::Guitar,
8 => Instrument::Chime,
9 => Instrument::Xylophone,
10 => Instrument::IronXylophone,
11 => Instrument::CowBell,
12 => Instrument::Didgeridoo,
13 => Instrument::Bit,
14 => Instrument::Banjo,
15 => Instrument::Pling,
_ => unreachable!(),
}
}

pub fn from_block_below(block: Block) -> Instrument {
match block {
// All stone materials
Block::Stone {}
| Block::CoalBlock {}
| Block::Quartz {}
| Block::Sandstone {}
| Block::Concrete { .. }
| Block::Terracotta {}
| Block::ColoredTerracotta { .. } => Instrument::Basedrum,
// All sand/aggregate materials: ConcretePowder
Block::Sand {} => Instrument::Snare,
// All glass materials: GlassPane
Block::Glass {} | Block::StainedGlass { .. } => Instrument::Hat,
// All wood materials: Log, Plank
Block::Sign { .. }
| Block::NoteBlock { .. }
| Block::Barrel {}
| Block::Composter { .. } => Instrument::Bass,
Block::Clay {} => Instrument::Flute,
Block::GoldBlock {} => Instrument::Bell,
Block::Wool { .. } => Instrument::Guitar,
Block::PackedIce {} => Instrument::Chime,
Block::BoneBlock {} => Instrument::Xylophone,
Block::IronBlock {} => Instrument::IronXylophone,
Block::SoulSand {} => Instrument::CowBell,
Block::Pumpkin {} => Instrument::Didgeridoo,
Block::EmeraldBlock {} => Instrument::Bit,
Block::HayBlock {} => Instrument::Banjo,
Block::Glowstone { .. } => Instrument::Pling,
_ => Instrument::Harp,
}
}

pub fn to_sound_id(&self) -> i32 {
match self {
Instrument::Harp => 705,
Instrument::Basedrum => 699,
Instrument::Snare => 708,
Instrument::Hat => 706,
Instrument::Bass => 700,
Instrument::Flute => 703,
Instrument::Bell => 701,
Instrument::Guitar => 704,
Instrument::Chime => 702,
Instrument::Xylophone => 709,
Instrument::IronXylophone => 710,
Instrument::CowBell => 711,
Instrument::Didgeridoo => 712,
Instrument::Bit => 713,
Instrument::Banjo => 714,
Instrument::Pling => 707,
}
}
}

impl ToString for Instrument {
fn to_string(&self) -> String {
match self {
Instrument::Harp => "harp",
Instrument::Basedrum => "basedrum",
Instrument::Snare => "snare",
Instrument::Hat => "hat",
Instrument::Bass => "bass",
Instrument::Flute => "flute",
Instrument::Bell => "bell",
Instrument::Guitar => "guitar",
Instrument::Chime => "chime",
Instrument::Xylophone => "xylophone",
Instrument::IronXylophone => "iron_xylophone",
Instrument::CowBell => "cow_bell",
Instrument::Didgeridoo => "didgeridoo",
Instrument::Bit => "bit",
Instrument::Banjo => "banjo",
Instrument::Pling => "pling",
}
.to_owned()
}
}

impl FromStr for Instrument {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"harp" => Instrument::Harp,
"basedrum" => Instrument::Basedrum,
"snare" => Instrument::Snare,
"hat" => Instrument::Hat,
"bass" => Instrument::Bass,
"flute" => Instrument::Flute,
"bell" => Instrument::Bell,
"guitar" => Instrument::Guitar,
"chime" => Instrument::Chime,
"xylophone" => Instrument::Xylophone,
"iron_xylophone" => Instrument::IronXylophone,
"cow_bell" => Instrument::CowBell,
"didgeridoo" => Instrument::Didgeridoo,
"bit" => Instrument::Bit,
"banjo" => Instrument::Banjo,
"pling" => Instrument::Pling,
_ => return Err(()),
})
}
}
66 changes: 66 additions & 0 deletions crates/blocks/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,72 @@ items! {
from_id(_id): 640 => {},
block: true,
},
NoteBlock {
props: {},
get_id: 608,
from_id(_id): 608 => {},
block: true,
},
Clay {
props: {},
get_id: 255,
from_id(_id): 255 => {},
block: true,
},
GoldBlock {
props: {},
get_id: 67,
from_id(_id): 67 => {},
block: true,
},
PackedIce {
props: {},
get_id: 390,
from_id(_id): 390 => {},
block: true,
},
BoneBlock {
props: {},
get_id: 449,
from_id(_id): 449 => {},
block: true,
},
IronBlock {
props: {},
get_id: 65,
from_id(_id): 65 => {},
block: true,
},
SoulSand {
props: {},
get_id: 269,
from_id(_id): 269 => {},
block: true,
},
Pumpkin {
props: {},
get_id: 265,
from_id(_id): 265 => {},
block: true,
},
EmeraldBlock {
props: {},
get_id: 317,
from_id(_id): 317 => {},
block: true,
},
HayBlock {
props: {},
get_id: 372,
from_id(_id): 372 => {},
block: true,
},
Sand {
props: {},
get_id: 37,
from_id(_id): 37 => {},
block: true,
},
Unknown {
props: {
id: u32
Expand Down
Loading
Loading