Added fast NamespacedKey creation

This commit is contained in:
Auxilor
2022-01-27 12:41:18 +00:00
parent c0be6a12ff
commit feb7ecee48
7 changed files with 72 additions and 5 deletions

View File

@@ -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'
}

View File

@@ -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)
}
}

View File

@@ -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
}
}

View File

@@ -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)
}
}

View File

@@ -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'
- 'org.apache.commons:commons-lang3:3.0'
- 'org.objenesis:objenesis:3.2'