Skip to content

Commit e3fdbe0

Browse files
committed
Separate MinecraftComposeGui mono file into multiple files
1 parent 82e5d49 commit e3fdbe0

File tree

3 files changed

+110
-95
lines changed

3 files changed

+110
-95
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package net.silkmc.silk.compose
2+
3+
import net.minecraft.network.protocol.game.ClientboundMapItemDataPacket
4+
import net.minecraft.world.level.saveddata.maps.MapItemSavedData
5+
import net.silkmc.silk.compose.internal.MapIdGenerator
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
internal class GuiChunk(
10+
val mapId: Int = MapIdGenerator.nextId(),
11+
private val colors: ByteArray = ByteArray(128 * 128),
12+
) {
13+
private var dirty = false
14+
private var startX = 0
15+
private var startY = 0
16+
private var endX = 0
17+
private var endY = 0
18+
19+
fun setColor(x: Int, y: Int, colorId: Byte) {
20+
val previousColorId = colors[x + y * 128]
21+
22+
if (previousColorId != colorId) {
23+
colors[x + y * 128] = colorId
24+
25+
if (dirty) {
26+
startX = min(startX, x); startY = min(startY, y)
27+
endX = max(endX, x); endY = max(endY, y)
28+
} else {
29+
dirty = true
30+
startX = x; startY = y
31+
endX = x; endY = y
32+
}
33+
}
34+
}
35+
36+
fun createPacket(): ClientboundMapItemDataPacket? {
37+
if (!dirty) return null
38+
dirty = false
39+
40+
val width = endX + 1 - startX
41+
val height = endY + 1 - startY
42+
val packetColors = ByteArray(width * height)
43+
44+
for (x in 0 until width) {
45+
for (y in 0 until height) {
46+
packetColors[x + y * width] = colors[startX + x + (startY + y) * 128]
47+
}
48+
}
49+
50+
val updateData = MapItemSavedData.MapPatch(startX, startY, width, height, packetColors)
51+
return ClientboundMapItemDataPacket(mapId, 0, false, null, updateData)
52+
}
53+
54+
fun createClearPacket(): ClientboundMapItemDataPacket {
55+
val updateData = MapItemSavedData.MapPatch(0, 0, 128, 128, ByteArray(128 * 128))
56+
return ClientboundMapItemDataPacket(mapId, 0, false, null, updateData)
57+
}
58+
}

silk-compose/src/main/kotlin/net/silkmc/silk/compose/MinecraftComposeGui.kt

+5-95
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,29 @@ import kotlinx.coroutines.*
1818
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
1919
import net.minecraft.core.BlockPos
2020
import net.minecraft.core.Direction
21-
import net.minecraft.core.Vec3i
2221
import net.minecraft.network.protocol.game.*
2322
import net.minecraft.server.level.ServerPlayer
2423
import net.minecraft.world.InteractionHand
2524
import net.minecraft.world.entity.decoration.GlowItemFrame
2625
import net.minecraft.world.item.Items
27-
import net.minecraft.world.level.saveddata.maps.MapItemSavedData
28-
import net.minecraft.world.phys.Vec3
2926
import net.silkmc.silk.compose.color.MaterialColorUtils
3027
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
3131
import net.silkmc.silk.core.annotations.ExperimentalSilkApi
3232
import net.silkmc.silk.core.event.Events
3333
import net.silkmc.silk.core.event.Server
3434
import net.silkmc.silk.core.logging.logError
3535
import net.silkmc.silk.core.task.mcSyncLaunch
3636
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
4437
import org.jetbrains.skia.Bitmap
4538
import org.jetbrains.skia.Canvas
4639
import org.jetbrains.skiko.FrameDispatcher
4740
import java.util.*
4841
import java.util.concurrent.ConcurrentHashMap
4942
import java.util.concurrent.Executors
50-
import kotlin.math.max
51-
import kotlin.math.min
43+
import kotlin.collections.set
5244

5345
/**
5446
* Creates a new server-side [MinecraftComposeGui]. This allows you to use any
@@ -128,88 +120,6 @@ class MinecraftComposeGui(
128120

129121
return gui.onScroll(scrollDelta * 3)
130122
}
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-
}
213123
}
214124

215125
private val coroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
@@ -353,7 +263,7 @@ class MinecraftComposeGui(
353263
}
354264

355265
private fun calculateOffset(): Offset? {
356-
val intersection = rayPlaneIntersection(
266+
val intersection = MathUtil.rayPlaneIntersection(
357267
player.eyePosition.toMkArray(),
358268
player.lookAngle.toMkArray(),
359269
planePoint,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.silkmc.silk.compose.util
2+
3+
import net.minecraft.core.BlockPos
4+
import net.minecraft.core.Direction
5+
import net.minecraft.core.Vec3i
6+
import net.minecraft.world.phys.Vec3
7+
import org.jetbrains.kotlinx.multik.api.linalg.dot
8+
import org.jetbrains.kotlinx.multik.api.mk
9+
import org.jetbrains.kotlinx.multik.api.ndarray
10+
import org.jetbrains.kotlinx.multik.ndarray.data.D1Array
11+
import org.jetbrains.kotlinx.multik.ndarray.data.get
12+
import org.jetbrains.kotlinx.multik.ndarray.operations.minus
13+
import org.jetbrains.kotlinx.multik.ndarray.operations.times
14+
15+
internal object MathUtil {
16+
17+
fun Vec3.toMkArray() = mk.ndarray(doubleArrayOf(x, y, z))
18+
fun Vec3i.toMkArray() = mk.ndarray(doubleArrayOf(x.toDouble(), y.toDouble(), z.toDouble()))
19+
20+
// taken from http://rosettacode.org/wiki/Find_the_intersection_of_a_line_with_a_plane#Kotlin
21+
fun rayPlaneIntersection(
22+
rayPoint: D1Array<Double>,
23+
rayVector: D1Array<Double>,
24+
planePoint: D1Array<Double>,
25+
planeNormal: D1Array<Double>,
26+
): D1Array<Double> {
27+
val diff = rayPoint - planePoint
28+
val prod1 = diff dot planeNormal
29+
val prod2 = rayVector dot planeNormal
30+
val prod3 = prod1 / prod2
31+
return rayPoint - rayVector * prod3
32+
}
33+
34+
fun BlockPos.withoutAxis(axis: Direction.Axis) = when (axis) {
35+
Direction.Axis.X -> z.toDouble() to y.toDouble()
36+
Direction.Axis.Z -> x.toDouble() to y.toDouble()
37+
// just for the compiler
38+
Direction.Axis.Y -> x.toDouble() to z.toDouble()
39+
}
40+
41+
fun D1Array<Double>.withoutAxis(axis: Direction.Axis) = when (axis) {
42+
Direction.Axis.X -> this[2] to this[1]
43+
Direction.Axis.Z -> this[0] to this[1]
44+
// just for the compiler
45+
Direction.Axis.Y -> this[0] to this[2]
46+
}
47+
}

0 commit comments

Comments
 (0)