Skip to content

Commit d61be37

Browse files
committed
1.7: Support mod loaded display
1 parent 5fe208c commit d61be37

6 files changed

Lines changed: 66 additions & 50 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A Minecraft mod that picks random title from a list to display as window title.
66
中文介绍见[MCBBS](https://www.mcbbs.net/thread-980991-1-1.html)
77

88
## Configuration
9-
See title.yml
9+
See [title.yml](https://github.com/PercyDan54/RandomTitle-Fabric/blob/master/src/main/resources/title.yml)
1010

1111
## Download
1212
Download from [Releases](https://github.com/PercyDan54/RandomTitle-Fabric/releases)

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
88
loader_version=0.11.1
99

1010
# Mod Properties
11-
mod_version = 1.6
11+
mod_version = 1.7
1212
maven_group = net.fabricmc
1313
archives_base_name = RandomTitle-Fabric
1414

src/main/java/me/PercyDan/RandomTitle/ConfigManager.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@
1717

1818
public class ConfigManager {
1919
private final Logger LOGGER = LogManager.getLogger("RandomTitle");
20-
Map config;
2120
File configFile = new File("title.yml");
21+
Map<String, Object> config;
22+
Map<String, Object> defaultConfig;
2223

2324
public ConfigManager() {
25+
LOGGER.info("RandomTitle by PercyDan https://github.com/PercyDan54/RandomTitle");
26+
defaultConfig = new Yaml().load(this.getClass().getResourceAsStream("/title.yml"));
2427
if (!configFile.exists()) {
2528
LOGGER.info("Config file not found, creating default");
2629
createDefault();
27-
config = new Yaml().load(this.getClass().getResourceAsStream("/title.yml"));
2830
}
2931
try {
3032
config = new Yaml().load(new FileInputStream(configFile));
3133
} catch (Exception e) {
3234
LOGGER.error("Failed to load config!", e);
35+
config = defaultConfig;
3336
}
3437
}
3538

@@ -50,29 +53,46 @@ public void createDefault() {
5053
}
5154
}
5255

56+
public Object Get(String key) {
57+
Object value = config.get(key);
58+
if (value == null) return defaultConfig.get(key);
59+
return value;
60+
}
5361

54-
public String getTitleFromList() {
62+
private String getTitleFromList() {
5563
String title = "";
5664
try {
57-
List titles = (List) config.get("title");
58-
title = titles.get(new Random().nextInt(titles.size())).toString();
65+
List<String> titles = (List<String>) Get("title");
66+
title = titles.get(new Random().nextInt(titles.size()));
5967
} catch (Throwable e) {
6068
LOGGER.error("Failed to get title from config!", e);
6169
}
6270
return title;
6371
}
6472

65-
public String getTitleFromHitokoto() {
66-
LOGGER.info("Getting title from hitokoto");
73+
private String getTitleFromHitokoto() {
74+
LOGGER.info("Getting title from Hitokoto API");
6775
String title;
6876
String response;
6977
try {
7078
response = EntityUtils.toString(HttpClients.createDefault().execute(new HttpGet("https://v1.hitokoto.cn/")).getEntity());
71-
LOGGER.info("Response String: " + response);
79+
LOGGER.info("Hitokoto Response String: " + response);
7280
JsonObject json = new JsonParser().parse(response).getAsJsonObject();
7381
String from = json.get("from").getAsString();
7482
String sentence = json.get("hitokoto").getAsString();
75-
title = sentence + " ——" + from;
83+
String type = json.get("type").getAsString();
84+
title = sentence + " —— ";
85+
switch (type) {
86+
case "e":
87+
title += json.get("creator").getAsString() + " 原创";
88+
break;
89+
case "f":
90+
title += "来自网络";
91+
break;
92+
default:
93+
title += from;
94+
}
95+
7696
} catch (Throwable e) {
7797
LOGGER.error("Failed to get title from API!", e);
7898
return getTitleFromList();
@@ -81,7 +101,8 @@ public String getTitleFromHitokoto() {
81101
}
82102

83103
public String getTitle() {
84-
int mode = (int) config.get("mode");
104+
int mode = (int) Get("mode");
105+
85106
switch (mode) {
86107
case 0:
87108
return getTitleFromHitokoto();
@@ -99,17 +120,8 @@ public String getTitle() {
99120
}
100121

101122
public String getPrefix() {
102-
List prefixes = (List) config.get("prefix");
103-
return prefixes.get((new Random()).nextInt(prefixes.size())).toString();
104-
}
105-
106-
public String getFormat() {
107-
return (String) config.get("format");
108-
}
109-
110-
public String getDateFormat() {
111-
112-
return (String) config.get("dateformat");
123+
List<String> prefixes = (List<String>) Get("prefix");
124+
return prefixes.get((new Random()).nextInt(prefixes.size()));
113125
}
114126

115127
}

src/main/java/me/PercyDan/RandomTitle/Mixins/MixinMinecraft.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.PercyDan.RandomTitle.Mixins;
22

33
import me.PercyDan.RandomTitle.ConfigManager;
4+
import net.fabricmc.loader.FabricLoader;
45
import net.minecraft.SharedConstants;
56
import net.minecraft.client.MinecraftClient;
67
import net.minecraft.client.gui.screen.Screen;
@@ -48,7 +49,7 @@ public abstract class MixinMinecraft {
4849

4950
@Inject(method = "getWindowTitle", at = @At("HEAD"), cancellable = true)
5051
private void getWindowTitle(CallbackInfoReturnable<String> ci) {
51-
String title = config.getFormat();
52+
String title = (String) config.Get("format");
5253
StringBuilder stringBuilder = new StringBuilder(SharedConstants.getGameVersion().getName());
5354
stringBuilder.append(" ");
5455
ClientPlayNetworkHandler clientPlayNetworkHandler = this.getNetworkHandler();
@@ -65,10 +66,11 @@ private void getWindowTitle(CallbackInfoReturnable<String> ci) {
6566
}
6667

6768
}
68-
String date = new SimpleDateFormat(config.getDateFormat()).format((System.currentTimeMillis()));
69+
String date = new SimpleDateFormat((String) config.Get("dateformat")).format((System.currentTimeMillis()));
6970
title = title.replace("%date%", date);
7071
title = title.replace("%prefix%", config.getPrefix().replace("%version%", stringBuilder.toString()));
7172
title = title.replace("%title%", RandTitle);
73+
title = title.replace("%mod%", String.valueOf(FabricLoader.INSTANCE.getAllMods().size()));
7274
ci.setReturnValue(title);
7375
ci.cancel();
7476
}

src/main/resources/fabric.mod.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"schemaVersion": 1,
33
"id": "randomtitle",
4-
"version": "1.6",
4+
"version": "1.7",
55

66
"name": "RandomTitle",
7-
"description": "Pick random sentences to be window title",
7+
"description": "Pick random sentences as window title",
88
"authors": [
99
"PercyDan"
1010
],
1111
"contact": {
1212
"homepage": "https://github.com/PercyDan54",
13-
"sources": "https://github.com/PercyDan54/RandomTitle-Fabric"
13+
"sources": "https://github.com/PercyDan54/RandomTitle"
1414
},
1515
"environment": "client",
1616
"mixins": [

src/main/resources/title.yml

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1-
#0 = Fetch from https://v1.hitokoto.cn
1+
#0 = Get Chinese quotes from https://v1.hitokoto.cn
22
#1 = Pick from the list below
33
#2 = Random of the two above
44
mode: 2
5+
6+
#Title list
57
title:
8+
- "All this time I was finding myself, and I didn't know I was lost... —— Avicii 《Wake Me Up》"
9+
- "I can't tell where the journey will end, but I know where to start —— Avicii 《Wake Me Up》"
10+
- "I tried carrying the weight of the world, but I only have two hands —— Avicii 《Wake Me Up》"
11+
- "Hope I get the chance to travel the world, but I don't have any plans —— Avicii 《Wake Me Up》"
12+
- "Life's a game made for everyone, and love is the prize —— Avicii 《Wake Me Up》"
13+
- "Strike a match and watch it burn, I'll set the world ablaze —— Fatal Force《Wildfire》"
14+
- "When all eyes are on you, you will know what to do —— Tristam / Braken《Frame of Mind》"
15+
- "Before you go and walk away, you better know where you're going ——Vicetone《Nevada》"
16+
- "If you can fly don't stop at the sky, cause there's footprints on the moon — it was you. —— Owl City《The Yacht Club》"
617
- "众生于我掌声,却未予我身份 ——《狐妖小红娘》"
7-
- "你给的美梦只剩下温柔 ——《镇魂街》"
818
- "一旦得到爱,一旦付出爱,就再也无法忘怀了 ——《夏目友人帐》"
9-
- "他们想要的成熟我想不通,他们嘴里的软弱我听不懂 ——《镇魂街》"
10-
- "所有为你而行的空幻梦想,都不及最后与你许下的愿望 ——《狐妖小红娘》"
11-
- "树大必有枯枝,人多必有白痴。 ——《谢谢你!坏运》"
12-
- "云深夜阑拥月色,少年携酒晚归程,酒香醉情抬眸间,白衣翩立似惊鸿。 —— 《魔道祖师》"
1319
- "无论是人类还是妖怪,只要内心希望他人接触,那么就是相同的存在;会因独处而感到寂寞,也会害怕踏出第一步 ——《夏目友人帐》"
14-
- "Life's a game made for everyone, and love is the prize ——Avicii 《Wake Me Up》"
15-
- "我们在生命的旅程中,逐渐失去着一切,仍坚信并追逐着光明 ——Do As Infinity《深い森》"
20+
- "我们在生命的旅程中,逐渐失去着一切,仍坚信并追逐着光明 —— Do As Infinity《深い森》"
1621
- "假如失去追寻的动力,人类将消失在永恒的黑暗中 —— Do As Infinity《深い森》"
1722
- "我曾难自拔于世界之大,也沉溺于其中梦话,不得真假,不做挣扎 ,不惧笑话 ———《起风了》"
1823
- "我们仅仅只是生存,渐渐的正在失去一切,被侮辱与谎言充斥,而我们仍旧毫无声息地站在那里 —— Do As Infinity《深い森》"
19-
- "Strike a match and watch it burn, I'll set the world ablaze —— Fatal Force《Wildfire》"
2024
- "理解何为孤单,努力想得到爱的你,内心的悲伤是否稍微宣泄了呢 ——《夏目友人帐》"
2125
- "所谓首领,只不过是被势力绑架上制高点的走狗而已。而且,都是身不由己的走狗 ——《狐妖小红娘》"
2226
- "这个世界上,做恶人,需要凶狠狡;做好人,需要比恶人更凶狠更狡猾 ——《万国志》"
23-
- "Hope I get the chance to travel the world, but I don't have any plans ——Avicii 《Wake Me Up》"
2427
- "不可结缘,徒增寂寞 ——《夏目友人帐》"
25-
- "我们彷徨着,无论在哪儿,生活还将继续。再重复,即使无路可走,也要继续前进,生活还将继续 —— Do As Infinity《深い森》"
26-
- "Are you there, or are you just a decoy dream in my head ——Owl City《On the wing》"
2728
- "其实,雨并不公道,因为下落在一个没有公道的世界上 ——《骆驼祥子》"
2829
- "花早晚会凋谢,记忆最终也会消散 ——《镇魂街》 "
2930
- "世事变幻无常,而近乎永恒不变者,唯你我头上的同一片星空 ——《万国志》"
30-
- "All this time I was finding myself, and I didn't know I was lost... ——Avicii 《Wake Me Up》"
31-
- "I can't tell where the journey will end, but I know where to start ——Avicii 《Wake Me Up》"
32-
- "If you can fly don't stop at the sky, cause there's footprints on the moon — it was you. ——Owl City《The Yacht Club》"
33-
- "I tried carrying the weight of the world, but I only have two hands ——Avicii 《Wake Me Up》"
3431
- "如果温柔是罪,那就只能用拳头来洗清我们犯下的罪孽 ——《镇魂街》"
35-
- "When all eyes are on you, you will know what to do ——Tristam / Braken《Frame of Mind》"
36-
- "Before you go and walk away, you better know where you're going ——Vicetone《Nevada》"
37-
prefix:
32+
3833
#The value of %prefix%, %version% for Minecraft version
34+
#If there are multiple items a random one will be used
35+
prefix:
3936
- "Minecraft %version%"
40-
format: "%prefix% | %title%"
37+
4138
#Variables:
42-
#%prefix%,%title%,%date%
43-
dateformat: "yyyy年MM月dd日 HH:mm:ss"
39+
#%prefix% as defined above
40+
#%title% - random title
41+
#%date% - Date formatted as dateformat below
42+
#%mod% - loaded mod count
43+
format: "%prefix% | %title%"
44+
45+
dateformat: "yyyy-MM-dd HH:mm:ss"

0 commit comments

Comments
 (0)