All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.carcadex.kotlinbukkitkit.exposed.delegate.LocationPos.kt Maven / Gradle / Ivy

The newest version!
package tech.carcadex.kotlinbukkitkit.exposed.delegate

import tech.carcadex.kotlinbukkitkit.utility.types.LocationPos
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.sql.Column
import kotlin.reflect.KProperty

public fun Entity<*>.locationPos(column: Column): ExposedDelegate = LocationPosExposedDelegate(column)

@JvmName("locationPosNullable")
public fun Entity<*>.locationPos(column: Column): ExposedDelegate = LocationPosExposedDelegateNullable(column)

public fun Entity<*>.locationPos(
    xColumn: Column,
    yColumn: Column,
    zColumn: Column,
    yawColumn: Column,
    pitchColumn: Column,
): ExposedDelegate = LocationPosMultiColumnExposedDelegate(xColumn, yColumn, zColumn, yawColumn, pitchColumn)

@JvmName("locationPosNullable")
public fun Entity<*>.locationPos(
    xColumn: Column,
    yColumn: Column,
    zColumn: Column,
    yawColumn: Column,
    pitchColumn: Column,
): ExposedDelegate = LocationPosMultiColumnExposedDelegateNullable(xColumn, yColumn, zColumn, yawColumn, pitchColumn)

public class LocationPosExposedDelegate(
    public val column: Column,
) : ExposedDelegate {
    override operator fun > getValue(
        entity: Entity,
        desc: KProperty<*>,
    ): LocationPos {
        val data = entity.run { column.getValue(this, desc) }
        val slices = data.split(";")
        return LocationPos(
            slices[0].toDouble(),
            slices[1].toDouble(),
            slices[2].toDouble(),
            slices[3].toFloat(),
            slices[4].toFloat(),
        )
    }

    override operator fun > setValue(
        entity: Entity,
        desc: KProperty<*>,
        value: LocationPos,
    ) {
        val parsed = value.run { "$x;$y;$z;$yaw;$pitch" }
        entity.apply { column.setValue(this, desc, parsed) }
    }
}

public class LocationPosExposedDelegateNullable(
    public val column: Column,
) : ExposedDelegate {
    override operator fun > getValue(
        entity: Entity,
        desc: KProperty<*>,
    ): LocationPos? {
        val data = entity.run { column.getValue(this, desc) }
        val slices = data?.split(";")
        return slices?.let {
            LocationPos(
                it[0].toDouble(),
                it[1].toDouble(),
                it[2].toDouble(),
                it[3].toFloat(),
                it[4].toFloat(),
            )
        }
    }

    override operator fun > setValue(
        entity: Entity,
        desc: KProperty<*>,
        value: LocationPos?,
    ) {
        val parsed = value?.run { "$x;$y;$z;$yaw;$pitch" }
        entity.apply { column.setValue(this, desc, parsed) }
    }
}

public class LocationPosMultiColumnExposedDelegate(
    public val xColumn: Column,
    public val yColumn: Column,
    public val zColumn: Column,
    public val yawColumn: Column,
    public val pitchColumn: Column,
) : ExposedDelegate {
    override operator fun > getValue(
        entity: Entity,
        desc: KProperty<*>,
    ): LocationPos {
        val x = entity.run { xColumn.getValue(this, desc) }
        val y = entity.run { yColumn.getValue(this, desc) }
        val z = entity.run { zColumn.getValue(this, desc) }
        val yaw = entity.run { yawColumn.getValue(this, desc) }
        val pitch = entity.run { pitchColumn.getValue(this, desc) }

        return LocationPos(
            x,
            y,
            z,
            yaw,
            pitch,
        )
    }

    override operator fun > setValue(
        entity: Entity,
        desc: KProperty<*>,
        value: LocationPos,
    ) {
        entity.apply {
            value.apply {
                xColumn.setValue(entity, desc, x)
                yColumn.setValue(entity, desc, y)
                zColumn.setValue(entity, desc, z)
                yawColumn.setValue(entity, desc, yaw)
                pitchColumn.setValue(entity, desc, pitch)
            }
        }
    }
}

public class LocationPosMultiColumnExposedDelegateNullable(
    public val xColumn: Column,
    public val yColumn: Column,
    public val zColumn: Column,
    public val yawColumn: Column,
    public val pitchColumn: Column,
) : ExposedDelegate {
    override operator fun > getValue(
        entity: Entity,
        desc: KProperty<*>,
    ): LocationPos? {
        val x = entity.run { xColumn.getValue(this, desc) }
        val y = entity.run { yColumn.getValue(this, desc) }
        val z = entity.run { zColumn.getValue(this, desc) }
        val yaw = entity.run { yawColumn.getValue(this, desc) }
        val pitch = entity.run { pitchColumn.getValue(this, desc) }

        return if (
            x != null && y != null && z != null &&
            yaw != null && pitch != null
        ) {
            LocationPos(
                x,
                y,
                z,
                yaw,
                pitch,
            )
        } else {
            null
        }
    }

    override operator fun > setValue(
        entity: Entity,
        desc: KProperty<*>,
        value: LocationPos?,
    ) {
        entity.apply {
            xColumn.setValue(entity, desc, value?.x)
            yColumn.setValue(entity, desc, value?.y)
            zColumn.setValue(entity, desc, value?.z)
            yawColumn.setValue(entity, desc, value?.yaw)
            pitchColumn.setValue(entity, desc, value?.pitch)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy