From c4fa62577a3cc14f941d64fc4772b91d0db2ad7c Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 21 Jul 2024 14:16:13 +0100 Subject: [PATCH] Fixed tool handler --- .../components/ToolComponentHandlerImpl.kt | 22 +++++++++++++------ .../willfp/ecoitems/display/ItemsDisplay.kt | 4 +++- .../com/willfp/ecoitems/items/EcoItem.kt | 6 +++-- .../main/resources/items/_example_food.yml | 5 ++--- .../main/resources/items/_example_tool.yml | 6 ++--- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/eco-core/core-modern/src/main/kotlin/com/willfp/ecoitems/compat/modern/components/ToolComponentHandlerImpl.kt b/eco-core/core-modern/src/main/kotlin/com/willfp/ecoitems/compat/modern/components/ToolComponentHandlerImpl.kt index 3c739b3..49ff62b 100644 --- a/eco-core/core-modern/src/main/kotlin/com/willfp/ecoitems/compat/modern/components/ToolComponentHandlerImpl.kt +++ b/eco-core/core-modern/src/main/kotlin/com/willfp/ecoitems/compat/modern/components/ToolComponentHandlerImpl.kt @@ -1,6 +1,7 @@ package com.willfp.ecoitems.compat.modern.components import com.willfp.eco.core.config.interfaces.Config +import com.willfp.eco.core.items.toSNBT import com.willfp.ecoitems.items.components.ToolComponentHandler import org.bukkit.Material import org.bukkit.Tag @@ -17,20 +18,19 @@ class ToolComponentHandlerImpl : ToolComponentHandler() { tool.damagePerBlock = config.getInt("damage-per-block") if (config.has("rules")) { - tool.rules = mutableListOf() - for (rule in config.getSubsections("rules")) { - val speed = config.getDoubleOrNull("speed")?.toFloat() - val drops = config.getBoolOrNull("drops") + val speed = rule.getDoubleOrNull("speed")?.toFloat() + val drops = rule.getBoolOrNull("drops") ?: true // Leaving null causes weird behavior - val materialIds = config.getStrings("blocks") + val materialIds = rule.getStrings("blocks") val materials = mutableSetOf() + val tags = mutableSetOf>() for (id in materialIds) { if (id.startsWith("#")) { val tag = getTagByName(id.substring(1)) if (tag != null) { - materials.addAll(tag.values) + tags.add(tag) } } else { val material = Material.getMaterial(id.uppercase()) @@ -40,7 +40,15 @@ class ToolComponentHandlerImpl : ToolComponentHandler() { } } - tool.addRule(materials, speed, drops) + // Add rules for each tag + for (tag in tags) { + tool.addRule(tag, speed, drops) + } + + // Add rules for all materials + if (materials.isNotEmpty()) { + tool.addRule(materials, speed, drops) + } } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/display/ItemsDisplay.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/display/ItemsDisplay.kt index f4a0b6b..0e9aa62 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/display/ItemsDisplay.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/display/ItemsDisplay.kt @@ -45,7 +45,9 @@ class ItemsDisplay(plugin: EcoPlugin) : DisplayModule(plugin, DisplayPriority.LO lore.addAll(fis.lore) - fis.displayName = ecoItem.displayName.formatEco(context) + if (ecoItem.displayName != null) { + fis.displayName = ecoItem.displayName.formatEco(context) + } fis.addItemFlags(*itemFast.itemFlags.toTypedArray()) fis.lore = lore } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/items/EcoItem.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/items/EcoItem.kt index d700ae5..2c9e8da 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/items/EcoItem.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/items/EcoItem.kt @@ -39,7 +39,7 @@ class EcoItem( val lore: List = config.getStrings("item.lore") - val displayName: String = config.getString("item.display-name") + val displayName: String? = config.getStringOrNull("item.display-name") val slot = SlotTypes[config.getString("slot")] ?: SlotTypeMainhand @@ -47,7 +47,9 @@ class EcoItem( private val _itemStack: ItemStack = run { val itemConfig = config.getSubsection("item") ItemStackBuilder(Items.lookup(itemConfig.getString("item")).item).apply { - setDisplayName(itemConfig.getFormattedString("display-name")) + if (itemConfig.has("display-name")) { + setDisplayName(itemConfig.getFormattedString("display-name")) + } addLoreLines( itemConfig.getFormattedStrings("lore").map { "${Display.PREFIX}$it" } ) diff --git a/eco-core/core-plugin/src/main/resources/items/_example_food.yml b/eco-core/core-plugin/src/main/resources/items/_example_food.yml index 2587a57..fa9e71c 100644 --- a/eco-core/core-plugin/src/main/resources/items/_example_food.yml +++ b/eco-core/core-plugin/src/main/resources/items/_example_food.yml @@ -1,9 +1,7 @@ # An example food item (requires Paper 1.21+) - item: - item: cooked_beef glint - display-name: "Enchanted Steak" + item: cooked_beef glint item_name:"Enchanted Steak" lore: [ ] craftable: true recipe: @@ -18,6 +16,7 @@ item: - "" # Options for the food +# These options do not update existing foods, only new ones food: # Read here: https://minecraft.fandom.com/wiki/Food#Hunger_and_saturation nutrition: 12 diff --git a/eco-core/core-plugin/src/main/resources/items/_example_tool.yml b/eco-core/core-plugin/src/main/resources/items/_example_tool.yml index 948895c..2236240 100644 --- a/eco-core/core-plugin/src/main/resources/items/_example_tool.yml +++ b/eco-core/core-plugin/src/main/resources/items/_example_tool.yml @@ -1,8 +1,7 @@ # An example tool (Requires Paper 1.21+) item: - item: netherite_pickaxe glint max_damage:4096 - display-name: "Hardened Netherite Pickaxe" + item: netherite_pickaxe glint max_damage:4096 item_name:"Hardened Netherite Pickaxe" lore: [ ] craftable: true recipe: @@ -19,6 +18,7 @@ item: - netherite_ingot # Options for the tool +# These options do not update existing tools, only new ones tool: # The default mining speed, if not overridden by any rules mining-speed: 1.0 @@ -33,7 +33,7 @@ tool: rules: - blocks: - "#mineable_pickaxe" # Tags start with a # - speed: 5.2 # The mining speed for these blocks + speed: 45.8 # The mining speed for these blocks - blocks: - "#incorrect_for_netherite_tool" speed: 1