aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/test')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/test/TestExportTools.kt59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/test/TestExportTools.kt b/src/main/java/at/hannibal2/skyhanni/test/TestExportTools.kt
new file mode 100644
index 000000000..7ac530452
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/test/TestExportTools.kt
@@ -0,0 +1,59 @@
+package at.hannibal2.skyhanni.test
+
+import at.hannibal2.skyhanni.SkyHanniMod
+import at.hannibal2.skyhanni.utils.*
+import com.google.gson.GsonBuilder
+import com.google.gson.JsonElement
+import net.minecraft.client.gui.inventory.GuiContainer
+import net.minecraft.item.ItemStack
+import net.minecraft.nbt.NBTTagCompound
+import net.minecraftforge.client.event.GuiScreenEvent
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import org.lwjgl.input.Keyboard
+import java.io.InputStreamReader
+import java.io.Reader
+
+object TestExportTools {
+ val gson = GsonBuilder()
+ .registerTypeAdapterFactory(KotlinTypeAdapterFactory())
+ .registerTypeAdapter(NBTTagCompound::class.java, NBTTypeAdapter)
+ .registerTypeAdapterFactory(ItemStackTypeAdapterFactory)
+ .create()
+
+ class Key<T> internal constructor(val name: String)
+
+ val Item = Key<ItemStack>("Item")
+
+ @KSerializable
+ data class TestValue(
+ val type: String,
+ val data: JsonElement,
+ )
+
+ fun <T> toJson(key: Key<T>, value: T): String {
+ return gson.toJson(TestValue(key.name, gson.toJsonTree(value)))
+ }
+
+ inline fun <reified T> fromJson(key: Key<T>, reader: Reader): T {
+ val serializable = gson.fromJson<TestValue>(reader)
+ require(key.name == serializable.type)
+ return gson.fromJson(serializable.data)
+ }
+
+ @SubscribeEvent
+ fun onKeybind(event: GuiScreenEvent.KeyboardInputEvent) {
+ if (!Keyboard.getEventKeyState() || Keyboard.getEventKey() != SkyHanniMod.feature.dev.copyNBTDataCompressed) return
+ val gui = event.gui as? GuiContainer ?: return
+ val focussedSlot = gui.slotUnderMouse ?: return
+ val stack = focussedSlot.stack ?: return
+ val json = toJson(Item, stack)
+ OSUtils.copyToClipboard(json)
+ LorenzUtils.chat("Copied test importable to clipbooard")
+ }
+
+
+ inline fun <reified T> getTestData(category: Key<T>, name: String): T {
+ val reader = InputStreamReader(javaClass.getResourceAsStream("/testdata/${category.name}/$name.json")!!)
+ return fromJson(category, reader)
+ }
+} \ No newline at end of file