Skip to content

Commit

Permalink
fix registering above/below Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Nov 8, 2022
1 parent 40b5686 commit f38ec83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/main/kotlin/com/mineinabyss/deeperworld/DeeperWorldPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ class DeeperWorldPlugin : JavaPlugin() {
service<WorldManager>(WorldManagerImpl())
service<PlayerManager>(PlayerManagerImpl())

// Register aboveKey / belowKey as new config breaks this
for (section in deeperConfig.sections) {
when (section) {
deeperConfig.sections.first() -> section.belowKey = deeperConfig.sections[1].key
deeperConfig.sections.last() -> section.aboveKey = deeperConfig.sections[deeperConfig.sections.size - 2].key
else -> {
section.aboveKey = deeperConfig.sections[deeperConfig.sections.indexOf(section) - 1].key
section.belowKey = deeperConfig.sections[deeperConfig.sections.indexOf(section) + 1].key
}
}
}

listeners(
MovementListener,
PlayerListener,
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/com/mineinabyss/deeperworld/world/Region.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.mineinabyss.deeperworld.world

import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
Expand Down Expand Up @@ -33,14 +31,17 @@ class Region(val a: CubePoint, val b: CubePoint) {
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)

object RegionSerializer : KSerializer<Region> {
private val serializer = ListSerializer(Int.serializer())
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, listOf(x, y, z, x2, y2, z2))
encoder.encodeSerializableValue(serializer, RegionPoints(x, y, z, x2, y2, z2))
}

override fun deserialize(decoder: Decoder): Region {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
package com.mineinabyss.deeperworld.world.section

import com.mineinabyss.deeperworld.world.Region
import com.mineinabyss.idofront.serialization.LocationSerializer
import com.mineinabyss.idofront.serialization.VectorSerializer
import com.mineinabyss.idofront.serialization.WorldSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.UseSerializers
import org.bukkit.Location
import org.bukkit.World
import org.bukkit.util.Vector

/**
* @property region the region within which this section is active
Expand All @@ -28,16 +29,15 @@ data class Section(
val name: String? = null,
val region: Region,
val world: @Serializable(WorldSerializer::class) World,
@SerialName("refTop") private val _refTop: @Serializable(VectorSerializer::class) Vector,
@SerialName("refBottom") private val _refBottom: @Serializable(VectorSerializer::class) Vector
@SerialName("refTop") private val _refTop: ReferenceLocation,
@SerialName("refBottom") private val _refBottom: ReferenceLocation
) {
@Transient
@Serializable(LocationSerializer::class)
val referenceTop = _refTop.toLocation(world)

@Transient
@Serializable(LocationSerializer::class)
val referenceBottom = _refBottom.toLocation(world)

@Transient
val key: SectionKey = name?.let { AbstractSectionKey.CustomSectionKey(name) }
?: AbstractSectionKey.InternalSectionKey()

Expand All @@ -49,3 +49,8 @@ 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())
}

0 comments on commit f38ec83

Please sign in to comment.