Improve item default attribute API

This commit is contained in:
Jake Potrebic 2021-05-08 15:01:54 -07:00
parent eb675d9110
commit c2b061d759
3 changed files with 29 additions and 8 deletions

View File

@ -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); return new AttributeModifier(CraftNamespacedKey.fromMinecraft(nms.id()), nms.amount(), AttributeModifier.Operation.values()[nms.operation().ordinal()], org.bukkit.inventory.EquipmentSlotGroup.ANY);
} }
public static AttributeModifier convert(net.minecraft.world.entity.ai.attributes.AttributeModifier nms, EquipmentSlot slot) { public static AttributeModifier convert(net.minecraft.world.entity.ai.attributes.AttributeModifier nms, net.minecraft.world.entity.EquipmentSlotGroup slot) { // Paper
return new AttributeModifier(CraftNamespacedKey.fromMinecraft(nms.id()), nms.amount(), AttributeModifier.Operation.values()[nms.operation().ordinal()], slot.getGroup()); return new AttributeModifier(CraftNamespacedKey.fromMinecraft(nms.id()), nms.amount(), AttributeModifier.Operation.values()[nms.operation().ordinal()], org.bukkit.craftbukkit.CraftEquipmentSlot.getSlot(slot)); // Paper
} }
} }

View File

@ -197,16 +197,33 @@ public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Han
// return CraftEquipmentSlot.getSlot(EntityInsentient.getEquipmentSlotForItem(CraftItemStack.asNMSCopy(ItemStack.of(this)))); // 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 @Override
public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(EquipmentSlot slot) { 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));
}
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(); ImmutableMultimap.Builder<Attribute, AttributeModifier> defaultAttributes = ImmutableMultimap.builder();
ItemAttributeModifiers nmsDefaultAttributes = this.item.components().getOrDefault(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY); ItemAttributeModifiers nmsDefaultAttributes = this.item.components().getOrDefault(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY);
// Paper start - improve/fix item default attribute API
nmsDefaultAttributes.forEach(CraftEquipmentSlot.getNMS(slot), (key, value) -> { for (final net.minecraft.world.item.component.ItemAttributeModifiers.Entry entry : nmsDefaultAttributes.modifiers()) {
Attribute attribute = CraftAttribute.minecraftToBukkit(key.value()); if (!slotPredicate.test(entry.slot())) continue;
defaultAttributes.put(attribute, CraftAttributeInstance.convert(value, slot)); 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(); return defaultAttributes.build();
} }

View File

@ -386,7 +386,11 @@ public final class CraftMagicNumbers implements UnsafeValues {
@Override @Override
public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(Material material, EquipmentSlot slot) { public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(Material material, EquipmentSlot slot) {
return material.getDefaultAttributeModifiers(slot); // 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 @Override