package com.thebrokenrail.twine.mixin;

import com.thebrokenrail.twine.component.StageDataComponent;
import com.thebrokenrail.twine.util.StageUtil;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.Monster;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
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.CallbackInfo;

@Mixin(LivingEntity.class)
public class MixinLivingEntity {
    @Inject(at = @At("HEAD"), method = "takeKnockback", cancellable = true)
    public void takeKnockback(float f, double d, double e, CallbackInfo info) {
        if (this instanceof Monster) {
            StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) ((LivingEntity) (Object) this).getEntityWorld());
            BlockPos blockPos = ((LivingEntity) (Object) this).getBlockPos();
            int stage = component.findEffectiveStageOfChunk((ServerWorld) (((LivingEntity) (Object) this).getEntityWorld()), blockPos);

            if (stage >= StageUtil.MOBS_TAKE_NO_KNOCKBACK) {
                info.cancel();
            }
        }
    }
}
