Skip to content

Commit

Permalink
Merge pull request #61 from flytegg/feat/entity-extensions
Browse files Browse the repository at this point in the history
Feat/entity extensions
  • Loading branch information
joshbker authored Mar 13, 2024
2 parents 906e1af + 8aeff63 commit d850588
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Maven
<dependency>
<groupId>gg.flyte</groupId>
<artifactId>twilight</artifactId>
<version>1.1.8</version>
<version>1.1.9</version>
</dependency>
```

Expand All @@ -31,14 +31,14 @@ maven {
url "https://repo.flyte.gg/releases"
}
implementation "gg.flyte:twilight:1.1.8"
implementation "gg.flyte:twilight:1.1.9"
```

Gradle (Kotlin DSL)
```kotlin
maven("https://repo.flyte.gg/releases")

implementation("gg.flyte:twilight:1.1.8")
implementation("gg.flyte:twilight:1.1.9")
```

Certain features of Twilight require configuration, which can be done via the Twilight class. To setup a Twilight class instance, you can use the `twilight` function as shown below:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "gg.flyte"
version = "1.1.8"
version = "1.1.9"

repositories {
mavenLocal()
Expand Down
71 changes: 70 additions & 1 deletion src/main/kotlin/gg/flyte/twilight/extension/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gg.flyte.twilight.extension

import gg.flyte.twilight.Twilight
import org.bukkit.entity.Entity
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.metadata.FixedMetadataValue

/**
Expand Down Expand Up @@ -31,6 +33,73 @@ fun Entity.isOnFire(): Boolean {
return fireTicks > 0
}

/**
* Gets the nearest player to this entity within the given radius.
* @param xRadius The x radius to search.
* @param yRadius The y radius to search.
* @param zRadius The z radius to search.
* @return The nearest player.
*/

fun Entity.getNearestPlayer(xRadius: Double = Double.MAX_VALUE, yRadius: Double = Double.MAX_VALUE, zRadius: Double = Double.MAX_VALUE): Player? {
var nearestDistance = Double.MAX_VALUE
var nearestPlayer: Player? = null
for (player in world.getNearbyPlayers(location, xRadius, yRadius, zRadius)) {
if (player == this) continue
val distance = location.distance(player.location)
if (distance < nearestDistance) {
nearestDistance = distance
nearestPlayer = player
}
}
return nearestPlayer
}


/**
* Gets the nearest entity to this entity within the given radius.
* @param xRadius The x radius to search.
* @param yRadius The y radius to search.
* @param zRadius The z radius to search.
* @return The nearest entity.
*/

fun Entity.getNearestEntity(xRadius: Double = Double.MAX_VALUE, yRadius: Double = Double.MAX_VALUE, zRadius: Double = Double.MAX_VALUE): Entity? {
var nearestDistance = Double.MAX_VALUE
var nearestEntity: Entity? = null
for (entity in world.getNearbyEntities(location, xRadius, yRadius, zRadius)) {
if (entity == this) continue
val distance = location.distance(entity.location)
if (distance < nearestDistance) {
nearestDistance = distance
nearestEntity = entity
}
}
return nearestEntity
}

/**
* Gets the nearest living entity to this entity within the given radius.
* @param xRadius The x radius to search.
* @param yRadius The y radius to search.
* @param zRadius The z radius to search.
* @return The nearest living entity.
*/

fun Entity.getNearestLivingEntity(xRadius: Double = Double.MAX_VALUE, yRadius: Double = Double.MAX_VALUE, zRadius: Double = Double.MAX_VALUE): LivingEntity? {
var nearestDistance = Double.MAX_VALUE
var nearestEntity: LivingEntity? = null
for (entity in world.getNearbyLivingEntities(location, xRadius, yRadius, zRadius)) {
if (entity == this) continue
val distance = location.distance(entity.location)
if (distance < nearestDistance) {
nearestDistance = distance
nearestEntity = entity
}
}
return nearestEntity
}

/**
* Freezes the entity.
*/
Expand All @@ -50,4 +119,4 @@ fun Entity.unfreeze() {
*
* @return `true` if the player is frozen, `false` otherwise.
*/
fun Entity.frozen(): Boolean = hasMetadata("frozen")
fun Entity.frozen(): Boolean = hasMetadata("frozen")

0 comments on commit d850588

Please sign in to comment.