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

import net.ccbluex.liquidbounce.config.types.nesting.ToggleableConfigurable
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.Category
import net.ccbluex.liquidbounce.features.module.ClientModule
import net.ccbluex.liquidbounce.utils.client.Chronometer
import net.ccbluex.liquidbounce.utils.client.notification
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket

/**
 * 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 net.minecraft.server.network.ServerPlayerEntity
 */
object ModuleAntiHunger : ClientModule("AntiHunger", Category.EXPLOIT) {

    /**
     * Cancels hunger decrease by making the player not sprint server side
     */
    private object NoSprint : ToggleableConfigurable(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 ClientCommandC2SPacket -> {
                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.mode == ClientCommandC2SPacket.Mode.START_SPRINTING) {
                    notification("AntiHunger",
                        message("warnSprint"),
                        NotificationEvent.Severity.INFO)
                    notificationCooldown.reset()
                    player.jump()
                }
            }

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

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

            is PlayerMoveC2SPacket -> {
                // Check if the player is inside a vehicle or breaking a block (to prevent slow-breaking)
                if (!keepFalling || player.hasVehicle() || interaction.isBreakingBlock) {
                    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.isTouchingWater || player.isSwimming || player.isSubmergedInWater) {
                    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
                }
            }
        }
    }
}
