Skip to content

Commit

Permalink
More detailed sound played event
Browse files Browse the repository at this point in the history
  • Loading branch information
Deftu committed Dec 23, 2024
1 parent 7cdcb6c commit a4cd653
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 98 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.polyfrost.oneconfig.api.event.v1.events;

public class SoundPlayedEvent implements Event {

private final String name;
private final Object category;
private Object sound;

public SoundPlayedEvent(String name, Object category, Object sound) {
this.name = name;
this.category = category;
this.sound = sound;
}

public String getName() {
return name;
}

@SuppressWarnings("unchecked")
public <T> T getCategory() {
return (T) category;
}

@SuppressWarnings("unchecked")
public <T> T getSound() {
return (T) sound;
}

}
Original file line number Diff line number Diff line change
@@ -1,53 +1,31 @@
/*
* This file is part of OneConfig.
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
* Copyright (C) 2021~2024 Polyfrost.
* <https://polyfrost.org> <https://github.com/Polyfrost/>
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
* General Public License as published by the Free Software Foundation, AND
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
* either version 1.0 of the Additional Terms, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License. If not, see <https://www.gnu.org/licenses/>. You should
* have also received a copy of the Additional Terms Applicable
* to OneConfig, as published by Polyfrost. If not, see
* <https://polyfrost.org/legal/oneconfig/additional-terms>
*/

package org.polyfrost.oneconfig.internal.mixin;

import net.minecraft.client.audio.ISound;
import net.minecraft.client.audio.SoundEventAccessorComposite;
import net.minecraft.client.audio.SoundManager;
import org.polyfrost.oneconfig.api.event.v1.EventManager;
import org.polyfrost.oneconfig.api.event.v1.events.SoundPlayEvent;
import org.polyfrost.oneconfig.api.event.v1.events.SoundPlayedEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Coerce;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

@Mixin(SoundManager.class)
public class SoundManagerMixin {
@Inject(method = "playSound", at = @At(value = "INVOKE",
//#if MC<=11202
target = "Lnet/minecraft/client/audio/SoundHandler;getSound(Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/client/audio/SoundEventAccessorComposite;"
//#else
//$$ target = "Lnet/minecraft/client/audio/ISound;getSoundLocation()Lnet/minecraft/util/ResourceLocation;"
//#endif
), cancellable = true)
private void onPlaySound(@Coerce Object sound, CallbackInfo ci) {
if (sound == null) return;
SoundPlayEvent ev = new SoundPlayEvent(sound);
EventManager.INSTANCE.post(ev);
if (ev.cancelled) ci.cancel();

@ModifyVariable(
method = "playSound",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/audio/SoundHandler;getSound(Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/client/audio/SoundEventAccessorComposite;"
),
argsOnly = true
)
private ISound oneconfig$playSound(ISound value) {
SoundEventAccessorComposite accessor = ((SoundManager) (Object) this).sndHandler.getSound(value.getSoundLocation());
String name = value.getSoundLocation().getResourcePath();
SoundPlayedEvent event = new SoundPlayedEvent(name, (accessor == null ? null : accessor.getSoundCategory()), value);
EventManager.INSTANCE.post(event);
return event.getSound();
}

}
1 change: 1 addition & 0 deletions versions/src/main/resources/mixins.oneconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"ShaderGroupAccessor",
"SoundManagerMixin",
"WorldClientMixin",
"SoundManagerMixin",
"compat.OptifineConfigMixin",
"compat.VigilantCompatMixin"
]
Expand Down

0 comments on commit a4cd653

Please sign in to comment.