diff --git a/src/main/java/land/chipmunk/chipmunkmod/commands/MusicCommand.java b/src/main/java/land/chipmunk/chipmunkmod/commands/MusicCommand.java index b2bc007..bb78038 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chipmunkmod/commands/MusicCommand.java @@ -24,6 +24,8 @@ import java.util.List; import static com.mojang.brigadier.arguments.BoolArgumentType.bool; import static com.mojang.brigadier.arguments.BoolArgumentType.getBool; +import static com.mojang.brigadier.arguments.FloatArgumentType.floatArg; +import static com.mojang.brigadier.arguments.FloatArgumentType.getFloat; import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.arguments.LongArgumentType.getLong; @@ -81,6 +83,7 @@ public class MusicCommand { .executes(instance::gotoCommand) ) ) + .then( literal("useCore") .then( @@ -88,6 +91,14 @@ public class MusicCommand { .executes(instance::useCore) ) ) + + .then( + literal("pitch") + .then( + argument("pitch", floatArg()) + .executes(instance::pitch) + ) + ) ); } @@ -279,4 +290,21 @@ public class MusicCommand { return 1; } + + public int pitch (CommandContext context) { + final FabricClientCommandSource source = context.getSource(); + + final float pitch = getFloat(context, "pitch"); + + SongPlayer.INSTANCE.pitch(pitch); + + source.sendFeedback( + Text.translatable( + "Set the pitch to: %s", + Text.literal(String.valueOf(pitch)) + ).formatted(Formatting.GREEN) + ); + + return 1; + } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/SongPlayer.java b/src/main/java/land/chipmunk/chipmunkmod/modules/SongPlayer.java index 2f537d9..7961201 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/SongPlayer.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/SongPlayer.java @@ -4,6 +4,7 @@ import land.chipmunk.chipmunkmod.song.Note; import land.chipmunk.chipmunkmod.song.Song; import land.chipmunk.chipmunkmod.song.SongLoaderException; import land.chipmunk.chipmunkmod.song.SongLoaderThread; +import land.chipmunk.chipmunkmod.util.MathUtilities; import lombok.Getter; import lombok.Setter; import net.kyori.adventure.text.Component; @@ -43,6 +44,8 @@ public class SongPlayer { @Getter @Setter private boolean useCore = true; + @Getter @Setter private float pitch = 0; + private final MinecraftClient client; public SongPlayer (MinecraftClient client) { @@ -204,7 +207,7 @@ public class SongPlayer { while (currentSong.reachedNextNote()) { final Note note = currentSong.getNextNote(); - final float floatingPitch = (float) Math.pow(2, (note.pitch - 12) / 12.0); + final float floatingPitch = MathUtilities.clamp((float) (0.5 * (Math.pow(2, ((note.pitch + (pitch / 10)) / 12)))), 0F, 2F); try { if (!useCore) { diff --git a/src/main/java/land/chipmunk/chipmunkmod/util/MathUtilities.java b/src/main/java/land/chipmunk/chipmunkmod/util/MathUtilities.java new file mode 100644 index 0000000..37cd5c2 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/util/MathUtilities.java @@ -0,0 +1,7 @@ +package land.chipmunk.chipmunkmod.util; + +public class MathUtilities { + public static float clamp (float value, float min, float max) { + return Math.max(Math.min(value, max), min); + } +}