/*
 * 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.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.protocol.common.ClientboundPingPacket

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

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

    private var b = false
    private var combatOccurred = false

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

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

    /**
     * Works by just simply ignoring transactions and not accepting them and sending invalid one
     * to bypass Verus transaction checks (that checks last time from transaction).
     * This could work on other anti-cheats that don't have a proper transaction handling system
     * but this will mostly be used on Verus, disabling any combat check related to transaction.
     */
    @Suppress("unused")
    val packetHandler = handler<PacketEvent> { event ->
        if (player.tickCount <= 20) {
            combatOccurred = false
            return@handler
        }

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

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