/*
 * 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.event.events.AttackEntityEvent
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.isOlderThanOrEqual1_8
import net.minecraft.network.protocol.game.ServerboundAttackPacket
import net.minecraft.world.InteractionHand
import net.minecraft.world.entity.vehicle.boat.Boat
import net.minecraft.world.entity.vehicle.minecart.Minecart

/**
 * VehicleOneHit module
 *
 * Allows you to break vehicles with a single hit.
 */
object ModuleVehicleOneHit : ClientModule("VehicleOneHit", ModuleCategories.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 Boat || entity is Minecart) {
            repeat(if (isOlderThanOrEqual1_8) 3 else 19) {
                if (isOlderThanOrEqual1_8) {
                    player.swing(InteractionHand.MAIN_HAND)
                } else {
                    network.send(ServerboundAttackPacket(entity.id))
                }

                if (isOlderThanOrEqual1_8) {
                    network.send(ServerboundAttackPacket(entity.id))
                } else {
                    player.swing(InteractionHand.MAIN_HAND)
                }
            }
        }
    }

}
