package me.jonathing.minecraft.foragecraft.coremod.mixin;

import me.jonathing.minecraft.foragecraft.coremod.hooks.ItemHooks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUseContext;
import net.minecraft.item.Items;
import net.minecraft.util.ActionResultType;
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;

/**
 * This mixin class is used to make certain modifications to the {@link Item} class to ForageCraft's needs.
 *
 * @author Jonathing
 * @see Item
 * @since 2.1.0
 */
@Mixin(Item.class)
public class ItemMixin
{
    /**
     * This mixin method hooks into the {@link org.spongepowered.asm.mixin.injection.points.MethodHead MethodHead} of
     * the {@link Item#useOn(ItemUseContext)} method to give the {@link ItemUseContext} to the
     * {@link ItemHooks#useOn(ItemUseContext)} method. If the result is not {@code null}, the mixin method sets
     * the return value to that method's result and cancels the method.
     *
     * @param pContext The item context in which the player right clicks any block with an item in hand.
     * @param callback Mixin's way of returning the resulting {@link ActionResultType} of the action.
     * @see ItemHooks#useOn(ItemUseContext)
     * @see Item#useOn(ItemUseContext)
     */
    @Inject(
            method = "useOn(Lnet/minecraft/item/ItemUseContext;)Lnet/minecraft/util/ActionResultType;",
            at = @At("HEAD"),
            cancellable = true
    )
    private void useOn(ItemUseContext pContext, CallbackInfoReturnable<ActionResultType> callback)
    {
        if (pContext != null && pContext.getItemInHand().getItem().equals(Items.STICK))
            callback.setReturnValue(ItemHooks.useOn(pContext));
    }
}
