diff --git a/modules/events/src/main/java/org/polyfrost/oneconfig/api/event/v1/events/SoundPlayEvent.java b/modules/events/src/main/java/org/polyfrost/oneconfig/api/event/v1/events/SoundPlayEvent.java deleted file mode 100644 index c26f8cdcf..000000000 --- a/modules/events/src/main/java/org/polyfrost/oneconfig/api/event/v1/events/SoundPlayEvent.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * This file is part of OneConfig. - * OneConfig - Next Generation Config Library for Minecraft: Java Edition - * Copyright (C) 2021~2024 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 . You should - * have also received a copy of the Additional Terms Applicable - * to OneConfig, as published by Polyfrost. If not, see - * - */ - -package org.polyfrost.oneconfig.api.event.v1.events; - -/** - * Called when a sound is played. - *
- * Due to Minecraft versioning, the sound object can be of different types. it is safe to cast this to the appropriate type for your version (e.g. ISound for 1.8.9) - */ -public class SoundPlayEvent extends Event.Cancellable { - private final Object sound; - - public SoundPlayEvent(Object sound) { - this.sound = sound; - } - - public T component1() { - return getSound(); - } - - /** - * Due to differences across Minecraft versions, this is a Duck method, meaning that it will return the expected type for that minecraft version. - *
    - *
  • For modern forge, this will be a ClientLevel.
  • - *
  • For fabric & forge pre-1.17, this will be a ClientWorld.
  • - *
- */ - @SuppressWarnings("unchecked") - public T getSound() { - return (T) sound; - } -} diff --git a/modules/events/src/main/java/org/polyfrost/oneconfig/api/event/v1/events/SoundPlayedEvent.java b/modules/events/src/main/java/org/polyfrost/oneconfig/api/event/v1/events/SoundPlayedEvent.java new file mode 100644 index 000000000..980192996 --- /dev/null +++ b/modules/events/src/main/java/org/polyfrost/oneconfig/api/event/v1/events/SoundPlayedEvent.java @@ -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 getCategory() { + return (T) category; + } + + @SuppressWarnings("unchecked") + public T getSound() { + return (T) sound; + } + +} diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/SoundManagerMixin.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/SoundManagerMixin.java index e6e4b9c9e..37ab5f685 100644 --- a/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/SoundManagerMixin.java +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/SoundManagerMixin.java @@ -1,53 +1,31 @@ -/* - * This file is part of OneConfig. - * OneConfig - Next Generation Config Library for Minecraft: Java Edition - * Copyright (C) 2021~2024 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 . You should - * have also received a copy of the Additional Terms Applicable - * to OneConfig, as published by Polyfrost. If not, see - * - */ - 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(); } + } diff --git a/versions/src/main/resources/mixins.oneconfig.json b/versions/src/main/resources/mixins.oneconfig.json index 6d9825830..92307fb91 100644 --- a/versions/src/main/resources/mixins.oneconfig.json +++ b/versions/src/main/resources/mixins.oneconfig.json @@ -17,6 +17,7 @@ "ShaderGroupAccessor", "SoundManagerMixin", "WorldClientMixin", + "SoundManagerMixin", "compat.OptifineConfigMixin", "compat.VigilantCompatMixin" ]