NamespacedKey changes

This commit is contained in:
Auxilor
2022-01-27 12:51:22 +00:00
parent aa553b96d6
commit 1cc2a585d6
8 changed files with 39 additions and 14 deletions

View File

@@ -6,6 +6,7 @@ import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.items.Items;
import com.willfp.eco.core.recipe.recipes.CraftingRecipe;
import com.willfp.eco.core.recipe.recipes.ShapedCraftingRecipe;
import com.willfp.eco.util.NamespacedKeyUtils;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -58,7 +59,10 @@ public final class Recipes {
}
if (key.getKey().contains("_displayed")) {
NamespacedKey otherKey = new NamespacedKey(key.getNamespace(), key.getKey().replace("_displayed", ""));
NamespacedKey otherKey = NamespacedKeyUtils.create(
key.getNamespace(),
key.getKey().replace("_displayed", "")
);
return RECIPES.get(otherKey);
}

View File

@@ -3,7 +3,11 @@ package com.willfp.eco.internal.fast
import org.bukkit.NamespacedKey
import org.objenesis.ObjenesisSerializer
object EcoFastNamespacedKeyFactory {
interface InternalNamespacedKeyFactory {
fun create(namespace: String, key: String): NamespacedKey
}
class FastInternalNamespacedKeyFactory : InternalNamespacedKeyFactory {
private val creator = ObjenesisSerializer().getInstantiatorOf(NamespacedKey::class.java)
private val namespaceField = NamespacedKey::class.java.getDeclaredField("namespace")
.apply { isAccessible = true }
@@ -11,10 +15,17 @@ object EcoFastNamespacedKeyFactory {
.apply { isAccessible = true }
fun create(namespace: String, key: String): NamespacedKey {
override fun create(namespace: String, key: String): NamespacedKey {
val namespacedKey = creator.newInstance()
namespaceField.set(keyField, key.lowercase())
namespaceField.set(namespaceField, namespace.lowercase())
return namespacedKey
}
}
}
class SafeInternalNamespacedKeyFactory : InternalNamespacedKeyFactory {
override fun create(namespace: String, key: String): NamespacedKey {
@Suppress("DEPRECATION")
return NamespacedKey(namespace, key)
}
}

View File

@@ -2,7 +2,7 @@ package com.willfp.eco.internal.items
import com.willfp.eco.core.fast.FastItemStack
import com.willfp.eco.core.items.args.LookupArgParser
import org.bukkit.NamespacedKey
import com.willfp.eco.util.NamespacedKeyUtils
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.EnchantmentStorageMeta
@@ -20,7 +20,7 @@ class ArgParserEnchantment : LookupArgParser {
continue
}
val enchant = Enchantment.getByKey(NamespacedKey.minecraft(argSplit[0].lowercase()))
val enchant = Enchantment.getByKey(NamespacedKeyUtils.create("minecraft", argSplit[0]))
val level = argSplit[1].toIntOrNull()
if (enchant != null && level != null) {

View File

@@ -1,6 +1,7 @@
package com.willfp.eco.internal.spigot.proxy.v1_16_R3.fast
import com.willfp.eco.internal.fast.EcoFastItemStack
import com.willfp.eco.util.NamespacedKeyUtils
import com.willfp.eco.util.StringUtils
import net.minecraft.server.v1_16_R3.Item
import net.minecraft.server.v1_16_R3.ItemEnchantedBook
@@ -11,7 +12,6 @@ import net.minecraft.server.v1_16_R3.NBTTagList
import net.minecraft.server.v1_16_R3.NBTTagString
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack
import org.bukkit.craftbukkit.v1_16_R3.util.CraftMagicNumbers
import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemFlag
import kotlin.experimental.and
@@ -29,7 +29,7 @@ class NMSFastItemStack(itemStack: org.bukkit.inventory.ItemStack) : EcoFastItemS
val compound = base as NBTTagCompound
val key = compound.getString("id")
val level: Int = ('\uffff'.code.toShort() and compound.getShort("lvl")).toInt()
val found = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(key))
val found = Enchantment.getByKey(NamespacedKeyUtils.fromString(key))
if (found != null) {
foundEnchantments[found] = level
}

View File

@@ -1,6 +1,7 @@
package com.willfp.eco.internal.spigot.proxy.v1_17_R1.fast
import com.willfp.eco.internal.fast.EcoFastItemStack
import com.willfp.eco.util.NamespacedKeyUtils
import com.willfp.eco.util.StringUtils
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.ListTag
@@ -11,7 +12,6 @@ import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack
import org.bukkit.craftbukkit.v1_17_R1.util.CraftMagicNumbers
import org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemFlag
import kotlin.experimental.and
@@ -31,7 +31,7 @@ class NMSFastItemStack(itemStack: org.bukkit.inventory.ItemStack) : EcoFastItemS
val compound = base as CompoundTag
val key = compound.getString("id")
val level = ('\uffff'.code.toShort() and compound.getShort("lvl")).toInt()
val found = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(key))
val found = Enchantment.getByKey(NamespacedKeyUtils.fromString(key))
if (found != null) {
foundEnchantments[found] = level
}

View File

@@ -1,6 +1,7 @@
package com.willfp.eco.internal.spigot.proxy.v1_18_R1.fast
import com.willfp.eco.internal.fast.EcoFastItemStack
import com.willfp.eco.util.NamespacedKeyUtils
import com.willfp.eco.util.StringUtils
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.ListTag
@@ -11,7 +12,6 @@ import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack
import org.bukkit.craftbukkit.v1_18_R1.util.CraftMagicNumbers
import org.bukkit.craftbukkit.v1_18_R1.util.CraftNamespacedKey
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemFlag
import kotlin.experimental.and
@@ -32,7 +32,7 @@ class NMSFastItemStack(itemStack: org.bukkit.inventory.ItemStack) : EcoFastItemS
val compound = base as CompoundTag
val key = compound.getString("id")
val level = ('\uffff'.code.toShort() and compound.getShort("lvl")).toInt()
val found = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(key))
val found = Enchantment.getByKey(NamespacedKeyUtils.fromString(key))
if (found != null) {
foundEnchantments[found] = level
}

View File

@@ -14,7 +14,8 @@ 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.fast.FastInternalNamespacedKeyFactory
import com.willfp.eco.internal.fast.SafeInternalNamespacedKeyFactory
import com.willfp.eco.internal.gui.EcoGUIFactory
import com.willfp.eco.internal.integrations.PlaceholderIntegrationPAPI
import com.willfp.eco.internal.logging.EcoLogger
@@ -45,6 +46,8 @@ class EcoHandler : EcoSpigotPlugin(), Handler {
if (this.configYml.getBool("mysql.enabled"))
MySQLDataHandler(this) else YamlDataHandler(this)
)
private val keyFactory = if (this.configYml.getBool("use-safer-namespacedkey-creation"))
SafeInternalNamespacedKeyFactory() else FastInternalNamespacedKeyFactory()
override fun createScheduler(plugin: EcoPlugin): EcoScheduler {
return EcoScheduler(plugin)
@@ -151,6 +154,6 @@ class EcoHandler : EcoSpigotPlugin(), Handler {
}
override fun createNamespacedKey(namespace: String, key: String): NamespacedKey {
return EcoFastNamespacedKeyFactory.create(namespace, key)
return keyFactory.create(namespace, key)
}
}

View File

@@ -46,6 +46,13 @@ display-frame-ttl: 17
# memory use.
item-cache-ttl: 6000
# The default bukkit NamespacedKey creation can cause decent amounts of lag under
# load due to the use of regex validation in the constructor. eco has its own system
# to create NamespacedKeys, however it can be unsafe as it skips most validation checks.
# If you encounter any bugs with this, then enable this option - however it will come
# at a performance penalty.
use-safer-namespacedkey-creation: false
# Window items packets have the option to be run asynchronously. This may cause
# some bugs and is considered experimental, however it has been tested without
# any apparent issues. Enable this if performance is absolutely crucial or if you