mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-02 13:10:55 +00:00
Refactor persistence into it's own feature.
This commit is contained in:
parent
fca1db8802
commit
78566d08ad
@ -7,6 +7,7 @@ import cloud.kubelet.foundation.core.features.player.PlayerFeature
|
||||
import cloud.kubelet.foundation.core.features.stats.StatsFeature
|
||||
import cloud.kubelet.foundation.core.features.update.UpdateFeature
|
||||
import cloud.kubelet.foundation.core.features.world.WorldFeature
|
||||
import cloud.kubelet.foundation.core.features.persist.PersistenceFeature
|
||||
import org.koin.dsl.module
|
||||
import java.nio.file.Path
|
||||
|
||||
@ -37,6 +38,7 @@ class FoundationCorePlugin : FoundationPlugin() {
|
||||
}
|
||||
|
||||
override fun createFeatures() = listOf(
|
||||
PersistenceFeature(),
|
||||
BackupFeature(),
|
||||
DevFeature(),
|
||||
PlayerFeature(),
|
||||
|
@ -0,0 +1,18 @@
|
||||
package cloud.kubelet.foundation.core.features.persist
|
||||
|
||||
import cloud.kubelet.foundation.core.abstraction.Feature
|
||||
import org.koin.core.component.inject
|
||||
import org.koin.core.module.Module
|
||||
import org.koin.dsl.module
|
||||
|
||||
class PersistenceFeature : Feature() {
|
||||
private val persistence = inject<PluginPersistence>()
|
||||
|
||||
override fun disable() {
|
||||
persistence.value.unload()
|
||||
}
|
||||
|
||||
override fun module(): Module = module {
|
||||
single { PluginPersistence() }
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package cloud.kubelet.foundation.core.persist
|
||||
package cloud.kubelet.foundation.core.features.persist
|
||||
|
||||
import cloud.kubelet.foundation.core.FoundationCorePlugin
|
||||
import jetbrains.exodus.entitystore.Entity
|
@ -1,5 +1,6 @@
|
||||
package cloud.kubelet.foundation.core.features.stats
|
||||
package cloud.kubelet.foundation.core.features.persist
|
||||
|
||||
import cloud.kubelet.foundation.core.features.stats.StatsFeature
|
||||
import org.bukkit.command.Command
|
||||
import org.bukkit.command.CommandExecutor
|
||||
import org.bukkit.command.CommandSender
|
||||
@ -23,7 +24,7 @@ class PersistentStoreCommand(
|
||||
|
||||
when (args[0]) {
|
||||
"stats" -> {
|
||||
statsFeature.persistentStores.forEach { (name, store) ->
|
||||
statsFeature.persistence.value.stores.forEach { (name, store) ->
|
||||
val counts = store.transact {
|
||||
entityTypes.associateWith { type -> getAll(type).size() }.toSortedMap()
|
||||
}
|
||||
@ -42,7 +43,7 @@ class PersistentStoreCommand(
|
||||
|
||||
val storeName = args[1]
|
||||
val entityTypeName = args[2]
|
||||
val store = statsFeature.getPersistentStore(storeName)
|
||||
val store = statsFeature.persistence.value.store(storeName)
|
||||
store.transact {
|
||||
val entities = getAll(entityTypeName).take(3)
|
||||
for (entity in entities) {
|
||||
@ -61,7 +62,7 @@ class PersistentStoreCommand(
|
||||
|
||||
val storeName = args[1]
|
||||
val entityTypeName = args[2]
|
||||
val store = statsFeature.getPersistentStore(storeName)
|
||||
val store = statsFeature.persistence.value.store(storeName)
|
||||
store.transact {
|
||||
store.deleteAllEntities(entityTypeName)
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package cloud.kubelet.foundation.core.features.persist
|
||||
|
||||
import cloud.kubelet.foundation.core.FoundationCorePlugin
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
class PluginPersistence : KoinComponent {
|
||||
private val plugin = inject<FoundationCorePlugin>()
|
||||
|
||||
val stores = ConcurrentHashMap<String, PersistentStore>()
|
||||
|
||||
/**
|
||||
* Fetch a persistent store by name. Make sure the name is path-safe, descriptive and consistent across server runs.
|
||||
*/
|
||||
fun store(name: String): PersistentStore =
|
||||
stores.getOrPut(name) { PersistentStore(plugin.value, name) }
|
||||
|
||||
fun unload() {
|
||||
stores.values.forEach { store -> store.close() }
|
||||
stores.clear()
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package cloud.kubelet.foundation.core.persist
|
||||
package cloud.kubelet.foundation.core.features.persist
|
||||
|
||||
import jetbrains.exodus.entitystore.Entity
|
||||
|
@ -1,39 +1,23 @@
|
||||
package cloud.kubelet.foundation.core.features.stats
|
||||
|
||||
import cloud.kubelet.foundation.core.FoundationCorePlugin
|
||||
import cloud.kubelet.foundation.core.abstraction.Feature
|
||||
import cloud.kubelet.foundation.core.persist.PersistentStore
|
||||
import cloud.kubelet.foundation.core.persist.setAllProperties
|
||||
import cloud.kubelet.foundation.core.features.persist.*
|
||||
import io.papermc.paper.event.player.AsyncChatEvent
|
||||
import net.kyori.adventure.text.TextComponent
|
||||
import org.bukkit.event.EventHandler
|
||||
import org.koin.core.component.inject
|
||||
import java.time.Instant
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
class StatsFeature : Feature() {
|
||||
private val plugin = inject<FoundationCorePlugin>()
|
||||
internal val persistence = inject<PluginPersistence>()
|
||||
private lateinit var chatLogStore: PersistentStore
|
||||
// TODO: Move persistence stuff to its own module.
|
||||
internal val persistentStores = ConcurrentHashMap<String, PersistentStore>()
|
||||
|
||||
override fun enable() {
|
||||
chatLogStore = getPersistentStore("chat-logs")
|
||||
chatLogStore = persistence.value.store("chat-logs")
|
||||
|
||||
registerCommandExecutor("pstore", PersistentStoreCommand(this))
|
||||
}
|
||||
|
||||
override fun disable() {
|
||||
persistentStores.values.forEach { store -> store.close() }
|
||||
persistentStores.clear()
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a persistent store by name. Make sure the name is path-safe, descriptive and consistent across server runs.
|
||||
*/
|
||||
fun getPersistentStore(name: String) =
|
||||
persistentStores.getOrPut(name) { PersistentStore(plugin.value, name) }
|
||||
|
||||
@EventHandler
|
||||
private fun logOnChatMessage(e: AsyncChatEvent) {
|
||||
val player = e.player
|
||||
|
Loading…
Reference in New Issue
Block a user