Skip to content

Instantly share code, notes, and snippets.

@Daomephsta
Created February 15, 2019 18:49
Show Gist options
  • Save Daomephsta/cf401106e2b59a76bd74b4788cb987da to your computer and use it in GitHub Desktop.
Save Daomephsta/cf401106e2b59a76bd74b4788cb987da to your computer and use it in GitHub Desktop.
Bach Mixin
{
"required": true,
"package": "daomephsta.bach.common.mixin",
"compatibilityLevel": "JAVA_8",
"mixins":
[
"item.food.MixinWolfEntity"
],
"injectors":
{
"defaultRequire": 1
}
}
{
"id": "bach",
"name": "Bach",
"description": "Composition over inheritance",
"version": "1.0.0",
"side": "universal",
"initializers":
[
"daomephsta.bach.common.BachInitialiser"
],
"requires":
{
"fabric": "*"
},
"mixins":
{
"client": "bach.client.json",
"common": "bach.common.json"
}
}
package daomephsta.bach.common.mixin.item.food;
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 daomephsta.bach.common.component.item.food.IEdible;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.passive.TameableEntity;
import net.minecraft.entity.passive.WolfEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.*;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
@Mixin(WolfEntity.class)
public abstract class MixinWolfEntity extends TameableEntity
{
public MixinWolfEntity(EntityType<?> entityType, World world)
{
super(entityType, world);
}
@Inject(method = "interactMob", at = @At("HEAD"))
public void interactMob(PlayerEntity player, Hand hand, CallbackInfoReturnable<Boolean> info)
{
System.out.println("Interact");
ItemStack handStack = player.getStackInHand(hand);
Item handItem = handStack.getItem();
if (this.isTamed() && !handStack.isEmpty())
{
IEdible edible = FoodHooks.getEdibleComponent(handItem);
if (edible != null)
{
if (edible.isWolfFood() && this.getHealth() < 20.0F)
{
if (!player.abilities.creativeMode)
handStack.subtractAmount(1);
this.heal(edible.getHungerRestored(handStack));
info.setReturnValue(Boolean.TRUE);
}
}
}
}
@Inject(method = "isBreedingItem", at = @At("HEAD"))
public void isBreedingItem(ItemStack stack, CallbackInfoReturnable<Boolean> info)
{
if (FoodHooks.isWolfFood(stack.getItem()))
info.setReturnValue(Boolean.TRUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment