aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-07-11 21:01:58 +0200
committernea <nea@nea.moe>2023-07-11 21:01:58 +0200
commit4d93f475aadc42c4bf83c3a0749af41659235c71 (patch)
treec8e01710defe66e06c50fa962c72fdac66a35a1f /src/main/kotlin/moe/nea/firmament/util
parent4444fcca44d9a53c8162d69e0e9f19fd214c2f54 (diff)
downloadFirmament-4d93f475aadc42c4bf83c3a0749af41659235c71.tar.gz
Firmament-4d93f475aadc42c4bf83c3a0749af41659235c71.tar.bz2
Firmament-4d93f475aadc42c4bf83c3a0749af41659235c71.zip
Bulk commit
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/FirmFormatters.kt5
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/MC.kt4
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/ScoreboardUtil.kt38
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt18
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/regex.kt4
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt4
6 files changed, 73 insertions, 0 deletions
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<Text> {
+ 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<Unit> { 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 <T> 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()