1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package moe.nea.firmament.features.world
import com.mojang.brigadier.arguments.IntegerArgumentType
import me.shedaniel.math.Color
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.seconds
import net.minecraft.command.argument.BlockPosArgumentType
import net.minecraft.text.Text
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d
import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.commands.get
import moe.nea.firmament.commands.thenArgument
import moe.nea.firmament.commands.thenExecute
import moe.nea.firmament.commands.thenLiteral
import moe.nea.firmament.events.CommandEvent
import moe.nea.firmament.events.TickEvent
import moe.nea.firmament.events.WorldRenderLastEvent
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.util.ClipboardUtils
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.mc.asFakeServer
import moe.nea.firmament.util.render.RenderInWorldContext
import moe.nea.firmament.util.tr
object Waypoints : FirmamentFeature {
override val identifier: String
get() = "waypoints"
object TConfig : ManagedConfig(identifier, Category.MINING) { // TODO: add to misc
val tempWaypointDuration by duration("temp-waypoint-duration", 0.seconds, 1.hours) { 30.seconds }
val showIndex by toggle("show-index") { true }
val skipToNearest by toggle("skip-to-nearest") { false }
// TODO: look ahead size
}
override val config get() = TConfig
var waypoints: FirmWaypoints? = null
var orderedIndex = 0
@Subscribe
fun onRenderOrderedWaypoints(event: WorldRenderLastEvent) {
val w = useNonEmptyWaypoints() ?: return
RenderInWorldContext.renderInWorld(event) {
if (!w.isOrdered) {
w.waypoints.withIndex().forEach {
block(it.value.blockPos, 0x800050A0.toInt())
if (TConfig.showIndex) withFacingThePlayer(it.value.blockPos.toCenterPos()) {
text(Text.literal(it.index.toString()))
}
}
} else {
orderedIndex %= w.waypoints.size
val firstColor = Color.ofRGBA(0, 200, 40, 180)
color(firstColor)
tracer(w.waypoints[orderedIndex].blockPos.toCenterPos(), lineWidth = 3f)
w.waypoints.withIndex().toList().wrappingWindow(orderedIndex, 3).zip(listOf(
firstColor,
Color.ofRGBA(180, 200, 40, 150),
Color.ofRGBA(180, 80, 20, 140),
)).reversed().forEach { (waypoint, col) ->
val (index, pos) = waypoint
block(pos.blockPos, col.color)
if (TConfig.showIndex) withFacingThePlayer(pos.blockPos.toCenterPos()) {
text(Text.literal(index.toString()))
}
}
}
}
}
@Subscribe
fun onTick(event: TickEvent) {
val w = useNonEmptyWaypoints() ?: return
if (!w.isOrdered) return
orderedIndex %= w.waypoints.size
val p = MC.player?.pos ?: return
if (TConfig.skipToNearest) {
orderedIndex =
(w.waypoints.withIndex().minBy { it.value.blockPos.getSquaredDistance(p) }.index + 1) % w.waypoints.size
} else {
if (w.waypoints[orderedIndex].blockPos.isWithinDistance(p, 3.0)) {
orderedIndex = (orderedIndex + 1) % w.waypoints.size
}
}
}
fun useEditableWaypoints(): FirmWaypoints {
var w = waypoints
if (w == null) {
w = FirmWaypoints("Unlabeled", "unknown", null, mutableListOf(), false)
waypoints = w
}
return w
}
fun useNonEmptyWaypoints(): FirmWaypoints? {
val w = waypoints
if (w == null) return null
if (w.waypoints.isEmpty()) return null
return w
}
val WAYPOINTS_SUBCOMMAND = "waypoints"
@Subscribe
fun onCommand(event: CommandEvent.SubCommand) {
event.subcommand("waypoint") {
thenArgument("pos", BlockPosArgumentType.blockPos()) { pos ->
thenExecute {
source
val position = pos.get(this).toAbsoluteBlockPos(source.asFakeServer())
val w = useEditableWaypoints()
w.waypoints.add(FirmWaypoints.Waypoint.from(position))
source.sendFeedback(Text.stringifiedTranslatable("firmament.command.waypoint.added",
position.x,
position.y,
position.z))
}
}
}
event.subcommand(WAYPOINTS_SUBCOMMAND) {
thenLiteral("clear") {
thenExecute {
waypoints = null
source.sendFeedback(Text.translatable("firmament.command.waypoint.clear"))
}
}
thenLiteral("toggleordered") {
thenExecute {
val w = useEditableWaypoints()
w.isOrdered = !w.isOrdered
if (w.isOrdered) {
val p = MC.player?.pos ?: Vec3d.ZERO
orderedIndex = // TODO: this should be extracted to a utility method
w.waypoints.withIndex().minByOrNull { it.value.blockPos.getSquaredDistance(p) }?.index ?: 0
}
source.sendFeedback(Text.translatable("firmament.command.waypoint.ordered.toggle.${w.isOrdered}"))
}
}
thenLiteral("skip") {
thenExecute {
val w = useNonEmptyWaypoints()
if (w != null && w.isOrdered) {
orderedIndex = (orderedIndex + 1) % w.size
source.sendFeedback(Text.translatable("firmament.command.waypoint.skip"))
} else {
source.sendError(Text.translatable("firmament.command.waypoint.skip.error"))
}
}
}
thenLiteral("remove") {
thenArgument("index", IntegerArgumentType.integer(0)) { indexArg ->
thenExecute {
val index = get(indexArg)
val w = useNonEmptyWaypoints()
if (w != null && index in w.waypoints.indices) {
w.waypoints.removeAt(index)
source.sendFeedback(Text.stringifiedTranslatable("firmament.command.waypoint.remove",
index))
} else {
source.sendError(Text.stringifiedTranslatable("firmament.command.waypoint.remove.error"))
}
}
}
}
}
}
fun textNothingToExport(): Text =
tr("firmament.command.waypoint.export.nowaypoints",
"No waypoints to export found. Add some with /firm waypoint ~ ~ ~.")
}
fun <E> List<E>.wrappingWindow(startIndex: Int, windowSize: Int): List<E> {
val result = ArrayList<E>(windowSize)
if (startIndex + windowSize < size) {
result.addAll(subList(startIndex, startIndex + windowSize))
} else {
result.addAll(subList(startIndex, size))
result.addAll(subList(0, minOf(windowSize - (size - startIndex), startIndex)))
}
return result
}
|