Skip to content

Commit 0609197

Browse files
authored
Merge pull request #68 from ming-sc/dev
fix: 修复一些 bug
2 parents 91f87ec + a403df9 commit 0609197

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

src/main/java/top/gregtao/concerto/music/KuGouMusic.java

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -275,25 +275,14 @@ public static MusicMetaData parseMetaData(JsonObject object) {
275275
.map(JsonElement::getAsLong)
276276
.orElseThrow();
277277

278-
List<String> authorList = optional.map(json -> json.getAsJsonArray("authors"))
279-
.map(arr -> arr.asList().stream()
280-
.map(JsonElement::getAsJsonObject)
281-
.map(Optional::of)
282-
.map(authorOpt -> Optionals
283-
.flatFirstOf(
284-
authorOpt,
285-
// getDetail 接口会再套一层 base 对象
286-
json -> Optional.ofNullable(json.getAsJsonObject("base")),
287-
Optional::ofNullable
288-
)
289-
// 获取到 author_name 所在对象
290-
.map(base -> base.get("author_name"))
291-
)
292-
.filter(nameElementOpt -> nameElementOpt.isPresent() && !nameElementOpt.get().isJsonNull())
293-
.map(nameElementOpt -> nameElementOpt.get().getAsString())
294-
.collect(Collectors.toList())
295-
)
296-
.orElseThrow();
278+
List<String> authorList = Optionals
279+
.flatFirstOf(
280+
optional,
281+
// 先尝试从 authors 中提取
282+
KuGouMusic::getAuthorsList,
283+
// 再尝试从 base 中提取
284+
KuGouMusic::getAuthorsListFromBase
285+
).orElseThrow();
297286

298287
String headPic = optional.map(json -> json.getAsJsonObject("album_info"))
299288
.map(albumInfo -> albumInfo.get("cover"))
@@ -310,6 +299,39 @@ public static MusicMetaData parseMetaData(JsonObject object) {
310299
}
311300
}
312301

302+
/**
303+
* 尝试从 authors 中提取作者列表, 可能有 authors 不存在的情况 (作者信息未上传)
304+
*/
305+
private static Optional<List<String>> getAuthorsList(JsonObject jsonObject) {
306+
return Optional.ofNullable(jsonObject)
307+
.map(json -> json.getAsJsonArray("authors"))
308+
.map(arr -> arr.asList().stream()
309+
.map(JsonElement::getAsJsonObject)
310+
.map(Optional::of)
311+
.map(authorOpt -> Optionals
312+
.flatFirstOf(
313+
authorOpt,
314+
// getDetail 接口会再套一层 base 对象
315+
json -> Optional.ofNullable(json.getAsJsonObject("base")),
316+
Optional::ofNullable
317+
)
318+
// 获取到 author_name 所在对象
319+
.map(base -> base.get("author_name"))
320+
)
321+
.filter(nameElementOpt -> nameElementOpt.isPresent() && !nameElementOpt.get().isJsonNull())
322+
.map(nameElementOpt -> nameElementOpt.get().getAsString())
323+
.collect(Collectors.toList())
324+
);
325+
}
326+
327+
public static Optional<List<String>> getAuthorsListFromBase(JsonObject jsonObject) {
328+
return Optional.ofNullable(jsonObject)
329+
.map(json -> json.getAsJsonObject("base"))
330+
.map(base -> base.get("author_name"))
331+
.map(JsonElement::getAsString)
332+
.map(names -> Arrays.asList(names.split("、")));
333+
}
334+
313335
/**
314336
* 解析音乐元数据, 处理 searchMusic 和 getPlayListAllTrack 返回的 Json 对象
315337
* @param object 音乐详情 Json 对象

src/main/java/top/gregtao/concerto/screen/kugou/KuGouMusicIndexScreen.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,19 @@ protected void init() {
5555
0, 0, 0, 0,
5656
Text.empty(),
5757
button -> {
58-
// 更新 VIP 状态
59-
KuGouMusicUser localUser = KuGouMusicApiClient.LOCAL_USER;
60-
if (localUser.isLoggedIn() && localUser.isVersionSame()) {
61-
Text tip;
62-
if (localUser.updateVIPStatus()) {
63-
tip = Text.translatable("concerto.screen.kugou.vip.update_success");
64-
} else {
65-
tip = Text.translatable("concerto.screen.kugou.vip.update_failed");
58+
ConcertoRunner.run(() -> {
59+
// 更新 VIP 状态
60+
KuGouMusicUser localUser = KuGouMusicApiClient.LOCAL_USER;
61+
if (localUser.isLoggedIn() && localUser.isVersionSame()) {
62+
Text tip;
63+
if (localUser.updateVIPStatus()) {
64+
tip = Text.translatable("concerto.screen.kugou.vip.update_success");
65+
} else {
66+
tip = Text.translatable("concerto.screen.kugou.vip.update_failed");
67+
}
68+
displayAlert(tip);
6669
}
67-
displayAlert(tip);
68-
}
70+
});
6971
},
7072
textRenderer
7173
));

src/main/java/top/gregtao/concerto/screen/widget/URLImageWidget.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,13 @@ public static BufferedImage readImageFromUrl(String url) throws IOException {
160160
}
161161

162162
private void uploadImage(BufferedImage image, Runnable callback) {
163+
// ImageIO.write() 是耗时操作, 会卡住渲染线程
164+
NativeImage nativeImage = toNativeImage(image);
163165
MinecraftClient.getInstance().submit(() -> {
164-
if (this.texture != null)
165-
MinecraftClient.getInstance().getTextureManager().destroyTexture(this.textureId);
166-
this.texture = new NativeImageBackedTexture(this.textureId::toString, toNativeImage(image));
166+
if (this.texture != null) {
167+
MinecraftClient.getInstance().getTextureManager().destroyTexture(this.textureId);
168+
}
169+
this.texture = new NativeImageBackedTexture(this.textureId::toString, nativeImage);
167170
MinecraftClient.getInstance().getTextureManager().registerTexture(this.textureId, this.texture);
168171
}).thenRun(callback);
169172
}

0 commit comments

Comments
 (0)