From 8e07c189f0dd911495c0886bc75ab921056d4911 Mon Sep 17 00:00:00 2001 From: Niklas Widmann Date: Mon, 23 Sep 2024 18:11:41 +0200 Subject: [PATCH] use MixinExtras for most redirects to avoid mixin conflicts --- .../oreganized/mixin/AbstractArrowMixin.java | 15 +++++++------ .../galena/oreganized/mixin/BlockMixin.java | 18 +++++++-------- .../oreganized/mixin/FoodDataMixin.java | 22 ++++++++++++++----- .../oreganized/mixin/IceBlockMixin.java | 11 ++++------ .../mixin/MobEffectInstanceMixin.java | 11 +++++----- .../oreganized/mixin/client/GuiAccessor.java | 14 ------------ .../oreganized/mixin/client/GuiMixin.java | 11 +++++----- .../compat/LogStrippingFakeRecipesMixin.java | 11 +++++----- src/main/resources/oreganized.mixins.json | 2 +- 9 files changed, 54 insertions(+), 61 deletions(-) delete mode 100644 src/main/java/galena/oreganized/mixin/client/GuiAccessor.java diff --git a/src/main/java/galena/oreganized/mixin/AbstractArrowMixin.java b/src/main/java/galena/oreganized/mixin/AbstractArrowMixin.java index cde265a8..204a5b4d 100644 --- a/src/main/java/galena/oreganized/mixin/AbstractArrowMixin.java +++ b/src/main/java/galena/oreganized/mixin/AbstractArrowMixin.java @@ -1,28 +1,29 @@ package galena.oreganized.mixin; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.sugar.Local; import galena.oreganized.content.entity.LeadBoltEntity; import galena.oreganized.index.ODamageSources; import net.minecraft.world.damagesource.DamageSource; -import net.minecraft.world.damagesource.DamageSources; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.projectile.AbstractArrow; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; @Mixin(AbstractArrow.class) public class AbstractArrowMixin { - @Redirect( + @ModifyExpressionValue( method = "onHitEntity(Lnet/minecraft/world/phys/EntityHitResult;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/damagesource/DamageSources;arrow(Lnet/minecraft/world/entity/projectile/AbstractArrow;Lnet/minecraft/world/entity/Entity;)Lnet/minecraft/world/damagesource/DamageSource;") ) - public DamageSource injectLeadBolt(DamageSources instance, AbstractArrow arrow, Entity user) { - if (arrow instanceof LeadBoltEntity) { - return instance.source(ODamageSources.LEAD_BOLT, arrow, user); + public DamageSource injectLeadBolt(DamageSource original, @Local(ordinal = 1) Entity user) { + var self = (AbstractArrow) (Object) this; + if (self instanceof LeadBoltEntity) { + return self.damageSources().source(ODamageSources.LEAD_BOLT, self, user); } - return instance.arrow(arrow, user); + return original; } } diff --git a/src/main/java/galena/oreganized/mixin/BlockMixin.java b/src/main/java/galena/oreganized/mixin/BlockMixin.java index 24f6199a..8110516c 100644 --- a/src/main/java/galena/oreganized/mixin/BlockMixin.java +++ b/src/main/java/galena/oreganized/mixin/BlockMixin.java @@ -1,10 +1,10 @@ package galena.oreganized.mixin; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import com.llamalad7.mixinextras.sugar.Local; import galena.oreganized.content.item.ScribeItem; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.enchantment.Enchantment; -import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; @@ -12,23 +12,23 @@ import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.ModifyVariable; -import org.spongepowered.asm.mixin.injection.Redirect; + +import java.util.List; @Mixin(Block.class) public class BlockMixin { - @ModifyVariable( + @WrapOperation( method = "getDrops(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/entity/BlockEntity;Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/item/ItemStack;)Ljava/util/List;", - at = @At(value = "STORE") + at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/BlockState;getDrops(Lnet/minecraft/world/level/storage/loot/LootParams$Builder;)Ljava/util/List;") ) - private static LootParams.Builder modifyLootBuilder(LootParams.Builder builder, @Local BlockState state, @Local ItemStack stack) { + private static List modifyLootBuilder(BlockState state, LootParams.Builder builder, Operation> original, @Local ItemStack stack) { if (stack.getItem() instanceof ScribeItem scribe && scribe.dropsLikeSilktouch(stack, state)) { var virtual = stack.copy(); virtual.enchant(Enchantments.SILK_TOUCH, 1); - return builder.withParameter(LootContextParams.TOOL, virtual); + builder.withParameter(LootContextParams.TOOL, virtual); } - return builder; + return original.call(state, builder); } } diff --git a/src/main/java/galena/oreganized/mixin/FoodDataMixin.java b/src/main/java/galena/oreganized/mixin/FoodDataMixin.java index 72b4b9f7..edb4a614 100644 --- a/src/main/java/galena/oreganized/mixin/FoodDataMixin.java +++ b/src/main/java/galena/oreganized/mixin/FoodDataMixin.java @@ -1,22 +1,32 @@ package galena.oreganized.mixin; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.sugar.Local; import galena.oreganized.index.OEffects; import net.minecraft.world.entity.player.Player; import net.minecraft.world.food.FoodData; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; @Mixin(FoodData.class) public class FoodDataMixin { - @Redirect( + @ModifyExpressionValue( method = "tick(Lnet/minecraft/world/entity/player/Player;)V", - at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;heal(F)V") + at = @At(value = "CONSTANT", args = "floatValue=6F", ordinal = 1) ) - private void modifyHealthAmount(Player player, float value) { - if (player.hasEffect(OEffects.STUNNING.get())) player.heal(value / 2); - else player.heal(value); + private float modifyHealthAmount(float value, @Local Player player) { + if (player.hasEffect(OEffects.STUNNING.get())) return value * 2; + return value; + } + + @ModifyExpressionValue( + method = "tick(Lnet/minecraft/world/entity/player/Player;)V", + at = @At(value = "CONSTANT", args = "floatValue=1F", ordinal = 1) + ) + private float modifyHealthAmount2(float value, @Local Player player) { + if (player.hasEffect(OEffects.STUNNING.get())) return value / 2; + return value; } } diff --git a/src/main/java/galena/oreganized/mixin/IceBlockMixin.java b/src/main/java/galena/oreganized/mixin/IceBlockMixin.java index 6ad061a6..49a299ba 100644 --- a/src/main/java/galena/oreganized/mixin/IceBlockMixin.java +++ b/src/main/java/galena/oreganized/mixin/IceBlockMixin.java @@ -1,29 +1,26 @@ package galena.oreganized.mixin; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import com.llamalad7.mixinextras.sugar.Local; import galena.oreganized.content.item.ScribeItem; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.enchantment.Enchantment; -import net.minecraft.world.item.enchantment.EnchantmentHelper; -import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.level.block.IceBlock; import net.minecraft.world.level.block.state.BlockState; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; @Mixin(IceBlock.class) public class IceBlockMixin { - @Redirect( + @ModifyExpressionValue( method = "playerDestroy(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/entity/BlockEntity;Lnet/minecraft/world/item/ItemStack;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/enchantment/EnchantmentHelper;getItemEnchantmentLevel(Lnet/minecraft/world/item/enchantment/Enchantment;Lnet/minecraft/world/item/ItemStack;)I") ) - private int modifyLootBuilder(Enchantment enchantment, ItemStack stack, @Local BlockState state) { + private int modifyLootBuilder(int original, @Local ItemStack stack, @Local BlockState state) { if (stack.getItem() instanceof ScribeItem scribe && scribe.isCorrectToolForDrops(stack, state)) { return 1; } - return EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, stack); + return original; } } diff --git a/src/main/java/galena/oreganized/mixin/MobEffectInstanceMixin.java b/src/main/java/galena/oreganized/mixin/MobEffectInstanceMixin.java index b63902b0..f63b823e 100644 --- a/src/main/java/galena/oreganized/mixin/MobEffectInstanceMixin.java +++ b/src/main/java/galena/oreganized/mixin/MobEffectInstanceMixin.java @@ -1,17 +1,17 @@ package galena.oreganized.mixin; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import galena.oreganized.content.effect.StunningEffect; import galena.oreganized.index.OEffects; import net.minecraft.world.effect.MobEffectInstance; import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; @Mixin(MobEffectInstance.class) public class MobEffectInstanceMixin { - @Redirect( + @ModifyExpressionValue( method = "update(Lnet/minecraft/world/effect/MobEffectInstance;)Z", at = @At( value = "FIELD", @@ -20,9 +20,10 @@ public class MobEffectInstanceMixin { opcode = Opcodes.GETFIELD ) ) - private int replaceStunning(MobEffectInstance instance) { - if (instance.getEffect() == OEffects.STUNNING.get()) return StunningEffect.MAX_AMPLIFIER + 1; - return instance.getAmplifier(); + private int replaceStunning(int original) { + var self = (MobEffectInstance) (Object) this; + if (self.getEffect() == OEffects.STUNNING.get()) return StunningEffect.MAX_AMPLIFIER + 1; + return original; } } diff --git a/src/main/java/galena/oreganized/mixin/client/GuiAccessor.java b/src/main/java/galena/oreganized/mixin/client/GuiAccessor.java deleted file mode 100644 index 9bfa19bd..00000000 --- a/src/main/java/galena/oreganized/mixin/client/GuiAccessor.java +++ /dev/null @@ -1,14 +0,0 @@ -package galena.oreganized.mixin.client; - -import net.minecraft.client.gui.Gui; -import net.minecraft.client.gui.GuiGraphics; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Invoker; - -@Mixin(Gui.class) -public interface GuiAccessor { - - @Invoker - void callRenderHeart(GuiGraphics p_283024_, Gui.HeartType p_281393_, int p_283636_, int p_283279_, int p_283188_, boolean p_283440_, boolean p_282496_); - -} diff --git a/src/main/java/galena/oreganized/mixin/client/GuiMixin.java b/src/main/java/galena/oreganized/mixin/client/GuiMixin.java index aeeaf803..fda4333e 100644 --- a/src/main/java/galena/oreganized/mixin/client/GuiMixin.java +++ b/src/main/java/galena/oreganized/mixin/client/GuiMixin.java @@ -1,5 +1,7 @@ package galena.oreganized.mixin.client; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import com.llamalad7.mixinextras.sugar.Local; import galena.oreganized.client.render.gui.OGui; import galena.oreganized.index.OEffects; @@ -8,13 +10,12 @@ import net.minecraft.world.entity.player.Player; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.Slice; @Mixin(Gui.class) public class GuiMixin { - @Redirect( + @WrapOperation( method = "renderHearts(Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/world/entity/player/Player;IIIIFIIIZ)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/Gui;renderHeart(Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/client/gui/Gui$HeartType;IIIZZ)V"), slice = @Slice( @@ -22,14 +23,12 @@ public class GuiMixin { to = @At("TAIL") ) ) - private void renderStunnedHeart(Gui instance, GuiGraphics graphics, Gui.HeartType type, int x, int y, int v, boolean blinking, boolean half, @Local Player player) { - var accessor = (GuiAccessor) instance; - + private void renderStunnedHeart(Gui instance, GuiGraphics graphics, Gui.HeartType type, int x, int y, int v, boolean blinking, boolean half, Operation original, @Local Player player) { if (player.hasEffect(OEffects.STUNNING.get()) && (type == Gui.HeartType.NORMAL || type == Gui.HeartType.POISIONED)) { var u = type.getX(half, blinking); OGui.renderStunnedHeart(graphics, u - 52, x, y, v / 5); } else { - accessor.callRenderHeart(graphics, type, x, y, v, blinking, half); + original.call(instance, graphics, type, x, y, v, blinking, half); } } diff --git a/src/main/java/galena/oreganized/mixin/compat/LogStrippingFakeRecipesMixin.java b/src/main/java/galena/oreganized/mixin/compat/LogStrippingFakeRecipesMixin.java index c0ff771b..f7ae08c2 100644 --- a/src/main/java/galena/oreganized/mixin/compat/LogStrippingFakeRecipesMixin.java +++ b/src/main/java/galena/oreganized/mixin/compat/LogStrippingFakeRecipesMixin.java @@ -1,5 +1,6 @@ package galena.oreganized.mixin.compat; +import com.llamalad7.mixinextras.injector.ModifyReturnValue; import com.simibubi.create.content.kinetics.deployer.ManualApplicationRecipe; import com.simibubi.create.content.processing.recipe.ProcessingOutput; import com.simibubi.create.content.processing.recipe.ProcessingRecipeBuilder; @@ -12,8 +13,6 @@ import net.minecraft.world.item.crafting.Ingredient; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.util.ArrayList; import java.util.List; @@ -21,9 +20,9 @@ @Mixin(value = LogStrippingFakeRecipes.class, remap = false) public class LogStrippingFakeRecipesMixin { - @Inject(method = "createRecipes", at = @At("RETURN"), require = 0, cancellable = true) - private static void addGroovedRecipes(CallbackInfoReturnable> cir) { - var newList = new ArrayList<>(cir.getReturnValue()); + @ModifyReturnValue(method = "createRecipes", at = @At("RETURN"), require = 0) + private static List addGroovedRecipes(List value) { + var newList = new ArrayList<>(value); ScribeItem.getGroovedBlocks().forEach(entry -> { var blockId = BuiltInRegistries.BLOCK.getKey(entry.getKey()).getPath(); @@ -36,7 +35,7 @@ private static void addGroovedRecipes(CallbackInfoReturnable