From 2d34898b55c97fcb97fd47275545641c54057172 Mon Sep 17 00:00:00 2001 From: Jake Potrebic <15055071+Machine-Maker@users.noreply.github.com> Date: Mon, 24 May 2021 00:52:31 -0700 Subject: [PATCH] Add methods for getting default item attributes (#5593) --- ...309-Attributes-API-for-item-defaults.patch | 53 +++++++++++++++++++ ...742-Attributes-API-for-item-defaults.patch | 42 +++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 Spigot-API-Patches/0309-Attributes-API-for-item-defaults.patch create mode 100644 Spigot-Server-Patches/0742-Attributes-API-for-item-defaults.patch diff --git a/Spigot-API-Patches/0309-Attributes-API-for-item-defaults.patch b/Spigot-API-Patches/0309-Attributes-API-for-item-defaults.patch new file mode 100644 index 0000000000..2ca00f5783 --- /dev/null +++ b/Spigot-API-Patches/0309-Attributes-API-for-item-defaults.patch @@ -0,0 +1,53 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Jake Potrebic +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 9b1c9e60dba9ea3ef8d8e164f13dd76daf57db8e..e2b3470e3c9a97671723f5a67f722fb86fb07fbf 100644 +--- a/src/main/java/org/bukkit/Material.java ++++ b/src/main/java/org/bukkit/Material.java +@@ -3600,6 +3600,19 @@ 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 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 e504567cf755557be8511f2c93c171572b78e722..379acee1b5f2d06e6a96f3444783f4a29ca24095 100644 +--- a/src/main/java/org/bukkit/UnsafeValues.java ++++ b/src/main/java/org/bukkit/UnsafeValues.java +@@ -165,6 +165,18 @@ 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 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/0742-Attributes-API-for-item-defaults.patch b/Spigot-Server-Patches/0742-Attributes-API-for-item-defaults.patch new file mode 100644 index 0000000000..7c9a583959 --- /dev/null +++ b/Spigot-Server-Patches/0742-Attributes-API-for-item-defaults.patch @@ -0,0 +1,42 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Jake Potrebic +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 7c9ef73983c2c7f4b7386a472e5a79dce7fa7ea7..0486a3158eb81947f78cef11aa4d4f141c40dfb5 100644 +--- a/src/main/java/net/minecraft/world/item/Item.java ++++ b/src/main/java/net/minecraft/world/item/Item.java +@@ -274,6 +274,7 @@ public class Item implements IMaterial { + return false; + } + ++ public Multimap getAttributesForSlot(EnumItemSlot enumItemSlot) { return a(enumItemSlot); } // Paper - OBFHELPER + public Multimap 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 a386beea01056a7fb448596ba45342d832a4c63d..6cb8608f8238c4a8c346b92ba672c8cc1c0cbcc4 100644 +--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java ++++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +@@ -495,6 +495,19 @@ 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 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 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();