// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.buildertools.utils;

import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import com.hypixel.hytale.server.core.inventory.Inventory;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.protocol.packets.inventory.SetActiveSlot;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.entity.entities.Player;

public final class PasteToolUtil
{
    private static final String PASTE_TOOL_ID = "EditorTool_Paste";
    
    private PasteToolUtil() {
    }
    
    public static void switchToPasteTool(@Nonnull final Player player, @Nonnull final PlayerRef playerRef) {
        final Inventory inventory = player.getInventory();
        final ItemContainer hotbar = inventory.getHotbar();
        final ItemContainer storage = inventory.getStorage();
        final ItemContainer tools = inventory.getTools();
        final int hotbarSize = hotbar.getCapacity();
        for (short slot = 0; slot < hotbarSize; ++slot) {
            final ItemStack itemStack = hotbar.getItemStack(slot);
            if (itemStack != null && !itemStack.isEmpty() && "EditorTool_Paste".equals(itemStack.getItemId())) {
                inventory.setActiveHotbarSlot((byte)slot);
                playerRef.getPacketHandler().writeNoCache(new SetActiveSlot(-1, (byte)slot));
                return;
            }
        }
        short emptySlot = -1;
        for (short slot2 = 0; slot2 < hotbarSize; ++slot2) {
            final ItemStack itemStack2 = hotbar.getItemStack(slot2);
            if (itemStack2 == null || itemStack2.isEmpty()) {
                emptySlot = slot2;
                break;
            }
        }
        if (emptySlot == -1) {
            return;
        }
        for (short slot2 = 0; slot2 < storage.getCapacity(); ++slot2) {
            final ItemStack itemStack2 = storage.getItemStack(slot2);
            if (itemStack2 != null && !itemStack2.isEmpty() && "EditorTool_Paste".equals(itemStack2.getItemId())) {
                storage.moveItemStackFromSlotToSlot(slot2, 1, hotbar, emptySlot);
                inventory.setActiveHotbarSlot((byte)emptySlot);
                playerRef.getPacketHandler().writeNoCache(new SetActiveSlot(-1, (byte)emptySlot));
                return;
            }
        }
        ItemStack pasteToolStack = null;
        for (short slot3 = 0; slot3 < tools.getCapacity(); ++slot3) {
            final ItemStack itemStack3 = tools.getItemStack(slot3);
            if (itemStack3 != null && !itemStack3.isEmpty() && "EditorTool_Paste".equals(itemStack3.getItemId())) {
                pasteToolStack = itemStack3;
                break;
            }
        }
        if (pasteToolStack == null) {
            return;
        }
        hotbar.setItemStackForSlot(emptySlot, new ItemStack(pasteToolStack.getItemId()));
        inventory.setActiveHotbarSlot((byte)emptySlot);
        playerRef.getPacketHandler().writeNoCache(new SetActiveSlot(-1, (byte)emptySlot));
    }
}
