diff --git a/README.md b/README.md index a4ae425..97fce01 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A Minecraft mod that picks random title from a list to display as window title. 中文介绍见[MCBBS](https://www.mcbbs.net/thread-980991-1-1.html) ## Configuration -See title.yml +See [title.yml](https://github.com/PercyDan54/RandomTitle-Fabric/blob/master/src/main/resources/title.yml) ## Download Download from [Releases](https://github.com/PercyDan54/RandomTitle-Fabric/releases) diff --git a/gradle.properties b/gradle.properties index 11cfef5..c92725e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.11.1 # Mod Properties - mod_version = 1.6 + mod_version = 1.7 maven_group = net.fabricmc archives_base_name = RandomTitle-Fabric diff --git a/src/main/java/me/PercyDan/RandomTitle/ConfigManager.java b/src/main/java/me/PercyDan/RandomTitle/ConfigManager.java index 22afcb1..b290e8b 100644 --- a/src/main/java/me/PercyDan/RandomTitle/ConfigManager.java +++ b/src/main/java/me/PercyDan/RandomTitle/ConfigManager.java @@ -17,19 +17,22 @@ public class ConfigManager { private final Logger LOGGER = LogManager.getLogger("RandomTitle"); - Map config; File configFile = new File("title.yml"); + Map config; + Map defaultConfig; public ConfigManager() { + LOGGER.info("RandomTitle by PercyDan https://github.com/PercyDan54/RandomTitle"); + defaultConfig = new Yaml().load(this.getClass().getResourceAsStream("/title.yml")); if (!configFile.exists()) { LOGGER.info("Config file not found, creating default"); createDefault(); - config = new Yaml().load(this.getClass().getResourceAsStream("/title.yml")); } try { config = new Yaml().load(new FileInputStream(configFile)); } catch (Exception e) { LOGGER.error("Failed to load config!", e); + config = defaultConfig; } } @@ -50,29 +53,46 @@ public void createDefault() { } } + public Object Get(String key) { + Object value = config.get(key); + if (value == null) return defaultConfig.get(key); + return value; + } - public String getTitleFromList() { + private String getTitleFromList() { String title = ""; try { - List titles = (List) config.get("title"); - title = titles.get(new Random().nextInt(titles.size())).toString(); + List titles = (List) Get("title"); + title = titles.get(new Random().nextInt(titles.size())); } catch (Throwable e) { LOGGER.error("Failed to get title from config!", e); } return title; } - public String getTitleFromHitokoto() { - LOGGER.info("Getting title from hitokoto"); + private String getTitleFromHitokoto() { + LOGGER.info("Getting title from Hitokoto API"); String title; String response; try { response = EntityUtils.toString(HttpClients.createDefault().execute(new HttpGet("https://v1.hitokoto.cn/")).getEntity()); - LOGGER.info("Response String: " + response); + LOGGER.info("Hitokoto Response String: " + response); JsonObject json = new JsonParser().parse(response).getAsJsonObject(); String from = json.get("from").getAsString(); String sentence = json.get("hitokoto").getAsString(); - title = sentence + " ——" + from; + String type = json.get("type").getAsString(); + title = sentence + " —— "; + switch (type) { + case "e": + title += json.get("creator").getAsString() + " 原创"; + break; + case "f": + title += "来自网络"; + break; + default: + title += from; + } + } catch (Throwable e) { LOGGER.error("Failed to get title from API!", e); return getTitleFromList(); @@ -81,7 +101,8 @@ public String getTitleFromHitokoto() { } public String getTitle() { - int mode = (int) config.get("mode"); + int mode = (int) Get("mode"); + switch (mode) { case 0: return getTitleFromHitokoto(); @@ -99,17 +120,8 @@ public String getTitle() { } public String getPrefix() { - List prefixes = (List) config.get("prefix"); - return prefixes.get((new Random()).nextInt(prefixes.size())).toString(); - } - - public String getFormat() { - return (String) config.get("format"); - } - - public String getDateFormat() { - - return (String) config.get("dateformat"); + List prefixes = (List) Get("prefix"); + return prefixes.get((new Random()).nextInt(prefixes.size())); } } diff --git a/src/main/java/me/PercyDan/RandomTitle/Mixins/MixinMinecraft.java b/src/main/java/me/PercyDan/RandomTitle/Mixins/MixinMinecraft.java index 190d31a..c38c2b5 100644 --- a/src/main/java/me/PercyDan/RandomTitle/Mixins/MixinMinecraft.java +++ b/src/main/java/me/PercyDan/RandomTitle/Mixins/MixinMinecraft.java @@ -1,6 +1,7 @@ package me.PercyDan.RandomTitle.Mixins; import me.PercyDan.RandomTitle.ConfigManager; +import net.fabricmc.loader.FabricLoader; import net.minecraft.SharedConstants; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; @@ -48,7 +49,7 @@ public abstract class MixinMinecraft { @Inject(method = "getWindowTitle", at = @At("HEAD"), cancellable = true) private void getWindowTitle(CallbackInfoReturnable ci) { - String title = config.getFormat(); + String title = (String) config.Get("format"); StringBuilder stringBuilder = new StringBuilder(SharedConstants.getGameVersion().getName()); stringBuilder.append(" "); ClientPlayNetworkHandler clientPlayNetworkHandler = this.getNetworkHandler(); @@ -65,10 +66,11 @@ private void getWindowTitle(CallbackInfoReturnable ci) { } } - String date = new SimpleDateFormat(config.getDateFormat()).format((System.currentTimeMillis())); + String date = new SimpleDateFormat((String) config.Get("dateformat")).format((System.currentTimeMillis())); title = title.replace("%date%", date); title = title.replace("%prefix%", config.getPrefix().replace("%version%", stringBuilder.toString())); title = title.replace("%title%", RandTitle); + title = title.replace("%mod%", String.valueOf(FabricLoader.INSTANCE.getAllMods().size())); ci.setReturnValue(title); ci.cancel(); } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 62495e1..31d6be7 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -1,16 +1,16 @@ { "schemaVersion": 1, "id": "randomtitle", - "version": "1.6", + "version": "1.7", "name": "RandomTitle", - "description": "Pick random sentences to be window title", + "description": "Pick random sentences as window title", "authors": [ "PercyDan" ], "contact": { "homepage": "https://github.com/PercyDan54", - "sources": "https://github.com/PercyDan54/RandomTitle-Fabric" + "sources": "https://github.com/PercyDan54/RandomTitle" }, "environment": "client", "mixins": [ diff --git a/src/main/resources/title.yml b/src/main/resources/title.yml index dc60724..d9cfe4c 100644 --- a/src/main/resources/title.yml +++ b/src/main/resources/title.yml @@ -1,43 +1,45 @@ -#0 = Fetch from https://v1.hitokoto.cn +#0 = Get Chinese quotes from https://v1.hitokoto.cn #1 = Pick from the list below #2 = Random of the two above mode: 2 + +#Title list title: + - "All this time I was finding myself, and I didn't know I was lost... —— Avicii 《Wake Me Up》" + - "I can't tell where the journey will end, but I know where to start —— Avicii 《Wake Me Up》" + - "I tried carrying the weight of the world, but I only have two hands —— Avicii 《Wake Me Up》" + - "Hope I get the chance to travel the world, but I don't have any plans —— Avicii 《Wake Me Up》" + - "Life's a game made for everyone, and love is the prize —— Avicii 《Wake Me Up》" + - "Strike a match and watch it burn, I'll set the world ablaze —— Fatal Force《Wildfire》" + - "When all eyes are on you, you will know what to do —— Tristam / Braken《Frame of Mind》" + - "Before you go and walk away, you better know where you're going ——Vicetone《Nevada》" + - "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》" - "众生于我掌声,却未予我身份 ——《狐妖小红娘》" - - "你给的美梦只剩下温柔 ——《镇魂街》" - "一旦得到爱,一旦付出爱,就再也无法忘怀了 ——《夏目友人帐》" - - "他们想要的成熟我想不通,他们嘴里的软弱我听不懂 ——《镇魂街》" - - "所有为你而行的空幻梦想,都不及最后与你许下的愿望 ——《狐妖小红娘》" - - "树大必有枯枝,人多必有白痴。 ——《谢谢你!坏运》" - - "云深夜阑拥月色,少年携酒晚归程,酒香醉情抬眸间,白衣翩立似惊鸿。 —— 《魔道祖师》" - "无论是人类还是妖怪,只要内心希望他人接触,那么就是相同的存在;会因独处而感到寂寞,也会害怕踏出第一步 ——《夏目友人帐》" - - "Life's a game made for everyone, and love is the prize ——Avicii 《Wake Me Up》" - - "我们在生命的旅程中,逐渐失去着一切,仍坚信并追逐着光明 ——Do As Infinity《深い森》" + - "我们在生命的旅程中,逐渐失去着一切,仍坚信并追逐着光明 —— Do As Infinity《深い森》" - "假如失去追寻的动力,人类将消失在永恒的黑暗中 —— Do As Infinity《深い森》" - "我曾难自拔于世界之大,也沉溺于其中梦话,不得真假,不做挣扎 ,不惧笑话 ———《起风了》" - "我们仅仅只是生存,渐渐的正在失去一切,被侮辱与谎言充斥,而我们仍旧毫无声息地站在那里 —— Do As Infinity《深い森》" - - "Strike a match and watch it burn, I'll set the world ablaze —— Fatal Force《Wildfire》" - "理解何为孤单,努力想得到爱的你,内心的悲伤是否稍微宣泄了呢 ——《夏目友人帐》" - "所谓首领,只不过是被势力绑架上制高点的走狗而已。而且,都是身不由己的走狗 ——《狐妖小红娘》" - "这个世界上,做恶人,需要凶狠狡;做好人,需要比恶人更凶狠更狡猾 ——《万国志》" - - "Hope I get the chance to travel the world, but I don't have any plans ——Avicii 《Wake Me Up》" - "不可结缘,徒增寂寞 ——《夏目友人帐》" - - "我们彷徨着,无论在哪儿,生活还将继续。再重复,即使无路可走,也要继续前进,生活还将继续 —— Do As Infinity《深い森》" - - "Are you there, or are you just a decoy dream in my head ——Owl City《On the wing》" - "其实,雨并不公道,因为下落在一个没有公道的世界上 ——《骆驼祥子》" - "花早晚会凋谢,记忆最终也会消散 ——《镇魂街》 " - "世事变幻无常,而近乎永恒不变者,唯你我头上的同一片星空 ——《万国志》" - - "All this time I was finding myself, and I didn't know I was lost... ——Avicii 《Wake Me Up》" - - "I can't tell where the journey will end, but I know where to start ——Avicii 《Wake Me Up》" - - "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》" - - "I tried carrying the weight of the world, but I only have two hands ——Avicii 《Wake Me Up》" - "如果温柔是罪,那就只能用拳头来洗清我们犯下的罪孽 ——《镇魂街》" - - "When all eyes are on you, you will know what to do ——Tristam / Braken《Frame of Mind》" - - "Before you go and walk away, you better know where you're going ——Vicetone《Nevada》" -prefix: + #The value of %prefix%, %version% for Minecraft version +#If there are multiple items a random one will be used +prefix: - "Minecraft %version%" -format: "%prefix% | %title%" + #Variables: -#%prefix%,%title%,%date% -dateformat: "yyyy年MM月dd日 HH:mm:ss" \ No newline at end of file +#%prefix% as defined above +#%title% - random title +#%date% - Date formatted as dateformat below +#%mod% - loaded mod count +format: "%prefix% | %title%" + +dateformat: "yyyy-MM-dd HH:mm:ss" \ No newline at end of file