/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.llamalad7.mixinextras.injector.ModifyExpressionValue
 *  com.llamalad7.mixinextras.injector.ModifyReturnValue
 *  net.minecraft.class_1297
 *  net.minecraft.class_1313
 *  net.minecraft.class_1657
 *  net.minecraft.class_2246
 *  net.minecraft.class_2248
 *  net.minecraft.class_243
 *  net.minecraft.class_4050
 *  net.minecraft.class_4184
 *  org.spongepowered.asm.mixin.Mixin
 *  org.spongepowered.asm.mixin.injection.At
 *  org.spongepowered.asm.mixin.injection.Inject
 *  org.spongepowered.asm.mixin.injection.ModifyArgs
 *  org.spongepowered.asm.mixin.injection.callback.CallbackInfo
 *  org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
 *  org.spongepowered.asm.mixin.injection.invoke.arg.Args
 */
package meteordevelopment.meteorclient.mixin;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.events.entity.EntityMoveEvent;
import meteordevelopment.meteorclient.events.entity.player.JumpVelocityMultiplierEvent;
import meteordevelopment.meteorclient.events.entity.player.PlayerMoveEvent;
import meteordevelopment.meteorclient.mixin.PlayerEntityAccessor;
import meteordevelopment.meteorclient.mixininterface.ICamera;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.systems.modules.combat.Hitboxes;
import meteordevelopment.meteorclient.systems.modules.movement.Flight;
import meteordevelopment.meteorclient.systems.modules.movement.Jesus;
import meteordevelopment.meteorclient.systems.modules.movement.NoFall;
import meteordevelopment.meteorclient.systems.modules.movement.NoSlow;
import meteordevelopment.meteorclient.systems.modules.movement.Velocity;
import meteordevelopment.meteorclient.systems.modules.movement.elytrafly.ElytraFly;
import meteordevelopment.meteorclient.systems.modules.render.ESP;
import meteordevelopment.meteorclient.systems.modules.render.FreeLook;
import meteordevelopment.meteorclient.systems.modules.render.Freecam;
import meteordevelopment.meteorclient.systems.modules.render.NoRender;
import meteordevelopment.meteorclient.systems.modules.world.HighwayBuilder;
import meteordevelopment.meteorclient.utils.Utils;
import meteordevelopment.meteorclient.utils.entity.fakeplayer.FakePlayerEntity;
import net.minecraft.class_1297;
import net.minecraft.class_1313;
import net.minecraft.class_1657;
import net.minecraft.class_2246;
import net.minecraft.class_2248;
import net.minecraft.class_243;
import net.minecraft.class_4050;
import net.minecraft.class_4184;
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.ModifyArgs;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;

@Mixin(value={class_1297.class})
public abstract class EntityMixin {
    @ModifyExpressionValue(method={"method_5692"}, at={@At(value="INVOKE", target="Lnet/minecraft/class_3610;method_15758(Lnet/minecraft/class_1922;Lnet/minecraft/class_2338;)Lnet/minecraft/class_243;")})
    private class_243 updateMovementInFluidFluidStateGetVelocity(class_243 vec) {
        if (this != MeteorClient.mc.field_1724) {
            return vec;
        }
        Velocity velocity = Modules.get().get(Velocity.class);
        if (velocity.isActive() && velocity.liquids.get().booleanValue()) {
            vec = vec.method_18805(velocity.getHorizontal(velocity.liquidsHorizontal), velocity.getVertical(velocity.liquidsVertical), velocity.getHorizontal(velocity.liquidsHorizontal));
        }
        return vec;
    }

    @Inject(method={"method_5799"}, at={@At(value="HEAD")}, cancellable=true)
    private void isTouchingWater(CallbackInfoReturnable<Boolean> info) {
        if (this != MeteorClient.mc.field_1724) {
            return;
        }
        if (Modules.get().get(Flight.class).isActive()) {
            info.setReturnValue((Object)false);
        }
        if (Modules.get().get(NoSlow.class).fluidDrag()) {
            info.setReturnValue((Object)false);
        }
    }

    @Inject(method={"method_5771"}, at={@At(value="HEAD")}, cancellable=true)
    private void isInLava(CallbackInfoReturnable<Boolean> info) {
        if (this != MeteorClient.mc.field_1724) {
            return;
        }
        if (Modules.get().get(Flight.class).isActive()) {
            info.setReturnValue((Object)false);
        }
        if (Modules.get().get(NoSlow.class).fluidDrag()) {
            info.setReturnValue((Object)false);
        }
    }

    @Inject(method={"method_5700"}, at={@At(value="HEAD")})
    private void onBubbleColumnSurfaceCollision(CallbackInfo info) {
        if (this != MeteorClient.mc.field_1724) {
            return;
        }
        Jesus jesus = Modules.get().get(Jesus.class);
        if (jesus.isActive()) {
            jesus.isInBubbleColumn = true;
        }
    }

    @Inject(method={"method_5764"}, at={@At(value="HEAD")})
    private void onBubbleColumnCollision(CallbackInfo info) {
        if (this != MeteorClient.mc.field_1724) {
            return;
        }
        Jesus jesus = Modules.get().get(Jesus.class);
        if (jesus.isActive()) {
            jesus.isInBubbleColumn = true;
        }
    }

    @ModifyExpressionValue(method={"method_5790"}, at={@At(value="INVOKE", target="Lnet/minecraft/class_1297;method_5869()Z")})
    private boolean isSubmergedInWater(boolean submerged) {
        if (this != MeteorClient.mc.field_1724) {
            return submerged;
        }
        if (Modules.get().get(NoSlow.class).fluidDrag()) {
            return false;
        }
        if (Modules.get().get(Flight.class).isActive()) {
            return false;
        }
        return submerged;
    }

    @ModifyArgs(method={"method_5697(Lnet/minecraft/class_1297;)V"}, at=@At(value="INVOKE", target="Lnet/minecraft/class_1297;method_5762(DDD)V"))
    private void onPushAwayFrom(Args args, class_1297 entity) {
        Velocity velocity = Modules.get().get(Velocity.class);
        if (this == MeteorClient.mc.field_1724 && velocity.isActive() && velocity.entityPush.get().booleanValue()) {
            double multiplier = velocity.entityPushAmount.get();
            args.set(0, (Object)((Double)args.get(0) * multiplier));
            args.set(2, (Object)((Double)args.get(2) * multiplier));
        } else if (entity instanceof FakePlayerEntity) {
            FakePlayerEntity player = (FakePlayerEntity)entity;
            if (player.doNotPush) {
                args.set(0, (Object)0.0);
                args.set(2, (Object)0.0);
            }
        }
    }

    @ModifyReturnValue(method={"method_23313"}, at={@At(value="RETURN")})
    private float onGetJumpVelocityMultiplier(float original) {
        if (this == MeteorClient.mc.field_1724) {
            JumpVelocityMultiplierEvent event = (JumpVelocityMultiplierEvent)MeteorClient.EVENT_BUS.post((Object)JumpVelocityMultiplierEvent.get());
            return original * event.multiplier;
        }
        return original;
    }

    @Inject(method={"method_5784"}, at={@At(value="HEAD")})
    private void onMove(class_1313 type, class_243 movement, CallbackInfo info) {
        if (this == MeteorClient.mc.field_1724) {
            MeteorClient.EVENT_BUS.post((Object)PlayerMoveEvent.get(type, movement));
        } else {
            MeteorClient.EVENT_BUS.post((Object)EntityMoveEvent.get((class_1297)this, movement));
        }
    }

    @ModifyExpressionValue(method={"method_23326"}, at={@At(value="INVOKE", target="Lnet/minecraft/class_2680;method_26204()Lnet/minecraft/class_2248;")})
    private class_2248 modifyVelocityMultiplierBlock(class_2248 original) {
        if (this != MeteorClient.mc.field_1724) {
            return original;
        }
        if (original == class_2246.field_10114 && Modules.get().get(NoSlow.class).soulSand()) {
            return class_2246.field_10340;
        }
        if (original == class_2246.field_21211 && Modules.get().get(NoSlow.class).honeyBlock()) {
            return class_2246.field_10340;
        }
        return original;
    }

    @ModifyReturnValue(method={"method_5756(Lnet/minecraft/class_1657;)Z"}, at={@At(value="RETURN")})
    private boolean isInvisibleToCanceller(boolean original) {
        if (!Utils.canUpdate()) {
            return original;
        }
        ESP esp = Modules.get().get(ESP.class);
        if (Modules.get().get(NoRender.class).noInvisibility() || esp.isActive() && !esp.shouldSkip((class_1297)this)) {
            return false;
        }
        return original;
    }

    @Inject(method={"method_5851"}, at={@At(value="HEAD")}, cancellable=true)
    private void isGlowing(CallbackInfoReturnable<Boolean> info) {
        if (Modules.get().get(NoRender.class).noGlowing()) {
            info.setReturnValue((Object)false);
        }
    }

    @Inject(method={"method_5871"}, at={@At(value="HEAD")}, cancellable=true)
    private void onGetTargetingMargin(CallbackInfoReturnable<Float> info) {
        double v = Modules.get().get(Hitboxes.class).getEntityValue((class_1297)this);
        if (v != 0.0) {
            info.setReturnValue((Object)Float.valueOf((float)v));
        }
    }

    @Inject(method={"method_5756"}, at={@At(value="HEAD")}, cancellable=true)
    private void onIsInvisibleTo(class_1657 player, CallbackInfoReturnable<Boolean> info) {
        if (player == null) {
            info.setReturnValue((Object)false);
        }
    }

    @Inject(method={"method_18376"}, at={@At(value="HEAD")}, cancellable=true)
    private void getPoseHook(CallbackInfoReturnable<class_4050> info) {
        if (this != MeteorClient.mc.field_1724) {
            return;
        }
        if (Modules.get().get(ElytraFly.class).canPacketEfly()) {
            info.setReturnValue((Object)class_4050.field_18077);
        }
    }

    @ModifyReturnValue(method={"method_18376"}, at={@At(value="RETURN")})
    private class_4050 modifyGetPose(class_4050 original) {
        if (this != MeteorClient.mc.field_1724) {
            return original;
        }
        if (original == class_4050.field_18081 && !MeteorClient.mc.field_1724.method_5715() && ((PlayerEntityAccessor)MeteorClient.mc.field_1724).meteor$canChangeIntoPose(class_4050.field_18076)) {
            return class_4050.field_18076;
        }
        return original;
    }

    @ModifyReturnValue(method={"method_21750"}, at={@At(value="RETURN")})
    private boolean cancelBounce(boolean original) {
        return Modules.get().get(NoFall.class).cancelBounce() || original;
    }

    @Inject(method={"method_5872"}, at={@At(value="HEAD")}, cancellable=true)
    private void updateChangeLookDirection(double cursorDeltaX, double cursorDeltaY, CallbackInfo ci) {
        if (this != MeteorClient.mc.field_1724) {
            return;
        }
        Freecam freecam = Modules.get().get(Freecam.class);
        FreeLook freeLook = Modules.get().get(FreeLook.class);
        if (freecam.isActive()) {
            freecam.changeLookDirection(cursorDeltaX * 0.15, cursorDeltaY * 0.15);
            ci.cancel();
        } else if (Modules.get().isActive(HighwayBuilder.class)) {
            class_4184 camera = MeteorClient.mc.field_1773.method_19418();
            ((ICamera)camera).meteor$setRot((double)camera.method_19330() + cursorDeltaX * 0.15, (double)camera.method_19329() + cursorDeltaY * 0.15);
            ci.cancel();
        } else if (freeLook.cameraMode()) {
            freeLook.cameraYaw += (float)(cursorDeltaX / (double)freeLook.sensitivity.get().floatValue());
            freeLook.cameraPitch += (float)(cursorDeltaY / (double)freeLook.sensitivity.get().floatValue());
            if (Math.abs(freeLook.cameraPitch) > 90.0f) {
                freeLook.cameraPitch = freeLook.cameraPitch > 0.0f ? 90.0f : -90.0f;
            }
            ci.cancel();
        }
    }
}

