Skip to content

Commit

Permalink
Merge pull request #60 from flytegg/feat/freeze
Browse files Browse the repository at this point in the history
Feat/freeze
  • Loading branch information
joshbker authored Mar 13, 2024
2 parents c84930a + 442c8a1 commit 906e1af
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 7 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.7</version>
<version>1.1.8</version>
</dependency>
```

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

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

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

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.7"
version = "1.1.8"

repositories {
mavenLocal()
Expand Down
9 changes: 8 additions & 1 deletion src/main/kotlin/gg/flyte/twilight/Twilight.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package gg.flyte.twilight

import gg.flyte.twilight.builders.item.ItemBuilder
import gg.flyte.twilight.data.MongoDB
import gg.flyte.twilight.data.service.NameCacheService
import gg.flyte.twilight.environment.Environment
import gg.flyte.twilight.event.custom.chat.command.ChatClickCommand
import gg.flyte.twilight.event.customEventListeners
import gg.flyte.twilight.extension.applyForEach
import gg.flyte.twilight.builders.item.ItemBuilder
import gg.flyte.twilight.server.ServerSoftware
import org.bukkit.plugin.java.JavaPlugin

class Twilight(javaPlugin: JavaPlugin) {

init {
plugin = javaPlugin
run {
ServerSoftware
customEventListeners
ItemBuilder.Companion
ChatClickCommand.register()
Expand All @@ -24,6 +26,11 @@ class Twilight(javaPlugin: JavaPlugin) {
lateinit var plugin: JavaPlugin
var usingEnv = false
val internalPdc by lazy { "_twilight_${plugin.name.lowercase()}" }

fun isFolia(): Boolean = ServerSoftware.isFolia()
fun isPaper(): Boolean = ServerSoftware.isPaper()
fun isSpigot(): Boolean = ServerSoftware.isSpigot()
fun isCraftBukkit(): Boolean = ServerSoftware.isCraftBukkit()
}

fun env(init: Environment.Settings.() -> Unit = {}) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/gg/flyte/twilight/event/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gg.flyte.twilight.event
import gg.flyte.twilight.Twilight
import gg.flyte.twilight.event.custom.admin.listener.OpEventListener
import gg.flyte.twilight.event.custom.interact.listener.InteractEventListener
import gg.flyte.twilight.event.custom.movement.listener.EntityMoveEventListener
import gg.flyte.twilight.extension.applyForEach
import gg.flyte.twilight.inventory.GUIListener
import org.bukkit.event.*
Expand Down Expand Up @@ -126,7 +127,8 @@ open class TwilightEvent(async: Boolean = false) : Event(async), Cancellable {
val customEventListeners = mutableSetOf(
GUIListener,
InteractEventListener,
OpEventListener
OpEventListener,
EntityMoveEventListener
)

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gg.flyte.twilight.event.custom.movement.listener

import gg.flyte.twilight.Twilight.Companion.isPaper
import gg.flyte.twilight.event.CustomTwilightListener
import gg.flyte.twilight.event.event
import gg.flyte.twilight.extension.frozen
import io.papermc.paper.event.entity.EntityMoveEvent
import org.bukkit.event.player.PlayerMoveEvent

object EntityMoveEventListener : CustomTwilightListener() {

init {
if (isPaper()) {
event<EntityMoveEvent> {
if (entity.frozen()) isCancelled = true
}
} else {
event<PlayerMoveEvent> {
if (player.frozen()) isCancelled = true
}
}
}

}
25 changes: 24 additions & 1 deletion src/main/kotlin/gg/flyte/twilight/extension/Entity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gg.flyte.twilight.extension

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

/**
* Retrieves a list of nearby entities within the specified range from the current entity.
Expand All @@ -27,4 +29,25 @@ fun Entity.getNearbyEntities(range: Double): MutableList<Entity> {
*/
fun Entity.isOnFire(): Boolean {
return fireTicks > 0
}
}

/**
* Freezes the entity.
*/
fun Entity.freeze() {
setMetadata("frozen", FixedMetadataValue(Twilight.plugin, true))
}

/**
* Unfreezes the entity.
*/
fun Entity.unfreeze() {
removeMetadata("frozen", Twilight.plugin)
}

/**
* Checks if the player is currently frozen.
*
* @return `true` if the player is frozen, `false` otherwise.
*/
fun Entity.frozen(): Boolean = hasMetadata("frozen")
18 changes: 18 additions & 0 deletions src/main/kotlin/gg/flyte/twilight/extension/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.bukkit.Bukkit
import org.bukkit.Sound
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.bukkit.metadata.FixedMetadataValue

/**
* Plays a sound at the player's current location with default volume and pitch.
Expand Down Expand Up @@ -81,4 +82,21 @@ fun Player.showPlayer() {
*/
fun Player.removeActivePotionEffects() {
activePotionEffects.forEach { removePotionEffect(it.type) }
}

/**
* Freezes the player.
*/
fun Player.freeze() {
setMetadata("frozen", FixedMetadataValue(Twilight.plugin, true))
walkSpeed = 0.0f
flySpeed = 0.0f
}
/**
* Unfreezes the player.
*/
fun Player.unfreeze() {
removeMetadata("frozen", Twilight.plugin)
walkSpeed = 0.2f
flySpeed = 0.2f
}
9 changes: 9 additions & 0 deletions src/main/kotlin/gg/flyte/twilight/extension/Server.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gg.flyte.twilight.extension

import gg.flyte.twilight.server.ServerSoftware
import org.bukkit.Server

fun Server.isFolia(): Boolean = ServerSoftware.isFolia()
fun Server.isPaper(): Boolean = ServerSoftware.isPaper()
fun Server.isSpigot(): Boolean = ServerSoftware.isSpigot()
fun Server.isCraftBukkit(): Boolean = ServerSoftware.isCraftBukkit()
31 changes: 31 additions & 0 deletions src/main/kotlin/gg/flyte/twilight/server/ServerSoftware.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gg.flyte.twilight.server

enum class ServerSoftware {

FOLIA,
PAPER,
SPIGOT,
CRAFT_BUKKIT;

companion object {
val current: ServerSoftware by lazy {
val hasClass = { name: String -> try { Class.forName(name); true } catch (e: ClassNotFoundException) { false } }

if (hasClass("io.papermc.paper.threadedregions.RegionizedServer")) {
FOLIA
} else if (hasClass("com.destroystokyo.paper.PaperConfig") || hasClass("io.papermc.paper.configuration.Configuration")) {
PAPER
} else if (hasClass("org.spigotmc.SpigotConfig")) {
SPIGOT
} else {
CRAFT_BUKKIT
}
}

fun isFolia(): Boolean = current == FOLIA
fun isPaper(): Boolean = current == PAPER
fun isSpigot(): Boolean = current == SPIGOT
fun isCraftBukkit(): Boolean = current == CRAFT_BUKKIT
}

}

0 comments on commit 906e1af

Please sign in to comment.