/*
 * 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.event.events.AttackEntityEvent
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.isOlderThanOrEqual1_8
import net.minecraft.entity.vehicle.BoatEntity
import net.minecraft.entity.vehicle.MinecartEntity
import net.minecraft.network.packet.c2s.play.PlayerInteractEntityC2SPacket
import net.minecraft.util.Hand

/**
 * VehicleOneHit module
 *
 * Allows you to break vehicles with a single hit.
 */
object ModuleVehicleOneHit : ClientModule("VehicleOneHit", Category.EXPLOIT) {

    /**
     * How many hits are required to break a vehicle?
     *
     * 1.8 needs 3 hits.
     * 1.16 needs 19 hits.
     *
     * Probably because of the lower attack strength.
     */
    val attackHandler = handler<AttackEntityEvent> { event ->
        val entity = event.entity

        if (entity is BoatEntity || entity is MinecartEntity) {
            repeat(if (isOlderThanOrEqual1_8) 3 else 19) {
                if (isOlderThanOrEqual1_8) {
                    player.swingHand(Hand.MAIN_HAND)
                } else {
                    network.sendPacket(PlayerInteractEntityC2SPacket.attack(entity, player.isSneaking))
                }

                if (isOlderThanOrEqual1_8) {
                    network.sendPacket(PlayerInteractEntityC2SPacket.attack(entity, player.isSneaking))
                } else {
                    player.swingHand(Hand.MAIN_HAND)
                }
            }
        }
    }

}
