Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LocationConverter #189

Open
LarsArtmann opened this issue Feb 19, 2020 · 0 comments
Open

Add LocationConverter #189

LarsArtmann opened this issue Feb 19, 2020 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers WithCodeExample

Comments

@LarsArtmann
Copy link
Member

LarsArtmann commented Feb 19, 2020

package name.name

import com.beust.klaxon.Converter
import com.beust.klaxon.JsonValue
import net.darkdevelopers.darkbedrock.darkness.spigot.functions.toBukkitWorld
import org.bukkit.Location

object LocationConverter : Converter {

    override fun canConvert(cls: Class<*>): Boolean = cls == Location::class.java

    override fun fromJson(jv: JsonValue): Location? {
        val obj = jv.obj ?: return null
        val world = obj["world"].toString().toBukkitWorld() ?: return null
        val x = obj["x"].toString().toDoubleOrNull() ?: 0.0
        val y = obj["y"].toString().toDoubleOrNull() ?: 0.0
        val z = obj["z"].toString().toDoubleOrNull() ?: 0.0
        val yaw = obj["yaw"].toString().toFloatOrNull() ?: 0f
        val pitch = obj["pitch"].toString().toFloatOrNull() ?: 0f
        return Location(world, x, y, z, yaw, pitch)
    }

    override fun toJson(value: Any): String {
        value as Location
        return """{"world": "${value.world?.name}", "x": ${value.x}, "y": ${value.y}, "z": ${value.z}, "yaw": ${value.yaw}, "pitch": ${value.pitch}}"""
    }

}
@LarsArtmann LarsArtmann added enhancement New feature or request good first issue Good for newcomers WithCodeExample labels Feb 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers WithCodeExample
Projects
None yet
Development

No branches or pull requests

1 participant