/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1297
 *  net.minecraft.class_2244
 *  net.minecraft.class_2338
 *  net.minecraft.class_2338$class_2339
 */
package meteordevelopment.meteorclient.systems.modules.movement;

import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.mixininterface.IVec3d;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.DoubleSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1297;
import net.minecraft.class_2244;
import net.minecraft.class_2338;

public class ReverseStep
extends Module {
    private final SettingGroup sgGeneral;
    private final Setting<Double> fallSpeed;
    private final Setting<Double> fallDistance;
    private final Setting<Boolean> vehicles;

    public ReverseStep() {
        super(Categories.Movement, "reverse-step", "Allows you to fall down blocks at a greater speed.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.fallSpeed = this.sgGeneral.add(((DoubleSetting.Builder)((DoubleSetting.Builder)new DoubleSetting.Builder().name("fall-speed")).description("How fast to fall in blocks per second.")).defaultValue(3.0).min(0.0).build());
        this.fallDistance = this.sgGeneral.add(((DoubleSetting.Builder)((DoubleSetting.Builder)new DoubleSetting.Builder().name("fall-distance")).description("The maximum fall distance this setting will activate at.")).defaultValue(3.0).min(0.0).build());
        this.vehicles = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("vehicles")).description("Whether or not reverse step should affect vehicles.")).defaultValue(false)).build());
    }

    @EventHandler
    private void onTick(TickEvent.Post event) {
        class_1297 vehicle = this.mc.field_1724.method_5854();
        if (vehicle != null && this.vehicles.get().booleanValue()) {
            if (this.canSnap(vehicle)) {
                ((IVec3d)vehicle.method_18798()).meteor$setY(-this.fallSpeed.get().doubleValue());
            }
        } else {
            if (this.mc.field_1724.method_21754() || this.mc.field_1724.field_6250 == 0.0f && this.mc.field_1724.field_6212 == 0.0f) {
                return;
            }
            if (!this.isOnBed() && this.canSnap((class_1297)this.mc.field_1724)) {
                ((IVec3d)this.mc.field_1724.method_18798()).meteor$setY(-this.fallSpeed.get().doubleValue());
            }
        }
    }

    private boolean canSnap(class_1297 entity) {
        if (!entity.method_24828() || entity.method_5869() || entity.method_5771() || this.mc.field_1690.field_1903.method_1434() || entity.field_5960) {
            return false;
        }
        return !this.mc.field_1687.method_18026(entity.method_5829().method_989(0.0, (double)((float)(-(this.fallDistance.get() + 0.01))), 0.0));
    }

    private boolean isOnBed() {
        class_2338.class_2339 blockPos = this.mc.field_1724.method_24515().method_25503();
        if (this.check(blockPos, 0, 0)) {
            return true;
        }
        double xa = this.mc.field_1724.method_23317() - (double)blockPos.method_10263();
        double za = this.mc.field_1724.method_23321() - (double)blockPos.method_10260();
        if (xa >= 0.0 && xa <= 0.3 && this.check(blockPos, -1, 0)) {
            return true;
        }
        if (xa >= 0.7 && this.check(blockPos, 1, 0)) {
            return true;
        }
        if (za >= 0.0 && za <= 0.3 && this.check(blockPos, 0, -1)) {
            return true;
        }
        if (za >= 0.7 && this.check(blockPos, 0, 1)) {
            return true;
        }
        if (xa >= 0.0 && xa <= 0.3 && za >= 0.0 && za <= 0.3 && this.check(blockPos, -1, -1)) {
            return true;
        }
        if (xa >= 0.0 && xa <= 0.3 && za >= 0.7 && this.check(blockPos, -1, 1)) {
            return true;
        }
        if (xa >= 0.7 && za >= 0.0 && za <= 0.3 && this.check(blockPos, 1, -1)) {
            return true;
        }
        return xa >= 0.7 && za >= 0.7 && this.check(blockPos, 1, 1);
    }

    private boolean check(class_2338.class_2339 blockPos, int x, int z) {
        blockPos.method_10100(x, 0, z);
        boolean is = this.mc.field_1687.method_8320((class_2338)blockPos).method_26204() instanceof class_2244;
        blockPos.method_10100(-x, 0, -z);
        return is;
    }
}

