aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/kotlin/features/world/ColeWeightCompat.kt94
-rw-r--r--src/main/kotlin/features/world/Waypoints.kt65
2 files changed, 96 insertions, 63 deletions
diff --git a/src/main/kotlin/features/world/ColeWeightCompat.kt b/src/main/kotlin/features/world/ColeWeightCompat.kt
index 6a17522..ce659d5 100644
--- a/src/main/kotlin/features/world/ColeWeightCompat.kt
+++ b/src/main/kotlin/features/world/ColeWeightCompat.kt
@@ -2,8 +2,16 @@ package moe.nea.firmament.features.world
import kotlinx.serialization.Serializable
import net.minecraft.text.Text
+import net.minecraft.util.math.BlockPos
import moe.nea.firmament.Firmament
-import moe.nea.firmament.util.ErrorUtil
+import moe.nea.firmament.annotations.Subscribe
+import moe.nea.firmament.commands.DefaultSource
+import moe.nea.firmament.commands.thenExecute
+import moe.nea.firmament.commands.thenLiteral
+import moe.nea.firmament.events.CommandEvent
+import moe.nea.firmament.util.ClipboardUtils
+import moe.nea.firmament.util.MC
+import moe.nea.firmament.util.tr
object ColeWeightCompat {
@Serializable
@@ -16,9 +24,15 @@ object ColeWeightCompat {
val b: Int = 0,
)
- fun intoFirm(waypoints: List<ColeWeightWaypoint>): FirmWaypoints {
+ fun fromFirm(waypoints: FirmWaypoints, relativeTo: BlockPos): List<ColeWeightWaypoint> {
+ return waypoints.waypoints.map {
+ ColeWeightWaypoint(it.x - relativeTo.x, it.y - relativeTo.y, it.z - relativeTo.z)
+ }
+ }
+
+ fun intoFirm(waypoints: List<ColeWeightWaypoint>, relativeTo: BlockPos): FirmWaypoints {
val w = waypoints.map {
- FirmWaypoints.Waypoint(it.x, it.y, it.z)
+ FirmWaypoints.Waypoint(it.x + relativeTo.x, it.y + relativeTo.y, it.z + relativeTo.z)
}
return FirmWaypoints(
"Imported Waypoints",
@@ -29,6 +43,80 @@ object ColeWeightCompat {
)
}
+ fun copyAndInform(
+ source: DefaultSource,
+ origin: BlockPos,
+ positiveFeedback: (Int) -> Text,
+ ) {
+ val waypoints = Waypoints.useNonEmptyWaypoints()
+ ?.let { fromFirm(it, origin) }
+ if (waypoints == null) {
+ source.sendError(tr("firmament.command.waypoint.export.nowaypoints",
+ "No waypoints to export found."))
+ return
+ }
+ val data =
+ Firmament.tightJson.encodeToString<List<ColeWeightWaypoint>>(waypoints)
+ ClipboardUtils.setTextContent(data)
+ source.sendFeedback(positiveFeedback(waypoints.size))
+ }
+
+ fun importAndInform(
+ source: DefaultSource,
+ pos: BlockPos,
+ positiveFeedback: (Int) -> Text
+ ) {
+ val text = ClipboardUtils.getTextContents()
+ val wr = tryParse(text).map { intoFirm(it, pos) }
+ val waypoints = wr.getOrElse {
+ source.sendError(
+ tr("firmament.command.waypoint.import.cw.error",
+ "Could not import ColeWeight waypoints."))
+ Firmament.logger.error(it)
+ return
+ }
+ Waypoints.waypoints = waypoints
+ source.sendFeedback(positiveFeedback(waypoints.size))
+ }
+
+ @Subscribe
+ fun onEvent(event: CommandEvent.SubCommand) {
+ event.subcommand(Waypoints.WAYPOINTS_SUBCOMMAND) {
+ thenLiteral("exportcw") {
+ thenExecute {
+ copyAndInform(source, BlockPos.ORIGIN) {
+ tr("firmament.command.waypoint.export.cw",
+ "Copied $it waypoints to clipboard in ColeWeight format.")
+ }
+ }
+ }
+ thenLiteral("exportrelativecw") {
+ thenExecute {
+ copyAndInform(source, MC.player?.blockPos ?: BlockPos.ORIGIN) {
+ tr("firmament.command.waypoint.export.relative",
+ "Copied $it relative waypoints to clipboard in ColeWeight format. Make sure to stand in the same position when importing.")
+ }
+ }
+ }
+ thenLiteral("import") {
+ thenExecute {
+ importAndInform(source, BlockPos.ORIGIN) { it: Int ->
+ Text.stringifiedTranslatable("firmament.command.waypoint.import",
+ it)
+ }
+ }
+ }
+ thenLiteral("importrelative") {
+ thenExecute {
+ importAndInform(source, MC.player!!.blockPos) {
+ tr("firmament.command.waypoint.import.relative",
+ "Imported $it relative waypoints from clipboard. Make sure you stand in the same position as when you exported these waypoints for them to line up correctly.")
+ }
+ }
+ }
+ }
+ }
+
fun tryParse(string: String): Result<List<ColeWeightWaypoint>> {
return runCatching {
Firmament.tightJson.decodeFromString<List<ColeWeightWaypoint>>(string)
diff --git a/src/main/kotlin/features/world/Waypoints.kt b/src/main/kotlin/features/world/Waypoints.kt
index 467fa0f..3e9a41a 100644
--- a/src/main/kotlin/features/world/Waypoints.kt
+++ b/src/main/kotlin/features/world/Waypoints.kt
@@ -91,7 +91,7 @@ object Waypoints : FirmamentFeature {
fun useEditableWaypoints(): FirmWaypoints {
var w = waypoints
if (w == null) {
- w = FirmWaypoints("Unlabeled", "unlabeled", null, mutableListOf(), false)
+ w = FirmWaypoints("Unlabeled", "unknown", null, mutableListOf(), false)
waypoints = w
}
return w
@@ -104,6 +104,8 @@ object Waypoints : FirmamentFeature {
return w
}
+ val WAYPOINTS_SUBCOMMAND = "waypoints"
+
@Subscribe
fun onCommand(event: CommandEvent.SubCommand) {
event.subcommand("waypoint") {
@@ -120,10 +122,10 @@ object Waypoints : FirmamentFeature {
}
}
}
- event.subcommand("waypoints") {
+ event.subcommand(WAYPOINTS_SUBCOMMAND) {
thenLiteral("clear") {
thenExecute {
- waypoints?.waypoints?.clear()
+ waypoints = null
source.sendFeedback(Text.translatable("firmament.command.waypoint.clear"))
}
}
@@ -165,65 +167,8 @@ object Waypoints : FirmamentFeature {
}
}
}
- thenLiteral("export") {
- thenExecute {
- TODO()
-// val data = Firmament.tightJson.encodeToString<List<ColeWeightWaypoint>>(waypoints.map {
-// ColeWeightWaypoint(it.x,
-// it.y,
-// it.z)
-// })
-// ClipboardUtils.setTextContent(data)
-// source.sendFeedback(tr("firmament.command.waypoint.export",
-// "Copied ${waypoints.size} waypoints to clipboard"))
- }
- }
- thenLiteral("exportrelative") {
- thenExecute {
- TODO()
-// val playerPos = MC.player!!.blockPos
-// val x = playerPos.x
-// val y = playerPos.y
-// val z = playerPos.z
-// val data = Firmament.tightJson.encodeToString<List<ColeWeightWaypoint>>(waypoints.map {
-// ColeWeightWaypoint(it.x - x,
-// it.y - y,
-// it.z - z)
-// })
-// ClipboardUtils.setTextContent(data)
-// source.sendFeedback(tr("firmament.command.waypoint.export.relative",
-// "Copied ${waypoints.size} relative waypoints to clipboard. Make sure to stand in the same position when importing."))
-//
- }
- }
- thenLiteral("import") {
- thenExecute {
- source.sendFeedback(
- importRelative(BlockPos.ORIGIN)// TODO: rework imports
- ?: Text.stringifiedTranslatable("firmament.command.waypoint.import",
- useNonEmptyWaypoints()?.waypoints?.size),
- )
- }
- }
- thenLiteral("importrelative") {
- thenExecute {
- source.sendFeedback(
- importRelative(MC.player!!.blockPos) ?: tr("firmament.command.waypoint.import.relative",
- "Imported ${useNonEmptyWaypoints()?.waypoints?.size} relative waypoints from clipboard. Make sure you stand in the same position as when you exported these waypoints for them to line up correctly."),
- )
- }
- }
}
}
-
- fun importRelative(pos: BlockPos): Text? {
- val contents = ClipboardUtils.getTextContents()
- val cw = ColeWeightCompat.tryParse(contents).map { ColeWeightCompat.intoFirm(it) }
- waypoints = cw.getOrNull() // TODO: directly parse firm waypoints
- return null // TODO: show error if this does not work
- // TODO: make relative imports work again
- }
-
}
fun <E> List<E>.wrappingWindow(startIndex: Int, windowSize: Int): List<E> {