From bfc69c3780fad402a89348cde6da7e18c8e15aed Mon Sep 17 00:00:00 2001 From: RubixDev Date: Thu, 30 May 2024 18:28:47 +0800 Subject: [PATCH] fix: incompatible with Farmer's Delight fixes #54 --- .../rug/mixins/HungerManagerMixin.java | 48 +++++++++++-------- .../rug/mixins/PlayerEntityMixin.java | 7 --- .../java/de/rubixdev/rug/util/Storage.java | 2 - 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/rubixdev/rug/mixins/HungerManagerMixin.java b/src/main/java/de/rubixdev/rug/mixins/HungerManagerMixin.java index 240a3ba..3a1fb6a 100644 --- a/src/main/java/de/rubixdev/rug/mixins/HungerManagerMixin.java +++ b/src/main/java/de/rubixdev/rug/mixins/HungerManagerMixin.java @@ -1,15 +1,17 @@ package de.rubixdev.rug.mixins; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import de.rubixdev.rug.RugSettings; -import de.rubixdev.rug.util.Storage; import net.minecraft.entity.player.HungerManager; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; -import net.minecraft.world.GameRules; import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Mixin; +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; //#if MC >= 12006 @@ -22,48 +24,54 @@ @Mixin(HungerManager.class) public class HungerManagerMixin { - @Redirect( + @Unique private int pendingHealing = 0; + + @WrapOperation( method = "update", at = @At( value = "FIELD", target = "Lnet/minecraft/entity/player/HungerManager;foodLevel:I", opcode = Opcodes.PUTFIELD)) - private void onUpdate(HungerManager hungerManager, int value) { + private void dontSetFoodLevel(HungerManager instance, int value, Operation original) { if (!RugSettings.peacefulHunger) { - hungerManager.setFoodLevel(value); + original.call(instance, value); } } - @Redirect( + @ModifyExpressionValue( method = "update", at = - @At( - value = "INVOKE", - target = "Lnet/minecraft/world/GameRules;getBoolean(Lnet/minecraft/world/GameRules$Key;)Z")) - private boolean onUpdate(GameRules gameRules, GameRules.Key rule) { - if (RugSettings.foodInstantHeal) { - return false; - } else { - return gameRules.getBoolean(rule); - } + @At( + value = "INVOKE", + target = "Lnet/minecraft/world/GameRules;getBoolean(Lnet/minecraft/world/GameRules$Key;)Z")) + private boolean noNaturalRegen(boolean original) { + return original && !RugSettings.foodInstantHeal; } @Inject(method = "eat", at = @At("HEAD"), cancellable = true) //#if MC >= 12006 - private void onEat(ItemStack stack, CallbackInfo ci) { + private void instantHeal(ItemStack stack, CallbackInfo ci) { FoodComponent foodComponent = stack.get(DataComponentTypes.FOOD); if (RugSettings.foodInstantHeal && foodComponent != null) { - Storage.player.heal(foodComponent.nutrition()); + pendingHealing += foodComponent.nutrition(); ci.cancel(); } } //#else - //$$ private void onEat(Item item, ItemStack stack, CallbackInfo ci) { + //$$ private void instantHeal(Item item, ItemStack stack, CallbackInfo ci) { //$$ if (RugSettings.foodInstantHeal && item.isFood()) { - //$$ Storage.player.heal(Objects.requireNonNull(item.getFoodComponent()).getHunger()); + //$$ pendingHealing += Objects.requireNonNull(item.getFoodComponent()).getHunger(); //$$ ci.cancel(); //$$ } //$$ } //#endif + + @Inject(method = "update", at = @At("HEAD")) + private void applyPendingHealing(PlayerEntity player, CallbackInfo ci) { + if (pendingHealing > 0) { + player.heal(pendingHealing); + pendingHealing = 0; + } + } } diff --git a/src/main/java/de/rubixdev/rug/mixins/PlayerEntityMixin.java b/src/main/java/de/rubixdev/rug/mixins/PlayerEntityMixin.java index 71164ff..cbd751b 100644 --- a/src/main/java/de/rubixdev/rug/mixins/PlayerEntityMixin.java +++ b/src/main/java/de/rubixdev/rug/mixins/PlayerEntityMixin.java @@ -1,11 +1,9 @@ package de.rubixdev.rug.mixins; import de.rubixdev.rug.RugSettings; -import de.rubixdev.rug.util.Storage; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.ItemStack; import net.minecraft.world.Difficulty; import net.minecraft.world.GameRules; import net.minecraft.world.World; @@ -36,11 +34,6 @@ private boolean onTickMovement(GameRules gameRules, GameRules.Key cir) { - Storage.player = (PlayerEntity) (Object) this; - } - @Inject(method = "canHarvest", at = @At("HEAD"), cancellable = true) private void onCanHarvest(BlockState block, CallbackInfoReturnable cir) { if ((RugSettings.silkTouchSpawners && block.isOf(Blocks.SPAWNER)) diff --git a/src/main/java/de/rubixdev/rug/util/Storage.java b/src/main/java/de/rubixdev/rug/util/Storage.java index 6ad89c7..97e5b6f 100644 --- a/src/main/java/de/rubixdev/rug/util/Storage.java +++ b/src/main/java/de/rubixdev/rug/util/Storage.java @@ -1,12 +1,10 @@ package de.rubixdev.rug.util; -import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.level.storage.LevelStorage; public class Storage { - public static PlayerEntity player; public static BlockPos blockPos; public static World world;