diff --git a/README.md b/README.md index 5fa295d..8173223 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Maven gg.flyte twilight - 1.1.7 + 1.1.8 ``` @@ -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: diff --git a/build.gradle.kts b/build.gradle.kts index 7c3791b..3c4fae7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "gg.flyte" -version = "1.1.7" +version = "1.1.8" repositories { mavenLocal() diff --git a/src/main/kotlin/gg/flyte/twilight/Twilight.kt b/src/main/kotlin/gg/flyte/twilight/Twilight.kt index 2a30ae7..7805db4 100644 --- a/src/main/kotlin/gg/flyte/twilight/Twilight.kt +++ b/src/main/kotlin/gg/flyte/twilight/Twilight.kt @@ -1,12 +1,13 @@ 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) { @@ -14,6 +15,7 @@ class Twilight(javaPlugin: JavaPlugin) { init { plugin = javaPlugin run { + ServerSoftware customEventListeners ItemBuilder.Companion ChatClickCommand.register() @@ -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 = {}) { diff --git a/src/main/kotlin/gg/flyte/twilight/event/Event.kt b/src/main/kotlin/gg/flyte/twilight/event/Event.kt index d7703b0..de0c6d6 100644 --- a/src/main/kotlin/gg/flyte/twilight/event/Event.kt +++ b/src/main/kotlin/gg/flyte/twilight/event/Event.kt @@ -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.* @@ -126,7 +127,8 @@ open class TwilightEvent(async: Boolean = false) : Event(async), Cancellable { val customEventListeners = mutableSetOf( GUIListener, InteractEventListener, - OpEventListener + OpEventListener, + EntityMoveEventListener ) /** diff --git a/src/main/kotlin/gg/flyte/twilight/event/custom/movement/listener/EntityMoveEventListener.kt b/src/main/kotlin/gg/flyte/twilight/event/custom/movement/listener/EntityMoveEventListener.kt new file mode 100644 index 0000000..01487b3 --- /dev/null +++ b/src/main/kotlin/gg/flyte/twilight/event/custom/movement/listener/EntityMoveEventListener.kt @@ -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 { + if (entity.frozen()) isCancelled = true + } + } else { + event { + if (player.frozen()) isCancelled = true + } + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/gg/flyte/twilight/extension/Entity.kt b/src/main/kotlin/gg/flyte/twilight/extension/Entity.kt index 97ddf9a..8072f4e 100644 --- a/src/main/kotlin/gg/flyte/twilight/extension/Entity.kt +++ b/src/main/kotlin/gg/flyte/twilight/extension/Entity.kt @@ -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. @@ -27,4 +29,25 @@ fun Entity.getNearbyEntities(range: Double): MutableList { */ fun Entity.isOnFire(): Boolean { return fireTicks > 0 -} \ No newline at end of file +} + +/** + * 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") \ No newline at end of file diff --git a/src/main/kotlin/gg/flyte/twilight/extension/Player.kt b/src/main/kotlin/gg/flyte/twilight/extension/Player.kt index bbe9ada..5726ffa 100644 --- a/src/main/kotlin/gg/flyte/twilight/extension/Player.kt +++ b/src/main/kotlin/gg/flyte/twilight/extension/Player.kt @@ -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. @@ -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 } \ No newline at end of file diff --git a/src/main/kotlin/gg/flyte/twilight/extension/Server.kt b/src/main/kotlin/gg/flyte/twilight/extension/Server.kt new file mode 100644 index 0000000..425e4cc --- /dev/null +++ b/src/main/kotlin/gg/flyte/twilight/extension/Server.kt @@ -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() \ No newline at end of file diff --git a/src/main/kotlin/gg/flyte/twilight/server/ServerSoftware.kt b/src/main/kotlin/gg/flyte/twilight/server/ServerSoftware.kt new file mode 100644 index 0000000..40434f8 --- /dev/null +++ b/src/main/kotlin/gg/flyte/twilight/server/ServerSoftware.kt @@ -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 + } + +} \ No newline at end of file