Skip to content

Commit

Permalink
MusicTweaks - I made it way better.
Browse files Browse the repository at this point in the history
and it only cost me my sanity :^)
  • Loading branch information
0xTas committed Jan 1, 2024
1 parent 0ea4392 commit 1675e2f
Show file tree
Hide file tree
Showing 5 changed files with 735 additions and 146 deletions.
53 changes: 0 additions & 53 deletions src/main/java/dev/stardust/mixin/PositionedSoundInstanceMixin.java

This file was deleted.

22 changes: 12 additions & 10 deletions src/main/java/dev/stardust/mixin/SoundSystemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ private void mixinTick(CallbackInfo ci) {
if (sound == null) continue;

String location = sound.getLocation().toString();
if (!location.startsWith("minecraft:sounds/music/")) continue;
if (!location.startsWith("minecraft:sounds/music/") && !sound.toString().contains("minecraft:records/")) continue;
Channel.SourceManager sourceManager = this.sources.get(instance);
songID = location.substring(location.lastIndexOf('/')+1);
songID = location.substring(location.lastIndexOf('/') + 1);

playing = true;
if (sourceManager == null) continue;
Source source = ((SourceManagerAccessor) sourceManager).getSource();

if (source == null) continue;

playing = true;
if (tweaks.isActive() && !tweaks.randomPitch()) {
this.dirtyPitch = true;
source.setPitch(1.0f + tweaks.getPitchAdjustment());
Expand All @@ -76,12 +76,14 @@ private void mixinTick(CallbackInfo ci) {
this.totalTicksPlaying = 0;
}

if (tweaks.isActive() && totalTicksPlaying % 30 == 0 && tweaks.shouldDisplayNowPlaying() && songID != null) {
String songName = tweaks.getSongName(songID);
switch (tweaks.getDisplayMode()) {
case Chat -> tweaks.sendNowPlayingMessage(songName);
// See NarratorManagerMixin.java lol
case Record -> tweaks.getClient().inGameHud.setRecordPlayingOverlay(Text.of(songName));
if (tweaks.isActive() && this.totalTicksPlaying % 30 == 0 && tweaks.shouldDisplayNowPlaying() && songID != null) {
if (this.totalTicksPlaying <= 90 || !tweaks.shouldFadeOut()) {
String songName = tweaks.getSongName(songID);
switch (tweaks.getDisplayMode()) {
case Chat -> tweaks.sendNowPlayingMessage(songName);
// See NarratorManagerMixin.java lol
case Record -> tweaks.getClient().inGameHud.setRecordPlayingOverlay(Text.of(songName));
}
}
}
}
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/dev/stardust/mixin/WeightedSoundSetMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dev.stardust.mixin;

import java.util.List;
import dev.stardust.modules.MusicTweaks;
import net.minecraft.client.sound.Sound;
import net.minecraft.sound.SoundCategory;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import io.netty.util.internal.ThreadLocalRandom;
import org.spongepowered.asm.mixin.injection.At;
import net.minecraft.client.sound.SoundContainer;
import net.minecraft.client.sound.WeightedSoundSet;
import org.spongepowered.asm.mixin.injection.Inject;
import meteordevelopment.meteorclient.systems.modules.Modules;
import net.minecraft.util.math.floatprovider.ConstantFloatProvider;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;


/**
* @author Tas [0xTas] <[email protected]>
**/
@Mixin(WeightedSoundSet.class)
public abstract class WeightedSoundSetMixin implements SoundContainer<Sound> {
@Shadow
@Final
private List<SoundContainer<Sound>> sounds;

// See MusicTweaks.java
@Inject(method = "getSound(Lnet/minecraft/util/math/random/Random;)Lnet/minecraft/client/sound/Sound;", at = @At("HEAD"), cancellable = true)
private void mixinGetSound(net.minecraft.util.math.random.Random random, CallbackInfoReturnable<Sound> cir) {
MusicTweaks tweaks = Modules.get().get(MusicTweaks.class);
if (tweaks == null || !tweaks.isActive()) return;

boolean overwrite = false;
for (SoundContainer<Sound> sound : this.sounds) {
String id = sound.getSound(random).toString();

if (id.contains("minecraft:music/")) {
overwrite = true;
break;
}
}

if (!overwrite) return;
List<String> soundIDs = tweaks.getSoundSet();
if (soundIDs.isEmpty()) return;

float adjustedPitch;
if (tweaks.randomPitch()) {
adjustedPitch = 1.0f + tweaks.getRandomPitch();
} else {
adjustedPitch = 1.0f + tweaks.getPitchAdjustment();
}
float adjustedVolume = tweaks.getClient().options.getSoundVolume(SoundCategory.MUSIC) + tweaks.getVolumeAdjustment();

cir.setReturnValue(
new Sound(
soundIDs.get(ThreadLocalRandom.current().nextInt(soundIDs.size())),
ConstantFloatProvider.create(adjustedVolume),
ConstantFloatProvider.create(adjustedPitch),
this.getWeight(), Sound.RegistrationType.SOUND_EVENT,
true, true, 16
)
);
}
}
Loading

0 comments on commit 1675e2f

Please sign in to comment.