From 4d93f475aadc42c4bf83c3a0749af41659235c71 Mon Sep 17 00:00:00 2001 From: nea Date: Tue, 11 Jul 2023 21:01:58 +0200 Subject: Bulk commit --- .../moe/nea/firmament/util/FirmFormatters.kt | 5 +++ src/main/kotlin/moe/nea/firmament/util/MC.kt | 4 +++ .../moe/nea/firmament/util/ScoreboardUtil.kt | 38 ++++++++++++++++++++++ .../moe/nea/firmament/util/item/NbtItemData.kt | 18 ++++++++++ src/main/kotlin/moe/nea/firmament/util/regex.kt | 4 +++ .../firmament/util/render/RenderInWorldContext.kt | 4 +++ 6 files changed, 73 insertions(+) create mode 100644 src/main/kotlin/moe/nea/firmament/util/ScoreboardUtil.kt create mode 100644 src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt create mode 100644 src/main/kotlin/moe/nea/firmament/util/regex.kt (limited to 'src/main/kotlin/moe/nea/firmament/util') diff --git a/src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt b/src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt index ba77b04..a3e93ae 100644 --- a/src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt +++ b/src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt @@ -2,6 +2,7 @@ package moe.nea.firmament.util import com.google.common.math.IntMath.pow import kotlin.math.absoluteValue +import kotlin.time.Duration object FirmFormatters { fun toString(float: Float, fractionalDigits: Int): String = toString(float.toDouble(), fractionalDigits) @@ -13,4 +14,8 @@ object FirmFormatters { return long.toString() + (if (digits.isEmpty()) "" else ".$digits") } + fun formatTimespan(duration: Duration): String { + return duration.toString() + } + } diff --git a/src/main/kotlin/moe/nea/firmament/util/MC.kt b/src/main/kotlin/moe/nea/firmament/util/MC.kt index e9ab6c9..d595b61 100644 --- a/src/main/kotlin/moe/nea/firmament/util/MC.kt +++ b/src/main/kotlin/moe/nea/firmament/util/MC.kt @@ -24,6 +24,10 @@ import net.minecraft.client.gui.screen.ingame.HandledScreen import net.minecraft.util.math.BlockPos object MC { + fun sendCommand(command: String) { + player?.networkHandler?.sendCommand(command) + } + inline val font get() = MinecraftClient.getInstance().textRenderer inline val soundManager get() = MinecraftClient.getInstance().soundManager inline val player get() = MinecraftClient.getInstance().player diff --git a/src/main/kotlin/moe/nea/firmament/util/ScoreboardUtil.kt b/src/main/kotlin/moe/nea/firmament/util/ScoreboardUtil.kt new file mode 100644 index 0000000..d1bd057 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/ScoreboardUtil.kt @@ -0,0 +1,38 @@ +package moe.nea.firmament.util + +import java.util.Optional +import net.minecraft.scoreboard.Scoreboard +import net.minecraft.scoreboard.Team +import net.minecraft.text.StringVisitable +import net.minecraft.text.Style +import net.minecraft.text.Text +import net.minecraft.util.Formatting + +fun getScoreboardLines(): List { + val scoreboard = MC.player?.scoreboard ?: return listOf() + val activeObjective = scoreboard.getObjectiveForSlot(Scoreboard.SIDEBAR_DISPLAY_SLOT_ID) ?: return listOf() + return scoreboard.getAllPlayerScores(activeObjective).reversed().take(15).map { + val team = scoreboard.getPlayerTeam(it.playerName) + Team.decorateName(team, Text.literal(it.playerName)) + } +} + + +fun Text.formattedString(): String { + val sb = StringBuilder() + visit(StringVisitable.StyledVisitor { style, string -> + val c = Formatting.byName(style.color?.name) + if (c != null) { + sb.append("§${c.code}") + } + if (style.isUnderlined) { + sb.append("§n") + } + if (style.isBold) { + sb.append("§l") + } + sb.append(string) + Optional.empty() + }, Style.EMPTY) + return sb.toString().replace("§[^a-f0-9]".toRegex(), "") +} diff --git a/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt b/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt new file mode 100644 index 0000000..2e721d8 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt @@ -0,0 +1,18 @@ +package moe.nea.firmament.util.item + +import net.minecraft.item.ItemStack +import net.minecraft.nbt.NbtElement +import net.minecraft.nbt.NbtString +import net.minecraft.text.Text + +val ItemStack.loreAccordingToNbt + get() = getOrCreateSubNbt(ItemStack.DISPLAY_KEY).getList(ItemStack.LORE_KEY, NbtElement.STRING_TYPE.toInt()) + .map { Text.Serializer.fromJson((it as NbtString).asString()) } + +val ItemStack.displayNameAccordingToNbt + get() = getOrCreateSubNbt(ItemStack.DISPLAY_KEY).let { + if (it.contains(ItemStack.NAME_KEY, NbtElement.STRING_TYPE.toInt())) + Text.Serializer.fromJson(it.getString(ItemStack.NAME_KEY)) + else + null + } diff --git a/src/main/kotlin/moe/nea/firmament/util/regex.kt b/src/main/kotlin/moe/nea/firmament/util/regex.kt new file mode 100644 index 0000000..6317b59 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/regex.kt @@ -0,0 +1,4 @@ +package moe.nea.firmament.util + +inline fun String.ifMatches(regex: Regex, block: (MatchResult) -> T): T? = + regex.matchEntire(this)?.let(block) diff --git a/src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt b/src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt index 1603b6b..1d51f13 100644 --- a/src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt +++ b/src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt @@ -76,6 +76,10 @@ class RenderInWorldContext private constructor( } } + fun waypoint(position: BlockPos, label: Text) { + text(position.toCenterPos(), label, Text.literal("§e${MC.player?.pos?.distanceTo(position.toCenterPos())}m")) + } + fun text(position: Vec3d, vararg texts: Text, verticalAlign: VerticalAlign = VerticalAlign.CENTER) { assertTrueOr(texts.isNotEmpty()) { return@text } matrixStack.push() -- cgit