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

import net.ccbluex.liquidbounce.features.module.modules.exploit.dupe.ModuleDupe
import net.ccbluex.liquidbounce.utils.client.SilentHotbar
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.ccbluex.liquidbounce.utils.inventory.ClickInventoryAction
import net.ccbluex.liquidbounce.utils.inventory.HotbarItemSlot
import net.ccbluex.liquidbounce.utils.inventory.Slots
import net.minecraft.item.Items
import net.minecraft.network.packet.c2s.play.BookUpdateC2SPacket
import java.util.*


/**
 * Implementation of the Paper Dupe Exploit for Paper 1.20.4 - 1.21.1
 *
 * Explanation: https://www.youtube.com/watch?v=3t3UR6qTLkY
 */
object DupePaper1204 : DupeExploit("Paper-1.20.4") {

    override fun enable() {
        val slot = Slots.Hotbar.findSlot(Items.WRITABLE_BOOK)?.hotbarSlot ?: run {
            chat(markAsError(ModuleDupe.requiresItem(Items.WRITABLE_BOOK)))
            return
        }

        // Select the book slot - This might not even be necessary
        SilentHotbar.selectSlotSilently(this, slot, 1)
        interaction.syncSelectedSlot()

        // Throw all items except the book
        Slots.All.filterNot { itemSlot ->
            itemSlot is HotbarItemSlot && itemSlot.hotbarSlot == slot || itemSlot.itemStack.isEmpty
        }.forEach { itemSlot ->
            ClickInventoryAction.performThrow(slot = itemSlot).performAction()
        }

        network.sendPacket(
            BookUpdateC2SPacket(
                slot,
                listOf("LiquidBounce", "Download LiquidBounce at liquidbounce.net"),
                // Exceeds the maximum title length
                Optional.of("Download LiquidBounce at liquidbounce.net")
            )
        )
        super.enable()
    }


}
