diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionMixin.java index 1a3d2bb..7895307 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionMixin.java @@ -4,24 +4,30 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.DecoderException; import land.chipmunk.chipmunkmod.listeners.Listener; import land.chipmunk.chipmunkmod.listeners.ListenerManager; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.world.ClientWorld; import net.minecraft.network.listener.PacketListener; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.c2s.play.RequestCommandCompletionsC2SPacket; import net.minecraft.network.packet.s2c.play.ParticleS2CPacket; import net.minecraft.network.packet.s2c.play.PlaySoundS2CPacket; -import net.minecraft.registry.RegistryKey; import net.minecraft.sound.SoundEvent; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import org.spongepowered.asm.mixin.Mixin; +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.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @Mixin(net.minecraft.network.ClientConnection.class) public class ClientConnectionMixin { + @Unique + private static final Pattern CUSTOM_PITCH_PATTERN = Pattern.compile(".*\\.pitch\\.(.*)"); + @Inject(at = @At("HEAD"), method = "disconnect", cancellable = true) public void disconnect (Text disconnectReason, CallbackInfo ci) { if (disconnectReason == ClientPlayNetworkHandlerAccessor.chatValidationFailedText()) { @@ -52,17 +58,40 @@ public class ClientConnectionMixin { ci.cancel(); } } else if (packet instanceof PlaySoundS2CPacket t_packet) { - if (t_packet.getVolume() != 1) return; + try { + final SoundEvent soundEvent = t_packet.getSound().value(); - final Optional> event = t_packet.getSound().getKey(); + final Identifier sound = soundEvent.getId(); - if (event.isEmpty()) return; + final Matcher matcher = CUSTOM_PITCH_PATTERN.matcher(sound.getPath()); - final Identifier sound = event.get().getValue(); + if (!matcher.find()) return; - if (!sound.getPath().equals("entity.enderman.scream")) return; + try { + final String stringPitch = matcher.group(1); - ci.cancel(); + final float pitch = Float.parseFloat(stringPitch); + + final MinecraftClient client = MinecraftClient.getInstance(); + + final ClientWorld world = client.world; + + if (world == null) return; + + // huge mess + final SoundEvent newSound = SoundEvent.of(new Identifier(sound.getNamespace(), sound.getPath().substring(0, sound.getPath().length() - (".pitch." + stringPitch).length()))); + + world.playSound(client.player, t_packet.getX(), t_packet.getY(), t_packet.getZ(), newSound, t_packet.getCategory(), t_packet.getVolume(), pitch, t_packet.getSeed()); + + ci.cancel(); + } catch (NumberFormatException e) { + e.printStackTrace(); + } + + if (t_packet.getVolume() == 1 && sound.getPath().equals("entity.enderman.scream")) ci.cancel(); + } catch (Exception e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/SoundSystemMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/SoundSystemMixin.java new file mode 100644 index 0000000..6d1d208 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/SoundSystemMixin.java @@ -0,0 +1,17 @@ +package land.chipmunk.chipmunkmod.mixin; + +import net.minecraft.client.sound.SoundInstance; +import net.minecraft.client.sound.SoundSystem; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(SoundSystem.class) +public class SoundSystemMixin { + @Inject(method = "getAdjustedPitch", at = @At("HEAD"), cancellable = true) + private void getAdjustedPitch (SoundInstance sound, CallbackInfoReturnable cir) { + cir.setReturnValue(sound.getPitch()); + cir.cancel(); + } +} diff --git a/src/main/resources/chipmunkmod.mixins.json b/src/main/resources/chipmunkmod.mixins.json index a19ab15..991e584 100644 --- a/src/main/resources/chipmunkmod.mixins.json +++ b/src/main/resources/chipmunkmod.mixins.json @@ -23,7 +23,8 @@ "PlayerListEntryAccessor", "SharedConstantsMixin", "TextSerializerMixin", - "CommandDispatcherMixin" + "CommandDispatcherMixin", + "SoundSystemMixin" ], "injectors": { "defaultRequire": 1