Skip to content

Commit

Permalink
Persistent data tweaks and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Aug 25, 2024
1 parent 1a816b0 commit 413ae4e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,11 @@ public final class PersistentDataKey<T> {
* @param key The key.
* @param type The data type.
* @param defaultValue The default value.
* @param isLocal If the key uses local storage.
*/
public PersistentDataKey(@NotNull final NamespacedKey key,
@NotNull final PersistentDataKeyType<T> type,
@NotNull final T defaultValue,
final boolean isLocal) {
this.key = key;
this.defaultValue = defaultValue;
this.type = type;
this.isLocal = isLocal;

Eco.get().registerPersistentKey(this);
@NotNull final T defaultValue) {
this(key, type, defaultValue, false);
}

/**
Expand All @@ -60,14 +53,16 @@ public PersistentDataKey(@NotNull final NamespacedKey key,
* @param key The key.
* @param type The data type.
* @param defaultValue The default value.
* @param isLocal If the key uses local storage.
*/
public PersistentDataKey(@NotNull final NamespacedKey key,
@NotNull final PersistentDataKeyType<T> type,
@NotNull final T defaultValue) {
@NotNull final T defaultValue,
final boolean isLocal) {
this.key = key;
this.defaultValue = defaultValue;
this.type = type;
this.isLocal = false;
this.isLocal = isLocal;

Eco.get().registerPersistentKey(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,20 @@
package com.willfp.eco.internal.spigot.data

import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.data.keys.PersistentDataKey
import com.willfp.eco.core.data.keys.PersistentDataKeyType
import org.bukkit.NamespacedKey
import java.math.BigDecimal

object KeyRegistry {
private val registry = mutableMapOf<NamespacedKey, PersistentDataKey<*>>()

fun registerKey(key: PersistentDataKey<*>) {
if (this.registry.containsKey(key.key)) {
this.registry.remove(key.key)
if (key.defaultValue == null) {
throw IllegalArgumentException("Default value cannot be null!")
}

validateKey(key)

this.registry[key.key] = key
}

fun getRegisteredKeys(): Set<PersistentDataKey<*>> {
return registry.values.toSet()
}

private fun <T> validateKey(key: PersistentDataKey<T>) {
val default = key.defaultValue

when (key.type) {
PersistentDataKeyType.INT -> if (default !is Int) {
throw IllegalArgumentException("Invalid Data Type! Should be Int")
}
PersistentDataKeyType.DOUBLE -> if (default !is Double) {
throw IllegalArgumentException("Invalid Data Type! Should be Double")
}
PersistentDataKeyType.BOOLEAN -> if (default !is Boolean) {
throw IllegalArgumentException("Invalid Data Type! Should be Boolean")
}
PersistentDataKeyType.STRING -> if (default !is String) {
throw IllegalArgumentException("Invalid Data Type! Should be String")
}
PersistentDataKeyType.STRING_LIST -> if (default !is List<*> || default.firstOrNull() !is String?) {
throw IllegalArgumentException("Invalid Data Type! Should be String List")
}
PersistentDataKeyType.CONFIG -> if (default !is Config) {
throw IllegalArgumentException("Invalid Data Type! Should be Config")
}
PersistentDataKeyType.BIG_DECIMAL -> if (default !is BigDecimal) {
throw IllegalArgumentException("Invalid Data Type! Should be BigDecimal")
}

else -> throw NullPointerException("Null value found!")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.upsert
import java.math.BigDecimal
import java.util.UUID
import kotlin.math.pow

class MySQLPersistentDataHandler(
config: Config
Expand Down Expand Up @@ -241,18 +242,19 @@ class MySQLPersistentDataHandler(
}

private inline fun <T> withRetries(action: () -> T): T {
var retries = 0
var retries = 1
while (true) {
try {
return action()
} catch (e: Exception) {
if (retries >= 5) {
if (retries > 5) {
throw e
}
retries++

// Exponential backoff
runBlocking {
delay(10)
delay(2.0.pow(retries.toDouble()).toLong())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions eco-core/core-plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# How player/server data is saved:
# yaml - Stored in data.yml: Good option for single-node servers (i.e. no BungeeCord/Velocity)
# mysql - Standard database, great option for multi-node servers (i.e. BungeeCord/Velocity)
# mongo - Alternative database, may suit some servers better
# mongo - Alternative database, great option for multi-node servers (i.e. BungeeCord/Velocity)
data-handler: yaml

# If data should be migrated automatically when changing data handler.
Expand Down Expand Up @@ -35,7 +35,7 @@ mysql:
port: 3306
database: database
user: username
password: passy
password: p4ssw0rd

# How many ticks to wait between committing data to a database. This doesn't
# affect yaml storage, only MySQL and MongoDB. By default, data is committed
Expand Down

0 comments on commit 413ae4e

Please sign in to comment.