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);
}
public static AttributeModifier convert(net.minecraft.world.entity.ai.attributes.AttributeModifier nms, EquipmentSlot slot) {
return new AttributeModifier(CraftNamespacedKey.fromMinecraft(nms.id()), nms.amount(), AttributeModifier.Operation.values()[nms.operation().ordinal()], slot.getGroup());
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()], 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))));
// }
// 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));
}
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();
ItemAttributeModifiers nmsDefaultAttributes = this.item.components().getOrDefault(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY);
nmsDefaultAttributes.forEach(CraftEquipmentSlot.getNMS(slot), (key, value) -> {
Attribute attribute = CraftAttribute.minecraftToBukkit(key.value());
defaultAttributes.put(attribute, CraftAttributeInstance.convert(value, slot));
});
// Paper start - improve/fix item default attribute API
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();
}

View File

@ -386,7 +386,11 @@ public final class CraftMagicNumbers implements UnsafeValues {
@Override
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