From 427fd43ea050d3c9df8f97096c2a27c1e3dbc92b Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 1 Sep 2022 19:32:24 +0700 Subject: [PATCH] CompoundTag.map and CompoundTag.ifCompound --- src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt index 4e8ea0863..e71a7cbed 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/Ext.kt @@ -111,6 +111,26 @@ operator fun IItemHandler.get(index: Int): ItemStack = getStackInSlot(index) operator fun JsonObject.set(s: String, value: JsonElement) = add(s, value) +inline fun CompoundTag.map(s: String, consumer: (T) -> R): R? { + val tag = get(s) + + if (tag is T) { + return consumer(tag) + } + + return null +} + +inline fun CompoundTag.ifCompound(s: String, consumer: (CompoundTag) -> T): T? { + val tag = get(s) + + if (tag is CompoundTag) { + return consumer(tag) + } + + return null +} + inline fun CompoundTag.ifHas(s: String, consumer: (Tag) -> Unit) { val tag = get(s)