Refactored item attribute changes for more mod compatibility

This commit is contained in:
RaphiMC 2024-03-04 16:37:37 +01:00
parent 2a5648fcca
commit 6abdbbce08
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
4 changed files with 137 additions and 55 deletions

View File

@ -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<EntityAttribute, EntityAttributeModifier> attributeModifiers;
@Unique
private final Multimap<EntityAttribute, EntityAttributeModifier> viaFabricPlus$AttributeModifiers_r1_8 = ImmutableMultimap.of();
@Inject(method = "use", at = @At("HEAD"), cancellable = true)
private void disableRightClick(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> 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<EntityAttribute, EntityAttributeModifier> changeAttributeModifiers(ArmorItem instance) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8) && DebugSettings.global().replaceAttributeModifiers.isEnabled()) {
return this.viaFabricPlus$AttributeModifiers_r1_8;
} else {
return this.attributeModifiers;
}
}
}

View File

@ -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<EntityAttribute, EntityAttributeModifier> modifyVariableGetAttributeModifiers(Multimap<EntityAttribute, EntityAttributeModifier> 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<ItemStack> cir) {
final IItemStack mixinItemStack = (IItemStack) (Object) cir.getReturnValue();

View File

@ -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<EntityAttribute, EntityAttributeModifier> attributeModifiers;
@Unique
private float viaFabricPlus$attackDamage_r1_8;
@Unique
private Multimap<EntityAttribute, EntityAttributeModifier> 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();
@Inject(method = "<init>", 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) {
return 2 + materialDamage;
this.viaFabricPlus$attackDamage_r1_8 = 2 + materialAttackDamage;
} else if ((Item) this instanceof ShovelItem) {
return 1 + materialDamage;
this.viaFabricPlus$attackDamage_r1_8 = 1 + materialAttackDamage;
} else if ((Item) this instanceof AxeItem) {
return 3 + materialDamage;
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<EntityAttribute, EntityAttributeModifier> 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<EntityAttribute, EntityAttributeModifier> changeAttributeModifiers(MiningToolItem instance) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8) && DebugSettings.global().replaceAttributeModifiers.isEnabled()) {
return this.viaFabricPlus$AttributeModifiers_r1_8;
} else {
return this.attributeModifiers;
}
}
}

View File

@ -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<EntityAttribute, EntityAttributeModifier> attributeModifiers;
@Unique
private float viaFabricPlus$attackDamage_r1_8;
@Unique
private Multimap<EntityAttribute, EntityAttributeModifier> viaFabricPlus$AttributeModifiers_r1_8;
public MixinSwordItem(ToolMaterial material, Settings settings) {
super(material, settings);
}
@Inject(method = "<init>", 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<EntityAttribute, EntityAttributeModifier> 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<ItemStack> 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<EntityAttribute, EntityAttributeModifier> 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<Float> cir) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(LegacyProtocolVersion.b1_8tob1_8_1)) {