Skip to content

Commit

Permalink
Remove FabricItem.getAttributeModifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed May 30, 2024
1 parent c585e0a commit afdfc92
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package net.fabricmc.fabric.api.item.v1;

import net.minecraft.component.type.AttributeModifiersComponent;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
Expand Down Expand Up @@ -64,19 +63,6 @@ default boolean allowContinuingBlockBreaking(PlayerEntity player, ItemStack oldS
return false;
}

/**
* Return the attribute modifiers to apply when this stack is worn in a living entity equipment slot.
* Stack-aware version of {@link Item#getAttributeModifiers()}.
*
* <p>Note that attribute modifiers are only updated when the stack changes, i.e. when {@code ItemStack.areEqual(old, new)} is false.
*
* @param stack the current stack
* @return the attribute modifiers
*/
default AttributeModifiersComponent getAttributeModifiers(ItemStack stack) {
return ((Item) this).getAttributeModifiers();
}

/**
* Returns a leftover item stack after {@code stack} is consumed in a recipe.
* (This is also known as "recipe remainder".)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.component.ComponentType;
import net.minecraft.component.type.AttributeModifiersComponent;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.Item;
Expand Down Expand Up @@ -75,27 +72,4 @@ The other damage method (which original.call discards) handles the creative mode

original.call(instance, amount, serverWorld, serverPlayerEntity, consumer);
}

@Redirect(
method = "appendAttributeModifiersTooltip",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;getOrDefault(Lnet/minecraft/component/ComponentType;Ljava/lang/Object;)Ljava/lang/Object;"
)
)
public Object appendAttributeModifiersTooltip(ItemStack stack, ComponentType<AttributeModifiersComponent> type, Object fallback) {
return getItem().getAttributeModifiers(stack);
}

@Redirect(
method = {"applyAttributeModifier", "applyAttributeModifiers"},
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/Item;getAttributeModifiers()Lnet/minecraft/component/type/AttributeModifiersComponent;"
)
)
public AttributeModifiersComponent applyAttributeModifiers(Item item) {
ItemStack stack = (ItemStack) (Object) this;
return item.getAttributeModifiers(stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package net.fabricmc.fabric.test.item;

import net.minecraft.block.BlockState;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.AttributeModifierSlot;
import net.minecraft.component.type.AttributeModifiersComponent;
import net.minecraft.entity.Entity;
Expand All @@ -37,7 +38,12 @@ public class UpdatingItem extends Item {
private final boolean allowUpdateAnimation;

public UpdatingItem(boolean allowUpdateAnimation) {
super(new Settings());
super(new Settings()
.component(DataComponentTypes.ATTRIBUTE_MODIFIERS, AttributeModifiersComponent.builder()
.add(EntityAttributes.GENERIC_ATTACK_DAMAGE, PLUS_FIVE, AttributeModifierSlot.MAINHAND)
.build()
)
);
this.allowUpdateAnimation = allowUpdateAnimation;
}

Expand All @@ -63,14 +69,6 @@ private boolean isEnabled(ItemStack stack) {
return !stack.contains(ItemUpdateAnimationTest.TICKS) || stack.getOrDefault(ItemUpdateAnimationTest.TICKS, 0) % 600 < 300;
}

@Override
public AttributeModifiersComponent getAttributeModifiers(ItemStack stack) {
// Give + 5 attack damage for 15 seconds every 30 seconds.
return AttributeModifiersComponent.builder()
.add(EntityAttributes.GENERIC_ATTACK_DAMAGE, PLUS_FIVE, AttributeModifierSlot.MAINHAND)
.build();
}

@Override
public float getMiningSpeed(ItemStack stack, BlockState state) {
return isEnabled(stack) ? 20 : super.getMiningSpeed(stack, state);
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx2560M
org.gradle.parallel=true
fabric.loom.multiProjectOptimisation=true

version=0.99.3
version=0.99.4
minecraft_version=1.21-pre1
yarn_version=+build.2
loader_version=0.15.11
Expand Down Expand Up @@ -30,7 +30,7 @@ fabric-entity-events-v1-version=1.6.12
fabric-events-interaction-v0-version=0.7.10
fabric-game-rule-api-v1-version=1.0.52
fabric-gametest-api-v1-version=2.0.0
fabric-item-api-v1-version=10.0.1
fabric-item-api-v1-version=11.0.0
fabric-item-group-api-v1-version=4.0.43
fabric-key-binding-api-v1-version=1.0.47
fabric-keybindings-v0-version=0.2.45
Expand Down

0 comments on commit afdfc92

Please sign in to comment.