From afb498b4bfecc59ef7731c8e13551723047fd382 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Fri, 22 Apr 2022 10:48:33 +0100 Subject: [PATCH] Added more mappings to FastItemStack --- .../willfp/eco/core/fast/FastItemStack.java | 58 +++++++++++++++++++ .../java/com/willfp/eco/core/items/Items.java | 26 +++++++++ .../spigot/proxy/common/NMSCommons.kt | 9 +++ .../proxy/common/item/ControllableItem.kt | 26 +++++++++ .../common/{fast => item}/EcoFastItemStack.kt | 58 ++++++++++++++++++- .../proxy/v1_17_R1/FastItemStackFactory.kt | 2 +- .../proxy/v1_18_R1/FastItemStackFactory.kt | 2 +- .../proxy/v1_18_R2/FastItemStackFactory.kt | 2 +- 8 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/item/ControllableItem.kt rename eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/{fast => item}/EcoFastItemStack.kt (83%) 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 0f3a5a72..223e34d7 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 @@ -212,6 +212,64 @@ public interface FastItemStack extends PersistentDataHolder { */ void setBaseTag(@Nullable PersistentDataContainer container); + /** + * Get the type of the item. + * + * @return The type. + */ + @NotNull + Material getType(); + + /** + * Set the type of the item. + * + * @param material The type. + */ + void setType(@NotNull Material material); + + /** + * Get the amount of the item. + * + * @return The amount. + */ + int getAmount(); + + /** + * Set the amount of the item. + * + * @param amount The amount. + */ + void setAmount(int amount); + + /** + * Get the custom model data. + * + * @return The data, or null if none. + */ + @Nullable + Integer getCustomModelData(); + + /** + * Set the custom model data. + * + * @param data The data, null to remove. + */ + void setCustomModelData(@Nullable Integer data); + + /** + * Get the speed multiplier. + * + * @return The multiplier. + */ + double getDestroySpeedMultiplier(); + + /** + * Set the speed multiplier. + * + * @param multiplier The multiplier. + */ + void setDestroySpeedMultiplier(double multiplier); + /** * Get the Bukkit ItemStack again. * diff --git a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java index f7752dcd..9cec9bff 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java +++ b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java @@ -517,6 +517,32 @@ public final class Items { return fis.unwrap(); } + /** + * Get the base NBT tag on an item. + * + * @param itemStack The ItemStack. + * @param multiplier The multiplier + * @return The item, modified. Not required to use, as this modifies the instance. + */ + @NotNull + public static ItemStack setDestroySpeedMultiplier(@NotNull final ItemStack itemStack, + final double multiplier) { + FastItemStack fis = FastItemStack.wrap(itemStack); + fis.setDestroySpeedMultiplier(multiplier); + return fis.unwrap(); + } + + /** + * Get the items destroy speed. + * + * @param itemStack The ItemStack. + * @return The speed multiplier. + */ + public static double getDestroySpeedMultiplier(@NotNull final ItemStack itemStack) { + FastItemStack fis = FastItemStack.wrap(itemStack); + return fis.getDestroySpeedMultiplier(); + } + private Items() { throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } diff --git a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/NMSCommons.kt b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/NMSCommons.kt index 4490a4cb..91b046ed 100644 --- a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/NMSCommons.kt +++ b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/NMSCommons.kt @@ -52,6 +52,15 @@ fun Material.toItem(): Item = .orElseThrow { IllegalArgumentException("Material is not item!") } } +private val ITEM_TO_MATERIAL = mutableMapOf() + +fun Item.toMaterial(): Material = + ITEM_TO_MATERIAL.getOrPut(this) { + val material = Material.getMaterial(Registry.ITEM.getKey(this).path.uppercase()) + ?: throw IllegalArgumentException("Invalid material!") + material + } + fun CompoundTag.makePdc(base: Boolean = false): PersistentDataContainer = impl.makePdc(this, base) diff --git a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/item/ControllableItem.kt b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/item/ControllableItem.kt new file mode 100644 index 00000000..1aa3932f --- /dev/null +++ b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/item/ControllableItem.kt @@ -0,0 +1,26 @@ +package com.willfp.eco.internal.spigot.proxy.common.item + +import net.minecraft.world.item.Item +import net.minecraft.world.item.ItemStack +import net.minecraft.world.level.block.state.BlockState + +class ControllableItem( + val handle: Item +) : Item( + Properties() + .apply { + handle.itemCategory?.let { tab(it) } + durability(handle.maxDamage) + rarity(handle.rarity) + stacksTo(handle.maxStackSize) + handle.craftingRemainingItem?.let { craftRemainder(it) } + if (handle.isFireResistant) fireResistant() + handle.foodProperties?.let { food(it) } + } +) { + var destroySpeedMultiplier: Double = 1.0 + + override fun getDestroySpeed(stack: ItemStack, state: BlockState): Float { + return handle.getDestroySpeed(stack, state) * destroySpeedMultiplier.toFloat() + } +} diff --git a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/fast/EcoFastItemStack.kt b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/item/EcoFastItemStack.kt similarity index 83% rename from eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/fast/EcoFastItemStack.kt rename to eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/item/EcoFastItemStack.kt index 9f0c5aed..1f4654f8 100644 --- a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/fast/EcoFastItemStack.kt +++ b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/item/EcoFastItemStack.kt @@ -1,4 +1,4 @@ -package com.willfp.eco.internal.spigot.proxy.common.fast +package com.willfp.eco.internal.spigot.proxy.common.item import com.willfp.eco.core.fast.FastItemStack import com.willfp.eco.internal.spigot.proxy.common.NBT_TAG_STRING @@ -7,6 +7,7 @@ import com.willfp.eco.internal.spigot.proxy.common.makePdc import com.willfp.eco.internal.spigot.proxy.common.mergeIfNeeded import com.willfp.eco.internal.spigot.proxy.common.setPdc import com.willfp.eco.internal.spigot.proxy.common.toItem +import com.willfp.eco.internal.spigot.proxy.common.toMaterial import com.willfp.eco.util.NamespacedKeyUtils import com.willfp.eco.util.StringUtils import com.willfp.eco.util.toComponent @@ -18,6 +19,7 @@ import net.minecraft.nbt.StringTag import net.minecraft.world.item.EnchantedBookItem import net.minecraft.world.item.Item import net.minecraft.world.item.Items +import org.bukkit.Material import org.bukkit.NamespacedKey import org.bukkit.enchantments.Enchantment import org.bukkit.inventory.ItemFlag @@ -208,12 +210,66 @@ class EcoFastItemStack( override fun setRepairCost(cost: Int) { handle.setRepairCost(cost) + apply() } override fun getPersistentDataContainer(): PersistentDataContainer { return ContinuallyAppliedPersistentDataContainer(this.pdc, this) } + override fun getAmount(): Int = handle.getCount() + + override fun setAmount(amount: Int) { + handle.setCount(amount) + } + + override fun setType(material: Material) { + if (material == Material.AIR) { + handle.setTag(null) + } + @Suppress("DEPRECATION") + handle.setItem(material.toItem()) + apply() + } + + override fun getType(): Material = handle.getItem().toMaterial() + + override fun getCustomModelData(): Int? = handle.getTag()?.getInt("CustomModelData") + + override fun setCustomModelData(data: Int?) { + if (data == null) { + val tag = handle.getTag() ?: return + tag.remove("CustomModelData") + } else { + handle.getOrCreateTag().putInt("CustomModelData", data) + } + + apply() + } + + override fun getDestroySpeedMultiplier(): Double { + val item = handle.item + return if (item is ControllableItem) { + item.destroySpeedMultiplier + } else { + 1.0 + } + } + + override fun setDestroySpeedMultiplier(multiplier: Double) { + val item = handle.item + if (item is ControllableItem) { + item.destroySpeedMultiplier = multiplier + } else { + @Suppress("DEPRECATION") + handle.setItem(ControllableItem(item).apply { + destroySpeedMultiplier = multiplier + }) + } + + apply() + } + override fun equals(other: Any?): Boolean { if (other !is EcoFastItemStack) { return false diff --git a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/FastItemStackFactory.kt b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/FastItemStackFactory.kt index d70f5443..b8b4bed6 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/FastItemStackFactory.kt +++ b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/FastItemStackFactory.kt @@ -2,7 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_17_R1 import com.willfp.eco.core.fast.FastItemStack import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy -import com.willfp.eco.internal.spigot.proxy.common.fast.EcoFastItemStack +import com.willfp.eco.internal.spigot.proxy.common.item.EcoFastItemStack import org.bukkit.inventory.ItemStack class FastItemStackFactory : FastItemStackFactoryProxy { diff --git a/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/FastItemStackFactory.kt b/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/FastItemStackFactory.kt index 52da347e..4fe48fc6 100644 --- a/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/FastItemStackFactory.kt +++ b/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/FastItemStackFactory.kt @@ -2,7 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_18_R1 import com.willfp.eco.core.fast.FastItemStack import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy -import com.willfp.eco.internal.spigot.proxy.common.fast.EcoFastItemStack +import com.willfp.eco.internal.spigot.proxy.common.item.EcoFastItemStack import org.bukkit.inventory.ItemStack class FastItemStackFactory : FastItemStackFactoryProxy { diff --git a/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/FastItemStackFactory.kt b/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/FastItemStackFactory.kt index 084a4d17..4df3ff09 100644 --- a/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/FastItemStackFactory.kt +++ b/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/FastItemStackFactory.kt @@ -2,7 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_18_R2 import com.willfp.eco.core.fast.FastItemStack import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy -import com.willfp.eco.internal.spigot.proxy.common.fast.EcoFastItemStack +import com.willfp.eco.internal.spigot.proxy.common.item.EcoFastItemStack import org.bukkit.inventory.ItemStack class FastItemStackFactory : FastItemStackFactoryProxy {