Skip to content

Commit

Permalink
refactor: Refactor database manager to json manager
Browse files Browse the repository at this point in the history
* Because a SQLite package resulted in too large jar package, I decided to abandon SQLite and use JSON to store player data
* Add interface DataService for possible future expansions
  • Loading branch information
half-nothing committed Sep 22, 2024
1 parent 3ad8121 commit d86991d
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 291 deletions.
6 changes: 2 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ dependencies {
modImplementation(libs.translations)

// For debug
modImplementation(libs.bundles.database)
modImplementation(libs.bundles.jdbc)
modImplementation(libs.bundles.konf)
modImplementation(libs.gson)

// For release
shadow(libs.bundles.database)
shadow(libs.bundles.jdbc)
shadow(libs.bundles.konf)
shadow(libs.gson)
}

base {
Expand Down
2 changes: 0 additions & 2 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,6 @@ style:
DataClassContainsFunctions:
active: true
allowOperators: true
DataClassShouldBeImmutable:
active: true
DestructuringDeclarationWithTooManyEntries:
active: false
ForbiddenComment:
Expand Down
12 changes: 2 additions & 10 deletions libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ yarn-mappings = "1.21.1+build.3"
translations = "2.3.1+1.21-pre2"
detekt = "1.23.7"
konf = "1.1.2"

exposed = "0.54.0"
sqlite-jdbc = "3.46.1.0"
gson = "2.11.0"

[libraries]
minecraft = { module = "net.minecraft:minecraft", version.ref = "minecraft" }
Expand All @@ -25,17 +23,11 @@ konf-core = { module = "com.uchuhimo:konf-core", version.ref = "konf"}
konf-toml = { module = "com.uchuhimo:konf-toml", version.ref = "konf"}
translations = { module = "xyz.nucleoid:server-translations-api", version.ref = "translations" }

exposed-core = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" }
exposed-dao = { module = "org.jetbrains.exposed:exposed-dao", version.ref = "exposed" }
exposed-jdbc = { module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "exposed" }
exposed-java-time = { module = "org.jetbrains.exposed:exposed-java-time", version.ref = "exposed" }
sqlite-jdbc = { module = "org.xerial:sqlite-jdbc", version.ref = "sqlite-jdbc" }
gson = {module = "com.google.code.gson:gson", version.ref="gson"}

[bundles]
fabric = ["fabric-api", "fabric-kotlin", "fabric-loader"]
konf = ["konf-core", "konf-toml"]
database = ["exposed-core", "exposed-dao", "exposed-java-time", "exposed-jdbc"]
jdbc = ["sqlite-jdbc"]

[plugins]
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/net/superricky/tpaplusplus/GlobalConst.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object GlobalConst {
const val CONFIG_FOLDER_PATH = MOD_ID
const val CONFIG_FILE_NAME = "$MOD_ID.toml"
const val CONFIG_FILE_PATH = "$CONFIG_FOLDER_PATH/$CONFIG_FILE_NAME"
const val PLAYER_DATA_FILE_NAME = "$MOD_ID.json"

const val GITHUB_URL = "https://github.com/SuperRicky14/TpaPlusPlus"
const val MODRINTH_URL = "https://modrinth.com/mod/tpa++"
Expand Down
17 changes: 6 additions & 11 deletions src/main/kotlin/net/superricky/tpaplusplus/TpaPlusPlus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import dev.architectury.event.events.common.TickEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.loader.api.FabricLoader
Expand All @@ -22,7 +21,8 @@ import net.superricky.tpaplusplus.async.AsyncCommandHelper
import net.superricky.tpaplusplus.command.CommandRegister
import net.superricky.tpaplusplus.config.AdvancedSpec
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.database.DatabaseManager
import net.superricky.tpaplusplus.database.DataService
import net.superricky.tpaplusplus.database.PlayerDataManager
import java.nio.file.Files
import kotlin.coroutines.CoroutineContext
import net.superricky.tpaplusplus.event.PlayerEvent as PlayerEventListener
Expand All @@ -31,6 +31,7 @@ object TpaPlusPlus : ModInitializer, CoroutineScope {
lateinit var server: MinecraftServer
override val coroutineContext: CoroutineContext = Dispatchers.IO
val version: Version = FabricLoader.getInstance().getModContainer(MOD_ID).get().metadata.version
var dataService: DataService = PlayerDataManager

override fun onInitialize() {
logger.info("Initializing TPA++ ${version.friendlyString}")
Expand Down Expand Up @@ -76,15 +77,8 @@ object TpaPlusPlus : ModInitializer, CoroutineScope {
logger.info("Main logic initializing...")
this.server = server

logger.info("Database initializing...")
DatabaseManager.setup()
DatabaseManager.ensureTables()

logger.info("Make database cache...")
launch {
DatabaseManager.setupCache()
logger.info("The database cache is successfully constructed")
}
logger.info("Data service initializing...")
dataService.initDataService()

logger.info("Add player event listener...")
PlayerEvent.PLAYER_JOIN.register(PlayerEventListener::joinEvent)
Expand All @@ -108,5 +102,6 @@ object TpaPlusPlus : ModInitializer, CoroutineScope {
logger.info("Shutting down TPA++")
AsyncCommandHelper.stopTickLoop()
coroutineContext.cancel()
dataService.savePlayerData()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import net.superricky.tpaplusplus.config.command.CommandCooldownSpec
import net.superricky.tpaplusplus.config.command.CommandDelaySpec
import net.superricky.tpaplusplus.config.command.CommandDistanceSpec
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.database.DatabaseManager
import net.superricky.tpaplusplus.utility.Context
import net.superricky.tpaplusplus.utility.LiteralNode
import net.superricky.tpaplusplus.utility.TextColorPallet
Expand Down Expand Up @@ -54,7 +53,7 @@ object BlockCommand : AsyncCommand(), BuildableCommand {
sender!!
target!!
TpaPlusPlus.launch {
if (DatabaseManager.insertBlockedPlayer(sender.uuid, target.uuid)) {
if (TpaPlusPlus.dataService.addBlockPlayer(sender.uuid, target.uuid)) {
source.sendFeedback(
{
Text.translatable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import net.superricky.tpaplusplus.config.command.CommandCooldownSpec
import net.superricky.tpaplusplus.config.command.CommandDelaySpec
import net.superricky.tpaplusplus.config.command.CommandDistanceSpec
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.database.DatabaseManager
import net.superricky.tpaplusplus.utility.Context
import net.superricky.tpaplusplus.utility.LiteralNode
import net.superricky.tpaplusplus.utility.toggleOff
Expand Down Expand Up @@ -53,7 +52,7 @@ object ToggleCommand : AsyncCommand(), BuildableCommand {
val sender = source.player
sender ?: return CommandResult.SENDER_NOT_EXIST.status
TpaPlusPlus.launch {
val blocked = DatabaseManager.playerSwitchBlock(sender.uuid)
val blocked = TpaPlusPlus.dataService.playerSwitchBlock(sender.uuid)
if (blocked) {
source.sendFeedback({ Text.translatable("command.toggle.success.on") }, false)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import net.superricky.tpaplusplus.config.command.CommandCooldownSpec
import net.superricky.tpaplusplus.config.command.CommandDelaySpec
import net.superricky.tpaplusplus.config.command.CommandDistanceSpec
import net.superricky.tpaplusplus.config.command.CommandNameSpec
import net.superricky.tpaplusplus.database.DatabaseManager
import net.superricky.tpaplusplus.utility.Context
import net.superricky.tpaplusplus.utility.LiteralNode
import net.superricky.tpaplusplus.utility.TextColorPallet
Expand Down Expand Up @@ -54,7 +53,7 @@ object UnblockCommand : AsyncCommand(), BuildableCommand {
sender!!
target!!
TpaPlusPlus.launch {
if (DatabaseManager.deleteBlockedPlayer(sender.uuid, target.uuid)) {
if (TpaPlusPlus.dataService.removeBlockPlayer(sender.uuid, target.uuid)) {
source.sendFeedback(
{
Text.translatable(
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/net/superricky/tpaplusplus/database/DataService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.superricky.tpaplusplus.database

import net.minecraft.server.network.ServerPlayerEntity
import net.superricky.tpaplusplus.utility.LevelBoundVec3
import java.util.*

interface DataService {
fun initDataService()
fun getPlayerData(player: ServerPlayerEntity): PlayerData
fun addBlockPlayer(uuid: UUID, blockPlayer: UUID): Boolean
fun removeBlockPlayer(uuid: UUID, blockPlayer: UUID): Boolean
fun playerSwitchBlock(uuid: UUID): Boolean
fun playerSwitchBlock(uuid: UUID, toggle: Boolean): Boolean
fun insertPlayer(uuid: UUID)
fun insertDeath(uuid: UUID, pos: LevelBoundVec3)
fun savePlayerData()
}

This file was deleted.

This file was deleted.

164 changes: 0 additions & 164 deletions src/main/kotlin/net/superricky/tpaplusplus/database/DatabaseManager.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.superricky.tpaplusplus.database

data class DeathPos(
var x: Double = 0.0,
var y: Double = 0.0,
var z: Double = 0.0,
var world: String = "",
var backed: Boolean = true
)
13 changes: 13 additions & 0 deletions src/main/kotlin/net/superricky/tpaplusplus/database/PlayerData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.superricky.tpaplusplus.database

import com.google.gson.annotations.SerializedName
import java.util.*

class PlayerData(
@SerializedName("block_players")
val blockPlayers: MutableSet<UUID> = Collections.synchronizedSet(HashSet()),
@SerializedName("last_death_pos")
val lastDeathPos: DeathPos = DeathPos(),
@SerializedName("toggle")
var toggle: Boolean = false
)
Loading

0 comments on commit d86991d

Please sign in to comment.