2024-05-23 20:44:07 +02:00
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] Improve item default attribute API
diff --git a/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttributeInstance.java b/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttributeInstance.java
2024-06-14 01:32:45 +02:00
index de0eba19c0c963adb4f17cea22333240021fd801..3b171a08bd0bedfe224905feb5838d2540199bce 100644
2024-05-23 20:44:07 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttributeInstance.java
+++ b/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttributeInstance.java
2024-06-14 01:32:45 +02:00
@@ -75,7 +75,7 @@ public class CraftAttributeInstance implements AttributeInstance {
return new AttributeModifier(CraftNamespacedKey.fromMinecraft(nms.id()), nms.amount(), AttributeModifier.Operation.values()[nms.operation().ordinal()], org.bukkit.inventory.EquipmentSlotGroup.ANY);
2024-05-23 20:44:07 +02:00
}
- public static AttributeModifier convert(net.minecraft.world.entity.ai.attributes.AttributeModifier nms, EquipmentSlot slot) {
2024-06-14 01:32:45 +02:00
- return new AttributeModifier(CraftNamespacedKey.fromMinecraft(nms.id()), nms.amount(), AttributeModifier.Operation.values()[nms.operation().ordinal()], slot.getGroup());
2024-05-23 20:44:07 +02:00
+ public static AttributeModifier convert(net.minecraft.world.entity.ai.attributes.AttributeModifier nms, net.minecraft.world.entity.EquipmentSlotGroup slot) { // Paper
2024-06-14 01:32:45 +02:00
+ return new AttributeModifier(CraftNamespacedKey.fromMinecraft(nms.id()), nms.amount(), AttributeModifier.Operation.values()[nms.operation().ordinal()], org.bukkit.craftbukkit.CraftEquipmentSlot.getSlot(slot)); // Paper
2024-05-23 20:44:07 +02:00
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
2024-07-06 21:19:14 +02:00
index 66d773cadb74f9176e6cf68a565568034f52ec63..a1f2b9d40d374e8cdbaf916b25fa74b6c0970f81 100644
2024-05-23 20:44:07 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
2024-07-06 21:19:14 +02:00
@@ -197,19 +197,36 @@ public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Han
2024-05-23 20:44:07 +02:00
// return CraftEquipmentSlot.getSlot(EntityInsentient.getEquipmentSlotForItem(CraftItemStack.asNMSCopy(ItemStack.of(this))));
// }
+ // Paper start - improve default attribute API
+ @Override
+ public @NotNull Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers() {
+ return this.getDefaultAttributeModifiers(sg -> true);
+ }
+ // Paper end - improve default attribute API
+
@Override
public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(EquipmentSlot slot) {
+ // Paper start - improve/fix item default attribute API
+ final net.minecraft.world.entity.EquipmentSlot nmsSlot = CraftEquipmentSlot.getNMS(slot);
+ return this.getDefaultAttributeModifiers(sg -> sg.test(nmsSlot));
+ }
2024-07-06 21:19:14 +02:00
+
+ private Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(final java.util.function.Predicate<net.minecraft.world.entity.EquipmentSlotGroup> slotPredicate) {
+ // Paper end - improve/fix item default attribute API
ImmutableMultimap.Builder<Attribute, AttributeModifier> defaultAttributes = ImmutableMultimap.builder();
2024-05-23 20:44:07 +02:00
2024-07-06 21:19:14 +02:00
ItemAttributeModifiers nmsDefaultAttributes = this.item.components().getOrDefault(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY);
if (nmsDefaultAttributes.modifiers().isEmpty()) {
nmsDefaultAttributes = this.item.getDefaultAttributeModifiers();
}
-
2024-05-23 20:44:07 +02:00
- nmsDefaultAttributes.forEach(CraftEquipmentSlot.getNMS(slot), (key, value) -> {
- Attribute attribute = CraftAttribute.minecraftToBukkit(key.value());
- defaultAttributes.put(attribute, CraftAttributeInstance.convert(value, slot));
- });
2024-07-06 21:19:14 +02:00
+ // Paper start - improve/fix item default attribute API
2024-05-23 20:44:07 +02:00
+ for (final net.minecraft.world.item.component.ItemAttributeModifiers.Entry entry : nmsDefaultAttributes.modifiers()) {
+ if (!slotPredicate.test(entry.slot())) continue;
+ final Attribute attribute = CraftAttribute.minecraftHolderToBukkit(entry.attribute());
+ final AttributeModifier modifier = CraftAttributeInstance.convert(entry.modifier(), entry.slot());
+ defaultAttributes.put(attribute, modifier);
+ }
+ // Paper end - improve/fix item default attribute API
return defaultAttributes.build();
}
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
2024-07-19 13:08:08 +02:00
index dec695198708838fd77285ba818e82861da49a98..17fc3d78f1f5002d8f9cb2113706a2cc6a6c821f 100644
2024-05-23 20:44:07 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
2024-07-19 13:08:08 +02:00
@@ -385,7 +385,11 @@ public final class CraftMagicNumbers implements UnsafeValues {
2024-05-23 20:44:07 +02:00
@Override
public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(Material material, EquipmentSlot slot) {
2024-07-06 21:19:14 +02:00
- return material.getDefaultAttributeModifiers(slot);
2024-05-23 20:44:07 +02:00
+ // Paper start - delegate to method on ItemType
+ final org.bukkit.inventory.ItemType item = material.asItemType();
+ Preconditions.checkArgument(item != null, material + " is not an item and does not have default attributes");
+ return item.getDefaultAttributeModifiers(slot);
+ // Paper end - delegate to method on ItemType
}
@Override