From df5b1f17ed7a62c38a305a6788dc659314561a9f Mon Sep 17 00:00:00 2001 From: RTAkland Date: Thu, 6 Feb 2025 16:23:04 +0800 Subject: [PATCH] feat: add auto adjust volume when monsters nearby player --- .../fengliu/cloudmusic/config/Configs.java | 20 ++++- .../mixin/ClientPlayerEntityMixin.java | 87 +++++++++++++++++++ .../fengliu/cloudmusic/util/MusicPlayer.java | 8 ++ .../assets/cloudmusic/lang/zh_cn.json | 5 +- src/main/resources/cloudmusic.mixins.json | 3 +- 5 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 src/main/java/fengliu/cloudmusic/mixin/ClientPlayerEntityMixin.java diff --git a/src/main/java/fengliu/cloudmusic/config/Configs.java b/src/main/java/fengliu/cloudmusic/config/Configs.java index 660a106..36808c7 100644 --- a/src/main/java/fengliu/cloudmusic/config/Configs.java +++ b/src/main/java/fengliu/cloudmusic/config/Configs.java @@ -76,6 +76,9 @@ public static class ALL { public static final ConfigHotkey PLAYLIST_DEL_MUSIC = ConfigUtil.addConfigHotkey("playlist.del.music"); public static final ConfigHotkey PLAYLIST_RANDOM = ConfigUtil.addConfigHotkey("playlist.random"); public static final ConfigBooleanHotkeyed CLICK_RUN_COMMAND = ConfigUtil.addConfigBooleanHotkeyed("click.run.command"); + public static final ConfigBooleanHotkeyed ENABLE_NEARBY_MONSTER_DECREASE_VOLUME = ConfigUtil.addConfigBooleanHotkeyed("enable.nearby.monster.decrease.volume"); + public static final ConfigInteger NEARBY_MONSTER_DECREASE_VOLUME_VALUE = ConfigUtil.addConfigInteger("nearby.monster.decrease.volume.value", 10); + public static final ConfigInteger NEARBY_MONSTER_DECREASE_VOLUME_RADIUS = ConfigUtil.addConfigInteger("nearby.monster.decrease.volume.radius", 10); public static final ImmutableList OPTIONS = ImmutableList.of( VOLUME, @@ -131,7 +134,10 @@ public static class ALL { LIKE_MUSIC, PLAYLIST_ADD_MUSIC, PLAYLIST_DEL_MUSIC, - PLAYLIST_RANDOM + PLAYLIST_RANDOM, + NEARBY_MONSTER_DECREASE_VOLUME_RADIUS, + NEARBY_MONSTER_DECREASE_VOLUME_VALUE, + ENABLE_NEARBY_MONSTER_DECREASE_VOLUME ); } @@ -147,6 +153,8 @@ public static class PLAY { public static final ConfigString CACHE_PATH = ALL.CACHE_PATH; public static final ConfigInteger CACHE_MAX_MB = ALL.CACHE_MAX_MB; public static final ConfigInteger CACHE_DELETE_MB = ALL.CACHE_DELETE_MB; + public static final ConfigInteger NEARBY_MONSTER_DECREASE_VOLUME_VALUE = ALL.NEARBY_MONSTER_DECREASE_VOLUME_VALUE; + public static final ConfigInteger NEARBY_MONSTER_DECREASE_VOLUME_RADIUS = ALL.NEARBY_MONSTER_DECREASE_VOLUME_RADIUS; public static final ImmutableList OPTIONS = ImmutableList.of( VOLUME, @@ -159,7 +167,9 @@ public static class PLAY { EXIT_GAME_STOP_MUSIC, CACHE_PATH, CACHE_MAX_MB, - CACHE_DELETE_MB + CACHE_DELETE_MB, + NEARBY_MONSTER_DECREASE_VOLUME_VALUE, + NEARBY_MONSTER_DECREASE_VOLUME_RADIUS ); } @@ -252,6 +262,7 @@ public static class ENABLE { public static final ConfigBooleanHotkeyed PLAY_AUTO_RANDOM = ALL.PLAY_AUTO_RANDOM; public static final ConfigBooleanHotkeyed MUSIC_INFO_EFFECT_OFFSET = ALL.MUSIC_INFO_EFFECT_OFFSET; public static final ConfigBooleanHotkeyed HTTP_PROXY = ALL.HTTP_PROXY; + public static final ConfigBooleanHotkeyed ENABLE_NEARBY_MONSTER_DECREASE_VOLUME = ALL.ENABLE_NEARBY_MONSTER_DECREASE_VOLUME; public static final ImmutableList HOTKEY_LIST = ImmutableList.of( MUSIC_INFO, @@ -259,7 +270,8 @@ public static class ENABLE { PLAY_LOOP, PLAY_AUTO_RANDOM, MUSIC_INFO_EFFECT_OFFSET, - HTTP_PROXY + HTTP_PROXY, + ENABLE_NEARBY_MONSTER_DECREASE_VOLUME ); } @@ -304,7 +316,7 @@ public void load() { File configFile = new File(FileUtils.getConfigDirectory(), CONFIG_FILE_NAME); if (configFile.isFile() && configFile.exists()) { JsonElement element = JsonUtils.parseJsonFile(configFile); - if(element == null || !element.isJsonObject()){ + if (element == null || !element.isJsonObject()) { return; } diff --git a/src/main/java/fengliu/cloudmusic/mixin/ClientPlayerEntityMixin.java b/src/main/java/fengliu/cloudmusic/mixin/ClientPlayerEntityMixin.java new file mode 100644 index 0000000..a9983fe --- /dev/null +++ b/src/main/java/fengliu/cloudmusic/mixin/ClientPlayerEntityMixin.java @@ -0,0 +1,87 @@ +/* + * Copyright © 2025 RTAkland + * Author: RTAkland + * Date: 2025/2/6 + */ + + +package fengliu.cloudmusic.mixin; + +import com.mojang.authlib.GameProfile; +import fengliu.cloudmusic.command.MusicCommand; +import fengliu.cloudmusic.config.Configs; +import fengliu.cloudmusic.music163.IMusic; +import fengliu.cloudmusic.util.MusicPlayer; +import net.minecraft.client.network.AbstractClientPlayerEntity; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.client.world.ClientWorld; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.mob.Monster; +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; + +@Mixin(ClientPlayerEntity.class) +public abstract class ClientPlayerEntityMixin extends AbstractClientPlayerEntity { + + @Shadow public float prevNauseaIntensity; + + public ClientPlayerEntityMixin(ClientWorld world, GameProfile profile) { + super(world, profile); + } + + @Unique + private boolean isVolumeAdjusted = false; + + @Unique + private int previousVolume = 0; + + @Inject(method = "tickMovement", at = @At("HEAD")) + public void tickMovement(CallbackInfo ci) { + if (Configs.ENABLE.ENABLE_NEARBY_MONSTER_DECREASE_VOLUME.getBooleanValue()) { + var player = (ClientPlayerEntity) (Object) this; + var world = player.getWorld(); + var nearbyMobs = world.getEntitiesByClass( + LivingEntity.class, + player.getBoundingBox().expand(Configs.ALL.NEARBY_MONSTER_DECREASE_VOLUME_RADIUS.getIntegerValue()), + entity -> entity instanceof Monster + ); + MusicPlayer musicPlayer = MusicCommand.getPlayer(); + IMusic playingMusic = musicPlayer.getPlayingMusic(); + if (playingMusic == null) { + return; + } + if (!nearbyMobs.isEmpty() && !isVolumeAdjusted) { + var currentVolume = musicPlayer.getVolumePercentage(); + this.previousVolume = currentVolume; + musicPlayer.volumeSet(currentVolume - Configs.ALL.NEARBY_MONSTER_DECREASE_VOLUME_VALUE.getIntegerValue()); + isVolumeAdjusted = true; + } + } + } + + @Inject(method = "tick", at = @At("HEAD")) + public void tick(CallbackInfo ci) { + if (Configs.ENABLE.ENABLE_NEARBY_MONSTER_DECREASE_VOLUME.getBooleanValue()) { + var player = (ClientPlayerEntity) (Object) this; + var world = player.getWorld(); + var nearbyMobs = world.getEntitiesByClass( + LivingEntity.class, + player.getBoundingBox().expand(10), + entity -> entity instanceof Monster + ); + if (nearbyMobs.isEmpty() && isVolumeAdjusted) { + isVolumeAdjusted = false; + MusicPlayer musicPlayer = MusicCommand.getPlayer(); + IMusic playingMusic = musicPlayer.getPlayingMusic(); + if (playingMusic == null) { + return; + } + musicPlayer.volumeSet(this.previousVolume); + } + } + } +} diff --git a/src/main/java/fengliu/cloudmusic/util/MusicPlayer.java b/src/main/java/fengliu/cloudmusic/util/MusicPlayer.java index da09a4f..689e228 100644 --- a/src/main/java/fengliu/cloudmusic/util/MusicPlayer.java +++ b/src/main/java/fengliu/cloudmusic/util/MusicPlayer.java @@ -259,6 +259,14 @@ public void volumeDown() { this.volumeSet(--this.volumePercentage); } + /** + * 获取音量百分比 + * @return 音量百分比 + */ + public int getVolumePercentage() { + return volumePercentage; + } + /** * 获取当前滚动到的歌词 * diff --git a/src/main/resources/assets/cloudmusic/lang/zh_cn.json b/src/main/resources/assets/cloudmusic/lang/zh_cn.json index 5b423f7..de188fc 100644 --- a/src/main/resources/assets/cloudmusic/lang/zh_cn.json +++ b/src/main/resources/assets/cloudmusic/lang/zh_cn.json @@ -470,5 +470,8 @@ "cloudmusic.config.exit.game.stop.music": "标题界面停止播放音乐 | exit.game.stop.music", "cloudmusic.config.exit.game.stop.music.comment": "返回标题界面后停止播放音乐", "cloudmusic.config.stop.play.show.ui": "停止播放后隐藏歌曲信息 | stop.play.show.ui", - "cloudmusic.config.stop.play.show.ui.comment": "停止播放后隐藏歌曲信息, 继续播放后显示歌曲信息" + "cloudmusic.config.stop.play.show.ui.comment": "停止播放后隐藏歌曲信息, 继续播放后显示歌曲信息", + "cloudmusic.config.enable.nearby.monster.decrease.volume": "开启附近有敌对生物时减小音量 | enable.nearby.decrease.volume", + "cloudmusic.config.nearby.monster.decrease.volume.value": "附近有地对生物时减少的音量值 | nearby.decrease.sound.value", + "cloudmusic.config.nearby.monster.decrease.volume.radius": "敌对生物检测半径 | nearby.decrease.sound.radiu" } \ No newline at end of file diff --git a/src/main/resources/cloudmusic.mixins.json b/src/main/resources/cloudmusic.mixins.json index 93a713f..c04acfe 100644 --- a/src/main/resources/cloudmusic.mixins.json +++ b/src/main/resources/cloudmusic.mixins.json @@ -7,7 +7,8 @@ "MinecraftClientMixin", "MixinChatHud", "MixinInGameHud", - "SoundSystemMixin" + "SoundSystemMixin", + "ClientPlayerEntityMixin" ], "injectors": { "defaultRequire": 1