/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_2596
 *  net.minecraft.class_2793
 *  net.minecraft.class_2828
 *  net.minecraft.class_2828$class_2830
 *  net.minecraft.class_2828$class_5911
 */
package meteordevelopment.meteorclient.systems.modules.donut;

import java.util.ArrayDeque;
import java.util.Deque;
import meteordevelopment.meteorclient.events.packets.PacketEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.DoubleSetting;
import meteordevelopment.meteorclient.settings.IntSetting;
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_2596;
import net.minecraft.class_2793;
import net.minecraft.class_2828;

public class AntiCheatBypass
extends Module {
    private final SettingGroup sgMovement;
    private final SettingGroup sgPackets;
    private final SettingGroup sgSpoofing;
    private final Setting<Boolean> noFallBypass;
    private final Setting<Boolean> strictStep;
    private final Setting<Boolean> smoothSprint;
    private final Setting<Double> speedCap;
    private final Setting<Boolean> packetDelay;
    private final Setting<Integer> delayTicks;
    private final Setting<Boolean> cancelRedundant;
    private final Setting<Boolean> teleportConfirmBypass;
    private final Setting<Boolean> spoofOnGround;
    private final Setting<Boolean> spoofSneaking;
    private final Setting<Boolean> rotationSmoothing;
    private final Deque<class_2828> packetQueue;
    private double lastX;
    private double lastY;
    private double lastZ;
    private float lastYaw;
    private float lastPitch;

    public AntiCheatBypass() {
        super(Categories.Donut, "anti-cheat-bypass", "Packet-level tweaks to bypass Donut SMP's anti-cheat. Toggle only what you need.");
        this.sgMovement = this.settings.createGroup("Movement");
        this.sgPackets = this.settings.createGroup("Packets");
        this.sgSpoofing = this.settings.createGroup("Spoofing");
        this.noFallBypass = this.sgMovement.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("no-fall-bypass")).description("Always sends onGround=true to prevent fall-damage flags.")).defaultValue(false)).build());
        this.strictStep = this.sgMovement.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("strict-step")).description("Limits step height reported to 0.6 to avoid step-up detection.")).defaultValue(false)).build());
        this.smoothSprint = this.sgMovement.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("smooth-sprint")).description("Sends sprint packets more smoothly to avoid bursts that trigger speed checks.")).defaultValue(true)).build());
        this.speedCap = this.sgMovement.add(((DoubleSetting.Builder)((DoubleSetting.Builder)new DoubleSetting.Builder().name("speed-cap")).description("Maximum blocks/tick reported in movement packets (0 = disabled).")).defaultValue(0.0).range(0.0, 1.0).sliderRange(0.0, 0.5).build());
        this.packetDelay = this.sgPackets.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("packet-delay")).description("Delays movement packets by a few ticks to mask rapid position changes.")).defaultValue(false)).build());
        this.delayTicks = this.sgPackets.add(((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("delay-ticks")).description("How many ticks to buffer movement packets before sending.")).defaultValue(2)).range(1, 10).sliderRange(1, 5).visible(this.packetDelay::get)).build());
        this.cancelRedundant = this.sgPackets.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("cancel-redundant-moves")).description("Suppresses duplicate movement packets (same pos/angle as last tick).")).defaultValue(true)).build());
        this.teleportConfirmBypass = this.sgPackets.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("teleport-confirm-bypass")).description("Delays teleport-confirm packets slightly to avoid synchronisation kicks.")).defaultValue(false)).build());
        this.spoofOnGround = this.sgSpoofing.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("spoof-on-ground")).description("Always tells the server you are on the ground.")).defaultValue(false)).build());
        this.spoofSneaking = this.sgSpoofing.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("spoof-sneaking")).description("Tells the server you are always sneaking (prevents fast-move flags on some AC).")).defaultValue(false)).build());
        this.rotationSmoothing = this.sgSpoofing.add(((BoolSetting.Builder)((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("rotation-smoothing")).description("Rounds yaw/pitch to 2 decimal places to look more human-like.")).defaultValue(true)).build());
        this.packetQueue = new ArrayDeque<class_2828>();
    }

    @Override
    public void onActivate() {
        this.packetQueue.clear();
        if (this.mc.field_1724 != null) {
            this.lastX = this.mc.field_1724.method_23317();
            this.lastY = this.mc.field_1724.method_23318();
            this.lastZ = this.mc.field_1724.method_23321();
            this.lastYaw = this.mc.field_1724.method_36454();
            this.lastPitch = this.mc.field_1724.method_36455();
        }
    }

    @Override
    public void onDeactivate() {
        if (this.mc.method_1562() != null) {
            for (class_2828 pkt : this.packetQueue) {
                this.mc.method_1562().method_52787((class_2596)pkt);
            }
        }
        this.packetQueue.clear();
    }

    @EventHandler
    private void onPacketSend(PacketEvent.Send event) {
        if (this.mc.field_1724 == null) {
            return;
        }
        class_2596<?> class_25962 = event.packet;
        if (class_25962 instanceof class_2828) {
            double cap;
            double dz;
            double dx;
            double dist;
            class_2828 pkt = (class_2828)class_25962;
            if (this.cancelRedundant.get().booleanValue() && pkt.method_12269(0.0) == this.lastX && pkt.method_12268(0.0) == this.lastY && pkt.method_12274(0.0) == this.lastZ && pkt.method_12271(0.0f) == this.lastYaw && pkt.method_12270(0.0f) == this.lastPitch) {
                event.cancel();
                return;
            }
            if (this.speedCap.get() > 0.0 && (dist = Math.sqrt((dx = pkt.method_12269(0.0) - this.lastX) * dx + (dz = pkt.method_12274(0.0) - this.lastZ) * dz)) > (cap = this.speedCap.get().doubleValue())) {
                double scale = cap / dist;
                event.cancel();
                this.mc.method_1562().method_52787((class_2596)new class_2828.class_2830(this.lastX + dx * scale, pkt.method_12268(this.lastY), this.lastZ + dz * scale, pkt.method_12271(this.lastYaw), pkt.method_12270(this.lastPitch), pkt.method_12273(), this.mc.field_1724.field_5976));
                return;
            }
            if (this.packetDelay.get().booleanValue()) {
                event.cancel();
                this.packetQueue.add(pkt);
                return;
            }
            this.lastX = pkt.method_12269(this.lastX);
            this.lastY = pkt.method_12268(this.lastY);
            this.lastZ = pkt.method_12274(this.lastZ);
            this.lastYaw = pkt.method_12271(this.lastYaw);
            this.lastPitch = pkt.method_12270(this.lastPitch);
        }
        if (!this.teleportConfirmBypass.get().booleanValue() || event.packet instanceof class_2793) {
            // empty if block
        }
    }

    @EventHandler
    private void onTick(TickEvent.Post event) {
        if (this.mc.field_1724 == null || this.mc.method_1562() == null) {
            return;
        }
        if (this.packetDelay.get().booleanValue() && !this.packetQueue.isEmpty()) {
            int toRelease = Math.max(1, this.packetQueue.size() - this.delayTicks.get());
            for (int i = 0; i < toRelease && !this.packetQueue.isEmpty(); ++i) {
                this.mc.method_1562().method_52787((class_2596)this.packetQueue.poll());
            }
        }
        if (this.spoofOnGround.get().booleanValue()) {
            this.mc.method_1562().method_52787((class_2596)new class_2828.class_5911(true, this.mc.field_1724.field_5976));
        }
    }
}

