mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-03 13:31:32 +00:00
Move persistence to plugin shared.
This commit is contained in:
@ -12,7 +12,4 @@ dependencies {
|
||||
|
||||
implementation("io.insert-koin:koin-core:3.3.2")
|
||||
testImplementation("io.insert-koin:koin-test:3.3.2")
|
||||
|
||||
implementation("org.jetbrains.xodus:xodus-openAPI:2.0.1")
|
||||
implementation("org.jetbrains.xodus:xodus-entity-store:2.0.1")
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ import gay.pizza.foundation.core.features.scheduler.SchedulerFeature
|
||||
import gay.pizza.foundation.core.features.stats.StatsFeature
|
||||
import gay.pizza.foundation.core.features.update.UpdateFeature
|
||||
import gay.pizza.foundation.core.features.world.WorldFeature
|
||||
import gay.pizza.foundation.shared.PersistentStore
|
||||
import gay.pizza.foundation.shared.PluginPersistence
|
||||
import org.koin.dsl.module
|
||||
import java.nio.file.Path
|
||||
|
||||
@ -34,6 +36,8 @@ class FoundationCorePlugin : IFoundationCore, FoundationPlugin() {
|
||||
_pluginDataPath = value
|
||||
}
|
||||
|
||||
override val persistence: PluginPersistence = PluginPersistence(this)
|
||||
|
||||
override fun onEnable() {
|
||||
// Create core plugin directory.
|
||||
pluginDataPath = dataFolder.toPath()
|
||||
|
@ -1,18 +1,21 @@
|
||||
package gay.pizza.foundation.core.features.persist
|
||||
|
||||
import gay.pizza.foundation.core.FoundationCorePlugin
|
||||
import gay.pizza.foundation.core.abstraction.Feature
|
||||
import gay.pizza.foundation.shared.PluginPersistence
|
||||
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>()
|
||||
private val core = inject<FoundationCorePlugin>()
|
||||
|
||||
override fun disable() {
|
||||
persistence.value.unload()
|
||||
}
|
||||
|
||||
override fun module(): Module = module {
|
||||
single { PluginPersistence() }
|
||||
single { core.value.persistence }
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +0,0 @@
|
||||
package gay.pizza.foundation.core.features.persist
|
||||
|
||||
import gay.pizza.foundation.core.FoundationCorePlugin
|
||||
import jetbrains.exodus.entitystore.Entity
|
||||
import jetbrains.exodus.entitystore.EntityIterable
|
||||
import jetbrains.exodus.entitystore.PersistentEntityStores
|
||||
import jetbrains.exodus.entitystore.StoreTransaction
|
||||
|
||||
class PersistentStore(corePlugin: FoundationCorePlugin, fileStoreName: String) : AutoCloseable {
|
||||
private val fileStorePath = corePlugin.pluginDataPath.resolve("persistence/${fileStoreName}")
|
||||
private val entityStore = PersistentEntityStores.newInstance(fileStorePath.toFile())
|
||||
|
||||
fun <R> transact(block: StoreTransaction.() -> R): R {
|
||||
var result: R? = null
|
||||
entityStore.executeInTransaction { tx ->
|
||||
result = block(tx)
|
||||
}
|
||||
return result!!
|
||||
}
|
||||
|
||||
fun create(entityTypeName: String, populate: Entity.() -> Unit) = transact {
|
||||
populate(newEntity(entityTypeName))
|
||||
}
|
||||
|
||||
fun <R> getAll(entityTypeName: String, block: (EntityIterable) -> R): R =
|
||||
transact { block(getAll(entityTypeName)) }
|
||||
|
||||
fun <T, R> find(entityTypeName: String, propertyName: String, value: Comparable<T>, block: (EntityIterable) -> R): R =
|
||||
transact { block(find(entityTypeName, propertyName, value)) }
|
||||
|
||||
fun deleteAllEntities(entityTypeName: String) =
|
||||
transact { entityStore.deleteEntityType(entityTypeName) }
|
||||
|
||||
override fun close() {
|
||||
entityStore.close()
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package gay.pizza.foundation.core.features.persist
|
||||
|
||||
import gay.pizza.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,10 +1,10 @@
|
||||
package gay.pizza.foundation.core.features.stats
|
||||
|
||||
import gay.pizza.foundation.core.abstraction.Feature
|
||||
import gay.pizza.foundation.core.features.persist.PersistentStore
|
||||
import gay.pizza.foundation.core.features.persist.PersistentStoreCommand
|
||||
import gay.pizza.foundation.core.features.persist.PluginPersistence
|
||||
import gay.pizza.foundation.core.features.persist.setAllProperties
|
||||
import gay.pizza.foundation.shared.PersistentStore
|
||||
import gay.pizza.foundation.shared.PluginPersistence
|
||||
import io.papermc.paper.event.player.AsyncChatEvent
|
||||
import net.kyori.adventure.text.TextComponent
|
||||
import org.bukkit.event.EventHandler
|
||||
|
Reference in New Issue
Block a user