Serialize the X-Ray blocks to the database

This commit is contained in:
Charlotte Som 2022-03-09 00:28:26 +00:00
parent 7de33468b2
commit 94075ba509
6 changed files with 57 additions and 15 deletions

View file

@ -41,6 +41,7 @@ object Hibiscus : ModInitializer {
data.runMigrations() data.runMigrations()
for (feature in features.getAllFeatures()) { for (feature in features.getAllFeatures()) {
feature.loadData()
commands.register(feature.createFeatureCommand()) commands.register(feature.createFeatureCommand())
} }

View file

@ -48,5 +48,7 @@ abstract class Feature(val name: String, val category: FeatureCategory) {
return FeatureCommand(this) return FeatureCommand(this)
} }
open fun loadData() {}
// TODO: Module commands // TODO: Module commands
} }

View file

@ -13,6 +13,10 @@ class DataManager {
TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
} }
init {
runMigrations()
}
fun runMigrations() { fun runMigrations() {
val latestId = try { val latestId = try {
txn { txn {

View file

@ -3,11 +3,32 @@ package codes.som.hibiscus.data
import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Transaction import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.insert
object HibiscusDataMigrations : IntIdTable("_hibiscus_migrations") object HibiscusDataMigrations : IntIdTable("_hibiscus_migrations")
// PLEASE ONLY APPEND
@Suppress("FunctionName") @Suppress("FunctionName")
fun HIBISCUS_MIGRATIONS(): Sequence<Transaction.() -> Unit> = sequenceOf( fun HIBISCUS_MIGRATIONS(): Sequence<Transaction.() -> Unit> = sequenceOf(
{ SchemaUtils.create(HibiscusDataMigrations) }, { SchemaUtils.create(HibiscusDataMigrations) },
{ SchemaUtils.create(MinecraftAccounts) } { SchemaUtils.create(MinecraftAccounts) },
{ SchemaUtils.create(XrayBlocks) },
{
for (defaultTag in sequenceOf(
"#minecraft:diamond_ores",
"#minecraft:iron_ores",
"#minecraft:portals",
"minecraft:obsidian",
"minecraft:furnace",
"minecraft:blast_furnace",
"minecraft:smoker",
"minecraft:dropper",
"minecraft:hopper",
"minecraft:barrel",
"minecraft:dispenser",
"minecraft:brewing_stand"
)) {
XrayBlocks.insert { it[tag] = defaultTag }
}
}
) )

View file

@ -0,0 +1,7 @@
package codes.som.hibiscus.data
import org.jetbrains.exposed.dao.id.IntIdTable
object XrayBlocks : IntIdTable("xray_blocks") {
val tag = varchar("tag", 1024)
}

View file

@ -1,31 +1,30 @@
package codes.som.hibiscus.features.visual package codes.som.hibiscus.features.visual
import codes.som.hibiscus.Hibiscus
import codes.som.hibiscus.HibiscusLog import codes.som.hibiscus.HibiscusLog
import codes.som.hibiscus.api.feature.Feature import codes.som.hibiscus.api.feature.Feature
import codes.som.hibiscus.api.feature.FeatureCategory import codes.som.hibiscus.api.feature.FeatureCategory
import codes.som.hibiscus.data.XrayBlocks
import codes.som.hibiscus.events.WorldCullingEvent import codes.som.hibiscus.events.WorldCullingEvent
import codes.som.hibiscus.mc import codes.som.hibiscus.mc
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.selectAll
class Xray : Feature("X-ray", FeatureCategory.VISUAL) { class Xray : Feature("X-ray", FeatureCategory.VISUAL) {
val xrayBlocks = mutableSetOf( val xrayBlocks = mutableSetOf<String>()
"#minecraft:diamond_ores",
"#minecraft:iron_ores",
"#minecraft:portals",
"minecraft:obsidian",
"minecraft:furnace",
"minecraft:blast_furnace",
"minecraft:smoker",
"minecraft:dropper",
"minecraft:hopper",
"minecraft:barrel",
"minecraft:dispenser",
"minecraft:brewing_stand",
)
init { init {
on { event: WorldCullingEvent -> event.cancel() } on { event: WorldCullingEvent -> event.cancel() }
} }
override fun loadData() {
Hibiscus.data.txn {
xrayBlocks.clear()
xrayBlocks.addAll(XrayBlocks.selectAll().map { it[XrayBlocks.tag] })
}
}
override fun onEnable() { override fun onEnable() {
mc.worldRenderer.reload() mc.worldRenderer.reload()
} }
@ -41,10 +40,18 @@ class Xray : Feature("X-ray", FeatureCategory.VISUAL) {
branch("add") { blockOrTag: String -> branch("add") { blockOrTag: String ->
xrayBlocks.add(blockOrTag) xrayBlocks.add(blockOrTag)
Hibiscus.data.txn {
XrayBlocks.insert { it[tag] = blockOrTag }
}
} }
branch("del") { blockOrTag: String -> branch("del") { blockOrTag: String ->
xrayBlocks.remove(blockOrTag) xrayBlocks.remove(blockOrTag)
Hibiscus.data.txn {
XrayBlocks.deleteWhere { XrayBlocks.tag eq blockOrTag }
}
} }
branch("list") { branch("list") {