/*
 * 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

import net.ccbluex.liquidbounce.config.types.group.ToggleableValueGroup
import net.ccbluex.liquidbounce.event.events.NotificationEvent
import net.ccbluex.liquidbounce.event.events.PacketEvent
import net.ccbluex.liquidbounce.event.events.SprintEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.module.ClientModule
import net.ccbluex.liquidbounce.features.module.ModuleCategories
import net.ccbluex.liquidbounce.utils.client.Chronometer
import net.ccbluex.liquidbounce.utils.client.notification
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket
import net.minecraft.network.protocol.game.ServerboundPlayerCommandPacket
import net.minecraft.server.level.ServerPlayer

/**
 * AntiHunger Module
 *
 * Prevents hunger from decreasing, will flag anticheats.
 * Tested on PurityVanilla. Ran about 2000 blocks in the nether and hunger did not decrease.
 *
 * Prevents some cases of exhaustion explained in the increaseTravelMotionStats method of
 * @see ServerPlayer
 */
object ModuleAntiHunger : ClientModule("AntiHunger", ModuleCategories.EXPLOIT) {

    /**
     * Cancels hunger decrease by making the player not sprint server side
     */
    private object NoSprint : ToggleableValueGroup(this, "NoSprint", true) {
        private val whileSwimming by boolean("WhileSwimming", false)

        override val running
            get() = super.running && (!player.isSwimming || whileSwimming)
    }

    init { tree(NoSprint) }

    /**
     * Cancels hunger decrease by making the player float (not on ground) permanently
     */
    private var keepFalling by boolean("KeepFloating", true)

    private var notificationCooldown = Chronometer()

    @Suppress("unused")
    private val sprintHandler = handler<SprintEvent> { event ->
        if (NoSprint.running && (event.source == SprintEvent.Source.NETWORK
                || event.source == SprintEvent.Source.INPUT)) {
            event.sprint = false
        }
    }

    @Suppress("unused")
    private val packetHandler = handler<PacketEvent> { event ->
        when (val packet = event.packet) {

            is ServerboundPlayerCommandPacket -> {
                if (!NoSprint.running || !notificationCooldown.hasElapsed(500)) {
                    return@handler
                }

                // This might be the case when using SuperKnockback, but we want to notify the user
                if (packet.action == ServerboundPlayerCommandPacket.Action.START_SPRINTING) {
                    notification("AntiHunger",
                        message("warnSprint"),
                        NotificationEvent.Severity.INFO)
                    notificationCooldown.reset()
                    player.jumpFromGround()
                }
            }

            is ServerboundPlayerActionPacket -> {
                if (!notificationCooldown.hasElapsed(500)) {
                    return@handler
                }

                if (packet.action == ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK) {
                    notification("AntiHunger",
                        message("warnBreak"),
                        NotificationEvent.Severity.INFO)
                    notificationCooldown.reset()
                }
            }

            is ServerboundMovePlayerPacket -> {
                // Check if the player is inside a vehicle or breaking a block (to prevent slow-breaking)
                if (!keepFalling || player.isPassenger || interaction.isDestroying) {
                    return@handler
                }

                // Check if player is touching water, swimming or submerged in water.
                // If so, we do not want to modify the onGround packet
                if (player.isInWater || player.isSwimming || player.isUnderWater) {
                    return@handler
                }

                // If player is on ground and not falling, we want to send a packet with onGround set to false
                // We cannot get any hunger decrease if we are not on ground, so we want to keep it that way
                if (packet.onGround && player.fallDistance <= 0.0) {
                    packet.onGround = false
                }
            }
        }
    }
}
