Added ExtendedPersistentDataContainer
This commit is contained in:
@@ -108,21 +108,25 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
return compound
|
||||
}
|
||||
|
||||
pdc as CraftPersistentDataContainer?
|
||||
val container = when (pdc) {
|
||||
is CraftPersistentDataContainer? -> pdc
|
||||
is ExtendedPersistentDataContainerFactory.EcoPersistentDataContainer? -> pdc?.handle
|
||||
else -> null
|
||||
}
|
||||
|
||||
if (item != null) {
|
||||
if (pdc != null && !pdc.isEmpty) {
|
||||
if (container != null && !container.isEmpty) {
|
||||
for (key in tag.allKeys.toSet()) {
|
||||
tag.remove(key)
|
||||
}
|
||||
|
||||
tag.merge(pdc.toTag())
|
||||
tag.merge(container.toTag())
|
||||
} else {
|
||||
item.setTag(null)
|
||||
}
|
||||
} else {
|
||||
if (pdc != null && !pdc.isEmpty) {
|
||||
tag.put("PublicBukkitValues", pdc.toTag())
|
||||
if (container != null && !container.isEmpty) {
|
||||
tag.put("PublicBukkitValues", container.toTag())
|
||||
} else {
|
||||
tag.remove("PublicBukkitValues")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
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 net.minecraft.nbt.Tag
|
||||
import org.bukkit.craftbukkit.v1_17_R1.persistence.CraftPersistentDataContainer
|
||||
import org.bukkit.craftbukkit.v1_17_R1.persistence.CraftPersistentDataTypeRegistry
|
||||
import org.bukkit.persistence.PersistentDataContainer
|
||||
import org.bukkit.persistence.PersistentDataType
|
||||
|
||||
class ExtendedPersistentDataContainerFactory: ExtendedPersistentDataContainerFactoryProxy {
|
||||
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
|
||||
return when (pdc) {
|
||||
is ExtendedPersistentDataContainer -> pdc
|
||||
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
|
||||
else -> throw IllegalArgumentException("Custom PDC instance is not supported!")
|
||||
}
|
||||
}
|
||||
|
||||
class EcoPersistentDataContainer(
|
||||
val handle: CraftPersistentDataContainer
|
||||
) : ExtendedPersistentDataContainer, PersistentDataContainer by handle {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private val customDataTags: MutableMap<String, Tag> =
|
||||
CraftPersistentDataContainer::class.java.getDeclaredField("customDataTags")
|
||||
.apply { isAccessible = true }.get(handle) as MutableMap<String, Tag>
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private val registry: CraftPersistentDataTypeRegistry =
|
||||
CraftPersistentDataContainer::class.java.getDeclaredField("registry")
|
||||
.apply { isAccessible = true }.get(handle) as CraftPersistentDataTypeRegistry
|
||||
|
||||
override fun <T : Any, Z : Any> set(key: String, dataType: PersistentDataType<T, Z>, value: Z) {
|
||||
customDataTags[key] = registry.wrap(dataType.primitiveType, dataType.toPrimitive(value, adapterContext))
|
||||
}
|
||||
|
||||
override fun <T : Any, Z : Any> has(key: String, dataType: PersistentDataType<T, Z>): Boolean {
|
||||
val value = customDataTags[key] ?: return false
|
||||
return registry.isInstanceOf(dataType.primitiveType, value)
|
||||
}
|
||||
|
||||
override fun <T : Any, Z : Any> get(key: String, dataType: PersistentDataType<T, Z>): Z? {
|
||||
val value = customDataTags[key] ?: return null
|
||||
return dataType.fromPrimitive(registry.extract(dataType.primitiveType, value), adapterContext)
|
||||
}
|
||||
|
||||
override fun <T : Any, Z : Any> getOrDefault(
|
||||
key: String,
|
||||
dataType: PersistentDataType<T, Z>,
|
||||
defaultValue: Z
|
||||
): Z {
|
||||
return get(key, dataType) ?: defaultValue
|
||||
}
|
||||
|
||||
override fun remove(key: String) {
|
||||
customDataTags.remove(key)
|
||||
}
|
||||
|
||||
override fun getAllKeys(): MutableSet<String> {
|
||||
return customDataTags.keys
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,21 +108,25 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
return compound
|
||||
}
|
||||
|
||||
pdc as CraftPersistentDataContainer?
|
||||
val container = when (pdc) {
|
||||
is CraftPersistentDataContainer? -> pdc
|
||||
is ExtendedPersistentDataContainerFactory.EcoPersistentDataContainer? -> pdc?.handle
|
||||
else -> null
|
||||
}
|
||||
|
||||
if (item != null) {
|
||||
if (pdc != null && !pdc.isEmpty) {
|
||||
if (container != null && !container.isEmpty) {
|
||||
for (key in tag.allKeys.toSet()) {
|
||||
tag.remove(key)
|
||||
}
|
||||
|
||||
tag.merge(pdc.toTag())
|
||||
tag.merge(container.toTag())
|
||||
} else {
|
||||
item.setTag(null)
|
||||
}
|
||||
} else {
|
||||
if (pdc != null && !pdc.isEmpty) {
|
||||
tag.put("PublicBukkitValues", pdc.toTag())
|
||||
if (container != null && !container.isEmpty) {
|
||||
tag.put("PublicBukkitValues", container.toTag())
|
||||
} else {
|
||||
tag.remove("PublicBukkitValues")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
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 net.minecraft.nbt.Tag
|
||||
import org.bukkit.craftbukkit.v1_18_R1.persistence.CraftPersistentDataContainer
|
||||
import org.bukkit.craftbukkit.v1_18_R1.persistence.CraftPersistentDataTypeRegistry
|
||||
import org.bukkit.persistence.PersistentDataContainer
|
||||
import org.bukkit.persistence.PersistentDataType
|
||||
|
||||
class ExtendedPersistentDataContainerFactory : ExtendedPersistentDataContainerFactoryProxy {
|
||||
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
|
||||
return when (pdc) {
|
||||
is ExtendedPersistentDataContainer -> pdc
|
||||
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
|
||||
else -> throw IllegalArgumentException("Custom PDC instance is not supported!")
|
||||
}
|
||||
}
|
||||
|
||||
class EcoPersistentDataContainer(
|
||||
val handle: CraftPersistentDataContainer
|
||||
) : ExtendedPersistentDataContainer, PersistentDataContainer by handle {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private val customDataTags: MutableMap<String, Tag> =
|
||||
CraftPersistentDataContainer::class.java.getDeclaredField("customDataTags")
|
||||
.apply { isAccessible = true }.get(handle) as MutableMap<String, Tag>
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private val registry: CraftPersistentDataTypeRegistry =
|
||||
CraftPersistentDataContainer::class.java.getDeclaredField("registry")
|
||||
.apply { isAccessible = true }.get(handle) as CraftPersistentDataTypeRegistry
|
||||
|
||||
override fun <T : Any, Z : Any> set(key: String, dataType: PersistentDataType<T, Z>, value: Z) {
|
||||
customDataTags[key] = registry.wrap(dataType.primitiveType, dataType.toPrimitive(value, adapterContext))
|
||||
}
|
||||
|
||||
override fun <T : Any, Z : Any> has(key: String, dataType: PersistentDataType<T, Z>): Boolean {
|
||||
val value = customDataTags[key] ?: return false
|
||||
return registry.isInstanceOf(dataType.primitiveType, value)
|
||||
}
|
||||
|
||||
override fun <T : Any, Z : Any> get(key: String, dataType: PersistentDataType<T, Z>): Z? {
|
||||
val value = customDataTags[key] ?: return null
|
||||
return dataType.fromPrimitive(registry.extract(dataType.primitiveType, value), adapterContext)
|
||||
}
|
||||
|
||||
override fun <T : Any, Z : Any> getOrDefault(
|
||||
key: String,
|
||||
dataType: PersistentDataType<T, Z>,
|
||||
defaultValue: Z
|
||||
): Z {
|
||||
return get(key, dataType) ?: defaultValue
|
||||
}
|
||||
|
||||
override fun remove(key: String) {
|
||||
customDataTags.remove(key)
|
||||
}
|
||||
|
||||
override fun getAllKeys(): MutableSet<String> {
|
||||
return customDataTags.keys
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,21 +108,25 @@ class CommonsInitializer : CommonsInitializerProxy {
|
||||
return compound
|
||||
}
|
||||
|
||||
pdc as CraftPersistentDataContainer?
|
||||
val container = when (pdc) {
|
||||
is CraftPersistentDataContainer? -> pdc
|
||||
is ExtendedPersistentDataContainerFactory.EcoPersistentDataContainer? -> pdc?.handle
|
||||
else -> null
|
||||
}
|
||||
|
||||
if (item != null) {
|
||||
if (pdc != null && !pdc.isEmpty) {
|
||||
if (container != null && !container.isEmpty) {
|
||||
for (key in tag.allKeys.toSet()) {
|
||||
tag.remove(key)
|
||||
}
|
||||
|
||||
tag.merge(pdc.toTag())
|
||||
tag.merge(container.toTag())
|
||||
} else {
|
||||
item.setTag(null)
|
||||
}
|
||||
} else {
|
||||
if (pdc != null && !pdc.isEmpty) {
|
||||
tag.put("PublicBukkitValues", pdc.toTag())
|
||||
if (container != null && !container.isEmpty) {
|
||||
tag.put("PublicBukkitValues", container.toTag())
|
||||
} else {
|
||||
tag.remove("PublicBukkitValues")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
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 net.minecraft.nbt.Tag
|
||||
import org.bukkit.craftbukkit.v1_18_R2.persistence.CraftPersistentDataContainer
|
||||
import org.bukkit.craftbukkit.v1_18_R2.persistence.CraftPersistentDataTypeRegistry
|
||||
import org.bukkit.persistence.PersistentDataContainer
|
||||
import org.bukkit.persistence.PersistentDataType
|
||||
|
||||
class ExtendedPersistentDataContainerFactory: ExtendedPersistentDataContainerFactoryProxy {
|
||||
override fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer {
|
||||
return when (pdc) {
|
||||
is ExtendedPersistentDataContainer -> pdc
|
||||
is CraftPersistentDataContainer -> EcoPersistentDataContainer(pdc)
|
||||
else -> throw IllegalArgumentException("Custom PDC instance is not supported!")
|
||||
}
|
||||
}
|
||||
|
||||
class EcoPersistentDataContainer(
|
||||
val handle: CraftPersistentDataContainer
|
||||
) : ExtendedPersistentDataContainer, PersistentDataContainer by handle {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private val customDataTags: MutableMap<String, Tag> =
|
||||
CraftPersistentDataContainer::class.java.getDeclaredField("customDataTags")
|
||||
.apply { isAccessible = true }.get(handle) as MutableMap<String, Tag>
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private val registry: CraftPersistentDataTypeRegistry =
|
||||
CraftPersistentDataContainer::class.java.getDeclaredField("registry")
|
||||
.apply { isAccessible = true }.get(handle) as CraftPersistentDataTypeRegistry
|
||||
|
||||
override fun <T : Any, Z : Any> set(key: String, dataType: PersistentDataType<T, Z>, value: Z) {
|
||||
customDataTags[key] = registry.wrap(dataType.primitiveType, dataType.toPrimitive(value, adapterContext))
|
||||
}
|
||||
|
||||
override fun <T : Any, Z : Any> has(key: String, dataType: PersistentDataType<T, Z>): Boolean {
|
||||
val value = customDataTags[key] ?: return false
|
||||
return registry.isInstanceOf(dataType.primitiveType, value)
|
||||
}
|
||||
|
||||
override fun <T : Any, Z : Any> get(key: String, dataType: PersistentDataType<T, Z>): Z? {
|
||||
val value = customDataTags[key] ?: return null
|
||||
return dataType.fromPrimitive(registry.extract(dataType.primitiveType, value), adapterContext)
|
||||
}
|
||||
|
||||
override fun <T : Any, Z : Any> getOrDefault(
|
||||
key: String,
|
||||
dataType: PersistentDataType<T, Z>,
|
||||
defaultValue: Z
|
||||
): Z {
|
||||
return get(key, dataType) ?: defaultValue
|
||||
}
|
||||
|
||||
override fun remove(key: String) {
|
||||
customDataTags.remove(key)
|
||||
}
|
||||
|
||||
override fun getAllKeys(): MutableSet<String> {
|
||||
return customDataTags.keys
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.internal.spigot
|
||||
import com.willfp.eco.core.EcoPlugin
|
||||
import com.willfp.eco.core.Handler
|
||||
import com.willfp.eco.core.PluginProps
|
||||
import com.willfp.eco.core.data.ExtendedPersistentDataContainer
|
||||
import com.willfp.eco.core.entities.ai.EntityController
|
||||
import com.willfp.eco.core.fast.FastItemStack
|
||||
import com.willfp.eco.core.integrations.placeholder.PlaceholderIntegration
|
||||
@@ -32,6 +33,7 @@ import com.willfp.eco.internal.spigot.integrations.bstats.MetricHandler
|
||||
import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.DummyEntityFactoryProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.EntityControllerFactoryProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.ExtendedPersistentDataContainerFactoryProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.MiniMessageTranslatorProxy
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences
|
||||
@@ -40,6 +42,7 @@ import org.bukkit.NamespacedKey
|
||||
import org.bukkit.entity.Entity
|
||||
import org.bukkit.entity.Mob
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.persistence.PersistentDataContainer
|
||||
import java.util.logging.Logger
|
||||
|
||||
@Suppress("UNUSED")
|
||||
@@ -150,4 +153,7 @@ class EcoHandler : EcoSpigotPlugin(), Handler {
|
||||
|
||||
override fun formatMiniMessage(message: String): String =
|
||||
getProxy(MiniMessageTranslatorProxy::class.java).format(message)
|
||||
|
||||
override fun adaptPdc(container: PersistentDataContainer): ExtendedPersistentDataContainer =
|
||||
getProxy(ExtendedPersistentDataContainerFactoryProxy::class.java).adapt(container)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.willfp.eco.internal.spigot.proxy
|
||||
|
||||
import com.willfp.eco.core.data.ExtendedPersistentDataContainer
|
||||
import org.bukkit.persistence.PersistentDataContainer
|
||||
|
||||
interface ExtendedPersistentDataContainerFactoryProxy {
|
||||
fun adapt(pdc: PersistentDataContainer): ExtendedPersistentDataContainer
|
||||
}
|
||||
Reference in New Issue
Block a user