-
Notifications
You must be signed in to change notification settings - Fork 784
feat: 实现启动界面的“小提示”文字 2.0 #4383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: 实现启动界面的“小提示”文字 2.0 #4383
Changes from 3 commits
95458af
4d8e756
b70ca5d
b7d538c
864f38f
9595378
b727b67
74ed377
fc82cfc
5ceb561
59a4d04
3ace3a2
d060af8
1f48055
7701948
806c474
e1650ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Hello Minecraft! Launcher | ||
| * Copyright (C) 2021 huangyuhui <[email protected]> and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/ >. | ||
| */ | ||
| package org.jackhuang.hmcl.util; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.IntStream; | ||
| import java.util.stream.Collectors; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
|
|
||
| import static org.jackhuang.hmcl.util.i18n.I18n.i18n; | ||
|
|
||
| public class RandomTip { | ||
|
|
||
| private static final List<String> tips; | ||
| private static final int maxTipNumber = 30; | ||
|
|
||
| static { | ||
| // Initialization tips list | ||
| tips = IntStream.rangeClosed(1, maxTipNumber) | ||
| .mapToObj(i -> i18n(String.format("message.tips_%s", i))) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public static String getRandomTip() { | ||
| String tip = tips.get(getRandomTipIndex()); | ||
| return formatTip(tip); | ||
| } | ||
|
|
||
| private static String formatTip(String tip) { | ||
|
||
| StringBuilder formattedTip = new StringBuilder(); | ||
| int lineLength = 0; | ||
|
|
||
| for (int i = 0; i < tip.length(); i++) { | ||
| char c = tip.charAt(i); | ||
| int charLength = 1; | ||
|
|
||
| if (Character.toString(c).matches("\\p{IsHan}")) { | ||
| charLength = 2; // One Chinese character is considered as two characters | ||
| } | ||
|
|
||
| if (lineLength + charLength > 50) { | ||
| // 49 characters per line | ||
| formattedTip.append("\n"); | ||
| lineLength = 0; | ||
| } | ||
|
|
||
| formattedTip.append(c); | ||
| lineLength += charLength; | ||
| } | ||
|
|
||
| return formattedTip.toString(); | ||
| } | ||
|
|
||
| private static int getRandomTipIndex() { | ||
| return ThreadLocalRandom.current().nextInt(tips.size()); | ||
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的数据结构完全可以修改为:
`
在 index 不断自增到
tips.length()后,使用Collections.shuffle打乱 tips,然后重置 index 到 0