|
1 | 1 | package NoMathExpectation.chatExchange.neoForged |
2 | 2 |
|
3 | | -import net.neoforged.api.distmarker.Dist |
4 | | -import net.neoforged.bus.api.SubscribeEvent |
5 | | -import net.neoforged.fml.common.EventBusSubscriber |
6 | | -import net.neoforged.fml.config.ModConfig |
7 | | -import net.neoforged.fml.event.config.ModConfigEvent |
8 | | -import net.neoforged.neoforge.common.ModConfigSpec |
9 | | -import thedarkcolour.kotlinforforge.neoforge.forge.LOADING_CONTEXT |
10 | | -import thedarkcolour.kotlinforforge.neoforge.forge.runWhenOn |
11 | | - |
12 | | -@EventBusSubscriber( |
| 3 | +import net.minecraftforge.api.distmarker.Dist |
| 4 | +import net.minecraftforge.common.ForgeConfigSpec |
| 5 | +import net.minecraftforge.eventbus.api.SubscribeEvent |
| 6 | +import net.minecraftforge.fml.common.Mod |
| 7 | +import net.minecraftforge.fml.config.ModConfig |
| 8 | +import net.minecraftforge.fml.event.config.ModConfigEvent |
| 9 | +import thedarkcolour.kotlinforforge.forge.LOADING_CONTEXT |
| 10 | +import thedarkcolour.kotlinforforge.forge.runWhenOn |
| 11 | + |
| 12 | +@Mod.EventBusSubscriber( |
13 | 13 | modid = ChatExchange.ID, |
14 | | - bus = EventBusSubscriber.Bus.MOD |
| 14 | + bus = Mod.EventBusSubscriber.Bus.MOD |
15 | 15 | ) |
16 | 16 | object ChatExchangeConfig { |
17 | | - private val builder = ModConfigSpec.Builder() |
| 17 | + private val builder = ForgeConfigSpec.Builder() |
18 | 18 |
|
19 | | - val host: ModConfigSpec.ConfigValue<String> = builder.comment("The host to bind the exchange server to.") |
| 19 | + val host: ForgeConfigSpec.ConfigValue<String> = builder.comment("The host to bind the exchange server to.") |
20 | 20 | .translation("modid.config.host") |
21 | 21 | .worldRestart() |
22 | 22 | .define("host", "0.0.0.0") |
23 | | - val port: ModConfigSpec.IntValue = builder.comment("The port to bind the exchange server to.") |
| 23 | + val port: ForgeConfigSpec.IntValue = builder.comment("The port to bind the exchange server to.") |
24 | 24 | .translation("modid.config.port") |
25 | 25 | .worldRestart() |
26 | 26 | .defineInRange("port", 9002, 0, 65535) |
27 | | - val token: ModConfigSpec.ConfigValue<String> = |
| 27 | + val token: ForgeConfigSpec.ConfigValue<String> = |
28 | 28 | builder.comment("The token to authenticate with the exchange server.", "Leave blank to disable authentication.") |
29 | 29 | .translation("modid.config.token") |
30 | 30 | .worldRestart() |
31 | 31 | .define("token", "") |
32 | | - val language: ModConfigSpec.ConfigValue<String> = builder.comment("The language the exchange server messages will be.", "Leave blank to use the language the game is using.") |
| 32 | + val language: ForgeConfigSpec.ConfigValue<String> = builder.comment( |
| 33 | + "The language the exchange server messages will be.", |
| 34 | + "Leave blank to use the language the game is using." |
| 35 | + ) |
33 | 36 | .translation("modid.config.language") |
34 | 37 | .worldRestart() |
35 | 38 | .define("language", "") |
36 | 39 |
|
37 | | - val mixinMode: ModConfigSpec.BooleanValue = builder.comment("Whether to use mixin instead of event to listen to server chats.", "If the exchange server isn't sending server chat, try turn this on.") |
| 40 | + val mixinMode: ForgeConfigSpec.BooleanValue = builder.comment( |
| 41 | + "Whether to use mixin instead of event to listen to server chats.", |
| 42 | + "If the exchange server isn't sending server chat, try turn this on." |
| 43 | + ) |
38 | 44 | .translation("modid.config.mixinMode") |
39 | 45 | .define("mixinMode", false) |
40 | 46 |
|
41 | | - val ignoreBotRegex: ModConfigSpec.ConfigValue<String> = builder.comment("The regex to match and ignore the bot players.", "Leave blank to disable.") |
42 | | - .translation("modid.config.ignoreBotRegex") |
43 | | - .define("ignoreBotRegex", "") { |
44 | | - kotlin.runCatching { |
45 | | - val str = it as String |
46 | | - if (str.isBlank()) { |
47 | | - return@runCatching true |
48 | | - } |
49 | | - |
50 | | - it.toRegex() |
51 | | - true |
52 | | - }.getOrDefault(false) |
53 | | - } |
54 | | - val chat: ModConfigSpec.BooleanValue = builder.comment("Whether to broadcast player chatting.", "Players can also broadcast their message by prefixing @broadcast.") |
| 47 | + val ignoreBotRegex: ForgeConfigSpec.ConfigValue<String> = |
| 48 | + builder.comment("The regex to match and ignore the bot players.", "Leave blank to disable.") |
| 49 | + .translation("modid.config.ignoreBotRegex") |
| 50 | + .define("ignoreBotRegex", "") { |
| 51 | + kotlin.runCatching { |
| 52 | + val str = it as String |
| 53 | + if (str.isBlank()) { |
| 54 | + return@runCatching true |
| 55 | + } |
| 56 | + |
| 57 | + it.toRegex() |
| 58 | + true |
| 59 | + }.getOrDefault(false) |
| 60 | + } |
| 61 | + val chat: ForgeConfigSpec.BooleanValue = builder.comment( |
| 62 | + "Whether to broadcast player chatting.", |
| 63 | + "Players can also broadcast their message by prefixing @broadcast." |
| 64 | + ) |
55 | 65 | .translation("modid.config.chat") |
56 | 66 | .define("chat", true) |
57 | | - val joinLeave: ModConfigSpec.BooleanValue = builder.comment("Whether to broadcast player joining and leaving.") |
| 67 | + val joinLeave: ForgeConfigSpec.BooleanValue = builder.comment("Whether to broadcast player joining and leaving.") |
58 | 68 | .translation("modid.config.joinLeave") |
59 | 69 | .define("joinLeave", true) |
60 | | - val death: ModConfigSpec.BooleanValue = builder.comment("Whether to broadcast player deaths.") |
| 70 | + val death: ForgeConfigSpec.BooleanValue = builder.comment("Whether to broadcast player deaths.") |
61 | 71 | .translation("modid.config.death") |
62 | 72 | .define("death", true) |
63 | | - val advancement: ModConfigSpec.BooleanValue = builder.comment("Whether to broadcast player advancements.") |
| 73 | + val advancement: ForgeConfigSpec.BooleanValue = builder.comment("Whether to broadcast player advancements.") |
64 | 74 | .translation("modid.config.advancement") |
65 | 75 | .define("advancement", true) |
66 | 76 |
|
67 | | - val broadcastTriggerPrefix: ModConfigSpec.ConfigValue<MutableList<out String>> = builder.comment("The prefix to recognize to trigger broadcast in chat message.") |
68 | | - .translation("modid.config.broadcastTriggerPrefix") |
69 | | - .defineListAllowEmpty( |
70 | | - "broadcastTriggerPrefix", |
71 | | - { mutableListOf("@广播", "@bc", "@broadcast") }, |
72 | | - { "@broadcast" }, |
73 | | - { true } |
74 | | - ) |
75 | | - val broadcastPrefix: ModConfigSpec.ConfigValue<String> = builder.comment("The prefix to prepend when displaying manually broadcast chat message.") |
76 | | - .translation("modid.config.broadcastPrefix") |
77 | | - .define("broadcastPrefix", "") |
78 | | - val commandBroadcastFormat: ModConfigSpec.ConfigValue<String> = builder.comment("The message format when player broadcast message using the command.", "Will not prepend broadcast prefix.") |
| 77 | + val broadcastTriggerPrefix: ForgeConfigSpec.ConfigValue<MutableList<out String>> = |
| 78 | + builder.comment("The prefix to recognize to trigger broadcast in chat message.") |
| 79 | + .translation("modid.config.broadcastTriggerPrefix") |
| 80 | + .defineListAllowEmpty( |
| 81 | + "broadcastTriggerPrefix", |
| 82 | + { mutableListOf("@广播", "@bc", "@broadcast") }, |
| 83 | + { true } |
| 84 | + ) |
| 85 | + val broadcastPrefix: ForgeConfigSpec.ConfigValue<String> = |
| 86 | + builder.comment("The prefix to prepend when displaying manually broadcast chat message.") |
| 87 | + .translation("modid.config.broadcastPrefix") |
| 88 | + .define("broadcastPrefix", "") |
| 89 | + val commandBroadcastFormat: ForgeConfigSpec.ConfigValue<String> = builder.comment( |
| 90 | + "The message format when player broadcast message using the command.", |
| 91 | + "Will not prepend broadcast prefix." |
| 92 | + ) |
79 | 93 | .translation("modid.config.commandBroadcastFormat") |
80 | | - .define("commandBroadcastFormat", "\"<%s> %s\"") |
81 | | - val receiveMessageFormat: ModConfigSpec.ConfigValue<String> = builder.comment("The message format when receiving message from outside.") |
82 | | - .translation("modid.config.receiveMessageFormat") |
83 | | - .define("receiveMessageFormat", "\"<%s> %s\"") |
| 94 | + .define("commandBroadcastFormat", "\"<%s> %s\"") { |
| 95 | + (it as? String)?.parseJsonToComponent() != null |
| 96 | + } |
| 97 | + val receiveMessageFormat: ForgeConfigSpec.ConfigValue<String> = |
| 98 | + builder.comment("The message format when receiving message from outside.") |
| 99 | + .translation("modid.config.receiveMessageFormat") |
| 100 | + .define("receiveMessageFormat", "\"<%s> %s\"") { |
| 101 | + (it as? String)?.parseJsonToComponent() != null |
| 102 | + } |
84 | 103 |
|
85 | | - val spec: ModConfigSpec = builder.build() |
| 104 | + val spec: ForgeConfigSpec = builder.build() |
86 | 105 |
|
87 | 106 | private var registered = false |
88 | 107 | internal fun register() { |
89 | 108 | if (registered) { |
90 | 109 | error("Config is already registered!") |
91 | 110 | } |
92 | 111 |
|
93 | | - val modContainer = LOADING_CONTEXT.activeContainer |
94 | | - modContainer.registerConfig(ModConfig.Type.COMMON, spec) |
| 112 | + LOADING_CONTEXT.registerConfig(ModConfig.Type.COMMON, spec) |
95 | 113 | runWhenOn(Dist.CLIENT) { |
96 | 114 | registerOnClient() |
97 | 115 | } |
|
0 commit comments