From 20e054c0f892c2ee08d59930d42c3c2c37bd4cc5 Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Sat, 2 Jan 2021 01:09:44 +0100 Subject: [PATCH] Fix getVoxelsInCube --- .../space/blokk/util/UntilPossiblyNegative.kt | 4 ++++ .../src/main/kotlin/space/blokk/world/World.kt | 16 +++++++--------- .../blokk/net/packet/play/ChunkDataPacket.kt | 10 +++++++++- .../main/kotlin/space/blokk/net/BlokkSession.kt | 2 +- .../space/blokk/net/LoginAndJoinProcedure.kt | 2 +- .../kotlin/space/blokk/testplugin/TestPlugin.kt | 3 +++ 6 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 blokk-api/src/main/kotlin/space/blokk/util/UntilPossiblyNegative.kt diff --git a/blokk-api/src/main/kotlin/space/blokk/util/UntilPossiblyNegative.kt b/blokk-api/src/main/kotlin/space/blokk/util/UntilPossiblyNegative.kt new file mode 100644 index 0000000..b823e4c --- /dev/null +++ b/blokk-api/src/main/kotlin/space/blokk/util/UntilPossiblyNegative.kt @@ -0,0 +1,4 @@ +package space.blokk.util + +infix fun Int.untilPossiblyNegative(other: Int) = + IntProgression.fromClosedRange(this, other, if (this > other) -1 else 1) diff --git a/blokk-api/src/main/kotlin/space/blokk/world/World.kt b/blokk-api/src/main/kotlin/space/blokk/world/World.kt index 0d56a0f..d5eacd9 100644 --- a/blokk-api/src/main/kotlin/space/blokk/world/World.kt +++ b/blokk-api/src/main/kotlin/space/blokk/world/World.kt @@ -5,6 +5,7 @@ import space.blokk.Blokk import space.blokk.entity.Entity import space.blokk.util.newSingleThreadDispatcher import space.blokk.util.supervisorChild +import space.blokk.util.untilPossiblyNegative import java.util.* import kotlin.coroutines.CoroutineContext @@ -59,17 +60,14 @@ abstract class World(val uuid: UUID) { */ @OptIn(ExperimentalStdlibApi::class) fun getVoxelsInCube(cornerA: VoxelLocation, cornerB: VoxelLocation, hollow: Boolean = false): List { - val xList = if (hollow) listOf(cornerA.x, cornerB.x).distinct() else (cornerA.x..cornerB.x).toList() - val zList = if (hollow) listOf(cornerA.z, cornerB.z).distinct() else (cornerA.z..cornerB.z).toList() - - val yList = - if (hollow) listOf(cornerA.y.toInt(), cornerB.y.toInt()).distinct() - else (cornerA.y.toInt()..cornerB.y.toInt()).toList() + fun getList(a: Int, b: Int) = + if (hollow) listOf(a, b).distinct() + else (a untilPossiblyNegative b).toList() return buildList { - for(x in xList) { - for(y in yList) { - for(z in zList) { + for(x in getList(cornerA.x, cornerB.x)) { + for(y in getList(cornerA.y.toInt(), cornerB.y.toInt())) { + for(z in getList(cornerA.z, cornerB.z)) { add(getVoxel(VoxelLocation(x, y.toUByte(), z))) } } diff --git a/blokk-packets/src/main/kotlin/space/blokk/net/packet/play/ChunkDataPacket.kt b/blokk-packets/src/main/kotlin/space/blokk/net/packet/play/ChunkDataPacket.kt index 019e542..9cb9d80 100644 --- a/blokk-packets/src/main/kotlin/space/blokk/net/packet/play/ChunkDataPacket.kt +++ b/blokk-packets/src/main/kotlin/space/blokk/net/packet/play/ChunkDataPacket.kt @@ -8,4 +8,12 @@ import space.blokk.world.ChunkData /** * Sent to inform the player about blocks and biomes in a chunk. */ -data class ChunkDataPacket(val key: Chunk.Key, val data: ChunkData) : OutgoingPacket() +data class ChunkDataPacket(val key: Chunk.Key, val data: ChunkData) : OutgoingPacket() { + init { + if (key.x == 0 && key.z == 0) { + data.sections[0]?.forEach { + print(it) + } + } + } +} diff --git a/blokk-server/src/main/kotlin/space/blokk/net/BlokkSession.kt b/blokk-server/src/main/kotlin/space/blokk/net/BlokkSession.kt index ad0e7c7..1a9851e 100644 --- a/blokk-server/src/main/kotlin/space/blokk/net/BlokkSession.kt +++ b/blokk-server/src/main/kotlin/space/blokk/net/BlokkSession.kt @@ -34,7 +34,7 @@ import kotlin.properties.Delegates class BlokkSession(private val channel: io.netty.channel.Channel, val server: BlokkServer) : Session() { override val address: InetAddress = (channel.remoteAddress() as InetSocketAddress).address - private val identifier = "BlokkSession(${address.hostAddress})" + System.identityHashCode(this) + private val identifier = "BlokkSession(${address.hostAddress})" val logger = Logger(identifier) override val coroutineContext: CoroutineContext = server.coroutineContext.supervisorChild(identifier) diff --git a/blokk-server/src/main/kotlin/space/blokk/net/LoginAndJoinProcedure.kt b/blokk-server/src/main/kotlin/space/blokk/net/LoginAndJoinProcedure.kt index 0d4cc95..c15a814 100644 --- a/blokk-server/src/main/kotlin/space/blokk/net/LoginAndJoinProcedure.kt +++ b/blokk-server/src/main/kotlin/space/blokk/net/LoginAndJoinProcedure.kt @@ -201,7 +201,7 @@ class LoginAndJoinProcedure(val session: BlokkSession) { session.send(UpdateViewPositionPacket(Chunk.Key.from(player.location.toVoxelLocation()))) - session.scheduleKeepAlivePacket() + session.scheduleKeepAlivePacket(true) player.sendChunksAndLight() // WorldBorder diff --git a/test-plugin/src/main/kotlin/space/blokk/testplugin/TestPlugin.kt b/test-plugin/src/main/kotlin/space/blokk/testplugin/TestPlugin.kt index a907fb2..01563fd 100644 --- a/test-plugin/src/main/kotlin/space/blokk/testplugin/TestPlugin.kt +++ b/test-plugin/src/main/kotlin/space/blokk/testplugin/TestPlugin.kt @@ -38,6 +38,9 @@ class TestPlugin: Plugin("Test", "1.0.0") { } Blokk.eventBus.on { event -> + event.gameMode = GameMode.CREATIVE + event.canFly = true + event.flying = true event.initialWorldAndLocation = WorldAndLocationWithRotation( world, VoxelLocation.of(0, 2, 0).atTopCenter().withRotation(0f, 0f)