From acfee45986ad701fff806c67060fad611929e969 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Mon, 24 May 2021 00:52:31 -0700
Subject: [PATCH] Add methods for getting default item attributes (#5593)

---
 .../Attributes-API-for-item-defaults.patch    | 53 +++++++++++++++++++
 .../Attributes-API-for-item-defaults.patch    | 42 +++++++++++++++
 2 files changed, 95 insertions(+)
 create mode 100644 Spigot-API-Patches/Attributes-API-for-item-defaults.patch
 create mode 100644 Spigot-Server-Patches/Attributes-API-for-item-defaults.patch

diff --git a/Spigot-API-Patches/Attributes-API-for-item-defaults.patch b/Spigot-API-Patches/Attributes-API-for-item-defaults.patch
new file mode 100644
index 0000000000..814c825117
--- /dev/null
+++ b/Spigot-API-Patches/Attributes-API-for-item-defaults.patch
@@ -0,0 +1,53 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Jake Potrebic <jake.m.potrebic@gmail.com>
+Date: Sat, 8 May 2021 15:02:00 -0700
+Subject: [PATCH] Attributes API for item defaults
+
+
+diff --git a/src/main/java/org/bukkit/Material.java b/src/main/java/org/bukkit/Material.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/org/bukkit/Material.java
++++ b/src/main/java/org/bukkit/Material.java
+@@ -0,0 +0,0 @@ public enum Material implements Keyed {
+     public io.papermc.paper.inventory.ItemRarity getItemRarity() {
+         return Bukkit.getUnsafe().getItemRarity(this);
+     }
++
++    /**
++     * Returns an immutable multimap of attributes for the slot.
++     * {@link #isItem()} must be true for this material.
++     *
++     * @param equipmentSlot the slot to get the attributes for
++     * @throws IllegalArgumentException if {@link #isItem()} is false
++     * @return an immutable multimap of attributes
++     */
++    @NotNull
++    public com.google.common.collect.Multimap<org.bukkit.attribute.Attribute, org.bukkit.attribute.AttributeModifier> getItemAttributes(@NotNull EquipmentSlot equipmentSlot) {
++        return Bukkit.getUnsafe().getItemAttributes(this, equipmentSlot);
++    }
+     // Paper end
+ 
+     /**
+diff --git a/src/main/java/org/bukkit/UnsafeValues.java b/src/main/java/org/bukkit/UnsafeValues.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/org/bukkit/UnsafeValues.java
++++ b/src/main/java/org/bukkit/UnsafeValues.java
+@@ -0,0 +0,0 @@ public interface UnsafeValues {
+      */
+     public boolean isValidRepairItemStack(@org.jetbrains.annotations.NotNull ItemStack itemToBeRepaired, @org.jetbrains.annotations.NotNull ItemStack repairMaterial);
+ 
++    /**
++     * Returns an immutable multimap of attributes for the material and slot.
++     * {@link Material#isItem()} must be true for this material.
++     *
++     * @param material the material
++     * @param equipmentSlot the slot to get the attributes for
++     * @throws IllegalArgumentException if {@link Material#isItem()} is false
++     * @return an immutable multimap of attributes
++     */
++    @org.jetbrains.annotations.NotNull
++    public com.google.common.collect.Multimap<org.bukkit.attribute.Attribute, org.bukkit.attribute.AttributeModifier> getItemAttributes(@org.jetbrains.annotations.NotNull Material material, @org.jetbrains.annotations.NotNull org.bukkit.inventory.EquipmentSlot equipmentSlot);
++
+     /**
+      * Returns the server's protocol version.
+      *
diff --git a/Spigot-Server-Patches/Attributes-API-for-item-defaults.patch b/Spigot-Server-Patches/Attributes-API-for-item-defaults.patch
new file mode 100644
index 0000000000..00023654b1
--- /dev/null
+++ b/Spigot-Server-Patches/Attributes-API-for-item-defaults.patch
@@ -0,0 +1,42 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Jake Potrebic <jake.m.potrebic@gmail.com>
+Date: Sat, 8 May 2021 15:01:54 -0700
+Subject: [PATCH] Attributes API for item defaults
+
+
+diff --git a/src/main/java/net/minecraft/world/item/Item.java b/src/main/java/net/minecraft/world/item/Item.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/net/minecraft/world/item/Item.java
++++ b/src/main/java/net/minecraft/world/item/Item.java
+@@ -0,0 +0,0 @@ public class Item implements IMaterial {
+         return false;
+     }
+ 
++    public Multimap<AttributeBase, AttributeModifier> getAttributesForSlot(EnumItemSlot enumItemSlot) { return a(enumItemSlot); } // Paper - OBFHELPER
+     public Multimap<AttributeBase, AttributeModifier> a(EnumItemSlot enumitemslot) {
+         return ImmutableMultimap.of();
+     }
+diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
++++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
+         return this.getItem(itemToBeRepaired.getType()).canRepair(CraftItemStack.asNMSCopy(itemToBeRepaired), CraftItemStack.asNMSCopy(repairMaterial));
+     }
+ 
++    @Override
++    public com.google.common.collect.Multimap<org.bukkit.attribute.Attribute, org.bukkit.attribute.AttributeModifier> getItemAttributes(org.bukkit.Material material, org.bukkit.inventory.EquipmentSlot equipmentSlot) {
++        Item item = this.getItem(material);
++        if (item == null) {
++            throw new IllegalArgumentException(material + " is not an item and therefore does not have attributes");
++        }
++        com.google.common.collect.ImmutableMultimap.Builder<org.bukkit.attribute.Attribute, org.bukkit.attribute.AttributeModifier> attributeMapBuilder = com.google.common.collect.ImmutableMultimap.builder();
++        item.getAttributesForSlot(org.bukkit.craftbukkit.CraftEquipmentSlot.getNMS(equipmentSlot)).forEach((attributeBase, attributeModifier) -> {
++            attributeMapBuilder.put(org.bukkit.Registry.ATTRIBUTE.get(CraftNamespacedKey.fromMinecraft(net.minecraft.core.IRegistry.ATTRIBUTE.getKey(attributeBase))), org.bukkit.craftbukkit.attribute.CraftAttributeInstance.convert(attributeModifier));
++        });
++        return attributeMapBuilder.build();
++    }
++
+     @Override
+     public int getProtocolVersion() {
+         return net.minecraft.SharedConstants.getGameVersion().getProtocolVersion();