diff --git a/eco-api/src/main/java/com/willfp/eco/core/Handler.java b/eco-api/src/main/java/com/willfp/eco/core/Handler.java index 4dabe844..69fbfd01 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/Handler.java +++ b/eco-api/src/main/java/com/willfp/eco/core/Handler.java @@ -19,6 +19,7 @@ import com.willfp.eco.core.requirement.RequirementFactory; import com.willfp.eco.core.scheduling.Scheduler; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.Location; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.ApiStatus; @@ -243,4 +244,19 @@ public interface Handler { */ @NotNull Entity createDummyEntity(@NotNull Location location); + + /** + * Create a {@link NamespacedKey} quickly + *

+ * Bypasses the constructor, allowing for the creation of invalid keys, + * therefore this is considered unsafe and should only be called after + * the key has been confirmed to be valid. + * + * @param namespace The namespace. + * @param key The key. + * @return The key. + */ + @NotNull + NamespacedKey createNamespacedKey(@NotNull String namespace, + @NotNull String key); } diff --git a/eco-api/src/main/java/com/willfp/eco/util/NamespacedKeyUtils.java b/eco-api/src/main/java/com/willfp/eco/util/NamespacedKeyUtils.java index e514b87a..9db2e90f 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/NamespacedKeyUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/NamespacedKeyUtils.java @@ -1,9 +1,10 @@ package com.willfp.eco.util; +import com.willfp.eco.core.Eco; import org.bukkit.NamespacedKey; import org.jetbrains.annotations.NotNull; -import java.util.Objects; +import java.util.Locale; /** * Utilities / API methods for {@link NamespacedKey}s. @@ -17,7 +18,7 @@ public final class NamespacedKeyUtils { */ @NotNull public static NamespacedKey createEcoKey(@NotNull final String string) { - return Objects.requireNonNull(NamespacedKey.fromString("eco:" + string)); + return NamespacedKeyUtils.create("eco", string); } /** @@ -30,7 +31,28 @@ public final class NamespacedKeyUtils { @NotNull public static NamespacedKey create(@NotNull final String namespace, @NotNull final String key) { - return Objects.requireNonNull(NamespacedKey.fromString(namespace + ":" + key)); + return Eco.getHandler().createNamespacedKey( + namespace.toLowerCase(Locale.ROOT), + key.toLowerCase(Locale.ROOT) + ); + } + + /** + * Create a NamespacedKey from a string. + *

+ * Preferred over {@link NamespacedKey#fromString(String)} for performance reasons. + * + * @param string The string. + * @return The key. + */ + @NotNull + public static NamespacedKey fromString(@NotNull final String string) { + int index = string.indexOf(":"); + + return NamespacedKeyUtils.create( + string.substring(0, index), + string.substring(index) + ); } private NamespacedKeyUtils() { diff --git a/eco-core/core-backend/build.gradle b/eco-core/core-backend/build.gradle index 33f4f699..e08aa262 100644 --- a/eco-core/core-backend/build.gradle +++ b/eco-core/core-backend/build.gradle @@ -8,4 +8,5 @@ dependencies { compileOnly 'net.kyori:adventure-text-minimessage:4.1.0-SNAPSHOT' compileOnly 'net.kyori:adventure-platform-bukkit:4.0.0' compileOnly 'com.google.guava:guava:31.0.1-jre' + compileOnly 'org.objenesis:objenesis:3.2' } \ No newline at end of file diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/factory/EcoNamespacedKeyFactory.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/factory/EcoNamespacedKeyFactory.kt index 72c064a3..7768362e 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/factory/EcoNamespacedKeyFactory.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/factory/EcoNamespacedKeyFactory.kt @@ -2,10 +2,11 @@ package com.willfp.eco.internal.factory import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.factory.NamespacedKeyFactory +import com.willfp.eco.util.NamespacedKeyUtils import org.bukkit.NamespacedKey class EcoNamespacedKeyFactory(private val plugin: EcoPlugin) : NamespacedKeyFactory { override fun create(key: String): NamespacedKey { - return NamespacedKey(plugin, key) + return NamespacedKeyUtils.create(plugin.name, key) } } \ No newline at end of file diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/fast/EcoFastNamespacedKeyFactory.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/fast/EcoFastNamespacedKeyFactory.kt new file mode 100644 index 00000000..a492b494 --- /dev/null +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/fast/EcoFastNamespacedKeyFactory.kt @@ -0,0 +1,20 @@ +package com.willfp.eco.internal.fast + +import org.bukkit.NamespacedKey +import org.objenesis.ObjenesisSerializer + +object EcoFastNamespacedKeyFactory { + private val creator = ObjenesisSerializer().getInstantiatorOf(NamespacedKey::class.java) + private val namespaceField = NamespacedKey::class.java.getDeclaredField("namespace") + .apply { isAccessible = true } + private val keyField = NamespacedKey::class.java.getDeclaredField("key") + .apply { isAccessible = true } + + + fun create(namespace: String, key: String): NamespacedKey { + val namespacedKey = creator.newInstance() + namespaceField.set(keyField, key.lowercase()) + namespaceField.set(namespaceField, namespace.lowercase()) + return namespacedKey + } +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt index 3bdd15ff..055aca47 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt @@ -14,6 +14,7 @@ import com.willfp.eco.internal.extensions.EcoExtensionLoader import com.willfp.eco.internal.factory.EcoMetadataValueFactory import com.willfp.eco.internal.factory.EcoNamespacedKeyFactory import com.willfp.eco.internal.factory.EcoRunnableFactory +import com.willfp.eco.internal.fast.EcoFastNamespacedKeyFactory import com.willfp.eco.internal.gui.EcoGUIFactory import com.willfp.eco.internal.integrations.PlaceholderIntegrationPAPI import com.willfp.eco.internal.logging.EcoLogger @@ -29,6 +30,7 @@ import com.willfp.eco.internal.spigot.proxy.DummyEntityProxy import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy import net.kyori.adventure.platform.bukkit.BukkitAudiences import org.bukkit.Location +import org.bukkit.NamespacedKey import org.bukkit.entity.Entity import org.bukkit.inventory.ItemStack import java.util.logging.Logger @@ -147,4 +149,8 @@ class EcoHandler : EcoSpigotPlugin(), Handler { override fun createDummyEntity(location: Location): Entity { return getProxy(DummyEntityProxy::class.java).createDummyEntity(location) } + + override fun createNamespacedKey(namespace: String, key: String): NamespacedKey { + return EcoFastNamespacedKeyFactory.create(namespace, key) + } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 67ad2679..35296d18 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -56,4 +56,5 @@ libraries: - 'mysql:mysql-connector-java:8.0.25' - 'com.google.guava:guava:31.0.1-jre' - 'com.zaxxer:HikariCP:5.0.0' - - 'org.apache.commons:commons-lang3:3.0' \ No newline at end of file + - 'org.apache.commons:commons-lang3:3.0' + - 'org.objenesis:objenesis:3.2' \ No newline at end of file