package ru.dbotthepony.mc.otm.mixin;

import net.minecraft.world.level.block.AnvilBlock;
import net.minecraft.world.level.block.state.BlockState;
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 ru.dbotthepony.mc.otm.registry.MBlocks;

@Mixin(AnvilBlock.class)
@SuppressWarnings("unused")
public class MixinAnvilBlock {
	@Inject(
		method = "damage(Lnet/minecraft/world/level/block/state/BlockState;)Lnet/minecraft/world/level/block/state/BlockState;",
		at = @At("HEAD"),
		cancellable = true)
	private static void damage(BlockState pState, CallbackInfoReturnable<BlockState> info) {
		var list = MBlocks.INSTANCE.getTRITANIUM_ANVIL();

		for (int i = 0; i < list.size(); i++) {
			if (pState.is(list.get(i))) {
				if (i == list.size() - 1) {
					info.setReturnValue(null);
				} else {
					info.setReturnValue(list.get(i + 1).defaultBlockState().setValue(AnvilBlock.FACING, pState.getValue(AnvilBlock.FACING)));
				}

				return;
			}
		}
	}
}
