package blade.addon.mixin;

import blade.addon.features.other.SwingAnimation;
import blade.addon.utils.config.values.Visual;
import blade.addon.utils.data.EntityUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.component.BlockTransformer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

/**
 * 26.3 removed the dedicated tool item classes (ShovelItem / AxeItem / HoeItem). Their
 * right-click behaviour is now data driven through the BLOCK_TRANSFORMER item component,
 * applied from Item#useOn via net.minecraft.core.component.BlockTransformer#transformBlock,
 * which is also where the transform sound is played. This replaces the old ShovelItemMixin,
 * which hooked ShovelItem#useOn at the Level#playSound call.
 */
@Mixin(BlockTransformer.class)
public class BlockTransformerMixin {

    @Redirect(method = "transformBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;playSound(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/core/BlockPos;Lnet/minecraft/sounds/SoundEvent;Lnet/minecraft/sounds/SoundSource;FF)V"))
    private void bladeAddons$onTransformSound(Level level, Entity entity, BlockPos pos, SoundEvent sound, SoundSource source, float volume, float pitch) {
        // Only the shovel flatten sound, so that e.g. axe stripping keeps its normal swing.
        if (Visual.stopShovelFlattening
                && sound != null
                && sound.location().equals(SoundEvents.SHOVEL_FLATTEN.value().location())
                && EntityUtil.isClientPlayer(entity)) {
            SwingAnimation.ignoreNext(2);
        }

        level.playSound(entity, pos, sound, source, volume, pitch);
    }
}