Skip to content

Commit

Permalink
feat: add auto adjust volume when monsters nearby player
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Feb 6, 2025
1 parent 5775cae commit df5b1f1
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/main/java/fengliu/cloudmusic/config/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<IConfigBase> OPTIONS = ImmutableList.of(
VOLUME,
Expand Down Expand Up @@ -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
);
}

Expand All @@ -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<IConfigBase> OPTIONS = ImmutableList.of(
VOLUME,
Expand All @@ -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
);
}

Expand Down Expand Up @@ -252,14 +262,16 @@ 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<ConfigBooleanHotkeyed> HOTKEY_LIST = ImmutableList.of(
MUSIC_INFO,
LYRIC,
PLAY_LOOP,
PLAY_AUTO_RANDOM,
MUSIC_INFO_EFFECT_OFFSET,
HTTP_PROXY
HTTP_PROXY,
ENABLE_NEARBY_MONSTER_DECREASE_VOLUME
);
}

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/fengliu/cloudmusic/util/MusicPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ public void volumeDown() {
this.volumeSet(--this.volumePercentage);
}

/**
* 获取音量百分比
* @return 音量百分比
*/
public int getVolumePercentage() {
return volumePercentage;
}

/**
* 获取当前滚动到的歌词
*
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/assets/cloudmusic/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
3 changes: 2 additions & 1 deletion src/main/resources/cloudmusic.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"MinecraftClientMixin",
"MixinChatHud",
"MixinInGameHud",
"SoundSystemMixin"
"SoundSystemMixin",
"ClientPlayerEntityMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit df5b1f1

Please sign in to comment.