@@ -18,37 +18,29 @@ import kotlinx.coroutines.*
18
18
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
19
19
import net.minecraft.core.BlockPos
20
20
import net.minecraft.core.Direction
21
- import net.minecraft.core.Vec3i
22
21
import net.minecraft.network.protocol.game.*
23
22
import net.minecraft.server.level.ServerPlayer
24
23
import net.minecraft.world.InteractionHand
25
24
import net.minecraft.world.entity.decoration.GlowItemFrame
26
25
import net.minecraft.world.item.Items
27
- import net.minecraft.world.level.saveddata.maps.MapItemSavedData
28
- import net.minecraft.world.phys.Vec3
29
26
import net.silkmc.silk.compose.color.MaterialColorUtils
30
27
import net.silkmc.silk.compose.internal.MapIdGenerator
28
+ import net.silkmc.silk.compose.util.MathUtil
29
+ import net.silkmc.silk.compose.util.MathUtil.toMkArray
30
+ import net.silkmc.silk.compose.util.MathUtil.withoutAxis
31
31
import net.silkmc.silk.core.annotations.ExperimentalSilkApi
32
32
import net.silkmc.silk.core.event.Events
33
33
import net.silkmc.silk.core.event.Server
34
34
import net.silkmc.silk.core.logging.logError
35
35
import net.silkmc.silk.core.task.mcSyncLaunch
36
36
import net.silkmc.silk.core.task.silkCoroutineScope
37
- import org.jetbrains.kotlinx.multik.api.linalg.dot
38
- import org.jetbrains.kotlinx.multik.api.mk
39
- import org.jetbrains.kotlinx.multik.api.ndarray
40
- import org.jetbrains.kotlinx.multik.ndarray.data.D1Array
41
- import org.jetbrains.kotlinx.multik.ndarray.data.get
42
- import org.jetbrains.kotlinx.multik.ndarray.operations.minus
43
- import org.jetbrains.kotlinx.multik.ndarray.operations.times
44
37
import org.jetbrains.skia.Bitmap
45
38
import org.jetbrains.skia.Canvas
46
39
import org.jetbrains.skiko.FrameDispatcher
47
40
import java.util.*
48
41
import java.util.concurrent.ConcurrentHashMap
49
42
import java.util.concurrent.Executors
50
- import kotlin.math.max
51
- import kotlin.math.min
43
+ import kotlin.collections.set
52
44
53
45
/* *
54
46
* Creates a new server-side [MinecraftComposeGui]. This allows you to use any
@@ -128,88 +120,6 @@ class MinecraftComposeGui(
128
120
129
121
return gui.onScroll(scrollDelta * 3 )
130
122
}
131
-
132
- private fun Vec3.toMkArray () = mk.ndarray(doubleArrayOf(x, y, z))
133
- private fun Vec3i.toMkArray () = mk.ndarray(doubleArrayOf(x.toDouble(), y.toDouble(), z.toDouble()))
134
-
135
- // taken from http://rosettacode.org/wiki/Find_the_intersection_of_a_line_with_a_plane#Kotlin
136
- private fun rayPlaneIntersection (
137
- rayPoint : D1Array <Double >,
138
- rayVector : D1Array <Double >,
139
- planePoint : D1Array <Double >,
140
- planeNormal : D1Array <Double >,
141
- ): D1Array <Double > {
142
- val diff = rayPoint - planePoint
143
- val prod1 = diff dot planeNormal
144
- val prod2 = rayVector dot planeNormal
145
- val prod3 = prod1 / prod2
146
- return rayPoint - rayVector * prod3
147
- }
148
-
149
- private fun BlockPos.withoutAxis (axis : Direction .Axis ) = when (axis) {
150
- Direction .Axis .X -> z.toDouble() to y.toDouble()
151
- Direction .Axis .Z -> x.toDouble() to y.toDouble()
152
- // just for the compiler
153
- Direction .Axis .Y -> x.toDouble() to z.toDouble()
154
- }
155
-
156
- private fun D1Array<Double>.withoutAxis (axis : Direction .Axis ) = when (axis) {
157
- Direction .Axis .X -> this [2 ] to this [1 ]
158
- Direction .Axis .Z -> this [0 ] to this [1 ]
159
- // just for the compiler
160
- Direction .Axis .Y -> this [0 ] to this [2 ]
161
- }
162
- }
163
-
164
- private class GuiChunk (
165
- val mapId : Int = MapIdGenerator .nextId(),
166
- private val colors : ByteArray = ByteArray (128 * 128),
167
- ) {
168
- private var dirty = false
169
- private var startX = 0
170
- private var startY = 0
171
- private var endX = 0
172
- private var endY = 0
173
-
174
- fun setColor (x : Int , y : Int , colorId : Byte ) {
175
- val previousColorId = colors[x + y * 128 ]
176
-
177
- if (previousColorId != colorId) {
178
- colors[x + y * 128 ] = colorId
179
-
180
- if (dirty) {
181
- startX = min(startX, x); startY = min(startY, y)
182
- endX = max(endX, x); endY = max(endY, y)
183
- } else {
184
- dirty = true
185
- startX = x; startY = y
186
- endX = x; endY = y
187
- }
188
- }
189
- }
190
-
191
- fun createPacket (): ClientboundMapItemDataPacket ? {
192
- if (! dirty) return null
193
- dirty = false
194
-
195
- val width = endX + 1 - startX
196
- val height = endY + 1 - startY
197
- val packetColors = ByteArray (width * height)
198
-
199
- for (x in 0 until width) {
200
- for (y in 0 until height) {
201
- packetColors[x + y * width] = colors[startX + x + (startY + y) * 128 ]
202
- }
203
- }
204
-
205
- val updateData = MapItemSavedData .MapPatch (startX, startY, width, height, packetColors)
206
- return ClientboundMapItemDataPacket (mapId, 0 , false , null , updateData)
207
- }
208
-
209
- fun createClearPacket (): ClientboundMapItemDataPacket {
210
- val updateData = MapItemSavedData .MapPatch (0 , 0 , 128 , 128 , ByteArray (128 * 128 ))
211
- return ClientboundMapItemDataPacket (mapId, 0 , false , null , updateData)
212
- }
213
123
}
214
124
215
125
private val coroutineContext = Executors .newSingleThreadExecutor().asCoroutineDispatcher()
@@ -353,7 +263,7 @@ class MinecraftComposeGui(
353
263
}
354
264
355
265
private fun calculateOffset (): Offset ? {
356
- val intersection = rayPlaneIntersection(
266
+ val intersection = MathUtil . rayPlaneIntersection(
357
267
player.eyePosition.toMkArray(),
358
268
player.lookAngle.toMkArray(),
359
269
planePoint,
0 commit comments