Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-7598

ListPersistentDataType can't have a complex type which isn't a list

XMLWordPrintable

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Minor Minor
    • None
    • None
    • None
    • 4056-Spigot-e9ec548-176c0ad (MC: 1.20.4) (Implementing API version 1.20.4-R0.1-SNAPSHOT)
    • Yes

      PersistentDataTypes can be implemented for the role of converter, like BooleanPersistentDataTypes which convert booleans(complex type) to bytes(primitive type).

      However, ListPersistentDataTypes only support List for complex type, and Spigot expects PersistentDataTypes with List primitive type must be a ListPersistentDataType.

      Therefore, it's impossible to implement PersistentDataTypes with List primitive type and Collection complex type like Set, Map, and so on.

      Here's my example code (kotlin):

      @Suppress("UNCHECKED_CAST")
      class MapPersistentDataType<K : Any, V : Any>(
          private val listDataType: ListPersistentDataType<PersistentDataContainer, Pair<K, V>>,
      ) : PersistentDataType<List<PersistentDataContainer>, Map<K, V>> {
      
          override fun getPrimitiveType(): Class<List<PersistentDataContainer>> =
              List::class.java as Class<List<PersistentDataContainer>>
      
          override fun getComplexType(): Class<Map<K, V>> =
              Map::class.java as Class<Map<K, V>>
      
          override fun toPrimitive(
              complex: Map<K, V>,
              context: PersistentDataAdapterContext,
          ): List<PersistentDataContainer> =
              listDataType.toPrimitive(complex.toList(), context)
      
          override fun fromPrimitive(
              primitive: List<PersistentDataContainer>,
              context: PersistentDataAdapterContext,
          ): Map<K, V> =
              listDataType.fromPrimitive(primitive, context).toMap()
      } 

       

      The error occurs when using it (persistentDataContainer.get(key, dataType)):

      [10:56:10] [Server thread/WARN]: java.lang.IllegalArgumentException: The found tag instance (NBTTagList) cannot store List
      [10:56:10] [Server thread/WARN]:     at com.google.common.base.Preconditions.checkArgument(Preconditions.java:445)
      [10:56:10] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_20_R3.persistence.CraftPersistentDataTypeRegistry.extract(CraftPersistentDataTypeRegistry.java:348)
      [10:56:10] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_20_R3.persistence.CraftPersistentDataContainer.get(CraftPersistentDataContainer.java:74)
       

            Unassigned Unassigned
            woodyn1002 WooJin Choi
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: