diff options
| author | Linnea Gräf <nea@nea.moe> | 2024-10-30 04:41:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-30 14:41:40 +1100 |
| commit | 2f4eda4736bd368afd5037ff221e5447ea93113c (patch) | |
| tree | 65c85c38bbae8e4fec68dc07ce27d681326fc514 | |
| parent | 79adf7aa070fbfaa23241c9b5b8495628650ef68 (diff) | |
| download | SkyHanni-2f4eda4736bd368afd5037ff221e5447ea93113c.tar.gz SkyHanni-2f4eda4736bd368afd5037ff221e5447ea93113c.tar.bz2 SkyHanni-2f4eda4736bd368afd5037ff221e5447ea93113c.zip | |
Backend: getFormattedTextCompat method (#2811)
Co-authored-by: Cal <cwolfson58@gmail.com>
| -rw-r--r-- | build.gradle.kts | 17 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/compat/TextCompat.kt | 53 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/compat/World.kt | 23 | ||||
| -rw-r--r-- | versions/1.21/buildpaths-test.txt | 2 | ||||
| -rw-r--r-- | versions/1.21/buildpaths.txt | 1 | ||||
| -rw-r--r-- | versions/1.21/src/test/kotlin/TestLegacyColorFormat.kt | 22 | ||||
| -rw-r--r-- | versions/1.21/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension | 0 | ||||
| -rw-r--r-- | versions/mapping-1.16.5-forge-1.12.2.txt | 2 |
8 files changed, 97 insertions, 23 deletions
diff --git a/build.gradle.kts b/build.gradle.kts index cbd8eda3a..15586f279 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -25,7 +25,7 @@ import kotlin.io.path.outputStream plugins { idea java - id("com.github.johnrengelman.shadow") version "7.1.2" + id("com.gradleup.shadow") version "8.3.4" id("gg.essential.loom") id("dev.deftu.gradle.preprocess") kotlin("jvm") @@ -277,6 +277,21 @@ if (target == ProjectTarget.MAIN) { } } +fun includeBuildPaths(buildPathsFile: File, sourceSet: Provider<SourceSet>) { + if (buildPathsFile.exists()) { + sourceSet.get().apply { + val buildPaths = buildPathsFile.readText().lineSequence() + .map { it.substringBefore("#").trim() } + .filter { it.isNotBlank() } + .toSet() + kotlin.include(buildPaths) + java.include(buildPaths) + } + } +} +includeBuildPaths(file("buildpaths.txt"), sourceSets.main) +includeBuildPaths(file("buildpaths-test.txt"), sourceSets.test) + tasks.withType<KotlinCompile> { compilerOptions.jvmTarget.set(JvmTarget.fromTarget(target.minecraftVersion.formattedJavaLanguageVersion)) } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/compat/TextCompat.kt b/src/main/java/at/hannibal2/skyhanni/utils/compat/TextCompat.kt new file mode 100644 index 000000000..f34efe473 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/compat/TextCompat.kt @@ -0,0 +1,53 @@ +package at.hannibal2.skyhanni.utils.compat + +import net.minecraft.util.IChatComponent +//#if MC > 1.16 +//$$ import net.minecraft.ChatFormatting +//$$ import net.minecraft.network.chat.TextColor +//#endif +//#if MC > 1.20 +//$$ import net.minecraft.text.MutableText +//$$ import net.minecraft.text.PlainTextContent +//#endif + +fun IChatComponent.getDirectlyContainedText() = +//#if MC < 1.16 + this.unformattedTextForChat +//#elseif MC < 1.20 +//$$ this.contents +//#else +//$$ (this.content as? PlainTextContent)?.string().orEmpty() +//#endif + +fun IChatComponent.getFormattedTextCompat() = +//#if MC < 1.16 + this.formattedText +//#else +//$$run { +//$$ val sb = StringBuilder() +//$$ for (component in iterator()) { +//$$ sb.append(component.style.color?.toChatFormatting()?.toString() ?: "§r") +//$$ sb.append(component.getDirectlyContainedText()) +//$$ sb.append("§r") +//$$ } +//$$ sb.toString() +//$$} +//$$ +//$$private val textColorLUT = ChatFormatting.entries +//$$ .mapNotNull { formatting -> formatting.color?.let { it to formatting } } +//$$ .toMap() +//$$ +//$$fun TextColor.toChatFormatting(): ChatFormatting? { +//$$ return textColorLUT[this.value] +//$$} +//$$ +//$$fun Component.iterator(): Sequence<Component> { +//$$ return sequenceOf(this) + siblings.asSequence().flatMap { it.iterator() } // TODO: in theory we want to properly inherit styles here +//$$} +//#endif + +//#if MC > 1.20 +//$$fun MutableText.withColor(formatting: Formatting): Text { +//$$ return this.styled { it.withColor(formatting) } +//$$} +//#endif diff --git a/src/main/java/at/hannibal2/skyhanni/utils/compat/World.kt b/src/main/java/at/hannibal2/skyhanni/utils/compat/World.kt index 7916eb4e5..6402e44b4 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/compat/World.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/compat/World.kt @@ -6,7 +6,6 @@ import net.minecraft.entity.Entity import net.minecraft.entity.item.EntityArmorStand import net.minecraft.entity.player.EntityPlayer import net.minecraft.potion.Potion -import net.minecraft.util.IChatComponent fun WorldClient.getLoadedPlayers(): List<EntityPlayer> = //#if MC < 1.14 @@ -15,18 +14,17 @@ fun WorldClient.getLoadedPlayers(): List<EntityPlayer> = //$$ this.players() //#endif - fun Entity.getNameAsString(): String = this.name //#if MC >= 1.14 -//$$ .getString() +//$$ .string //#endif fun EntityArmorStand.getArmorOrFullInventory() = //#if MC < 1.12 this.inventory //#else -//$$ this.getArmorInventoryList() +//$$ this.armorInventoryList //#endif fun Minecraft.isOnMainThread() = @@ -36,21 +34,6 @@ fun Minecraft.isOnMainThread() = //$$ this.isSameThread //#endif -fun IChatComponent.getFormattedTextCompat() = -//#if MC < 1.16 - this.formattedText -//#else -//$$ run { -//$$ val sb = StringBuilder() -//$$ for (component in iterator()) { -//$$ sb.append(component.style.formattingCode) -//$$ sb.append(component.unformattedComponentText) -//$$ sb.append("§r") -//$$ } -//$$ sb.toString() -//$$ } -//#endif - object Effects { val invisibility = //#if MC <1.12 @@ -59,5 +42,3 @@ object Effects { //$$ net.minecraft.init.PotionTypes.INVISIBILITY //#endif } - - diff --git a/versions/1.21/buildpaths-test.txt b/versions/1.21/buildpaths-test.txt new file mode 100644 index 000000000..6a660321a --- /dev/null +++ b/versions/1.21/buildpaths-test.txt @@ -0,0 +1,2 @@ +at/hannibal2/skyhanni/utils/compat/TextCompat.kt +TestLegacyColorFormat.kt diff --git a/versions/1.21/buildpaths.txt b/versions/1.21/buildpaths.txt new file mode 100644 index 000000000..867bab8c0 --- /dev/null +++ b/versions/1.21/buildpaths.txt @@ -0,0 +1 @@ +at/hannibal2/skyhanni/utils/compat/TextCompat.kt diff --git a/versions/1.21/src/test/kotlin/TestLegacyColorFormat.kt b/versions/1.21/src/test/kotlin/TestLegacyColorFormat.kt new file mode 100644 index 000000000..f1c2323ef --- /dev/null +++ b/versions/1.21/src/test/kotlin/TestLegacyColorFormat.kt @@ -0,0 +1,22 @@ +import net.minecraft.text.Text +import net.minecraft.util.Formatting +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import at.hannibal2.skyhanni.utils.compat.* + +class TestLegacyColorFormat { + @Test + fun testLegacyColorFormatString() { + val text = Text.literal("") + .append(Text.literal("[").withColor(Formatting.DARK_GRAY)) + .append(Text.literal("302").withColor(Formatting.BLUE)) + .append(Text.literal("] ").withColor(Formatting.DARK_GRAY)) + .append(Text.literal("♫ ").withColor(Formatting.GOLD)) + .append(Text.literal("[MVP").withColor(Formatting.AQUA)) + .append(Text.literal("+").withColor(Formatting.LIGHT_PURPLE)) + .append(Text.literal("] lrg89").withColor(Formatting.AQUA)) + .append(Text.literal(": test").withColor(Formatting.WHITE)) + Assertions.assertEquals("§r§r§8[§r§9302§r§8] §r§6♫ §r§b[MVP§r§d+§r§b] lrg89§r§f: test§r", text.getFormattedTextCompat()) + } + +} diff --git a/versions/1.21/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension b/versions/1.21/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/versions/1.21/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension diff --git a/versions/mapping-1.16.5-forge-1.12.2.txt b/versions/mapping-1.16.5-forge-1.12.2.txt index 71f6c71d2..1293441ec 100644 --- a/versions/mapping-1.16.5-forge-1.12.2.txt +++ b/versions/mapping-1.16.5-forge-1.12.2.txt @@ -1,4 +1,3 @@ - com.mojang.blaze3d.systems.RenderSystem net.minecraft.client.renderer.GlStateManager com.mojang.blaze3d.systems.RenderSystem color4f() color() com.mojang.blaze3d.systems.RenderSystem translatef() translate() @@ -146,6 +145,7 @@ net.minecraft.network.protocol.game.ServerboundPickItemPacket net.minecraft.netw net.minecraft.server.packs.resources.ResourceManager net.minecraft.client.resources.IResourceManager net.minecraft.world.effect.MobEffect net.minecraft.potion.Potion +net.minecraft.world.effect.MobEffects net.minecraft.init.PotionTypes net.minecraft.world.entity.Entity level getEntityWorld() net.minecraft.world.entity.Entity net.minecraft.entity.Entity |
