Skip to content

Commit

Permalink
fix: incompatible with Farmer's Delight
Browse files Browse the repository at this point in the history
fixes #54
  • Loading branch information
RubixDev committed May 30, 2024
1 parent 24e660f commit bfc69c3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
48 changes: 28 additions & 20 deletions src/main/java/de/rubixdev/rug/mixins/HungerManagerMixin.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Void> 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<GameRules.BooleanRule> 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;
}
}
}
7 changes: 0 additions & 7 deletions src/main/java/de/rubixdev/rug/mixins/PlayerEntityMixin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,11 +34,6 @@ private boolean onTickMovement(GameRules gameRules, GameRules.Key<GameRules.Bool
return !RugSettings.foodInstantHeal && gameRules.getBoolean(rule);
}

@Inject(method = "eatFood", at = @At("HEAD"))
private void savePlayer(World world, ItemStack stack, CallbackInfoReturnable<ItemStack> cir) {
Storage.player = (PlayerEntity) (Object) this;
}

@Inject(method = "canHarvest", at = @At("HEAD"), cancellable = true)
private void onCanHarvest(BlockState block, CallbackInfoReturnable<Boolean> cir) {
if ((RugSettings.silkTouchSpawners && block.isOf(Blocks.SPAWNER))
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/de/rubixdev/rug/util/Storage.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit bfc69c3

Please sign in to comment.