forked from hannibal002/SkyHanni
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic language support for changing colour to color and the other…
… way around thanks nea for the code <3
- Loading branch information
1 parent
c0bf1d1
commit 6a94de6
Showing
5 changed files
with
177 additions
and
7 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/main/java/at/hannibal2/skyhanni/config/BritishSpellingWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/at/hannibal2/skyhanni/events/minecraft/LanguageChangeEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
17 changes: 17 additions & 0 deletions
17
src/main/java/at/hannibal2/skyhanni/mixins/transformers/LanguageChangeEventPatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |