Skip to content

Commit

Permalink
add basic language support for changing colour to color and the other…
Browse files Browse the repository at this point in the history
… way around

thanks nea for the code <3
Co-authored-by: nea <[email protected]>
  • Loading branch information
catgirlseraid committed Oct 15, 2024
1 parent c0bf1d1 commit 9532d2e
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 7 deletions.
103 changes: 103 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/config/BritishSpellingWrapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package at.hannibal2.skyhanni.config

import io.github.notenoughupdates.moulconfig.Config
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption
import io.github.notenoughupdates.moulconfig.processor.ConfigProcessorDriver
import io.github.notenoughupdates.moulconfig.processor.ConfigStructureReader
import java.lang.reflect.Field
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy

abstract class RenamingWrapper(val wrappedProcessor: ConfigStructureReader) : ConfigStructureReader {
override fun pushPath(fieldPath: String?) {
wrappedProcessor.pushPath(fieldPath)
}

override fun popPath() {
wrappedProcessor.popPath()
}

override fun beginConfig(
configClass: Class<out Config?>?,
driver: ConfigProcessorDriver?,
configObject: Config?
) {
wrappedProcessor.beginConfig(configClass, driver, configObject)
}

override fun endConfig() {
wrappedProcessor.endConfig()
}

override fun setCategoryParent(field: Field?) {
wrappedProcessor.setCategoryParent(field)
}

fun wrapOption(option: ConfigOption): ConfigOption {
val originalHandler = Proxy.getInvocationHandler(option)
return Proxy.newProxyInstance(
option.javaClass.classLoader,
arrayOf<Class<*>>(ConfigOption::class.java),
object : InvocationHandler {
override fun invoke(
proxy: Any?,
method: Method,
args: Array<out Any?>?
): Any? {
var proxyResult = originalHandler.invoke(proxy, method, args)
if (method.name == "name" || method.name == "desc") {
proxyResult = mapText(proxyResult as String)
}
return proxyResult
}
}) as ConfigOption
}

abstract fun mapText(original: String): String

override fun beginCategory(
baseObject: Any?,
field: Field?,
name: String,
description: String,
) {
wrappedProcessor.beginCategory(baseObject, field, mapText(name), mapText(description))
}

override fun endCategory() {
wrappedProcessor.endCategory()
}

override fun beginAccordion(
baseObject: Any?,
field: Field?,
option: ConfigOption,
id: Int,
) {
wrappedProcessor.beginAccordion(baseObject, field, wrapOption(option), id)
}

override fun endAccordion() {
wrappedProcessor.endAccordion()
}

override fun emitOption(
baseObject: Any?,
field: Field?,
option: ConfigOption,
) {
wrappedProcessor.emitOption(baseObject, field, wrapOption(option))
}
}

class BritishSpellingWrapper(wrappedProcessor: ConfigStructureReader) : RenamingWrapper(wrappedProcessor) {
override fun mapText(original: String): String {
return original.replace("(?i)(col)(o)(r)".toRegex()) {
it.groupValues[1] +
it.groupValues[2] +
(if (it.groupValues[2].single().isUpperCase()) "U" else "u") +
it.groupValues[3]
}
}
}
24 changes: 22 additions & 2 deletions src/main/java/at/hannibal2/skyhanni/config/ConfigGuiManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,38 @@ object ConfigGuiManager {

var editor: MoulConfigEditor<Features>? = null


private val replacedSearchTerms = mapOf(
"color" to "colour",
"colour" to "color",
"armor" to "armour",
"armour" to "armor",
"endermen" to "enderman",
"enderman" to "endermen",
"hotkey" to "keybind",
"gray" to "grey"
"keybind" to "hotkey",
"gray" to "grey",
"grey" to "gray",
)

private fun getPossibleAltWords(word: String): List<String> {
return buildList {
replacedSearchTerms.forEach { first, second ->
if (first.startsWith(word, ignoreCase = true)) {
add(second)
}

}
}
}

fun getEditorInstance() = editor ?: MoulConfigEditor(SkyHanniMod.configManager.processor).also {
it.setSearchFunction { optionEditor, word ->
return@setSearchFunction optionEditor.fulfillsSearch(replacedSearchTerms[word] ?: word)
if (optionEditor.fulfillsSearch(word)) return@setSearchFunction true
getPossibleAltWords(word).forEach{
if (optionEditor.fulfillsSearch(it)) return@setSearchFunction true
}
return@setSearchFunction false
}
editor = it
}
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package at.hannibal2.skyhanni.config

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.core.config.Position
import at.hannibal2.skyhanni.config.core.config.PositionList
import at.hannibal2.skyhanni.data.jsonobjects.local.FriendsJson
import at.hannibal2.skyhanni.data.jsonobjects.local.JacobContestsJson
import at.hannibal2.skyhanni.data.jsonobjects.local.KnownFeaturesJson
import at.hannibal2.skyhanni.data.jsonobjects.local.VisualWordsJson
import at.hannibal2.skyhanni.events.LorenzEvent
import at.hannibal2.skyhanni.events.minecraft.LanguageChangeEvent
import at.hannibal2.skyhanni.features.misc.update.UpdateManager
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.DelayedRun
Expand All @@ -26,6 +29,7 @@ import io.github.notenoughupdates.moulconfig.annotations.ConfigLink
import io.github.notenoughupdates.moulconfig.processor.BuiltinMoulConfigGuis
import io.github.notenoughupdates.moulconfig.processor.ConfigProcessorDriver
import io.github.notenoughupdates.moulconfig.processor.MoulConfigProcessor
import net.minecraft.client.Minecraft
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.File
Expand Down Expand Up @@ -62,6 +66,7 @@ class ConfigManager {
private val jsonHolder: Map<ConfigFileType, Any> = EnumMap(ConfigFileType::class.java)

lateinit var processor: MoulConfigProcessor<Features>
private var hasBeenInited = false
private var disableSaving = false

private fun setConfigHolder(type: ConfigFileType, value: Any) {
Expand All @@ -88,16 +93,35 @@ class ConfigManager {
}

val features = SkyHanniMod.feature
processor = MoulConfigProcessor(SkyHanniMod.feature)
BuiltinMoulConfigGuis.addProcessors(processor)
UpdateManager.injectConfigProcessor(processor)
ConfigProcessorDriver(processor).processConfig(features)
recreateProcessor()

try {
findPositionLinks(features, mutableSetOf())
} catch (e: Exception) {
if (LorenzEvent.isInGuardedEventHandler) throw e
}
hasBeenInited = true
}

@SkyHanniModule
object LanguageManager {
@HandleEvent
fun onLanguageSwitch(event: LanguageChangeEvent) {
if (SkyHanniMod.configManager.hasBeenInited) {
SkyHanniMod.configManager.recreateProcessor()
ConfigGuiManager.editor = null
}
}
}

fun recreateProcessor() {
processor = MoulConfigProcessor(SkyHanniMod.feature)
BuiltinMoulConfigGuis.addProcessors(processor)
UpdateManager.injectConfigProcessor(processor)
ConfigProcessorDriver(
if (Minecraft.getMinecraft().languageManager.currentLanguage.languageCode.contains("gb", ignoreCase = true))
BritishSpellingWrapper(processor) else processor,
).processConfig(SkyHanniMod.feature)
}

// Some position elements don't need config links as they don't have a config option.
Expand Down Expand Up @@ -153,7 +177,7 @@ class ConfigManager {
println("")
println(
"This crash is here to remind you to fix the missing " +
"@ConfigLink annotation over your new config position config element."
"@ConfigLink annotation over your new config position config element.",
)
println("")
println("Steps to fix:")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package at.hannibal2.skyhanni.events.minecraft

import at.hannibal2.skyhanni.api.event.SkyHanniEvent
import net.minecraft.client.resources.Language

data class LanguageChangeEvent(val newLanguage: Language) : SkyHanniEvent()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package at.hannibal2.skyhanni.mixins.transformers;

import at.hannibal2.skyhanni.events.minecraft.LanguageChangeEvent;
import net.minecraft.client.resources.Language;
import net.minecraft.client.resources.LanguageManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(LanguageManager.class)
public class LanguageChangeEventPatch {
@Inject(method = "setCurrentLanguage", at = @At("TAIL"))
private void onLanguageSet(Language currentLanguageIn, CallbackInfo ci) {
new LanguageChangeEvent(currentLanguageIn).post();
}
}

0 comments on commit 9532d2e

Please sign in to comment.