Skip to content

Commit

Permalink
Add SoundSystemMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Dec 31, 2024
1 parent ef65215 commit 17bdb1d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.mmodding.mmodding_lib.mixin.injectors.client;

import com.mmodding.mmodding_lib.library.sounds.client.SoundQueue;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.client.sound.SoundSystem;
import net.minecraft.client.sound.TickableSoundInstance;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.ArrayList;
import java.util.List;

@Mixin(SoundSystem.class)
public abstract class SoundSystemMixin {

@Unique
private final List<TickableSoundInstance> soundQueues = new ArrayList<>();

@Shadow
public abstract boolean isPlaying(SoundInstance sound);

@Inject(method = "play(Lnet/minecraft/client/sound/SoundInstance;)V", at = @At("HEAD"), cancellable = true)
private void injectSoundQueueToTickingSounds(SoundInstance sound, CallbackInfo ci) {
if (sound instanceof SoundQueue queue) {
this.soundQueues.add(queue);
ci.cancel();
}
}

@Inject(method = "tick()V", at = @At("HEAD"))
private void tickSoundQueues(CallbackInfo ci) {
for (TickableSoundInstance queue : this.soundQueues) {
queue.tick();
if (queue.isDone()) {
this.soundQueues.remove(queue);
}
}
this.soundQueues.forEach(TickableSoundInstance::tick);
}
}
1 change: 1 addition & 0 deletions src/main/resources/mmodding_lib.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"injectors.client.ItemRendererMixin",
"injectors.client.LivingEntityRendererMixin",
"injectors.client.ModelLoaderMixin",
"injectors.client.SoundSystemMixin",
"injectors.client.StuckArrowsFeatureRendererMixin",
"injectors.client.StuckObjectsFeatureRendererMixin",
"injectors.client.TridentEntityRendererMixin",
Expand Down

0 comments on commit 17bdb1d

Please sign in to comment.