/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2026 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.disablers

import net.ccbluex.liquidbounce.config.types.group.ToggleableValueGroup
import net.ccbluex.liquidbounce.event.events.PlayerTickEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.ModuleDisabler
import net.ccbluex.liquidbounce.features.module.modules.world.scaffold.ModuleScaffold.isTowering
import net.ccbluex.liquidbounce.features.module.modules.world.scaffold.techniques.normal.ScaffoldTellyFeature.isTellyBridging
import net.ccbluex.liquidbounce.utils.network.send1_21_5StopSneaking
import net.ccbluex.liquidbounce.utils.network.sendStartSprinting
import net.ccbluex.liquidbounce.utils.network.sendStopSprinting
import kotlin.random.Random

/**
 * Tested on eu.loyisa.cn, anticheat-test.com
 * Author: TrimoneWasTaken
 *
 * Sending start sprint and stop sprint disables vulcan direction sprint checks,
 * since vulcan thinks you are just spamming sprint.
 *
 * Most vulcan scaffold checks (including limit) check if you have sneaked in
 * the last 15 ticks. By using packets to unsneak, you can exploit this check and
 * disable limit checks.
 */

internal object DisablerVulcanScaffold : ToggleableValueGroup(ModuleDisabler, "VulcanScaffold", false) {

    @Suppress("ComplexCondition", "unused")
    private val tickHandler = handler<PlayerTickEvent> {
        if (player.isInLiquid || player.isDeadOrDying || player.isSuppressingSlidingDownLadder
            || player.abilities.flying) {
            // Not in a state where scaffold is possible or useful
            return@handler
        }

        // Fix for the disabler not triggering while telly bridging
        if (isTellyBridging && player.tickCount % 9 == 0 && Random.nextFloat() <= 0.7f) {
            network.send1_21_5StopSneaking()
        }

        if (player.onGround() && isTowering && Random.nextFloat() <= 0.2f) {
            /*
             * We use probability to send unsneak when the player is on ground and is towering to fix the
             * flag of scaffold-packets check and scaffold K check, this implementation
             * works with low hop towers like karhu, or any tower that sometimes hit the ground.
             */

            network.send1_21_5StopSneaking()
        }

        network.sendStartSprinting()
        network.sendStopSprinting()

        if (player.tickCount % 9 == 0 && player.onGround() && !isTellyBridging && !isTowering) {
            network.send1_21_5StopSneaking()
        }
    }
}
