diff --git a/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java b/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java index 39aa255c..b57a90c0 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java +++ b/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java @@ -4,6 +4,7 @@ import com.willfp.eco.core.Eco; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Map; @@ -35,7 +36,7 @@ public interface FastItemStack { * * @param lore The lore. */ - void setLore(@NotNull List lore); + void setLore(@Nullable List lore); /** * Get the item lore. diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/fast/EcoFastItemStack.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/fast/EcoFastItemStack.kt index 75f98050..54f396ca 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/fast/EcoFastItemStack.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/fast/EcoFastItemStack.kt @@ -3,7 +3,7 @@ package com.willfp.eco.internal.fast import com.willfp.eco.core.fast.FastItemStack import org.bukkit.inventory.ItemStack -abstract class EcoFastItemStack( +abstract class EcoFastItemStack( val handle: T, val bukkit: ItemStack ) : FastItemStack { diff --git a/eco-core/core-nms/v1_17_R1/build.gradle b/eco-core/core-nms/v1_17_R1/build.gradle index 516f18e2..a4dcd952 100644 --- a/eco-core/core-nms/v1_17_R1/build.gradle +++ b/eco-core/core-nms/v1_17_R1/build.gradle @@ -1,5 +1,6 @@ plugins { id 'xyz.jpenilla.special-gradle' version '1.0.0-SNAPSHOT' + id 'org.jetbrains.kotlin.jvm' version '1.5.21' } group 'com.willfp' diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.java deleted file mode 100644 index e05c9cc3..00000000 --- a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.willfp.eco.proxy.v1_17_R1.fast; - -import com.willfp.eco.internal.fast.EcoFastItemStack; -import com.willfp.eco.util.StringUtils; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.StringTag; -import net.minecraft.nbt.Tag; -import net.minecraft.world.item.EnchantedBookItem; -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.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class NMSFastItemStack extends EcoFastItemStack { - private List loreCache = null; - - public NMSFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) { - super(FastItemStackUtils.getNMSStack(itemStack), itemStack); - } - - @Override - public Map getEnchantmentsOnItem(final boolean checkStored) { - ListTag enchantmentNBT = checkStored && this.getHandle().getItem() == Items.ENCHANTED_BOOK ? EnchantedBookItem.getEnchantments(this.getHandle()) : this.getHandle().getEnchantmentTags(); - Map foundEnchantments = new HashMap<>(); - - for (Tag base : enchantmentNBT) { - CompoundTag compound = (CompoundTag) base; - String key = compound.getString("id"); - int level = '\uffff' & compound.getShort("lvl"); - - Enchantment found = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(key)); - if (found != null) { - foundEnchantments.put(found, level); - } - } - return foundEnchantments; - } - - @Override - public int getLevelOnItem(@NotNull final Enchantment enchantment, - final boolean checkStored) { - ListTag enchantmentNBT = checkStored && this.getHandle().getItem() == Items.ENCHANTED_BOOK ? EnchantedBookItem.getEnchantments(this.getHandle()) : this.getHandle().getEnchantmentTags(); - - for (Tag base : enchantmentNBT) { - CompoundTag compound = (CompoundTag) base; - String key = compound.getString("id"); - if (!key.equals(enchantment.getKey().toString())) { - continue; - } - - return '\uffff' & compound.getShort("lvl"); - } - return 0; - } - - @Override - public void setLore(@Nullable final List lore) { - loreCache = null; - - List jsonLore = new ArrayList<>(); - if (lore != null) { - for (String s : lore) { - jsonLore.add(StringUtils.legacyToJson(s)); - } - } - - CompoundTag displayTag = this.getHandle().getOrCreateTagElement("display"); - if (!displayTag.contains("Lore")) { - displayTag.put("Lore", new ListTag()); - } - - ListTag loreTag = displayTag.getList("Lore", CraftMagicNumbers.NBT.TAG_STRING); - loreTag.clear(); - for (String s : jsonLore) { - loreTag.add(StringTag.valueOf(s)); - } - - apply(); - } - - @Override - public List getLore() { - if (loreCache != null) { - return loreCache; - } - - List lore = new ArrayList<>(); - - for (String s : this.getLoreJSON()) { - lore.add(StringUtils.jsonToLegacy(s)); - } - - loreCache = lore; - - return lore; - } - - private List getLoreJSON() { - CompoundTag displayTag = this.getHandle().getOrCreateTagElement("display"); - - if (displayTag.contains("Lore")) { - ListTag loreTag = displayTag.getList("Lore", CraftMagicNumbers.NBT.TAG_STRING); - List lore = new ArrayList<>(loreTag.size()); - for (int i = 0; i < loreTag.size(); i++) { - lore.add(loreTag.getString(i)); - } - return lore; - } else { - return new ArrayList<>(); - } - } - - public void apply() { - if (!(this.getBukkit() instanceof CraftItemStack)) { - this.getBukkit().setItemMeta(CraftItemStack.asCraftMirror(this.getHandle()).getItemMeta()); - } - } -} diff --git a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.kt b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.kt new file mode 100644 index 00000000..40dc7331 --- /dev/null +++ b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.kt @@ -0,0 +1,123 @@ +package com.willfp.eco.proxy.v1_17_R1.fast + +import com.willfp.eco.internal.fast.EcoFastItemStack +import com.willfp.eco.util.StringUtils +import net.minecraft.nbt.CompoundTag +import net.minecraft.nbt.ListTag +import net.minecraft.nbt.StringTag +import net.minecraft.world.item.EnchantedBookItem +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 kotlin.experimental.and + +class NMSFastItemStack(itemStack: org.bukkit.inventory.ItemStack) : EcoFastItemStack( + FastItemStackUtils.getNMSStack(itemStack), itemStack +) { + private var loreCache: List? = null + + override fun getEnchantmentsOnItem(checkStored: Boolean): Map { + val enchantmentNBT = + if (checkStored && handle.item === Items.ENCHANTED_BOOK) EnchantedBookItem.getEnchantments( + handle + ) else handle.enchantmentTags + val foundEnchantments: MutableMap = HashMap() + for (base in enchantmentNBT) { + 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)) + if (found != null) { + foundEnchantments[found] = level + } + } + return foundEnchantments + } + + override fun getLevelOnItem( + enchantment: Enchantment, + checkStored: Boolean + ): Int { + val enchantmentNBT = + if (checkStored && handle.item === Items.ENCHANTED_BOOK) EnchantedBookItem.getEnchantments( + handle + ) else handle.enchantmentTags + for (base in enchantmentNBT) { + val compound = base as CompoundTag + val key = compound.getString("id") + if (key != enchantment.key.toString()) { + continue + } + return ('\uffff'.code.toShort() and compound.getShort("lvl")).toInt() + } + return 0 + } + + override fun setLore(lore: List?) { + loreCache = null + val jsonLore: MutableList = ArrayList() + + if (lore != null) { + for (s in lore) { + jsonLore.add(StringUtils.legacyToJson(s)) + } + } + + val displayTag = handle.getOrCreateTagElement("display") + + if (!displayTag.contains("Lore")) { + displayTag.put("Lore", ListTag()) + } + + val loreTag = displayTag.getList("Lore", CraftMagicNumbers.NBT.TAG_STRING) + + loreTag.clear() + + for (s in jsonLore) { + loreTag.add(StringTag.valueOf(s)) + } + + apply() + } + + override fun getLore(): List { + if (loreCache != null) { + return loreCache as List + } + + val lore: MutableList = ArrayList() + + for (s in loreJSON) { + lore.add(StringUtils.jsonToLegacy(s)) + } + + loreCache = lore + return lore + } + + private val loreJSON: List + get() { + val displayTag = handle.getOrCreateTagElement("display") + return if (displayTag.contains("Lore")) { + val loreTag = displayTag.getList("Lore", CraftMagicNumbers.NBT.TAG_STRING) + val lore: MutableList = ArrayList(loreTag.size) + + for (i in loreTag.indices) { + lore.add(loreTag.getString(i)) + } + + lore + } else { + ArrayList() + } + } + + private fun apply() { + if (bukkit !is CraftItemStack) { + bukkit.itemMeta = CraftItemStack.asCraftMirror(handle).itemMeta + } + } +} \ No newline at end of file