-
in e.g. abstract class NamedBinaryTag<T : Any, Z : Any> : PersistentDataType<T, Z> {
abstract fun toPrimitive(complex: Z): T
override fun toPrimitive(complex: Z, context: PersistentDataAdapterContext): T {
return toPrimitive(complex)
}
abstract fun fromPrimitive(primitive: T): Z
override fun fromPrimitive(primitive: T, context: PersistentDataAdapterContext): Z {
return fromPrimitive(primitive)
}
}
class _SERIALIZABLE<T : Serializable>(private val type: Class<T>) : NamedBinaryTag<ByteArray, T>() {
override fun getPrimitiveType(): Class<ByteArray> {
return ByteArray::class.java
}
override fun getComplexType(): Class<T> {
return this.type
}
override fun toPrimitive(complex: T): ByteArray {
val bos = ByteArrayOutputStream()
val oos = ObjectOutputStream(bos)
oos.writeObject(complex)
oos.flush()
return bos.toByteArray()
}
override fun fromPrimitive(primitive: ByteArray): T {
val bis = ByteArrayInputStream(primitive)
val ois = ObjectInputStream(bis)
@Suppress("UNCHECKED_CAST")
return ois.readObject() as T
}
} certainly, current im very new to can i have some examples of how to achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can use This is only in answer to your byte array question, feel free to ask if you wonder about another specific type |
Beta Was this translation helpful? Give feedback.
The Tag API is ultimately a way to interact with a NBT Compound, so you can do anything using
Tag.NBT
and there is a dedicated Byte Array type present in the format: https://minecraft.fandom.com/wiki/NBT_format#Data_typesYou would have to do something like
For enums we do not have an util method for it, you have to choose between serializing the enum name using
Tag.String
, or its ordinal withTag.Integer