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

net.projecttl.kuma.mc.api.utils.AreaUtils.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0-beta.8
Show newest version
package net.projecttl.kuma.mc.api.utils

import net.minestom.server.coordinate.Pos
import net.minestom.server.coordinate.Vec
import net.minestom.server.entity.Player

data class Area(val pos1: Vec, val pos2: Vec)

class AreaUtils(private val area: Area, private val height: Boolean) {
    private fun swapNumber(pos1: Double, pos2: Double): Pair {
        if (pos1 < pos2) {
            return Pair(pos1, pos2)
        }

        return Pair(pos2, pos1)
    }
    private fun check(target: Double, pos1: Double, pos2: Double): Boolean {
        val swap = swapNumber(pos1, pos2)
        if (target in swap.first..swap.second) {
            return true
        }

        return false
    }

    fun Player.inArea(): Boolean {
        if (check(position.x, area.pos1.x, area.pos2.x) &&
            check(position.z, area.pos1.z, area.pos2.z)) {
            if (height) {
                if (check(position.y, area.pos1.y, area.pos2.y)) {
                    return true
                }

                return false
            }

            return true
        }

        return false
    }

    fun Pos.inArea(): Boolean {
        if (check(this.x, area.pos1.x, area.pos2.x) &&
            check(this.z, area.pos1.z, area.pos2.z)) {
            if (height) {
                if (check(this.y, area.pos1.y, area.pos2.y)) {
                    return true
                }

                return false
            }

            return true
        }

        return false
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy