/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.remains_cave.mixin;

import net.minecraft.core.component.DataComponentType;
import net.minecraft.world.item.ItemStack;
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 software.bernie.geckolib.GeckoLibConstants;

import java.lang.reflect.Field;
import java.util.function.Supplier;

@Mixin(ItemStack.class)
public class ItemStackMixin {
    private static final DataComponentType<Long> GECKOLIB_ID_COMPONENT;

    static {
        DataComponentType<Long> comp = null;
        try {
            // GeckoLibConstants.STACK_ANIMATABLE_ID_COMPONENT 是一个 Supplier<DataComponentType<Long>>
            Field field = GeckoLibConstants.class.getDeclaredField("STACK_ANIMATABLE_ID_COMPONENT");
            field.setAccessible(true);
            Supplier<DataComponentType<Long>> supplier = (Supplier<DataComponentType<Long>>) field.get(null);
            comp = supplier.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        GECKOLIB_ID_COMPONENT = comp;
    }

    @Inject(method = "copy", at = @At("RETURN"))
    private void onCopy(CallbackInfoReturnable<ItemStack> cir) {
        ItemStack copy = cir.getReturnValue();
        if (GECKOLIB_ID_COMPONENT != null) {
            copy.remove(GECKOLIB_ID_COMPONENT);
        }
    }
}