-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persistent data tweaks and improvements
- Loading branch information
Showing
4 changed files
with
15 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 2 additions & 37 deletions
39
eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/KeyRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters