From 760b77364ac67107929fb9189d443b1405b6750d Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Tue, 7 Feb 2023 19:51:25 -0500 Subject: [PATCH] Reform Heimdall to move event collectors to the event class themselves. --- .../plugin/FoundationHeimdallPlugin.kt | 100 +++--------------- .../heimdall/plugin/buffer/EventBuffer.kt | 4 +- .../heimdall/plugin/buffer/IEventBuffer.kt | 7 ++ .../heimdall/plugin/event/BlockBreak.kt | 12 +++ .../heimdall/plugin/event/BlockPlace.kt | 12 +++ .../heimdall/plugin/event/EntityKill.kt | 23 ++++ .../heimdall/plugin/event/EventCollector.kt | 8 ++ .../plugin/event/EventCollectorProvider.kt | 7 ++ .../plugin/event/EventCollectorProviders.kt | 14 +++ .../plugin/event/ExposedExtensions.kt | 6 +- .../plugin/event/PlayerAdvancement.kt | 12 +++ .../heimdall/plugin/event/PlayerDeath.kt | 23 ++++ .../heimdall/plugin/event/PlayerPosition.kt | 12 +++ .../heimdall/plugin/event/PlayerSession.kt | 40 +++++++ .../heimdall/plugin/event/WorldChange.kt | 21 ++++ 15 files changed, 210 insertions(+), 91 deletions(-) create mode 100644 foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/buffer/IEventBuffer.kt create mode 100644 foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollector.kt create mode 100644 foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollectorProvider.kt create mode 100644 foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollectorProviders.kt diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/FoundationHeimdallPlugin.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/FoundationHeimdallPlugin.kt index b43f6c8..1b8c05a 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/FoundationHeimdallPlugin.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/FoundationHeimdallPlugin.kt @@ -4,29 +4,20 @@ import com.charleskorn.kaml.Yaml import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import gay.pizza.foundation.common.FoundationCoreLoader -import gay.pizza.foundation.shared.PluginMainClass -import gay.pizza.foundation.shared.copyDefaultConfig import gay.pizza.foundation.heimdall.plugin.buffer.BufferFlushThread import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer -import gay.pizza.foundation.heimdall.plugin.event.* +import gay.pizza.foundation.heimdall.plugin.event.EventCollector +import gay.pizza.foundation.heimdall.plugin.event.EventCollectorProviders import gay.pizza.foundation.heimdall.plugin.export.ExportAllChunksCommand import gay.pizza.foundation.heimdall.plugin.load.ImportWorldLoadCommand import gay.pizza.foundation.heimdall.plugin.model.HeimdallConfig -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer -import org.bukkit.event.EventHandler +import gay.pizza.foundation.shared.PluginMainClass +import gay.pizza.foundation.shared.copyDefaultConfig import org.bukkit.event.Listener -import org.bukkit.event.block.BlockBreakEvent -import org.bukkit.event.block.BlockPlaceEvent -import org.bukkit.event.entity.EntityDeathEvent -import org.bukkit.event.entity.PlayerDeathEvent -import org.bukkit.event.player.* import org.bukkit.plugin.java.JavaPlugin import org.jetbrains.exposed.sql.Database import org.postgresql.Driver import java.time.Duration -import java.time.Instant -import java.util.* -import java.util.concurrent.ConcurrentHashMap import kotlin.io.path.inputStream @PluginMainClass @@ -38,15 +29,15 @@ class FoundationHeimdallPlugin : JavaPlugin(), Listener { private val buffer = EventBuffer() private val bufferFlushThread = BufferFlushThread(this, buffer) - private val playerJoinTimes = ConcurrentHashMap() - - private val legacyComponentSerializer = LegacyComponentSerializer.builder().build() + private val collectors = mutableListOf>() override fun onEnable() { - val exportChunksCommand = getCommand("export_all_chunks") ?: throw Exception("Failed to get export_all_chunks command") + val exportChunksCommand = getCommand("export_all_chunks") ?: + throw Exception("Failed to get export_all_chunks command") exportChunksCommand.setExecutor(ExportAllChunksCommand(this)) - val importWorldLoadCommand = getCommand("import_world_load") ?: throw Exception("Failed to get import_world_load command") + val importWorldLoadCommand = getCommand("import_world_load") ?: + throw Exception("Failed to get import_world_load command") importWorldLoadCommand.setExecutor(ImportWorldLoadCommand(this)) val foundation = FoundationCoreLoader.get(server) @@ -98,79 +89,20 @@ class FoundationHeimdallPlugin : JavaPlugin(), Listener { db = Database.connect(pool) server.pluginManager.registerEvents(this, this) bufferFlushThread.start() - } - @EventHandler - fun onPlayerMove(event: PlayerMoveEvent) = buffer.push(PlayerPosition(event)) - - @EventHandler - fun onBlockBroken(event: BlockPlaceEvent) = buffer.push(BlockPlace(event)) - - @EventHandler - fun onBlockBroken(event: BlockBreakEvent) = buffer.push(BlockBreak(event)) - - @EventHandler - fun onPlayerJoin(event: PlayerJoinEvent) { - playerJoinTimes[event.player.uniqueId] = Instant.now() - } - - @EventHandler - fun onPlayerQuit(event: PlayerQuitEvent) { - val startTime = playerJoinTimes.remove(event.player.uniqueId) ?: return - val endTime = Instant.now() - buffer.push(PlayerSession(event.player.uniqueId, event.player.name, startTime, endTime)) - } - - @EventHandler - fun onPlayerDeath(event: PlayerDeathEvent) { - val deathMessage = event.deathMessage() - val deathMessageString = if (deathMessage != null) { - legacyComponentSerializer.serialize(deathMessage) - } else { - null + for (collectorProvider in EventCollectorProviders.all) { + val collector = collectorProvider.collector(buffer) + server.pluginManager.registerEvents(collector, this) + collectors.add(collector) } - buffer.push(PlayerDeath(event, deathMessageString)) - } - - @EventHandler - fun onPlayerAdvancementDone(event: PlayerAdvancementDoneEvent) = buffer.push(PlayerAdvancement(event)) - - @EventHandler - fun onWorldLoad(event: PlayerChangedWorldEvent) = buffer.push( - WorldChange( - event.player.uniqueId, - event.from.uid, - event.from.name, - event.player.world.uid, - event.player.world.name - ) - ) - - @EventHandler - fun onEntityDeath(event: EntityDeathEvent) { - val killer = event.entity.killer ?: return - buffer.push( - EntityKill( - killer.uniqueId, - killer.location, - event.entity.uniqueId, - event.entityType.key.toString() - ) - ) } override fun onDisable() { bufferFlushThread.stop() - val endTime = Instant.now() - for (playerId in playerJoinTimes.keys().toList()) { - val startTime = playerJoinTimes.remove(playerId) ?: continue - buffer.push(PlayerSession( - playerId, - server.getPlayer(playerId)?.name ?: "__unknown__", - startTime, - endTime - )) + for (collector in collectors) { + collector.onPluginDisable(server) } + collectors.clear() bufferFlushThread.flush() } } diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/buffer/EventBuffer.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/buffer/EventBuffer.kt index e83be87..2be0b2b 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/buffer/EventBuffer.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/buffer/EventBuffer.kt @@ -3,7 +3,7 @@ package gay.pizza.foundation.heimdall.plugin.buffer import gay.pizza.foundation.heimdall.plugin.event.HeimdallEvent import org.jetbrains.exposed.sql.Transaction -class EventBuffer { +class EventBuffer : IEventBuffer { private var events = mutableListOf() fun flush(transaction: Transaction): Long { @@ -18,7 +18,7 @@ class EventBuffer { return count } - fun push(event: HeimdallEvent) { + override fun push(event: HeimdallEvent) { events.add(event) } diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/buffer/IEventBuffer.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/buffer/IEventBuffer.kt new file mode 100644 index 0000000..5eb72a3 --- /dev/null +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/buffer/IEventBuffer.kt @@ -0,0 +1,7 @@ +package gay.pizza.foundation.heimdall.plugin.buffer + +import gay.pizza.foundation.heimdall.plugin.event.HeimdallEvent + +interface IEventBuffer { + fun push(event: HeimdallEvent) +} diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/BlockBreak.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/BlockBreak.kt index 7e276fb..ca3057f 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/BlockBreak.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/BlockBreak.kt @@ -1,8 +1,11 @@ package gay.pizza.foundation.heimdall.plugin.event +import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer +import gay.pizza.foundation.heimdall.plugin.buffer.IEventBuffer import gay.pizza.foundation.heimdall.table.BlockBreakTable import org.bukkit.Location import org.bukkit.Material +import org.bukkit.event.EventHandler import org.bukkit.event.block.BlockBreakEvent import org.jetbrains.exposed.sql.Transaction import org.jetbrains.exposed.sql.insert @@ -29,4 +32,13 @@ class BlockBreak( } } } + + class Collector(val buffer: IEventBuffer) : EventCollector { + @EventHandler + fun onBlockBroken(event: BlockBreakEvent) = buffer.push(BlockBreak(event)) + } + + companion object : EventCollectorProvider { + override fun collector(buffer: EventBuffer): EventCollector = Collector(buffer) + } } diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/BlockPlace.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/BlockPlace.kt index 967e8e5..c27c55c 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/BlockPlace.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/BlockPlace.kt @@ -1,8 +1,11 @@ package gay.pizza.foundation.heimdall.plugin.event +import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer +import gay.pizza.foundation.heimdall.plugin.buffer.IEventBuffer import gay.pizza.foundation.heimdall.table.BlockPlaceTable import org.bukkit.Location import org.bukkit.Material +import org.bukkit.event.EventHandler import org.bukkit.event.block.BlockPlaceEvent import org.jetbrains.exposed.sql.Transaction import org.jetbrains.exposed.sql.insert @@ -29,4 +32,13 @@ class BlockPlace( } } } + + class Collector(val buffer: IEventBuffer) : EventCollector { + @EventHandler + fun onBlockPlaced(event: BlockPlaceEvent) = buffer.push(BlockPlace(event)) + } + + companion object : EventCollectorProvider { + override fun collector(buffer: EventBuffer): EventCollector = Collector(buffer) + } } diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EntityKill.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EntityKill.kt index b4bad47..4f4ca6e 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EntityKill.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EntityKill.kt @@ -1,7 +1,11 @@ package gay.pizza.foundation.heimdall.plugin.event +import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer +import gay.pizza.foundation.heimdall.plugin.buffer.IEventBuffer import gay.pizza.foundation.heimdall.table.EntityKillTable import org.bukkit.Location +import org.bukkit.event.EventHandler +import org.bukkit.event.entity.EntityDeathEvent import org.jetbrains.exposed.sql.Transaction import org.jetbrains.exposed.sql.insert import java.time.Instant @@ -23,4 +27,23 @@ class EntityKill( } } } + + class Collector(val buffer: IEventBuffer) : EventCollector { + @EventHandler + fun onEntityDeath(event: EntityDeathEvent) { + val killer = event.entity.killer ?: return + buffer.push( + EntityKill( + killer.uniqueId, + killer.location, + event.entity.uniqueId, + event.entityType.key.toString() + ) + ) + } + } + + companion object : EventCollectorProvider { + override fun collector(buffer: EventBuffer): EventCollector = Collector(buffer) + } } diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollector.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollector.kt new file mode 100644 index 0000000..30167ed --- /dev/null +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollector.kt @@ -0,0 +1,8 @@ +package gay.pizza.foundation.heimdall.plugin.event + +import org.bukkit.Server +import org.bukkit.event.Listener + +interface EventCollector : Listener { + fun onPluginDisable(server: Server) {} +} diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollectorProvider.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollectorProvider.kt new file mode 100644 index 0000000..bbe4eeb --- /dev/null +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollectorProvider.kt @@ -0,0 +1,7 @@ +package gay.pizza.foundation.heimdall.plugin.event + +import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer + +interface EventCollectorProvider { + fun collector(buffer: EventBuffer): EventCollector +} diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollectorProviders.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollectorProviders.kt new file mode 100644 index 0000000..05f021b --- /dev/null +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/EventCollectorProviders.kt @@ -0,0 +1,14 @@ +package gay.pizza.foundation.heimdall.plugin.event + +object EventCollectorProviders { + val all = listOf>( + BlockBreak, + BlockPlace, + EntityKill, + PlayerAdvancement, + PlayerDeath, + PlayerPosition, + PlayerSession, + WorldChange + ) +} diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/ExposedExtensions.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/ExposedExtensions.kt index 0165741..8ef8cb1 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/ExposedExtensions.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/ExposedExtensions.kt @@ -30,11 +30,7 @@ fun T.putPlayerTimedLocalEvent( location: Location, player: UUID ) { - statement[this.time] = time - statement[this.world] = location.world.uid - statement[this.x] = location.x - statement[this.y] = location.y - statement[this.z] = location.z + putTimedLocalEvent(statement, time, location) statement[this.player] = player statement[this.pitch] = location.pitch.toDouble() statement[this.yaw] = location.yaw.toDouble() diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerAdvancement.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerAdvancement.kt index b463bf5..c428405 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerAdvancement.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerAdvancement.kt @@ -1,8 +1,11 @@ package gay.pizza.foundation.heimdall.plugin.event +import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer +import gay.pizza.foundation.heimdall.plugin.buffer.IEventBuffer import gay.pizza.foundation.heimdall.table.PlayerAdvancementTable import org.bukkit.Location import org.bukkit.advancement.Advancement +import org.bukkit.event.EventHandler import org.bukkit.event.player.PlayerAdvancementDoneEvent import org.jetbrains.exposed.sql.Transaction import org.jetbrains.exposed.sql.insert @@ -29,4 +32,13 @@ class PlayerAdvancement( } } } + + class Collector(val buffer: IEventBuffer) : EventCollector { + @EventHandler + fun onPlayerAdvancementDone(event: PlayerAdvancementDoneEvent) = buffer.push(PlayerAdvancement(event)) + } + + companion object : EventCollectorProvider { + override fun collector(buffer: EventBuffer): EventCollector = Collector(buffer) + } } diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerDeath.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerDeath.kt index 710d703..26041b8 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerDeath.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerDeath.kt @@ -1,7 +1,11 @@ package gay.pizza.foundation.heimdall.plugin.event +import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer +import gay.pizza.foundation.heimdall.plugin.buffer.IEventBuffer import gay.pizza.foundation.heimdall.table.PlayerDeathTable +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer import org.bukkit.Location +import org.bukkit.event.EventHandler import org.bukkit.event.entity.PlayerDeathEvent import org.jetbrains.exposed.sql.Transaction import org.jetbrains.exposed.sql.insert @@ -31,4 +35,23 @@ class PlayerDeath( } } } + + class Collector(val buffer: IEventBuffer) : EventCollector { + private val legacyComponentSerializer = LegacyComponentSerializer.builder().build() + + @EventHandler + fun onPlayerDeath(event: PlayerDeathEvent) { + val deathMessage = event.deathMessage() + val deathMessageString = if (deathMessage != null) { + legacyComponentSerializer.serialize(deathMessage) + } else { + null + } + buffer.push(PlayerDeath(event, deathMessageString)) + } + } + + companion object : EventCollectorProvider { + override fun collector(buffer: EventBuffer): EventCollector = Collector(buffer) + } } diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerPosition.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerPosition.kt index c78871d..5434d1a 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerPosition.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerPosition.kt @@ -1,7 +1,10 @@ package gay.pizza.foundation.heimdall.plugin.event +import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer +import gay.pizza.foundation.heimdall.plugin.buffer.IEventBuffer import gay.pizza.foundation.heimdall.table.PlayerPositionTable import org.bukkit.Location +import org.bukkit.event.EventHandler import org.bukkit.event.player.PlayerMoveEvent import org.jetbrains.exposed.sql.Transaction import org.jetbrains.exposed.sql.insert @@ -25,4 +28,13 @@ class PlayerPosition( } } } + + class Collector(val buffer: IEventBuffer) : EventCollector { + @EventHandler + fun onPlayerMove(event: PlayerMoveEvent) = buffer.push(PlayerPosition(event)) + } + + companion object : EventCollectorProvider { + override fun collector(buffer: EventBuffer): EventCollector = Collector(buffer) + } } diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerSession.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerSession.kt index dd93549..b0e2aa5 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerSession.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/PlayerSession.kt @@ -1,10 +1,17 @@ package gay.pizza.foundation.heimdall.plugin.event +import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer +import gay.pizza.foundation.heimdall.plugin.buffer.IEventBuffer import gay.pizza.foundation.heimdall.table.PlayerSessionTable +import org.bukkit.Server +import org.bukkit.event.EventHandler +import org.bukkit.event.player.PlayerJoinEvent +import org.bukkit.event.player.PlayerQuitEvent import org.jetbrains.exposed.sql.Transaction import org.jetbrains.exposed.sql.insert import java.time.Instant import java.util.* +import java.util.concurrent.ConcurrentHashMap class PlayerSession( val playerUniqueIdentity: UUID, @@ -23,4 +30,37 @@ class PlayerSession( } } } + + class Collector(val buffer: IEventBuffer) : EventCollector { + private val playerJoinTimes = ConcurrentHashMap() + + @EventHandler + fun onPlayerJoin(event: PlayerJoinEvent) { + playerJoinTimes[event.player.uniqueId] = Instant.now() + } + + @EventHandler + fun onPlayerQuit(event: PlayerQuitEvent) { + val startTime = playerJoinTimes.remove(event.player.uniqueId) ?: return + val endTime = Instant.now() + buffer.push(PlayerSession(event.player.uniqueId, event.player.name, startTime, endTime)) + } + + override fun onPluginDisable(server: Server) { + val endTime = Instant.now() + for (playerId in playerJoinTimes.keys().toList()) { + val startTime = playerJoinTimes.remove(playerId) ?: continue + buffer.push(PlayerSession( + playerId, + server.getPlayer(playerId)?.name ?: "__unknown__", + startTime, + endTime + )) + } + } + } + + companion object : EventCollectorProvider { + override fun collector(buffer: EventBuffer): EventCollector = Collector(buffer) + } } diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/WorldChange.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/WorldChange.kt index b63c655..9d123c1 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/WorldChange.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/event/WorldChange.kt @@ -1,6 +1,10 @@ package gay.pizza.foundation.heimdall.plugin.event +import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer +import gay.pizza.foundation.heimdall.plugin.buffer.IEventBuffer import gay.pizza.foundation.heimdall.table.WorldChangeTable +import org.bukkit.event.EventHandler +import org.bukkit.event.player.PlayerChangedWorldEvent import org.jetbrains.exposed.sql.Transaction import org.jetbrains.exposed.sql.insert import java.time.Instant @@ -26,4 +30,21 @@ class WorldChange( } } } + + class Collector(val buffer: IEventBuffer) : EventCollector { + @EventHandler + fun onWorldLoad(event: PlayerChangedWorldEvent) = buffer.push( + WorldChange( + event.player.uniqueId, + event.from.uid, + event.from.name, + event.player.world.uid, + event.player.world.name + ) + ) + } + + companion object : EventCollectorProvider { + override fun collector(buffer: EventBuffer): EventCollector = Collector(buffer) + } }