From 84df266240dec6e4a805f442d05e64c51fa59b0f Mon Sep 17 00:00:00 2001 From: amy <144570677+amyavi@users.noreply.github.com> Date: Sun, 29 Mar 2026 15:39:30 -0300 Subject: [PATCH] feat: support trumpets in nbs v5 format --- .../chipmunk/chipmunkmod/song/Instrument.java | 8 +--- .../chipmunkmod/song/NBSConverter.java | 44 +++++++++++++------ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkmod/song/Instrument.java b/src/main/java/land/chipmunk/chipmunkmod/song/Instrument.java index d7c2f6e..79d9cdb 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/song/Instrument.java +++ b/src/main/java/land/chipmunk/chipmunkmod/song/Instrument.java @@ -30,24 +30,20 @@ public class Instrument { public final int offset; public final String sound; - private Instrument (final int id, final String name, final int offset, final String sound) { + protected Instrument (final int id, final String name, final int offset, final String sound) { this.id = id; this.name = name; this.offset = offset; this.sound = sound; } - private Instrument (final int id, final String name, final int offset) { + protected Instrument (final int id, final String name, final int offset) { this.id = id; this.name = name; this.offset = offset; this.sound = "block.note_block." + name; } - public static Instrument of (final String sound) { - return new Instrument(-1, null, 0, sound); - } - public static Instrument fromId (final int id) { return VALUES[id]; } diff --git a/src/main/java/land/chipmunk/chipmunkmod/song/NBSConverter.java b/src/main/java/land/chipmunk/chipmunkmod/song/NBSConverter.java index 19c2507..960f5f6 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/song/NBSConverter.java +++ b/src/main/java/land/chipmunk/chipmunkmod/song/NBSConverter.java @@ -4,6 +4,9 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; + +import net.minecraft.resources.Identifier; +import net.minecraft.util.Util; import net.minecraft.world.phys.Vec3; public class NBSConverter { @@ -120,12 +123,12 @@ public class NBSConverter { if (buffer.hasRemaining()) { final byte customInstrumentCount = buffer.get(); for (int i = 0; i < customInstrumentCount; i++) { - final NBSCustomInstrument customInstrument = new NBSCustomInstrument(); - customInstrument.name = getString(buffer, bytes.length); - customInstrument.file = getString(buffer, bytes.length); - customInstrument.pitch = buffer.get(); - customInstrument.key = buffer.get() != 0; - customInstruments.add(customInstrument); + final String name = getString(buffer, bytes.length); + final String file = getString(buffer, bytes.length); + final byte pitch = buffer.get(); + final boolean visible = buffer.get() != 0; // "Press piano key" + + customInstruments.add(new NBSCustomInstrument(name, file, pitch, visible)); } } @@ -145,12 +148,10 @@ public class NBSConverter { key = (double) ((note.key * 100) + note.pitch) / 100; } else { final int index = note.instrument - vanillaInstrumentCount; - if (index >= customInstruments.size()) continue; - final NBSCustomInstrument customInstrument = customInstruments.get(index); - instrument = Instrument.of(customInstrument.name); + instrument = customInstrument; key = (note.key) + (customInstrument.pitch + (double) note.pitch / 100); } @@ -220,10 +221,25 @@ public class NBSConverter { public byte stereo = 100; } - private static class NBSCustomInstrument { - public String name; - public String file; - public byte pitch = 0; - public boolean key = false; + private static class NBSCustomInstrument extends Instrument { + public final byte pitch; + public final boolean visible; + + public NBSCustomInstrument(final String name, final String optionalPath, final byte pitch, final boolean visible) { + // some implementations straight up just don't save the path, so we perform conversions on name on those + final String path = optionalPath.isBlank() ? name : optionalPath; + String sound = Util.sanitizeName(path, Identifier::isAllowedInIdentifier) + .replaceFirst("\\.ogg$", "") + .replaceFirst("^minecraft/", "") + .replace('/', '.'); + + // if there's no path, it's probably a note block sound + // this happens when you have trumpets saved in a v5 format NBS for example + if (!sound.contains(".")) sound = "block.note_block." + sound; + + super(-1, name, 0, sound); + this.pitch = pitch; + this.visible = visible; + } } }