Skip to content

Commit

Permalink
use MixinExtras for most redirects to avoid mixin conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Sep 23, 2024
1 parent 845437a commit 8e07c18
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 61 deletions.
15 changes: 8 additions & 7 deletions src/main/java/galena/oreganized/mixin/AbstractArrowMixin.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
18 changes: 9 additions & 9 deletions src/main/java/galena/oreganized/mixin/BlockMixin.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
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;
import net.minecraft.world.level.storage.loot.LootParams;
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<ItemStack> modifyLootBuilder(BlockState state, LootParams.Builder builder, Operation<List<ItemStack>> 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);
}

}
22 changes: 16 additions & 6 deletions src/main/java/galena/oreganized/mixin/FoodDataMixin.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
11 changes: 4 additions & 7 deletions src/main/java/galena/oreganized/mixin/IceBlockMixin.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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;
}

}
14 changes: 0 additions & 14 deletions src/main/java/galena/oreganized/mixin/client/GuiAccessor.java

This file was deleted.

11 changes: 5 additions & 6 deletions src/main/java/galena/oreganized/mixin/client/GuiMixin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,28 +10,25 @@
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(
from = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/Gui;renderHeart(Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/client/gui/Gui$HeartType;IIIZZ)V", ordinal = 1),
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<Void> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,18 +13,16 @@
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;

@Mixin(value = LogStrippingFakeRecipes.class, remap = false)
public class LogStrippingFakeRecipesMixin {

@Inject(method = "createRecipes", at = @At("RETURN"), require = 0, cancellable = true)
private static void addGroovedRecipes(CallbackInfoReturnable<List<ManualApplicationRecipe>> cir) {
var newList = new ArrayList<>(cir.getReturnValue());
@ModifyReturnValue(method = "createRecipes", at = @At("RETURN"), require = 0)
private static List<ManualApplicationRecipe> addGroovedRecipes(List<ManualApplicationRecipe> value) {
var newList = new ArrayList<>(value);

ScribeItem.getGroovedBlocks().forEach(entry -> {
var blockId = BuiltInRegistries.BLOCK.getKey(entry.getKey()).getPath();
Expand All @@ -36,7 +35,7 @@ private static void addGroovedRecipes(CallbackInfoReturnable<List<ManualApplicat
);
});

cir.setReturnValue(newList);
return newList;
}

}
2 changes: 1 addition & 1 deletion src/main/resources/oreganized.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"compat.LogStrippingFakeRecipesMixin"
],
"client": [
"client.GuiAccessor", "client.GuiMixin", "client.HumanoidArmorLayerAccessor", "client.HumanoidArmorLayerMixin",
"client.GuiMixin", "client.HumanoidArmorLayerAccessor", "client.HumanoidArmorLayerMixin",
"client.HumanoidModelMixin"
],
"server": [],
Expand Down

0 comments on commit 8e07c18

Please sign in to comment.