From 8f6fa02860e51dd39b0577348da04e5a38d9da74 Mon Sep 17 00:00:00 2001 From: RitikShah Date: Tue, 8 Oct 2024 21:18:36 -0500 Subject: [PATCH] fix: adjust --- .gitattributes | 1 + beet.yml | 2 +- src/generate_songs.bolt | 8 ++++---- src/note.py | 44 +++++++++++++++++++++++++++++++---------- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/.gitattributes b/.gitattributes index dfe0770..8d0d1e6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ # Auto detect text files and perform LF normalization * text=auto +*.bolt linguist-language=mcfunction diff --git a/beet.yml b/beet.yml index a37ba04..961f456 100644 --- a/beet.yml +++ b/beet.yml @@ -4,7 +4,7 @@ data_pack: name: nbs-smithed-summit-24 description: "Data pack for NBS on the Smithed Summit 2024" load: - - "data/nbs/module": src + - data/nbs/module: src resource_pack: name: nbs-smithed-summit-24 diff --git a/src/generate_songs.bolt b/src/generate_songs.bolt index e9dadac..cc7ec96 100644 --- a/src/generate_songs.bolt +++ b/src/generate_songs.bolt @@ -71,12 +71,12 @@ for song_stuff in enumerate(SONGS.glob("*.nbs")): notes = song[1] function f"nbs:{song_name}": execute store result storage nbs:main songindex int 1 run scoreboard players get songindex nbs - function ~/chord/tick: + function f"nbs:{song_name}/chord/{tick}": for note in notes: instruments.add(note.instrument) - note.play_speakers() - note.play_loudspeakers() - note.play_headphones() + note.play_speakers(ctx) + note.play_loudspeakers(ctx) + note.play_headphones(ctx) for instrument in instruments: sound_path = EXTRA_NOTES.get(instrument) diff --git a/src/note.py b/src/note.py index fc266f9..8791ea6 100644 --- a/src/note.py +++ b/src/note.py @@ -10,7 +10,10 @@ from typing import Any, Iterator, List, Tuple import pynbs +from beet import Context from beet.core.utils import FileSystemPath +from bolt import Runtime +from mecha import Mecha NBS_DEFAULT_INSTRUMENTS = [ "block.note_block.harp", @@ -61,7 +64,7 @@ class Note: pitch: float = 1 panning: float = 0 - def play_speakers(self, stereo_separation: float = 4): + def play_speakers(self, ctx: Context, stereo_separation: float = 4): """ Play a sound that can be heard in a small radius by all players in range. """ @@ -101,11 +104,11 @@ def rolloff_curve(x: float) -> float: stereo_offset = self.panning * stereo_separation // 2 position = f"^{stereo_offset} ^ ^" - return self.play( - execute_as="@e[tag=nbs_speaker]", radius=radius, position=position + self.play( + ctx, execute_as="@e[tag=nbs_speaker]", radius=radius, position=position ) - def play_loudspeakers(self, stereo_separation: float = 8): + def play_loudspeakers(self, ctx: Context, stereo_separation: float = 8): """ Play a sound that can be heard in a large radius by all players in range. """ @@ -133,7 +136,8 @@ def play_loudspeakers(self, stereo_separation: float = 8): stereo_offset = self.panning * stereo_separation // 2 position = f"^{stereo_offset} ^ ^" - return self.play( + self.play( + ctx, execute_as="@e[tag=nbs_loudspeaker]", radius=radius, volume=volume, @@ -141,7 +145,7 @@ def play_loudspeakers(self, stereo_separation: float = 8): position=position, ) - def play_headphones(self): + def play_headphones(self, ctx: Context): """ Play a sound that can be globally heard by players with headphones. """ @@ -156,7 +160,8 @@ def play_headphones(self): tag = "nbs_headphones" position = f"0 64 {-self.panning * 256}" - return self.play( + self.play( + ctx, min_volume=min_volume, volume=volume, tag=tag, @@ -165,6 +170,7 @@ def play_headphones(self): def play( self, + ctx: Context, execute_as: str | None = None, radius: float | None = None, tag: str | None = None, @@ -172,7 +178,7 @@ def play( position: str = "^ ^ ^", volume: float = 1, min_volume: float = 1, - ) -> str: + ): """Return the /playsound command to play the note for the given player.""" selector_arguments = [] @@ -186,10 +192,28 @@ def play( if execute_as is not None: execute_command = f"execute as {execute_as} at @n run" - playsound_command = f"playsound {self.instrument} {source} {target_selector} {position} {volume} {self.pitch} {min_volume}" + if self.pitch > 2: + # print("Warning pitch", self.pitch, "is larger than 2", source) + pitch = 2 + else: + pitch = self.pitch + + if min_volume > 1: + # print("Warning min_volume", min_volume, "is larger than 1", target_selector) + min_volume = 1 + + playsound_command = f"playsound {self.instrument} {source} {target_selector} {position} {volume} {pitch} {min_volume}" full_command = " ".join([execute_command, playsound_command]) - return full_command + + mc = ctx.inject(Mecha) + runtime = ctx.inject(Runtime) + + try: + runtime.commands.append(mc.parse(full_command, using="command")) + except Exception as e: + print(full_command) + raise e def load_nbs(filename: FileSystemPath) -> Iterator[Tuple[int, List["Note"]]]: