From 5a7d12e5fce95749c6ee1d3d16758d4ec3be10a6 Mon Sep 17 00:00:00 2001 From: Boy0000 Date: Sat, 26 Nov 2022 16:21:13 +0100 Subject: [PATCH] update config format (#97) --- .../mineinabyss/deeperworld/world/Region.kt | 26 +++++++------ .../deeperworld/world/section/Section.kt | 17 +++++---- src/main/resources/config.yml | 37 ++++--------------- 3 files changed, 31 insertions(+), 49 deletions(-) diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/world/Region.kt b/src/main/kotlin/com/mineinabyss/deeperworld/world/Region.kt index d6ef8b8..28bb8d1 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/world/Region.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/world/Region.kt @@ -12,44 +12,46 @@ import kotlin.math.min * Represents a region of the world. */ @Serializable(with = RegionSerializer::class) -class Region(val a: CubePoint, val b: CubePoint) { - val min = CubePoint(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)) - val max = CubePoint(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)) +class Region(val start: CubePoint, val end: CubePoint) { + val min = CubePoint(min(start.x, end.x), min(start.y, end.y), min(start.z, end.z)) + val max = CubePoint(max(start.x, end.x), max(start.y, end.y), max(start.z, end.z)) constructor(ax: Int, ay: Int, az: Int, bx: Int, by: Int, bz: Int) : this( CubePoint(ax, ay, az), CubePoint(bx, by, bz) ) - val center: CubePoint get() = a.plus(b).div(2) + val center: CubePoint get() = start.plus(end).div(2) fun contains(x: Int, y: Int, z: Int): Boolean = - x in min(a.x, b.x)..max(a.x, b.x) && - y in min(a.y, b.y)..max(a.y, b.y) && - z in min(a.z, b.z)..max(a.z, b.z) + x in min(start.x, end.x)..max(start.x, end.x) && + y in min(start.y, end.y)..max(start.y, end.y) && + z in min(start.z, end.z)..max(start.z, end.z) operator fun contains(p: CubePoint) = contains(p.x, p.y, p.z) } @Serializable -data class RegionPoints(val x1: Int, val y1: Int, val z1: Int, val x2: Int, val y2: Int, val z2: Int) +data class RegionPoints(val start: String, val end: String) object RegionSerializer : KSerializer { private val serializer = RegionPoints.serializer() override val descriptor: SerialDescriptor = serializer.descriptor override fun serialize(encoder: Encoder, value: Region) { - val (x, y, z) = value.a - val (x2, y2, z2) = value.b - encoder.encodeSerializableValue(serializer, RegionPoints(x, y, z, x2, y2, z2)) + val start = value.start.toString() + val end = value.end.toString() + encoder.encodeSerializableValue(serializer, RegionPoints(start, end)) } override fun deserialize(decoder: Decoder): Region { - val (x, y, z, x2, y2, z2) = decoder.decodeSerializableValue(serializer) + val decoded = decoder.decodeSerializableValue(serializer) + val (x, y, z, x2, y2, z2) = decoded.start.getCoordinates() + decoded.end.getCoordinates() return Region(x, y, z, x2, y2, z2) } } private operator fun List.component6(): E = this[5] +internal fun String.getCoordinates() = this.replace(" ", "").split(",", limit = 3).map { it.toIntOrNull() ?: 0 } diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/world/section/Section.kt b/src/main/kotlin/com/mineinabyss/deeperworld/world/section/Section.kt index 6a94864..8d65442 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/world/section/Section.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/world/section/Section.kt @@ -3,6 +3,7 @@ package com.mineinabyss.deeperworld.world.section import com.mineinabyss.deeperworld.world.Region +import com.mineinabyss.deeperworld.world.getCoordinates import com.mineinabyss.idofront.serialization.LocationSerializer import com.mineinabyss.idofront.serialization.VectorSerializer import com.mineinabyss.idofront.serialization.WorldSerializer @@ -22,15 +23,15 @@ import org.bukkit.World * @property referenceTop the reference location between this section and the one above it. * This and the section above's [referenceBottom] represent the same location in physical space. * @property referenceBottom the reference location between this section and the one below it. - * This and the section belows's [referenceTop] represent the same location in physical space. + * This and the section belows' [referenceTop] represent the same location in physical space. */ @Serializable data class Section( val name: String? = null, val region: Region, val world: @Serializable(WorldSerializer::class) World, - @SerialName("refTop") private val _refTop: ReferenceLocation, - @SerialName("refBottom") private val _refBottom: ReferenceLocation + @SerialName("refTop") private val _refTop: String, + @SerialName("refBottom") private val _refBottom: String ) { @Serializable(LocationSerializer::class) val referenceTop = _refTop.toLocation(world) @@ -38,6 +39,11 @@ data class Section( @Serializable(LocationSerializer::class) val referenceBottom = _refBottom.toLocation(world) + fun String.toLocation(world: World): Location { + val (x,y,z) = this.getCoordinates().map { it.toDouble() } + return Location(world, x, y, z) + } + val key: SectionKey = name?.let { AbstractSectionKey.CustomSectionKey(name) } ?: AbstractSectionKey.InternalSectionKey() @@ -49,8 +55,3 @@ data class Section( override fun toString() = key.toString() } - -@Serializable -data class ReferenceLocation(val x: Int, val y: Int, val z: Int) { - fun toLocation(world: World) = Location(world, x.toDouble(), y.toDouble(), z.toDouble()) -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5e15865..a6776c3 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,40 +1,19 @@ ### Example config: sections: - name: section1 - refTop: - x: 0 - y: 0 - z: 0 + refTop: 0, 0, 0 # refBottom should connect to the refTop of the next section. - refBottom: - x: 0 - y: 16 - z: 0 - #x1, y1, z1, x2, y2, z2 + refBottom: 0, 16, 0 region: - x1: 0 - y1: 0 - z1: 0 - x2: 1000 - y2: 256 - z2: 1000 + start: 0, 0, 0 + end: 1000, 256, 1000 world: world - name: section2 - refTop: - x: 1000 - y: 240 - z: 0 - refBottom: - x: 2000 - y: 16 - z: 0 + refTop: 1000, 240, 0 + refBottom: 2000, 16, 0 region: - x1: 1000 - y1: 0 - z1: 0 - x2: 2000 - y2: 256 - z2: 1000 + start: 1000, 0, 0 + end: 2000, 256, 1000 world: world # The damage players will take when outside a managed section. damageOutsideSections: 1.0