get item type via name

This commit is contained in:
alexcrea 2025-07-10 15:54:46 +02:00
parent b121d314d3
commit 84d4e811e2
Signed by: alexcrea
GPG key ID: E346CD16413450E3
2 changed files with 19 additions and 35 deletions

View file

@ -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)

View file

@ -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<String>
) {
//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,7 +94,7 @@ 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(
@ -102,22 +103,9 @@ class ItemGroupManager {
}
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.