From c7729af67f6bcd2a8815345a65f2beb1f2293db0 Mon Sep 17 00:00:00 2001 From: Username404 Date: Thu, 20 May 2021 14:24:13 +0200 Subject: [PATCH] Add a @Ignore annotation, make Macro.command a jvm field and add a isValidForButtonCollection function to Snowy.kt --- common/src/main/kotlin/fr/username404/snowygui/Snowy.kt | 3 +++ .../kotlin/fr/username404/snowygui/gui/feature/ButtonInfo.kt | 5 +++++ .../main/kotlin/fr/username404/snowygui/gui/feature/Macro.kt | 3 ++- .../main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt | 2 +- .../main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt | 2 +- 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index c504471..a004421 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -4,12 +4,15 @@ import com.google.gson.Gson import com.google.gson.GsonBuilder import fr.username404.snowygui.EventSnowy.Companion.useKey import fr.username404.snowygui.config.Configuration.obtained +import fr.username404.snowygui.gui.feature.Ignored import fr.username404.snowygui.misc.AddKeyMaps import io.github.config4k.getValue import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.Logger +import kotlin.reflect.full.hasAnnotation abstract class Snowy { + protected fun Class<*>.isValidForButtonCollection(): Boolean = (!(kotlin.isAbstract || kotlin.hasAnnotation())) private val displayInitMessage: Boolean by obtained companion object { val Gson: Gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonInfo.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonInfo.kt index e0bca79..6b6a72a 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonInfo.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonInfo.kt @@ -26,3 +26,8 @@ annotation class ButtonInfo( MACROS("snowy.clickbox.macros", Colors.GREEN); init { ClickGui.components.add(ClickBox(y = 4.0, name = TranslatableComponent(translationKey), color = categoryColor)) } } + +/** + * Excludes a class from button collection + */ +annotation class Ignored \ No newline at end of file diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Macro.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Macro.kt index 3a68974..a069bfc 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Macro.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Macro.kt @@ -4,9 +4,10 @@ import com.google.gson.annotations.Expose import net.minecraft.client.Minecraft import fr.username404.snowygui.gui.feature.ButtonInfo.Companion.Type +@Ignored @ButtonInfo(Category.MACROS, kind = Type.CLICK) data class Macro( - @Expose private val command: String, + @Expose @JvmField private val command: String, @Expose override val title: String = command ): ButtonImpl() { override fun execAction() = Minecraft.getInstance().player!!.chat("/$command") diff --git a/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt b/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt index 9d6433a..22176ac 100644 --- a/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt +++ b/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt @@ -29,6 +29,6 @@ class FabricInit: Snowy(), ClientModInitializer { .setUrls(ClasspathHelper.forPackage(FeaturePackage)) .setScanners(SubTypesScanner(false), TypeAnnotationsScanner()) .useParallelExecutor() - ).getTypesAnnotatedWith(ButtonInfo::class.java).filterNot { it.kotlin.isAbstract || it.kotlin.isData; }.toSet() + ).getTypesAnnotatedWith(ButtonInfo::class.java).filter { it.isValidForButtonCollection() }.toSet() } } \ No newline at end of file diff --git a/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt b/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt index 1dec3c6..3b25b8e 100644 --- a/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt +++ b/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt @@ -42,7 +42,7 @@ class ForgeInit: Snowy() { (data!!.javaClass.getDeclaredField("clazz").apply { isAccessible = true }.get(data) as Type).className.let { classname -> classname.startsWith(FeaturePackage).let { it && with(Class.forName(classname)) { - (!(kotlin.isAbstract || kotlin.isData)) && declaredAnnotations.any { annotation -> + isValidForButtonCollection() && declaredAnnotations.any { annotation -> annotation is ButtonInfo } }