Skip to content

Commit

Permalink
Fixed FastItemStack on 1.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed May 11, 2024
1 parent bff502c commit 697e0b7
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public interface ExtendedPersistentDataContainer {
* @return The extended container.
*/
static ExtendedPersistentDataContainer extend(@NotNull PersistentDataContainer base) {
if (base instanceof ExtendedPersistentDataContainer) {
return (ExtendedPersistentDataContainer) base;
}

return Eco.get().adaptPdc(base);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_17_R1

import com.willfp.eco.core.data.ExtendedPersistentDataContainer
import com.willfp.eco.internal.spigot.proxy.ExtendedPersistentDataContainerFactoryProxy
import com.willfp.eco.internal.spigot.proxy.common.item.ContinuallyAppliedPersistentDataContainer
import net.minecraft.nbt.Tag
import org.bukkit.Material
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack
Expand All @@ -28,7 +29,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance is not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_18_R1

import com.willfp.eco.core.data.ExtendedPersistentDataContainer
import com.willfp.eco.internal.spigot.proxy.ExtendedPersistentDataContainerFactoryProxy
import com.willfp.eco.internal.spigot.proxy.common.item.ContinuallyAppliedPersistentDataContainer
import net.minecraft.nbt.Tag
import org.bukkit.Material
import org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack
Expand All @@ -28,7 +29,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance is not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_18_R2

import com.willfp.eco.core.data.ExtendedPersistentDataContainer
import com.willfp.eco.internal.spigot.proxy.ExtendedPersistentDataContainerFactoryProxy
import com.willfp.eco.internal.spigot.proxy.common.item.ContinuallyAppliedPersistentDataContainer
import net.minecraft.nbt.Tag
import org.bukkit.Material
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack
Expand All @@ -28,7 +29,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance is not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance ims not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_19_R2

import com.willfp.eco.core.data.ExtendedPersistentDataContainer
import com.willfp.eco.internal.spigot.proxy.ExtendedPersistentDataContainerFactoryProxy
import com.willfp.eco.internal.spigot.proxy.common.item.ContinuallyAppliedPersistentDataContainer
import net.minecraft.nbt.Tag
import org.bukkit.Material
import org.bukkit.craftbukkit.v1_19_R2.inventory.CraftItemStack
Expand All @@ -28,7 +29,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance ims not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance ims not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance ims not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ class FastItemStackFactory : FastItemStackFactoryProxy {
@Suppress("USELESS_CAST")
private val handle = bukkit.asNMSStack() as net.minecraft.world.item.ItemStack

private val pdc = if (handle.has(DataComponents.CUSTOM_DATA)) {
handle.get(DataComponents.CUSTOM_DATA)!!.copyTag().makePdc()
} else {
val tag = CompoundTag()
handle.set(DataComponents.CUSTOM_DATA, CustomData.of(tag))
tag.makePdc()
}
private val pdc = (handle.get(DataComponents.CUSTOM_DATA)?.copyTag() ?: CompoundTag()).makePdc()

override fun getEnchants(checkStored: Boolean): Map<Enchantment, Int> {
val enchantments = handle.get(DataComponents.ENCHANTMENTS) ?: return emptyMap()
Expand Down Expand Up @@ -340,6 +334,7 @@ class FastItemStackFactory : FastItemStackFactoryProxy {

override fun setAmount(amount: Int) {
handle.setCount(amount)
apply()
}

override fun setType(material: org.bukkit.Material) {
Expand Down Expand Up @@ -376,11 +371,15 @@ class FastItemStackFactory : FastItemStackFactoryProxy {
}

override fun apply() {
val customData = handle.get(DataComponents.CUSTOM_DATA)
if (customData != null) {
val tag = customData.copyTag()
tag.setPdc(pdc)
handle.set(DataComponents.CUSTOM_DATA, CustomData.of(tag))
val customData = handle.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY)
val updated = customData.update {
it.setPdc(pdc)
}

if (updated.isEmpty) {
handle.remove(DataComponents.CUSTOM_DATA)
} else {
handle.set(DataComponents.CUSTOM_DATA, updated)
}

bukkit.mergeIfNeeded(handle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance ims not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance ims not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_20_R3

import com.willfp.eco.core.data.ExtendedPersistentDataContainer
import com.willfp.eco.internal.spigot.proxy.ExtendedPersistentDataContainerFactoryProxy
import com.willfp.eco.internal.spigot.proxy.common.item.ContinuallyAppliedPersistentDataContainer
import net.minecraft.nbt.Tag
import org.bukkit.Material
import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack
Expand All @@ -28,7 +29,7 @@ class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFa
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
return when (pdc) {
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
else -> throw IllegalArgumentException("Custom PDC instance ims not supported!")
else -> throw IllegalArgumentException("Custom PDC instance ${pdc::class.java.name} is not supported!")
}
}

Expand Down

0 comments on commit 697e0b7

Please sign in to comment.