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

walkmc.interfaces.Localizable.kt Maven / Gradle / Ivy

package walkmc.interfaces

import org.bukkit.*
import org.bukkit.block.*
import org.bukkit.entity.*
import walkmc.*
import walkmc.extensions.*

/**
 * A localizable interface, representing a object that have a location.
 */
interface Localizable {
	
	/**
	 * The location of this localizable.
	 */
	var location: Location
}

/**
 * Returns the distance between the location of this localizable object
 * and the specified [to] location.
 */
fun Localizable.distance(to: Location) = location.distanceSquared(to)

/**
 * Returns the distance between the location of this localizable object
 * and the specified [to] block.
 */
fun Localizable.distance(to: Block) = location.distanceSquared(to.location)

/**
 * Returns the distance between the location of this localizable object
 * and the specified [to] entity.
 */
fun Localizable.distance(to: Entity) = location.distanceSquared(to.location)

/**
 * Returns if the location of this localizable object is nearby to another
 * location in the specified radius.
 */
fun Localizable.isNear(to: Location, radius: Double) = distance(to) <= radius

/**
 * Returns if the location of this localizable object is nearby to location of
 * the specified entity in the specified radius.
 */
fun Localizable.isNear(to: Entity, radius: Double) = distance(to) <= radius

/**
 * Returns if the location of this localizable object is nearby to location of
 * the specified block in the specified radius.
 */
fun Localizable.isNear(to: Block, radius: Double) = distance(to) <= radius

/**
 * Returns all nearby entities with this location as central.
 */
fun Localizable.getNearbyEntities(x: Double, y: Double, z: Double) =
	location.world.getNearbyEntities(location, x, y, z).toList()

/**
 * Returns the nearest entity in the specified radius
 */
fun Localizable.getNearestEntity(x: Double, y: Double, z: Double) = getNearbyEntities(x, y, z).firstOrNull()

/**
 * Returns the nearest entities filtered in the specified radius as list
 */
inline fun Localizable.getNearbyEntities(
	x: Double, y: Double, z: Double, predicate: (Entity) -> Boolean
) = getNearbyEntities(x, y, z).filter(predicate)

/**
 * Returns the nearest entity filtered in the specified radius
 */
inline fun Localizable.getNearestEntity(
	x: Double, y: Double, z: Double, predicate: (Entity) -> Boolean
) = getNearbyEntities(x, y, z).firstOrNull(predicate)

/**
 * Returns the nearest entities filtered in the specified radius as list
 */
@JvmName("nearbyEntitiesType")
inline fun  Localizable.getNearbyEntitiesOf(
	x: Double, y: Double, z: Double
) = getNearbyEntities(x, y, z).filterIsInstance()

/**
 * Returns the nearest entity filtered in the specified radius
 */
@JvmName("nearestEntityType")
inline fun  Localizable.getNearestEntityOf(
	x: Double, y: Double, z: Double
) = getNearbyEntitiesOf(x, y, z).firstOrNull()

/**
 * Returns the nearest players filtered in the specified radius as list
 */
fun Localizable.getNearbyPlayers(
	x: Double, y: Double, z: Double
) = getNearbyEntities(x, y, z).filterIsInstance()

/**
 * Returns the nearest player filtered in the specified radius
 */
fun Localizable.getNearestPlayer(
	x: Double, y: Double, z: Double
) = getNearbyPlayers(x, y, z).firstOrNull()

/**
 * Returns all nearby blocks centered by the location of this localizable object.
 */
fun Localizable.getNearbyBlocks(x: Int, y: Int, z: Int) = buildList {
	for (xx in -x until x) {
		for (yy in -y until y) {
			for (zz in -z until z) {
				add(location.getRelative(xx.toDouble(), yy.toDouble(), zz.toDouble()).block)
			}
		}
	}
}

/**
 * Returns all filtered nearby blocks centered by the location of this localizable object.
 */
inline fun Localizable.getNearbyBlocks(
	x: Int, y: Int, z: Int, predicate: (Block) -> Boolean
) = getNearbyBlocks(x, y, z).filter(predicate)

/**
 * Returns all filtered nearby blocks centered by the location of this localizable object.
 */
fun Localizable.getNearbyBlocks(x: Int, y: Int, z: Int, material: Materials) =
	getNearbyBlocks(x, y, z) { it.material == material }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy