-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(custom-events): add PlayerChangeOpEvent, PlayerOpEvent and Playe…
…rDeopEvent
- Loading branch information
Showing
7 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ plugins { | |
} | ||
|
||
group = "gg.flyte" | ||
version = "1.0.25" | ||
version = "1.0.26" | ||
|
||
repositories { | ||
mavenCentral() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/gg/flyte/twilight/event/custom/admin/PlayerDeopEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/gg/flyte/twilight/event/custom/admin/PlayerOpChangeEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/gg/flyte/twilight/event/custom/admin/PlayerOpEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/gg/flyte/twilight/event/custom/admin/listener/OpEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |