From 395ad80d917634d338b3de295213e3155849f318 Mon Sep 17 00:00:00 2001 From: RK_01 <50594595+RaphiMC@users.noreply.github.com> Date: Sun, 23 Jun 2024 14:18:15 +0200 Subject: [PATCH] Add 3D Shareware 8-Bit sound (#459) --- .../viafabricplus/fixes/ClientsideFixes.java | 6 ++ .../fixes/minecraft/MixinStaticSound.java | 101 ++++++++++++++++++ src/main/resources/viafabricplus.mixins.json | 1 + 3 files changed, 108 insertions(+) create mode 100644 src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/MixinStaticSound.java diff --git a/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java b/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java index 6a25134e..e1372ec4 100644 --- a/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java +++ b/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java @@ -40,6 +40,7 @@ import net.minecraft.client.network.ServerAddress; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.RegistryByteBuf; import net.minecraft.registry.Registries; +import net.raphimc.viaaprilfools.api.AprilFoolsProtocolVersion; import net.raphimc.viabedrock.api.BedrockProtocolVersion; import net.raphimc.viabedrock.protocol.data.ProtocolConstants; import net.raphimc.vialegacy.api.LegacyProtocolVersion; @@ -117,6 +118,11 @@ public class ClientsideFixes { if (newVersion.olderThanOrEqualTo(LegacyProtocolVersion.c0_28toc0_30)) { GridItemSelectionScreen.INSTANCE.itemGrid = null; } + + // Reload sound system when switching between 3D Shareware and normal versions + if (oldVersion.equals(AprilFoolsProtocolVersion.s3d_shareware) || newVersion.equals(AprilFoolsProtocolVersion.s3d_shareware)) { + MinecraftClient.getInstance().getSoundManager().reloadSounds(); + } })); // Register the footstep particle diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/MixinStaticSound.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/MixinStaticSound.java new file mode 100644 index 00000000..ae451bb5 --- /dev/null +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/MixinStaticSound.java @@ -0,0 +1,101 @@ +/* + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2024 FlorianMichael/EnZaXD and RK_01/RaphiMC + * Copyright (C) 2023-2024 contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft; + +import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; +import net.minecraft.client.sound.StaticSound; +import net.raphimc.viaaprilfools.api.AprilFoolsProtocolVersion; +import org.spongepowered.asm.mixin.Final; +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 javax.sound.sampled.AudioFormat; +import java.nio.ByteBuffer; + +@Mixin(StaticSound.class) +public abstract class MixinStaticSound { + + @Shadow + @Final + private AudioFormat format; + + @Inject(method = "", at = @At("RETURN")) + private void modifyBuffer(ByteBuffer sample, AudioFormat format, CallbackInfo ci) { + if (ProtocolTranslator.getTargetVersion().equals(AprilFoolsProtocolVersion.s3d_shareware)) { + this.viaFabricPlus$apply8BitSound(sample); + } + } + + @Unique + private void viaFabricPlus$apply8BitSound(final ByteBuffer byteBuffer) { + if (byteBuffer == null) { + return; + } + if (this.format.getChannels() == 1) { + this.viaFabricPlus$apply8BitMono(byteBuffer); + } else { + this.viaFabricPlus$apply8BitStereo(byteBuffer); + } + } + + @Unique + private void viaFabricPlus$apply8BitMono(final ByteBuffer byteBuffer) { + short short2 = 0; + int integer3 = 0; + while (byteBuffer.hasRemaining()) { + if (integer3 == 0) { + byteBuffer.mark(); + short2 = (short) (byteBuffer.getShort() & 0xFFFFFFFC); + byteBuffer.reset(); + integer3 = 15; + } else { + --integer3; + } + byteBuffer.putShort(short2); + } + byteBuffer.flip(); + } + + @Unique + private void viaFabricPlus$apply8BitStereo(final ByteBuffer byteBuffer) { + short short2 = 0; + short short3 = 0; + int integer4 = 0; + while (byteBuffer.hasRemaining()) { + if (integer4 == 0) { + byteBuffer.mark(); + short2 = (short) (byteBuffer.getShort() & 0xFFFFFFFC); + short3 = (short) (byteBuffer.getShort() & 0xFFFFFFFC); + byteBuffer.reset(); + integer4 = 15; + } else { + --integer4; + } + byteBuffer.putShort(short2); + byteBuffer.putShort(short3); + } + byteBuffer.flip(); + } + +} diff --git a/src/main/resources/viafabricplus.mixins.json b/src/main/resources/viafabricplus.mixins.json index 7880d0ca..1ea21e2c 100644 --- a/src/main/resources/viafabricplus.mixins.json +++ b/src/main/resources/viafabricplus.mixins.json @@ -58,6 +58,7 @@ "fixes.minecraft.MixinRedirectResolver", "fixes.minecraft.MixinServerAddress", "fixes.minecraft.MixinServerResourcePackLoader_4", + "fixes.minecraft.MixinStaticSound", "fixes.minecraft.MixinStringHelper", "fixes.minecraft.MixinTextRenderer", "fixes.minecraft.MixinWorldRenderer",