/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2025 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.nesting.ToggleableConfigurable
import net.ccbluex.liquidbounce.event.events.AttackEntityEvent
import net.ccbluex.liquidbounce.event.events.PacketEvent
import net.ccbluex.liquidbounce.event.events.WorldChangeEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.ModuleDisabler
import net.minecraft.network.packet.s2c.common.CommonPingS2CPacket

/**
 * Verus Combat Disabler
 */
internal object DisablerVerusCombat : ToggleableConfigurable(ModuleDisabler, "VerusCombat", false) {

    private var waitUntilCombat by boolean("WaitUntilCombat", true)

    private var b = false
    private var combatOccured = false

    @Suppress("unused")
    val attackHandler = handler<AttackEntityEvent> {
        combatOccured = true
    }

    @Suppress("unused")
    val worldChangeEvent = handler<WorldChangeEvent> {
        combatOccured = false
    }

    /**
     * Works by just simply ignore transaction and not accepting them and send invalid one
     * to bypass Verus cancel transaction "patch" (not sending transaction for too long)
     * This could work on other anti-cheat that don't have a proper transaction handling system
     * But this will mostly be used on Verus, disabled any combat check related to transaction.
     */
    @Suppress("unused")
    val packetHandler = handler<PacketEvent> { event ->
        if (player.age <= 20) {
            combatOccured = false
            return@handler
        }

        if (waitUntilCombat && !combatOccured) {
            return@handler
        }

        val packet = event.packet
        if (packet is CommonPingS2CPacket) {
            packet.parameter = if (b) -1 else 1
            // We can't have the same ping all the time.
            b = !b
        }
    }
}
