From 84d4e811e27b8ea07d5760c9c2a85540bcccef44 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Thu, 10 Jul 2025 15:54:46 +0200 Subject: [PATCH] get item type via name --- .../dependency/datapack/DataPackDependency.kt | 24 +++++++-------- .../cuanvil/group/ItemGroupManager.kt | 30 ++++++------------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/datapack/DataPackDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/datapack/DataPackDependency.kt index ae1155a..f853a02 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/datapack/DataPackDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/datapack/DataPackDependency.kt @@ -2,8 +2,9 @@ package xyz.alexcrea.cuanvil.dependency.datapack import io.delilaheve.CustomAnvil import io.papermc.paper.datapack.Datapack +import io.papermc.paper.registry.RegistryAccess +import io.papermc.paper.registry.RegistryKey import org.bukkit.Bukkit -import org.bukkit.Material import org.bukkit.NamespacedKey import org.bukkit.configuration.file.FileConfiguration import org.bukkit.configuration.file.YamlConfiguration @@ -135,8 +136,7 @@ object DataPackDependency { // Order matter for this file // Could rewrite to not matter but not really important, so I keep it like that private fun handleItemGroups(yml: YamlConfiguration) { - //TODO see below todo - //val itemRegistry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ITEM) + val itemRegistry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ITEM) for (groupName in yml.getKeys(false)) { val section = yml.getConfigurationSection(groupName) ?: continue @@ -147,19 +147,15 @@ object DataPackDependency { if (group == null) group = IncludeGroup(groupName) for (name in section.getStringList("items")) { - //TODO get item key from - /*val key = NamespacedKey.fromString(name.lowercase()) - if (key == null) throw IllegalStateException("Invalid item type: " + name) - val type = itemRegistry.get(key)*/ + val key = NamespacedKey.fromString(name.lowercase()) + if (key == null) throw IllegalStateException("Invalid item type: $name") - val mat = Material.getMaterial(name.uppercase()) - - - if (mat == null) { - CustomAnvil.instance.logger.warning("Could not find material $name for item group $groupName") - continue + val type = itemRegistry.get(key) + if (type == null) { + throw IllegalStateException("Could not find item type $name for item group $groupName") } - group.addToPolicy(mat.asItemType()!!) + + group.addToPolicy(type) } for (name in section.getStringList("groups")) { val otherGroup = MaterialGroupApi.getGroup(name) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt index 87aa2c6..815c778 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt @@ -1,10 +1,13 @@ package xyz.alexcrea.cuanvil.group import io.delilaheve.CustomAnvil -import org.bukkit.Material +import io.papermc.paper.registry.RegistryAccess +import io.papermc.paper.registry.RegistryKey +import org.bukkit.NamespacedKey import org.bukkit.configuration.ConfigurationSection import java.util.* +@SuppressWarnings("UnstableApiUsage") class ItemGroupManager { companion object { @@ -76,14 +79,12 @@ class ItemGroupManager { config: ConfigurationSection, keys: Set ) { - //TODO see below todo - //val itemRegistry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ITEM) + val itemRegistry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ITEM) // Read material to include in this group policy val materialList = groupSection.getStringList(MATERIAL_LIST_PATH) for (typeName in materialList) { - //TODO get item key from - /*val key = NamespacedKey.fromString(typeName.lowercase()) + val key = NamespacedKey.fromString(typeName.lowercase()) if (key == null) { CustomAnvil.instance.logger.warning( "Malformed item type $typeName on group ${group.getName()}" @@ -93,31 +94,18 @@ class ItemGroupManager { } val type = itemRegistry.get(key) - if(type == null){ + if (type == null) { // Check if we should warn the user if (typeName !in FUTURE_MATERIAL) { CustomAnvil.instance.logger.warning( "Unknown item type $typeName on group ${group.getName()}" ) - } - continue - }*/ - - - val materialName = typeName.uppercase(Locale.getDefault()) - val material = Material.getMaterial(materialName) - if (material == null) { - // Check if we should warn the user - if (materialName !in FUTURE_MATERIAL) { - CustomAnvil.instance.logger.warning( - "Unknown material $materialName on group ${group.getName()}" - ) - } continue } - group.addToPolicy(material.asItemType()!!) + + group.addToPolicy(type) } // Read group to include in this group policy.