Skip to content

Commit

Permalink
optimizing code, add Configs
Browse files Browse the repository at this point in the history
  • Loading branch information
FengLiuFeseliud committed Feb 5, 2025
1 parent 3faa7da commit 510a23d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
12 changes: 12 additions & 0 deletions src/main/java/fengliu/cloudmusic/config/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public static class ALL {
public static final ConfigBooleanHotkeyed PLAY_AUTO_RANDOM = ConfigUtil.addConfigBooleanHotkeyed("play.auto.random", false, "");
public static final ConfigOptionList PLAY_QUALITY = ConfigUtil.addConfigOptionList("play.quality", Quality.EXHIGH);
public static final ConfigBoolean DJRADIO_PLAY_ASC = ConfigUtil.addConfigBoolean("dj.radio.play.asc");
public static final ConfigBoolean NOT_PLAY_GAME_MUSIC = ConfigUtil.addConfigBoolean("play.not.game.music", true);
public static final ConfigBoolean EXIT_GAME_STOP_MUSIC = ConfigUtil.addConfigBoolean("exit.game.stop.music");
public static final ConfigBoolean STOP_PLAY_SHOW_UI = ConfigUtil.addConfigBoolean("stop.play.show.ui");
public static final ConfigString CACHE_PATH = ConfigUtil.addConfigString("cache.path", (new File(FileUtils.getMinecraftDirectory(), "cloud_music_cache")).getAbsolutePath());
public static final ConfigInteger CACHE_MAX_MB = ConfigUtil.addConfigInteger("cache.max.mb", 512, 512, 8000);
public static final ConfigInteger CACHE_DELETE_MB = ConfigUtil.addConfigInteger("cache.delete.mb", 126, 126, 8000);
Expand Down Expand Up @@ -81,6 +84,9 @@ public static class ALL {
PLAY_AUTO_RANDOM,
PLAY_QUALITY,
DJRADIO_PLAY_ASC,
NOT_PLAY_GAME_MUSIC,
EXIT_GAME_STOP_MUSIC,
STOP_PLAY_SHOW_UI,
CACHE_PATH,
CACHE_MAX_MB,
CACHE_DELETE_MB,
Expand Down Expand Up @@ -136,6 +142,8 @@ public static class PLAY {
public static final ConfigBooleanHotkeyed PLAY_AUTO_RANDOM = ALL.PLAY_AUTO_RANDOM;
public static final ConfigOptionList PLAY_QUALITY = ALL.PLAY_QUALITY;
public static final ConfigBoolean DJRADIO_PLAY_ASC = ALL.DJRADIO_PLAY_ASC;
public static final ConfigBoolean NOT_PLAY_GAME_MUSIC = ALL.NOT_PLAY_GAME_MUSIC;
public static final ConfigBoolean EXIT_GAME_STOP_MUSIC = ALL.EXIT_GAME_STOP_MUSIC;
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;
Expand All @@ -147,6 +155,8 @@ public static class PLAY {
PLAY_AUTO_RANDOM,
PLAY_QUALITY,
DJRADIO_PLAY_ASC,
NOT_PLAY_GAME_MUSIC,
EXIT_GAME_STOP_MUSIC,
CACHE_PATH,
CACHE_MAX_MB,
CACHE_DELETE_MB
Expand All @@ -156,6 +166,7 @@ public static class PLAY {
public static class GUI {
public static final ConfigBooleanHotkeyed MUSIC_INFO = ALL.MUSIC_INFO;
public static final ConfigBooleanHotkeyed LYRIC = ALL.LYRIC;
public static final ConfigBoolean STOP_PLAY_SHOW_UI = ALL.STOP_PLAY_SHOW_UI;
public static final ConfigInteger PAGE_LIMIT = ALL.PAGE_LIMIT;
public static final ConfigInteger MUSIC_INFO_X = ALL.MUSIC_INFO_X;
public static final ConfigInteger MUSIC_INFO_Y = ALL.MUSIC_INFO_Y;
Expand All @@ -176,6 +187,7 @@ public static class GUI {
public static final ImmutableList<IConfigBase> OPTIONS = ImmutableList.of(
MUSIC_INFO,
LYRIC,
STOP_PLAY_SHOW_UI,
PAGE_LIMIT,
MUSIC_INFO_X,
MUSIC_INFO_Y,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fengliu.cloudmusic.mixin;

import fengliu.cloudmusic.command.MusicCommand;
import fengliu.cloudmusic.config.Configs;
import fengliu.cloudmusic.util.MusicPlayer;
import net.minecraft.client.MinecraftClient;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -16,6 +17,10 @@ public class MinecraftClientMixin {
*/
@Inject(method = "onDisconnected", at = @At("HEAD"))
private void onDisconnected(CallbackInfo ci) {
if(!Configs.PLAY.NOT_PLAY_GAME_MUSIC.getBooleanValue()){
return;
}

MusicPlayer player = MusicCommand.getPlayer();
player.stop();
}
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/fengliu/cloudmusic/mixin/MixinInGameHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ public void initLyricRender(MinecraftClient client, CallbackInfo ci) {
HudRenderCallback.EVENT.register((drawContext, renderTickCounter) -> {
MusicPlayer player = MusicCommand.getPlayer();
IMusic playingMusic = player.getPlayingMusic();
if (playingMusic == null) return;
if (playingMusic == null){
return;
}

if (Configs.GUI.STOP_PLAY_SHOW_UI.getBooleanValue() && !player.isPlaying()){
return;
}

float lyricScale = (float) Configs.GUI.LYRIC_SCALE.getDoubleValue();
int lyricY = Configs.GUI.LYRIC_Y.getIntegerValue();
int lyricX = Configs.GUI.LYRIC_X.getIntegerValue();
Expand Down Expand Up @@ -96,6 +103,10 @@ public void render(DrawContext context, RenderTickCounter tickCounter, CallbackI
return;
}

if (Configs.GUI.STOP_PLAY_SHOW_UI.getBooleanValue() && !player.isPlaying()){
return;
}

RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
if (!Configs.GUI.MUSIC_INFO.getBooleanValue()) {
return;
Expand Down
39 changes: 26 additions & 13 deletions src/main/java/fengliu/cloudmusic/mixin/SoundSystemMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fengliu.cloudmusic.mixin;

import fengliu.cloudmusic.command.MusicCommand;
import fengliu.cloudmusic.config.Configs;
import fengliu.cloudmusic.music163.IMusic;
import fengliu.cloudmusic.util.MusicPlayer;
import net.minecraft.client.sound.SoundInstance;
Expand All @@ -22,26 +23,38 @@ public abstract class SoundSystemMixin {
@Unique
public SoundCategory currentCategory;

@Inject(method = "play(Lnet/minecraft/client/sound/SoundInstance;)V", at = @At("HEAD"), cancellable = true)
public void play(SoundInstance soundInstance, CallbackInfo ci) {
currentCategory = soundInstance.getCategory();
/**
* 判断是否需要停止播放背景音乐
* @param soundCategory 音乐类
* @return false 不播放
*/
private static boolean canStopGameMusic(SoundCategory soundCategory){
if (!Configs.PLAY.NOT_PLAY_GAME_MUSIC.getBooleanValue()){
return false;
}

MusicPlayer player = MusicCommand.getPlayer();
IMusic playingMusic = player.getPlayingMusic();
if (playingMusic != null) {
if (currentCategory == SoundCategory.MUSIC) {
ci.cancel();
}
if (playingMusic == null) {
return false;
}

return soundCategory == SoundCategory.MUSIC;
}

@Inject(method = "play(Lnet/minecraft/client/sound/SoundInstance;)V", at = @At("HEAD"), cancellable = true)
public void play(SoundInstance soundInstance, CallbackInfo ci) {
if (!canStopGameMusic(soundInstance.getCategory())){
return;
}
ci.cancel();
}

@Inject(method = "tick()V", at = @At("HEAD"), cancellable = true)
public void tick(CallbackInfo ci) {
MusicPlayer player = MusicCommand.getPlayer();
IMusic playingMusic = player.getPlayingMusic();
if (playingMusic != null) {
if (currentCategory == SoundCategory.MUSIC) {
ci.cancel();
}
if (!canStopGameMusic(currentCategory)){
return;
}
ci.cancel();
}
}
8 changes: 0 additions & 8 deletions src/main/java/fengliu/cloudmusic/util/MusicPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,6 @@ public void stop() {
notifyAll();
}
this.loopPlayIn = false;
this.clearPlayingMusic();
}

/**
* 让正在播放的音乐为null, 以便停止渲染
*/
private void clearPlayingMusic() {
this.playingMusic = null;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/assets/cloudmusic/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -464,5 +464,11 @@
"cloudmusic.config.hotkey.playlist.del.music": "歌单删除歌曲 | playlist.del.music",
"cloudmusic.config.hotkey.playlist.del.music.comment": "歌单删除当前播放歌曲",
"cloudmusic.config.hotkey.playlist.random": "随机播放 | playlist.random",
"cloudmusic.config.hotkey.playlist.random.comment": "将播放队列随机并重新播放"
"cloudmusic.config.hotkey.playlist.random.comment": "将播放队列随机并重新播放",
"cloudmusic.config.play.not.game.music": "禁止播放游戏背景音乐 | play.not.game.music",
"cloudmusic.config.play.not.game.music.comment": "播放音乐时自动停止播放游戏背景音乐 ",
"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": "停止播放后隐藏歌曲信息, 继续播放后显示歌曲信息"
}

0 comments on commit 510a23d

Please sign in to comment.