/*
 * 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.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.HotbarItemSlot
import net.ccbluex.liquidbounce.utils.inventory.InventoryAction
import net.ccbluex.liquidbounce.utils.inventory.Slots
import net.minecraft.network.protocol.game.ServerboundEditBookPacket
import net.minecraft.world.item.Items
import java.util.Optional


/**
 * 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)?.inventorySlot ?: 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.ensureHasSentCarriedItem()

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

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


}
