feat: Add item type function
This commit is contained in:
@@ -131,6 +131,7 @@ object AbilityUtils {
|
||||
return abilities
|
||||
}
|
||||
|
||||
// TODO: memoize
|
||||
fun getAbilities(itemStack: ItemStack): List<ItemAbility> {
|
||||
return getAbilities(itemStack.loreAccordingToNbt)
|
||||
}
|
||||
|
||||
@@ -1,21 +1,41 @@
|
||||
package moe.nea.firmament.util.skyblock
|
||||
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
import kotlin.reflect.KProperty
|
||||
import net.minecraft.item.ItemStack
|
||||
import moe.nea.firmament.util.directLiteralStringContent
|
||||
import moe.nea.firmament.util.mc.loreAccordingToNbt
|
||||
import moe.nea.firmament.util.petData
|
||||
|
||||
|
||||
class ItemType(val name: String) {
|
||||
@JvmInline
|
||||
value class ItemType private constructor(val name: String) {
|
||||
companion object {
|
||||
private val generated = object : ReadOnlyProperty<Any?, ItemType> {
|
||||
override fun getValue(thisRef: Any?, property: KProperty<*>): ItemType {
|
||||
return ItemType.ofName(property.name)
|
||||
}
|
||||
}
|
||||
|
||||
fun ofName(name: String): ItemType {
|
||||
return ItemType(name)
|
||||
}
|
||||
|
||||
val SWORD by generated
|
||||
fun fromItemStack(itemStack: ItemStack): ItemType? {
|
||||
if (itemStack.petData != null)
|
||||
return PET
|
||||
val typeText =
|
||||
itemStack.loreAccordingToNbt.lastOrNull()
|
||||
?.siblings?.find {
|
||||
!it.style.isObfuscated && !it.directLiteralStringContent.isNullOrBlank()
|
||||
}?.directLiteralStringContent
|
||||
if (typeText != null) {
|
||||
val type = typeText.substringAfter(' ', missingDelimiterValue = "").trim()
|
||||
if (type.isEmpty()) return null
|
||||
return ofName(type)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
val SWORD = ofName("SWORD")
|
||||
val DRILL = ofName("DRILL")
|
||||
val PICKAXE = ofName("PICKAXE")
|
||||
|
||||
/**
|
||||
* This one is not really official (it never shows up in game).
|
||||
*/
|
||||
val PET = ofName("PET")
|
||||
}
|
||||
}
|
||||
|
||||
53
src/test/kotlin/util/skyblock/ItemTypeTest.kt
Normal file
53
src/test/kotlin/util/skyblock/ItemTypeTest.kt
Normal file
@@ -0,0 +1,53 @@
|
||||
package moe.nea.firmament.test.util.skyblock
|
||||
|
||||
import io.kotest.core.spec.style.AnnotationSpec
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import moe.nea.firmament.test.testutil.ItemResources
|
||||
import moe.nea.firmament.util.skyblock.ItemType
|
||||
|
||||
class ItemTypeTest : AnnotationSpec() {
|
||||
@Test
|
||||
fun testPetItem() {
|
||||
Assertions.assertEquals(
|
||||
ItemType.PET,
|
||||
ItemType.fromItemStack(ItemResources.loadItem("pets/lion-item"))
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPetInUI() {
|
||||
Assertions.assertEquals(
|
||||
ItemType.PET,
|
||||
ItemType.fromItemStack(ItemResources.loadItem("pets/rabbit-selected"))
|
||||
)
|
||||
Assertions.assertEquals(
|
||||
ItemType.PET,
|
||||
ItemType.fromItemStack(ItemResources.loadItem("pets/mithril-golem-not-selected"))
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAOTV() {
|
||||
Assertions.assertEquals(
|
||||
ItemType.SWORD,
|
||||
ItemType.fromItemStack(ItemResources.loadItem("aspect-of-the-void"))
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDrill() {
|
||||
Assertions.assertEquals(
|
||||
ItemType.DRILL,
|
||||
ItemType.fromItemStack(ItemResources.loadItem("titanium-drill"))
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPickaxe() {
|
||||
Assertions.assertEquals(
|
||||
ItemType.PICKAXE,
|
||||
ItemType.fromItemStack(ItemResources.loadItem("diamond-pickaxe"))
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user