Skip to content

Commit

Permalink
feat(custom-events): add PlayerChangeOpEvent, PlayerOpEvent and Playe…
Browse files Browse the repository at this point in the history
…rDeopEvent
  • Loading branch information
joshbker committed Nov 5, 2023
1 parent c7a55da commit fe1ec23
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Maven
<dependency>
<groupId>gg.flyte</groupId>
<artifactId>twilight</artifactId>
<version>1.0.25</version>
<version>1.0.26</version>
</dependency>
```

Expand All @@ -29,14 +29,14 @@ maven {
url "https://repo.flyte.gg/releases"
}
implementation "gg.flyte:twilight:1.0.25"
implementation "gg.flyte:twilight:1.0.26"
```

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

implementation("gg.flyte:twilight:1.0.25")
implementation("gg.flyte:twilight:1.0.26")
```

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` method 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 @@ -5,7 +5,7 @@ plugins {
}

group = "gg.flyte"
version = "1.0.25"
version = "1.0.26"

repositories {
mavenCentral()
Expand Down
11 changes: 9 additions & 2 deletions src/main/kotlin/gg/flyte/twilight/event/Event.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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.extension.applyForEach
import gg.flyte.twilight.inventory.GUIListener
import org.bukkit.event.*
import org.bukkit.scheduler.BukkitTask
import java.time.Instant

/**
Expand Down Expand Up @@ -47,8 +49,12 @@ open class CustomTwilightListener {
* The list of events registered to this custom Twilight Event
*/
val events = mutableListOf<TwilightListener>()
val runnables = mutableListOf<BukkitTask>()

fun unregister() = events.applyForEach { unregister() }
fun unregister() {
events.applyForEach { unregister() }
runnables.applyForEach { cancel() }
}
}

/**
Expand Down Expand Up @@ -113,6 +119,7 @@ open class TwilightEvent(async: Boolean = false) : Event(async), Cancellable {
*/
val customEvents = listOf<CustomTwilightListener>(
GUIListener,
InteractEventListener
InteractEventListener,
OpEventListener
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gg.flyte.twilight.event.custom.admin

import gg.flyte.twilight.event.TwilightEvent
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.block.Block
import org.bukkit.block.BlockFace
import org.bukkit.entity.Player
import org.bukkit.event.block.Action
import org.bukkit.inventory.ItemStack
import org.bukkit.util.Vector
import java.util.*

/**
* Represents an event that occurs when a player is de-op'd (de-promoted from operator status).
* This event contains information about the affected player's UUID and name.
*/
class PlayerDeopEvent(
val uuid: UUID, // The UUID of the player being de-op'd.
val name: String // The name of the player being de-op'd.
) : TwilightEvent() {
/**
* Represents the offline player associated with this event.
*/
val offlinePlayer: OfflinePlayer = Bukkit.getOfflinePlayer(uuid)

/**
* Represents the online player associated with this event, if available.
* This property will be null if the player is not currently online.
*/
val player: Player? = if (offlinePlayer.isOnline) offlinePlayer.player else null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gg.flyte.twilight.event.custom.admin

import gg.flyte.twilight.event.TwilightEvent
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.block.Block
import org.bukkit.block.BlockFace
import org.bukkit.entity.Player
import org.bukkit.event.block.Action
import org.bukkit.inventory.ItemStack
import org.bukkit.util.Vector
import java.util.*

/**
* Represents an event that occurs when a player's operator status changes.
* This event contains information about the affected player's UUID and name.
*/
class PlayerOpChangeEvent(
val uuid: UUID, // The UUID of the player whose operator status changed.
val name: String // The name of the player whose operator status changed.
) : TwilightEvent() {
/**
* Represents the offline player associated with this event.
*/
val offlinePlayer: OfflinePlayer = Bukkit.getOfflinePlayer(uuid)

/**
* Represents the online player associated with this event, if available.
* This property will be null if the player is not currently online.
*/
val player: Player? = if (offlinePlayer.isOnline) offlinePlayer.player else null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gg.flyte.twilight.event.custom.admin

import gg.flyte.twilight.event.TwilightEvent
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.block.Block
import org.bukkit.block.BlockFace
import org.bukkit.entity.Player
import org.bukkit.event.block.Action
import org.bukkit.inventory.ItemStack
import org.bukkit.util.Vector
import java.util.*

/**
* Represents an event that occurs when a player is op'd (promoted to operator status).
* This event contains information about the affected player's UUID and name.
*/
class PlayerOpEvent(
val uuid: UUID, // The UUID of the player being op'd.
val name: String // The name of the player being op'd.
) : TwilightEvent() {
/**
* Represents the offline player associated with this event.
*/
val offlinePlayer: OfflinePlayer = Bukkit.getOfflinePlayer(uuid)

/**
* Represents the online player associated with this event, if available.
* This property will be null if the player is not currently online.
*/
val player: Player? = if (offlinePlayer.isOnline) offlinePlayer.player else null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package gg.flyte.twilight.event.custom.admin.listener

import gg.flyte.twilight.Twilight
import gg.flyte.twilight.event.CustomTwilightListener
import gg.flyte.twilight.event.custom.admin.PlayerDeopEvent
import gg.flyte.twilight.event.custom.admin.PlayerOpChangeEvent
import gg.flyte.twilight.event.custom.admin.PlayerOpEvent
import gg.flyte.twilight.extension.applyForEach
import gg.flyte.twilight.gson.GSON
import gg.flyte.twilight.scheduler.repeat
import java.io.File
import java.util.*

object OpEventListener : CustomTwilightListener() {
private val opsFile = File("${Twilight.plugin.dataFolder}/../../ops.json")
private var opsData = mutableSetOf<OpData>()
private var opsLastChanged: Long? = null

init {
if (opsFile.exists()) {
opsLastChanged = opsFile.lastModified()
opsData += getOpsData()
}

runnables += repeat(async = true) {
if (!opsFile.exists()) return@repeat cancel()
if (!hasOpsChanged()) return@repeat

val newOpsData = getOpsData()

// Removed ops
opsData.subtract(newOpsData).toSet().applyForEach {
opsData -= this
Twilight.plugin.server.pluginManager.callEvent(PlayerDeopEvent(uuid, name))
Twilight.plugin.server.pluginManager.callEvent(PlayerOpChangeEvent(uuid, name))
}

// Added ops
newOpsData.subtract(opsData).toSet().applyForEach {
opsData += this
Twilight.plugin.server.pluginManager.callEvent(PlayerOpEvent(uuid, name))
Twilight.plugin.server.pluginManager.callEvent(PlayerOpChangeEvent(uuid, name))
}
}
}

data class OpData(
val uuid: UUID,
val name: String,
val level: Int,
val bypassPlayerLimit: Boolean
)

private fun getOpsData(): Set<OpData> = GSON.fromJson(opsFile.reader(), Array<OpData>::class.java).toSet()

private fun hasOpsChanged(): Boolean = (opsLastChanged ?: 0) < opsFile.lastModified()

}

0 comments on commit fe1ec23

Please sign in to comment.