diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinArmorMaterials.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinArmorMaterials.java index 86b9f9e8..c4336399 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinArmorMaterials.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinArmorMaterials.java @@ -48,7 +48,6 @@ public abstract class MixinArmorMaterials { return 24; } } - return this.durabilityMultiplier; } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItem.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItem.java index a5018fbb..f8eebad8 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItem.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItem.java @@ -47,7 +47,8 @@ public abstract class MixinItem { @Redirect(method = {"getMaxDamage", "isDamageable", "getItemBarStep", "getItemBarColor"}, at = @At(value = "FIELD", target = "Lnet/minecraft/item/Item;maxDamage:I")) private int changeCrossbowDamage(Item instance) { if (instance instanceof ArmorItem armor && ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(LegacyProtocolVersion.b1_8tob1_8_1)) { - return armor.getMaterial().getDurability(armor.getType()); // Other part of fix in MixinArmorMaterials + // Counterpart in MixinArmorMaterials + return armor.getMaterial().getDurability(armor.getType()); } else if (instance instanceof CrossbowItem && ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_17_1)) { return 326; } else { 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 3a4536d5..5a7b4418 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 @@ -64,19 +64,18 @@ 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()) { - return modifiers; + if (DebugSettings.global().replaceAttributeModifiers.isEnabled() && !modifiers.isEmpty()) { + modifiers = HashMultimap.create(modifiers); // We need to edit the modifiers + modifiers.removeAll(EntityAttributes.GENERIC_ATTACK_DAMAGE); + if (getItem() instanceof MiningToolItem tool && !(tool instanceof HoeItem) /* Hope items didn't use the tool abstraction back then */) { + 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); } - - modifiers = HashMultimap.create(modifiers); - modifiers.removeAll(EntityAttributes.GENERIC_ATTACK_DAMAGE); - OptionalDouble defaultAttackDamage = viaFabricPlus$getDefaultAttackDamage(getItem()); - if (defaultAttackDamage.isPresent()) { - modifiers.put(EntityAttributes.GENERIC_ATTACK_DAMAGE, new EntityAttributeModifier(Item.ATTACK_DAMAGE_MODIFIER_ID, "Tool modifier", defaultAttackDamage.getAsDouble(), EntityAttributeModifier.Operation.ADDITION)); - } - modifiers.removeAll(EntityAttributes.GENERIC_ATTACK_SPEED); - modifiers.removeAll(EntityAttributes.GENERIC_ARMOR); - modifiers.removeAll(EntityAttributes.GENERIC_ARMOR_TOUGHNESS); return modifiers; } @@ -88,23 +87,6 @@ public abstract class MixinItemStack implements IItemStack { } } - @Unique - private OptionalDouble viaFabricPlus$getDefaultAttackDamage(Item item) { - if (item instanceof ToolItem toolItem) { - final float materialBonus = toolItem.getMaterial().getAttackDamage(); - if (item instanceof SwordItem) { - return OptionalDouble.of(4 + materialBonus); - } else if (item instanceof PickaxeItem) { - return OptionalDouble.of(2 + materialBonus); - } else if (item instanceof ShovelItem) { - return OptionalDouble.of(1 + materialBonus); - } else if (item instanceof AxeItem) { - return OptionalDouble.of(3 + materialBonus); - } - } - return OptionalDouble.empty(); - } - @Override public boolean viaFabricPlus$has1_10Tag() { return this.viaFabricPlus$has1_10Tag; 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 new file mode 100644 index 00000000..ba9ff2c5 --- /dev/null +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinMiningToolItem.java @@ -0,0 +1,57 @@ +/* + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2024 FlorianMichael/EnZaXD and RK_01/RaphiMC + * Copyright (C) 2023-2024 contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.item; + +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; +import net.minecraft.item.*; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(MiningToolItem.class) +public abstract class MixinMiningToolItem extends ToolItem { + + @Shadow @Final private float attackDamage; + + public MixinMiningToolItem(ToolMaterial material, Settings settings) { + super(material, settings); + } + + /** + * @author Mojang, FlorianMichael/EnZaXD + * @reason Change attack damage calculation + */ + @Overwrite + public float getAttackDamage() { + if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) { + if ((Item) this instanceof PickaxeItem) { + return 2 + getMaterial().getAttackDamage(); + } else if ((Item) this instanceof ShovelItem) { + return 1 + getMaterial().getAttackDamage(); + } else if ((Item) this instanceof AxeItem) { + return 3 + getMaterial().getAttackDamage(); + } + } + 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 136de7b8..ffdddde3 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 @@ -24,16 +24,16 @@ import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.ItemStack; -import net.minecraft.item.SwordItem; -import net.minecraft.item.ToolItem; -import net.minecraft.item.ToolMaterial; +import net.minecraft.item.*; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; import net.minecraft.util.UseAction; 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.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @@ -41,6 +41,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(SwordItem.class) public abstract class MixinSwordItem extends ToolItem { + @Shadow @Final private float attackDamage; + public MixinSwordItem(ToolMaterial material, Settings settings) { super(material, settings); } @@ -74,6 +76,19 @@ public abstract class MixinSwordItem extends ToolItem { } } + /** + * @author Mojang, FlorianMichael/EnZaXD + * @reason Change attack damage calculation + */ + @Overwrite + public float getAttackDamage() { + if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) { + return 4 + this.getMaterial().getAttackDamage(); + } else { + return this.attackDamage; + } + } + @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)) { diff --git a/src/main/resources/viafabricplus.mixins.json b/src/main/resources/viafabricplus.mixins.json index 292497e1..566eb011 100644 --- a/src/main/resources/viafabricplus.mixins.json +++ b/src/main/resources/viafabricplus.mixins.json @@ -111,6 +111,7 @@ "fixes.minecraft.entity.MixinSquidEntity", "fixes.minecraft.entity.MixinWolfEntity", "fixes.minecraft.item.MixinArmorItem", + "fixes.minecraft.item.MixinArmorMaterials", "fixes.minecraft.item.MixinAxeItem", "fixes.minecraft.item.MixinBlockItem", "fixes.minecraft.item.MixinBowItem", @@ -200,7 +201,7 @@ "vialegacy.MixinViaLegacyConfig", "viaversion.MixinConfig", "viaversion.MixinProtocolVersion", - "fixes.minecraft.item.MixinArmorMaterials" + "fixes.minecraft.item.MixinMiningToolItem" ], "injectors": { "defaultRequire": 1