diff --git a/src/main/java/toughasnails/mixin/MixinFoodData.java b/src/main/java/toughasnails/mixin/MixinFoodData.java new file mode 100644 index 00000000..77d74389 --- /dev/null +++ b/src/main/java/toughasnails/mixin/MixinFoodData.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright 2023, the Glitchfiend Team. + * All rights reserved. + ******************************************************************************/ +package toughasnails.mixin; + +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.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import toughasnails.thirst.ThirstHooks; + +@Mixin(FoodData.class) +public abstract class MixinFoodData +{ + @Inject(method="tick", at=@At(value="HEAD"), cancellable = true) + public void onTick(Player player, CallbackInfo ci) + { + ThirstHooks.doFoodDataTick((FoodData)(Object)this, player); + ci.cancel(); + } +} diff --git a/src/main/java/toughasnails/mixin/MixinPlayer.java b/src/main/java/toughasnails/mixin/MixinPlayer.java new file mode 100644 index 00000000..d119e2ee --- /dev/null +++ b/src/main/java/toughasnails/mixin/MixinPlayer.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright 2023, the Glitchfiend Team. + * All rights reserved. + ******************************************************************************/ +package toughasnails.mixin; + +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.player.Abilities; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.food.FoodData; +import net.minecraft.world.level.Level; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import toughasnails.thirst.ThirstHooks; + +@Mixin(Player.class) +public abstract class MixinPlayer extends LivingEntity +{ + @Shadow + protected FoodData foodData = new FoodData(); + + @Shadow + @Final + private Abilities abilities; + + private MixinPlayer(EntityType type, Level level) + { + super(type, level); + } + + @Inject(method="causeFoodExhaustion", at=@At(value="HEAD")) + public void onCauseFoodExhaustion(float exhaustion, CallbackInfo ci) + { + if (!this.abilities.invulnerable) + { + if (!this.level().isClientSide) + { + ThirstHooks.onCauseFoodExhaustion((Player)(Object)this, exhaustion); + } + } + } +} diff --git a/src/main/resources/META-INF/coremods.json b/src/main/resources/META-INF/coremods.json deleted file mode 100644 index cf63b520..00000000 --- a/src/main/resources/META-INF/coremods.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "tan_transformer": "transformers/tan.js" -} \ No newline at end of file diff --git a/src/main/resources/toughasnails.mixins.json b/src/main/resources/toughasnails.mixins.json index 2c6e62ed..61564c99 100644 --- a/src/main/resources/toughasnails.mixins.json +++ b/src/main/resources/toughasnails.mixins.json @@ -4,6 +4,8 @@ "compatibilityLevel": "JAVA_17", "refmap": "toughasnails.refmap.json", "mixins": [ + "MixinFoodData", + "MixinPlayer" ], "client": [ "client.MixinGui", diff --git a/src/main/resources/transformers/tan.js b/src/main/resources/transformers/tan.js deleted file mode 100644 index e61aa531..00000000 --- a/src/main/resources/transformers/tan.js +++ /dev/null @@ -1,103 +0,0 @@ -/******************************************************************************* - * Copyright 2021, the Glitchfiend Team. - * All rights reserved. - ******************************************************************************/ - var ASM = Java.type("net.minecraftforge.coremod.api.ASMAPI"); - - var Opcodes = Java.type('org.objectweb.asm.Opcodes'); - var InsnList = Java.type('org.objectweb.asm.tree.InsnList'); - var InsnNode = Java.type('org.objectweb.asm.tree.InsnNode'); - var VarInsnNode = Java.type('org.objectweb.asm.tree.VarInsnNode'); - -var BLIT = ASM.mapMethod("m_93228_"); - -function Transformation(name, desc) { - this.name = name; - this.desc = desc; - this.funcs = []; - - for (var i = 2; i < arguments.length; i++) { - this.funcs.push(arguments[i]); - } - } - - var TRANSFORMATIONS = { - "net/minecraft/world/entity/player/Player": [ - new Transformation(ASM.mapMethod("m_36399_"), "(F)V", patchCauseFoodExhaustion) //causeFoodExhaustion - ], - "net/minecraft/world/food/FoodData": [ - new Transformation(ASM.mapMethod("m_38710_"), "(Lnet/minecraft/world/entity/player/Player;)V", patchFoodDataTick) //tick - ] - }; - - function applyTransformations(classNode, method) { - if (!(classNode.name in TRANSFORMATIONS)) { - return; - } - - var transformations = TRANSFORMATIONS[classNode.name]; - - for (var i = 0; i < transformations.length; i++) { - var transformation = transformations[i]; - - if (transformation.name == method.name && transformation.desc == method.desc) { - log("Transforming " + method.name + " " + method.desc + " in " + classNode.name); - - for each (var func in transformation.funcs) { - func(method); - } - break; - } - } - } - - function log(message) { - print("[Tough As Nails Transformer]: " + message); - } - - function initializeCoreMod() { - return { - "thirst_transformer": { - "target": { - "type": "CLASS", - "names": function(listofclasses) { return Object.keys(TRANSFORMATIONS); } - }, - "transformer": function(classNode) { - for each (var method in classNode.methods) { - applyTransformations(classNode, method); - } - - return classNode; - } - } - }; - } - - function patchCauseFoodExhaustion(node) { - var insns = new InsnList(); - insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); - insns.add(new VarInsnNode(Opcodes.FLOAD, 1)); - insns.add(ASM.buildMethodCall( - "toughasnails/thirst/ThirstHooks", - "onCauseFoodExhaustion", - "(Lnet/minecraft/world/entity/player/Player;F)V", - ASM.MethodType.STATIC - )); - node.instructions.insertBefore(node.instructions.getFirst(), insns); - log("Successfully patched causeFoodExhaustion"); - } - - function patchFoodDataTick(node) { - var insns = new InsnList(); - insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); - insns.add(new VarInsnNode(Opcodes.ALOAD, 1)); - insns.add(ASM.buildMethodCall( - "toughasnails/thirst/ThirstHooks", - "doFoodDataTick", - "(Lnet/minecraft/world/food/FoodData;Lnet/minecraft/world/entity/player/Player;)V", - ASM.MethodType.STATIC - )); - insns.add(new InsnNode(Opcodes.RETURN)); - node.instructions.insertBefore(node.instructions.getFirst(), insns); - log("Successfully patched tick"); - } \ No newline at end of file