Add player repository

This commit is contained in:
TDSTOS
2025-12-11 19:47:06 +01:00
parent 3092a19086
commit 31c1e6e316
4 changed files with 179 additions and 5 deletions

View File

@@ -43,8 +43,8 @@ dependencies {
compileOnly("org.popcraft:chunky-common:1.3.38")
implementation("club.mcscrims:core:1.4.2")
implementation("club.mcscrims:spigot:1.4.2")
implementation("club.mcscrims:core:1.4.3.1")
implementation("club.mcscrims:spigot:1.4.3.1")
compileOnly("io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT")
paperweight.paperDevBundle("1.21.1-R0.1-SNAPSHOT")

View File

@@ -15,6 +15,7 @@ import club.mcscrims.speedhg.config.PluginConfig
import club.mcscrims.speedhg.database.StatsRepository
import club.mcscrims.speedhg.game.GameManager
import club.mcscrims.speedhg.command.KitsCommand
import club.mcscrims.speedhg.database.PlayerRepository
import club.mcscrims.speedhg.kit.KitInventoryListener
import club.mcscrims.speedhg.kit.KitInventoryManager
import club.mcscrims.speedhg.kit.KitListener
@@ -34,6 +35,7 @@ import kotlinx.coroutines.runBlocking
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.luckperms.api.LuckPerms
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
class SpeedHG : JavaPlugin() {
@@ -60,6 +62,7 @@ class SpeedHG : JavaPlugin() {
private lateinit var mongoManager: MongoManager
internal lateinit var statsRepository: StatsRepository
internal lateinit var playerRepository: PlayerRepository
internal lateinit var networkManager: SpigotNetworkManager
internal lateinit var schedulerManager: SchedulerManager
@@ -95,7 +98,8 @@ class SpeedHG : JavaPlugin() {
chatFormatter = ChatFormatter.create(
plugin = this,
configClass = MessageConfig::class,
messageExtractor = { config -> config.getAllMessages() }
messageExtractor = { config -> config.getAllMessages() },
listExtractor = { config -> config.getListMessages() }
)
chatManager = ChatManager.withCustomConfig( this, chatFormatter )
@@ -128,7 +132,7 @@ class SpeedHG : JavaPlugin() {
override fun onDisable()
{
kitManager.clearAll()
mongoManager.shutdown()
closeDatabase()
networkManager.shutdown()
}
@@ -175,6 +179,7 @@ class SpeedHG : JavaPlugin() {
// Repositories initialisieren
statsRepository = StatsRepository( connection )
playerRepository = PlayerRepository( connection )
// Indizes erstellen
runBlocking {
@@ -185,6 +190,7 @@ class SpeedHG : JavaPlugin() {
connection.createIndex( "player_stats", Indexes.ascending( "wins" ))
connection.createIndex( "player_stats", Indexes.ascending( "unathleticIndex" ))
connection.createIndex( "player_stats", Indexes.ascending( "ironFarmed" ))
connection.createIndex( "kit_players", Indexes.text( "server" ))
}
catch ( ex: Exception ) {
logger.warning( "Failed to create MongoDB indexes: ${ex.message}" )
@@ -195,6 +201,29 @@ class SpeedHG : JavaPlugin() {
logger.info( "Successfully enabled MongoDB" )
}
private fun closeDatabase()
{
// Alle Server von Kit-Spielern auf NULL setzen
runBlocking {
try
{
val players = Bukkit.getOnlinePlayers()
players.forEach { player ->
playerRepository.updateServer( player.uniqueId, "NULL" )
}
logger.info( "Updated ${players.size} players server to NULL." )
}
catch ( ex: Exception ) {
logger.warning( "Failed to update player server: ${ex.message}" )
}
}.also {
// MongoManager beenden
mongoManager.shutdown()
logger.info( "Successfully disabled MongoDB" )
}
}
/*
* CONFIG >>
*/

View File

@@ -13,7 +13,10 @@ data class MessageConfig(
val defaultMessages: Map<String, String> = getDefaultMessages(),
@ConfigField(name = "commands", description = "Command messages")
val commandMessages: Map<String, String> = getCommandMessages()
val commandMessages: Map<String, String> = getCommandMessages(),
@ConfigField(name = "death", description = "Death messages")
val deathMessages: Map<String, List<String>> = getDeathMessages()
) {
fun getAllMessages(): Map<String, String>
@@ -21,6 +24,11 @@ data class MessageConfig(
return defaultMessages + commandMessages
}
fun getListMessages(): Map<String, List<String>>
{
return deathMessages
}
}
private fun getDefaultMessages(): Map<String, String> = mapOf(
@@ -33,4 +41,16 @@ private fun getDefaultMessages(): Map<String, String> = mapOf(
private fun getCommandMessages(): Map<String, String> = mapOf(
"commands.unknown" to "%prefix% <red>Unknown subcommand: {unknown}</red> \n%prefix% <gray>Use /{command} for an overview.</gray>"
)
private fun getDeathMessages(): Map<String, List<String>> = mapOf(
"player" to listOf(
"<black>☠</black> <red>{player} was killed by {killer}!</red>"
),
"entity" to listOf(
"<black>☠</black> <red>{player} was killed by {entity}!</red>"
),
"world" to listOf(
"<black>☠</black> <red>{player} has died!</red>"
)
)

View File

@@ -0,0 +1,125 @@
package club.mcscrims.speedhg.database
import club.mcscrims.core.database.mongodb.MongoConnection
import club.mcscrims.core.database.mongodb.MongoRepository
import com.mongodb.client.model.Filters
import com.mongodb.client.model.Updates
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.codecs.pojo.annotations.BsonProperty
import org.bson.conversions.Bson
import org.bson.types.ObjectId
import org.bukkit.Bukkit
import java.util.UUID
/**
* MongoDB-Entity für einen Spieler
*/
data class KitPlayer(
@BsonId
val id: ObjectId = ObjectId(),
@BsonProperty
val uuid: String,
@BsonProperty
val isAlive: Boolean,
@BsonProperty
val unlockedKits: List<String>,
@BsonProperty
val server: String
)
/**
* MongoDB-Repository für Spieler-Daten
*/
class PlayerRepository(
connection: MongoConnection
): MongoRepository<KitPlayer>( connection, "kit_players", KitPlayer::class.java ) {
override fun getId(
entity: KitPlayer
): ObjectId
{
return entity.id
}
override fun setId(
entity: KitPlayer,
id: ObjectId
): KitPlayer
{
return entity.copy( id = id )
}
override fun entityToUpdateDocument(
entity: KitPlayer
): Bson = Updates.combine(
Updates.set("isAlive", entity.isAlive),
Updates.set("unlockedKits", entity.unlockedKits),
Updates.set("server", entity.server)
)
/**
* Findet einen Spieler anhand seiner UUID
*/
suspend fun findByUuid(
uuid: UUID
): KitPlayer?
{
val filter = Filters.eq("uuid", uuid.toString())
return findFirst(filter)
}
/**
* Findet alle lebenden Spieler für einen Server
*/
suspend fun findAlivePlayers(
server: String
): List<KitPlayer>
{
val filter = Filters.and(
Filters.eq("server", server),
Filters.eq("isAlive", true)
)
return find(filter, Bukkit.getMaxPlayers())
}
/**
* Aktualisiert den Alive-Status eines Spielers
*/
suspend fun updateAliveStatus(
uuid: UUID,
isAlive: Boolean
) {
val filter = Filters.eq("uuid", uuid.toString())
val update = Updates.set("isAlive", isAlive)
connection.updateOne(collectionName, filter, update)
}
/**
* Aktualisiert den Server eines Spielers
*/
suspend fun updateServer(
uuid: UUID,
server: String
) {
val filter = Filters.eq("uuid", uuid.toString())
val update = Updates.set("server", server)
connection.updateOne(collectionName, filter, update)
}
/**
* Aktualisiert die freigeschalteten Kits eines Spielers
*/
suspend fun updateUnlockedKits(
uuid: UUID,
unlockedKits: List<String>
) {
val filter = Filters.eq("uuid", uuid.toString())
val update = Updates.set("unlockedKits", unlockedKits)
connection.updateOne(collectionName, filter, update)
}
}