From a99dab51b4cacc7294961a3a46313c03af2db61e Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 29 Jan 2023 23:19:40 +0700 Subject: [PATCH] fix enums --- .../mc/otm/core/util/DataStreams.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/DataStreams.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/DataStreams.kt index 516a47e4d..83ca6a005 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/DataStreams.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/DataStreams.kt @@ -76,7 +76,7 @@ val VarLongValueCodec = StreamCodec(DataInputStream::readVarLongLE, DataOutputSt val BinaryStringCodec = StreamCodec(DataInputStream::readBinaryString, DataOutputStream::writeBinaryString) class EnumValueCodec>(clazz: Class, val writeByIndices: Boolean = false) : IStreamCodec { - private val values = clazz.enumConstants ?: throw ClassCastException("$clazz does not have enum constants. Not an enum?") + private val values = search(clazz) ?: throw ClassCastException("$clazz does not have enum constants. Not an enum?") override fun read(stream: DataInputStream): V { if (writeByIndices) { @@ -102,6 +102,23 @@ class EnumValueCodec>(clazz: Class, val writeByIndices: Boole override fun compare(a: V, b: V): Boolean { return a === b } + + companion object { + /** + * FIXME: enums with abstract methods which get compiled to subclasses, whose DO NOT expose "parent's" enum constants array + * + * is there an already existing solution? + */ + fun > search(clazz: Class): Array? { + var search: Class<*> = clazz + + while (search.enumConstants == null && search.superclass != null) { + search = search.superclass + } + + return search.enumConstants as? Array + } + } } fun OutputStream.writeInt(value: Int) {