From 6abdbbce0881302f2143d371409ac2ef4b10b173 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:37:37 +0100 Subject: [PATCH] Refactored item attribute changes for more mod compatibility --- .../fixes/minecraft/item/MixinArmorItem.java | 26 +++++++ .../fixes/minecraft/item/MixinItemStack.java | 33 ++------ .../minecraft/item/MixinMiningToolItem.java | 77 ++++++++++++++----- .../fixes/minecraft/item/MixinSwordItem.java | 56 +++++++++++--- 4 files changed, 137 insertions(+), 55 deletions(-) diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinArmorItem.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinArmorItem.java index 90be9acb..7b4a56f9 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinArmorItem.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinArmorItem.java @@ -19,7 +19,13 @@ package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.item; +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Multimap; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; +import de.florianmichael.viafabricplus.settings.impl.DebugSettings; +import net.minecraft.entity.attribute.EntityAttribute; +import net.minecraft.entity.attribute.EntityAttributeModifier; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ArmorItem; import net.minecraft.item.ItemStack; @@ -28,14 +34,25 @@ import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; import net.minecraft.world.World; import net.raphimc.vialegacy.api.LegacyProtocolVersion; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ArmorItem.class) public abstract class MixinArmorItem { + @Shadow + @Final + private Multimap attributeModifiers; + + @Unique + private final Multimap viaFabricPlus$AttributeModifiers_r1_8 = ImmutableMultimap.of(); + @Inject(method = "use", at = @At("HEAD"), cancellable = true) private void disableRightClick(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable> cir) { if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(LegacyProtocolVersion.r1_4_6tor1_4_7)) { @@ -43,4 +60,13 @@ public abstract class MixinArmorItem { } } + @Redirect(method = "getAttributeModifiers", at = @At(value = "FIELD", target = "Lnet/minecraft/item/ArmorItem;attributeModifiers:Lcom/google/common/collect/Multimap;")) + private Multimap changeAttributeModifiers(ArmorItem instance) { + if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8) && DebugSettings.global().replaceAttributeModifiers.isEnabled()) { + return this.viaFabricPlus$AttributeModifiers_r1_8; + } else { + return this.attributeModifiers; + } + } + } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java index faba5991..7d027ec1 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java @@ -19,25 +19,22 @@ package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.item; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import de.florianmichael.viafabricplus.injection.access.IItemStack; import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; -import de.florianmichael.viafabricplus.settings.impl.DebugSettings; import net.minecraft.entity.attribute.EntityAttribute; -import net.minecraft.entity.attribute.EntityAttributeModifier; -import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.*; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.*; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.Slice; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import java.util.OptionalDouble; - @Mixin(value = ItemStack.class, priority = 1) public abstract class MixinItemStack implements IItemStack { @@ -61,24 +58,6 @@ public abstract class MixinItemStack implements IItemStack { } } - @SuppressWarnings({"InvalidInjectorMethodSignature", "MixinAnnotationTarget"}) - @ModifyVariable(method = "getAttributeModifiers", ordinal = 0, at = @At(value = "STORE", ordinal = 1)) - private Multimap modifyVariableGetAttributeModifiers(Multimap modifiers) { - if (DebugSettings.global().replaceAttributeModifiers.isEnabled() && !modifiers.isEmpty()) { - modifiers = HashMultimap.create(modifiers); - modifiers.removeAll(EntityAttributes.GENERIC_ATTACK_DAMAGE); - if (getItem() instanceof MiningToolItem tool && !(tool instanceof HoeItem) /* hoe doesn't use the tool abstraction in 1.8 */) { - modifiers.put(EntityAttributes.GENERIC_ATTACK_DAMAGE, new EntityAttributeModifier(Item.ATTACK_DAMAGE_MODIFIER_ID, "Tool modifier", tool.getAttackDamage(), EntityAttributeModifier.Operation.ADDITION)); - } else if (getItem() instanceof SwordItem sword) { - modifiers.put(EntityAttributes.GENERIC_ATTACK_DAMAGE, new EntityAttributeModifier(Item.ATTACK_DAMAGE_MODIFIER_ID, "Weapon modifier", sword.getAttackDamage(), EntityAttributeModifier.Operation.ADDITION)); - } - modifiers.removeAll(EntityAttributes.GENERIC_ATTACK_SPEED); - modifiers.removeAll(EntityAttributes.GENERIC_ARMOR); - modifiers.removeAll(EntityAttributes.GENERIC_ARMOR_TOUGHNESS); - } - return modifiers; - } - @Inject(method = "copy", at = @At("RETURN")) private void copyViaFabricPlusData(CallbackInfoReturnable cir) { final IItemStack mixinItemStack = (IItemStack) (Object) cir.getReturnValue(); diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinMiningToolItem.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinMiningToolItem.java index 1c59fb49..e64c5300 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinMiningToolItem.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinMiningToolItem.java @@ -19,40 +19,81 @@ package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.item; +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Multimap; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; +import de.florianmichael.viafabricplus.settings.impl.DebugSettings; +import net.minecraft.entity.attribute.EntityAttribute; +import net.minecraft.entity.attribute.EntityAttributeModifier; +import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.item.*; +import net.minecraft.registry.tag.TagKey; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(MiningToolItem.class) public abstract class MixinMiningToolItem extends ToolItem { - @Shadow @Final private float attackDamage; + @Shadow + @Final + private float attackDamage; + + @Shadow + @Final + private Multimap attributeModifiers; + + @Unique + private float viaFabricPlus$attackDamage_r1_8; + + @Unique + private Multimap viaFabricPlus$AttributeModifiers_r1_8; public MixinMiningToolItem(ToolMaterial material, Settings settings) { super(material, settings); } - /** - * @author FlorianMichael/EnZaXD - * @reason Change attack damage calculation - */ - @Overwrite - public float getAttackDamage() { - if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) { - final float materialDamage = getMaterial().getAttackDamage(); - if ((Item) this instanceof PickaxeItem) { - return 2 + materialDamage; - } else if ((Item) this instanceof ShovelItem) { - return 1 + materialDamage; - } else if ((Item) this instanceof AxeItem) { - return 3 + materialDamage; - } + @Inject(method = "", at = @At("RETURN")) + private void init1_8Fields(float attackDamage, float attackSpeed, ToolMaterial material, TagKey effectiveBlocks, Settings settings, CallbackInfo ci) { + final float materialAttackDamage = material.getAttackDamage(); + if ((Item) this instanceof PickaxeItem) { + this.viaFabricPlus$attackDamage_r1_8 = 2 + materialAttackDamage; + } else if ((Item) this instanceof ShovelItem) { + this.viaFabricPlus$attackDamage_r1_8 = 1 + materialAttackDamage; + } else if ((Item) this instanceof AxeItem) { + this.viaFabricPlus$attackDamage_r1_8 = 3 + materialAttackDamage; + } else { // HoeItem didn't use MiningToolItem abstraction in 1.8 + this.viaFabricPlus$AttributeModifiers_r1_8 = ImmutableMultimap.of(); + return; + } + + final ImmutableMultimap.Builder builder = ImmutableMultimap.builder(); + builder.put(EntityAttributes.GENERIC_ATTACK_DAMAGE, new EntityAttributeModifier(ATTACK_DAMAGE_MODIFIER_ID, "Tool modifier", this.viaFabricPlus$attackDamage_r1_8, EntityAttributeModifier.Operation.ADDITION)); + this.viaFabricPlus$AttributeModifiers_r1_8 = builder.build(); + } + + @Redirect(method = "getAttackDamage", at = @At(value = "FIELD", target = "Lnet/minecraft/item/MiningToolItem;attackDamage:F")) + private float changeAttackDamage(MiningToolItem instance) { + if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) { + return this.viaFabricPlus$attackDamage_r1_8; + } else { + return this.attackDamage; + } + } + + @Redirect(method = "getAttributeModifiers", at = @At(value = "FIELD", target = "Lnet/minecraft/item/MiningToolItem;attributeModifiers:Lcom/google/common/collect/Multimap;")) + private Multimap changeAttributeModifiers(MiningToolItem instance) { + if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8) && DebugSettings.global().replaceAttributeModifiers.isEnabled()) { + return this.viaFabricPlus$AttributeModifiers_r1_8; + } else { + return this.attributeModifiers; } - return this.attackDamage; } } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinSwordItem.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinSwordItem.java index 056b8dc9..3a51a589 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinSwordItem.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinSwordItem.java @@ -19,12 +19,21 @@ package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.item; +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Multimap; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; +import de.florianmichael.viafabricplus.settings.impl.DebugSettings; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.entity.attribute.EntityAttribute; +import net.minecraft.entity.attribute.EntityAttributeModifier; +import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.*; +import net.minecraft.item.ItemStack; +import net.minecraft.item.SwordItem; +import net.minecraft.item.ToolItem; +import net.minecraft.item.ToolMaterial; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; import net.minecraft.util.UseAction; @@ -32,21 +41,43 @@ import net.minecraft.world.World; import net.raphimc.vialegacy.api.LegacyProtocolVersion; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(SwordItem.class) public abstract class MixinSwordItem extends ToolItem { - @Shadow @Final private float attackDamage; + @Shadow + @Final + private float attackDamage; + + @Shadow + @Final + private Multimap attributeModifiers; + + @Unique + private float viaFabricPlus$attackDamage_r1_8; + + @Unique + private Multimap viaFabricPlus$AttributeModifiers_r1_8; public MixinSwordItem(ToolMaterial material, Settings settings) { super(material, settings); } + @Inject(method = "", at = @At("RETURN")) + private void init1_8Fields(ToolMaterial toolMaterial, int attackDamage, float attackSpeed, Settings settings, CallbackInfo ci) { + this.viaFabricPlus$attackDamage_r1_8 = 4 + toolMaterial.getAttackDamage(); + final ImmutableMultimap.Builder builder = ImmutableMultimap.builder(); + builder.put(EntityAttributes.GENERIC_ATTACK_DAMAGE, new EntityAttributeModifier(ATTACK_DAMAGE_MODIFIER_ID, "Weapon modifier", this.viaFabricPlus$attackDamage_r1_8, EntityAttributeModifier.Operation.ADDITION)); + this.viaFabricPlus$AttributeModifiers_r1_8 = builder.build(); + } + @Override public TypedActionResult use(World world, PlayerEntity user, Hand hand) { if (ProtocolTranslator.getTargetVersion().betweenInclusive(LegacyProtocolVersion.b1_8tob1_8_1, ProtocolVersion.v1_8)) { @@ -76,19 +107,24 @@ public abstract class MixinSwordItem extends ToolItem { } } - /** - * @author FlorianMichael/EnZaXD - * @reason Change attack damage calculation - */ - @Overwrite - public float getAttackDamage() { + @Redirect(method = "getAttackDamage", at = @At(value = "FIELD", target = "Lnet/minecraft/item/SwordItem;attackDamage:F")) + private float changeAttackDamage(SwordItem instance) { if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) { - return 4 + this.getMaterial().getAttackDamage(); + return this.viaFabricPlus$attackDamage_r1_8; } else { return this.attackDamage; } } + @Redirect(method = "getAttributeModifiers", at = @At(value = "FIELD", target = "Lnet/minecraft/item/SwordItem;attributeModifiers:Lcom/google/common/collect/Multimap;")) + private Multimap changeAttributeModifiers(SwordItem instance) { + if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8) && DebugSettings.global().replaceAttributeModifiers.isEnabled()) { + return this.viaFabricPlus$AttributeModifiers_r1_8; + } else { + return this.attributeModifiers; + } + } + @Inject(method = "getMiningSpeedMultiplier", at = @At("HEAD"), cancellable = true) private void changeMiningSpeed(ItemStack stack, BlockState state, CallbackInfoReturnable cir) { if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(LegacyProtocolVersion.b1_8tob1_8_1)) {