Added more kotlin utilities
This commit is contained in:
@@ -3,6 +3,9 @@ package com.willfp.eco.util;
|
||||
import com.willfp.eco.core.Eco;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Utilities / API methods for {@link NamespacedKey}s.
|
||||
@@ -45,8 +48,25 @@ public final class NamespacedKeyUtils {
|
||||
*/
|
||||
@NotNull
|
||||
public static NamespacedKey fromString(@NotNull final String string) {
|
||||
return Objects.requireNonNull(NamespacedKeyUtils.fromStringOrNull(string));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a NamespacedKey from a string.
|
||||
* <p>
|
||||
* Preferred over {@link NamespacedKey#fromString(String)} for performance reasons.
|
||||
*
|
||||
* @param string The string.
|
||||
* @return The key, or null if not a key.
|
||||
*/
|
||||
@Nullable
|
||||
public static NamespacedKey fromStringOrNull(@NotNull final String string) {
|
||||
int index = string.indexOf(":");
|
||||
|
||||
if (index < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return NamespacedKeyUtils.create(
|
||||
string.substring(0, index),
|
||||
string.substring(index + 1)
|
||||
|
||||
@@ -13,3 +13,9 @@ fun <T> List<T>.toFrequencyMap(): Map<T, Int> =
|
||||
*/
|
||||
fun Iterable<String>.containsIgnoreCase(element: String): Boolean =
|
||||
ListUtils.containsIgnoreCase(this, element)
|
||||
|
||||
/**
|
||||
* @see ListUtils.create2DList
|
||||
*/
|
||||
fun <T> create2DList(rows: Int, columns: Int): List<List<T>> =
|
||||
ListUtils.create2DList(rows, columns)
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
@file:JvmName("NamespacedKeyUtilsExtensions")
|
||||
|
||||
package com.willfp.eco.util
|
||||
|
||||
/**
|
||||
* @see NamespacedKeyUtils.fromString
|
||||
*/
|
||||
fun namespacedKeyOf(string: String) =
|
||||
NamespacedKeyUtils.fromString(string)
|
||||
|
||||
/**
|
||||
* @see NamespacedKeyUtils.fromString
|
||||
*/
|
||||
fun safeNamespacedKeyOf(string: String) =
|
||||
NamespacedKeyUtils.fromStringOrNull(string)
|
||||
|
||||
/**
|
||||
* @see NamespacedKeyUtils.create
|
||||
*/
|
||||
fun namespacedKeyOf(namespace: String, key: String) =
|
||||
NamespacedKeyUtils.create(namespace, key)
|
||||
Reference in New Issue
Block a user