Skip to content

Commit

Permalink
refactor: check for adding empty attributes with hiding them in lore
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Aug 10, 2024
1 parent a0684f3 commit 7cb6f37
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.mineinabyss.idofront.nms

import net.minecraft.core.component.DataComponents
import org.bukkit.craftbukkit.inventory.CraftItemStack
import org.bukkit.inventory.ItemFlag
import org.bukkit.inventory.ItemStack

/**
* In 1.20.5, HIDE_ATTIRBUTES ItemFlag has funky behaviour as the component holds the show_in_tooltip property
* This method properly hides the Attributes from the tooltip if an item has the flag set
*/
fun ItemStack.hideAttributeTooltipWithItemFlagSet(): ItemStack {
if (!hasItemFlag(ItemFlag.HIDE_ATTRIBUTES)) return this
fun ItemStack.hideAttributeTooltipWithItemFlagSet() = editMeta { meta ->
if (!hasItemFlag(ItemFlag.HIDE_ATTRIBUTES)) return@editMeta
if (meta.attributeModifiers?.isEmpty == false) return@editMeta
if (type.defaultAttributeModifiers != meta.attributeModifiers) return@editMeta

return CraftItemStack.asCraftCopy(this).apply {
handle.set(DataComponents.ATTRIBUTE_MODIFIERS, (handle.get(DataComponents.ATTRIBUTE_MODIFIERS) ?: handle.item.defaultAttributeModifiers).withTooltip(false))
}
meta.attributeModifiers = type.defaultAttributeModifiers
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ data class BaseSerializableItemStack(
hideTooltips?.let { meta.isHideTooltip = hideTooltips }

applyTo.itemMeta = meta
return applyTo.hideAttributeTooltipWithItemFlagSet()
applyTo.hideAttributeTooltipWithItemFlagSet()
return applyTo
}

fun toItemStackOrNull(applyTo: ItemStack = ItemStack(type ?: Material.AIR)) =
Expand Down

0 comments on commit 7cb6f37

Please sign in to comment.