/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2020  SteamWar.de-Serverteam
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem.commands;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.tracer.record.RecordStateMachine;
import de.steamwar.bausystem.tracer.show.TraceShowManager;
import de.steamwar.bausystem.world.*;
import de.steamwar.bausystem.world.regions.GlobalRegion;
import de.steamwar.bausystem.world.regions.Region;
import de.steamwar.command.SWCommand;
import de.steamwar.core.Core;
import de.steamwar.inventory.SWAnvilInv;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import de.steamwar.sql.BauweltMember;
import de.steamwar.sql.SteamwarUser;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffectType;

import java.util.*;

public class CommandGUI extends SWCommand implements Listener {

    private static final Set<Player> OPEN_INVS = new HashSet<>();
    private static final Set<Player> OPEN_TRACER_INVS = new HashSet<>();
    private static final Set<Player> LAST_F_PLAYER = new HashSet<>();
    private static boolean isRefreshing = false;

    public CommandGUI() {
        super("gui");
        Bukkit.getScheduler().runTaskTimerAsynchronously(BauSystem.getPlugin(), LAST_F_PLAYER::clear, 0, 20);
    }

    @Register(help = true)
    public void genericHelp(Player p, String... args) {
        p.sendMessage("§8/§egui §8- §7Öffne die GUI");
        p.sendMessage("§8/§egui item §8- §7Gebe das GUI item");
    }

    @Register
    public void genericCommand(Player p) {
        openBauGui(p);
        OPEN_INVS.add(p);
    }

    @Register({"item"})
    public void itemCommand(Player p) {
        SWUtils.giveItemToPlayer(p, new ItemStack(Material.NETHER_STAR));
    }

    public static void openBauGui(Player player) {
        Region region = Region.getRegion(player.getLocation());
        SWInventory inv = new SWInventory(player, 5 * 9, SteamwarUser.get(BauSystem.getOwner()).getUserName() + "s Bau");
        inv.setCallback(-1, clickType -> {
            if (!isRefreshing)
                OPEN_INVS.remove(player);
        });

        inv.setItem(43, getMaterial("GLASS_PANE", "THIN_GLASS"), "§7Platzhalter", clickType -> {
        });
        inv.setItem(42, Material.NETHER_STAR, "§7Bau GUI Item", Arrays.asList("§7Du kannst dieses Item zum Öffnen der BauGUI nutzen", "§7oder Doppel F (Swap hands) drücken."), false, clickType -> {
            player.closeInventory();
            player.performCommand("gui item");
        });

        ItemStack dtWand = wand(player, Detonator.WAND, "§8/§7dt wand", Permission.WORLD, "§cDu hast keine Worldrechte");
        inv.setItem(39, dtWand, clickType -> {
            if (Welt.noPermission(player, Permission.WORLD))
                return;
            player.closeInventory();
            player.performCommand("dt wand");
        });

        ItemStack redstoneWand = wand(player, CommandRedstoneTester.WAND, "§8/§7redstonetester", null, "");
        inv.setItem(37, redstoneWand, clickType -> {
            player.closeInventory();
            player.performCommand("redstonetester");
        });

        inv.setItem(40, getMaterial("WOODEN_AXE", "WOOD_AXE"), "§eWorldedit Axt", getNoPermsLore(Arrays.asList("§8//§7wand"), player, "§cDu hast keine Worldeditrechte", Permission.WORLDEDIT), false, clickType -> {
            if (Welt.noPermission(player, Permission.WORLD))
                return;
            player.closeInventory();
            player.performCommand("/wand");
        });
        inv.setItem(41, getMaterial("DEBUG_STICK", "STICK"), "§eDebugstick", getNoPermsLore(Arrays.asList("§8/§7debugstick"), player, "§cDu hast keine Worldrechte", Permission.WORLD), Core.getVersion() < 13, clickType -> {
            if (Welt.noPermission(player, Permission.WORLD))
                return;
            player.closeInventory();
            player.performCommand("debugstick");
        });

        inv.setItem(20, Material.COMPASS, "§7TPS Limitieren", getNoPermsLore(Arrays.asList("§7Aktuell: §e" + CommandTPSLimiter.getCurrentTPSLimit(), "§8/§7tpslimit §8[§e0,5 - " + (TPSUtils.isWarpAllowed() ? 40 : 20) + "§8]"), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
            if (Welt.noPermission(player, Permission.WORLD))
                return;
            SWAnvilInv anvilInv = new SWAnvilInv(player, "TPS Limitieren");
            anvilInv.setItem(Material.COMPASS);
            anvilInv.setCallback(s -> player.performCommand("tpslimit " + s));
            anvilInv.open();
        });
        inv.setItem(5, Material.FEATHER, "§7Geschwindigkeit", Arrays.asList("§7Aktuell: §e" + player.getFlySpeed() * 10, "§8/§7speed §8[§e1 - 10§8]"), false, clickType -> {
            SWAnvilInv anvilInv = new SWAnvilInv(player, "Geschwindigkeit");
            anvilInv.setItem(Material.FEATHER);
            anvilInv.setCallback(s -> player.performCommand("speed " + s));
            anvilInv.open();
        });

        if (player.getUniqueId().equals(BauSystem.getOwner())) {
            SWItem skull = SWItem.getPlayerSkull(player.getName());
            skull.setName("§7Bau verwalten");
            List<String> skullLore = new ArrayList<>();
            skullLore.add("§7TNT: §e" + region.getTntMode().getName());
            skullLore.add("§7StopLag: §e" + (region.isFreeze() ? "Eingeschaltet" : "Ausgeschaltet"));
            skullLore.add("§7Fire: §e" + (region.isFire() ? "Ausgeschaltet" : "Eingeschaltet"));
            skullLore.add("§7Members: §e" + (BauweltMember.getMembers(BauSystem.getOwnerID()).size() - 1));
            skull.setLore(skullLore);
            inv.setItem(4, skull);
        }

        inv.setItem(6, Material.BOOK, "§7Script Bücher", Arrays.asList("§7Aktuell §e" + PredefinedBook.getBookCount() + " §7Bücher"), true, clickType -> {
            player.closeInventory();
            scriptBooksGUI(player);
        });

        inv.setItem(21, Material.OBSERVER, "§7Tracer", getNoPermsLore(Arrays.asList("§7Status: §e" + RecordStateMachine.getRecordStatus().getName()), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
            if (Welt.noPermission(player, Permission.WORLD))
                return;
            player.closeInventory();
            OPEN_TRACER_INVS.add(player);
            traceGUI(player);
        });

        inv.setItem(22, Material.DISPENSER, "§7Auto-Loader", getNoPermsLore(Arrays.asList("§7Status: " + (AutoLoader.hasLoader(player) ? "§aan" : "§caus")), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
            if (Welt.noPermission(player, Permission.WORLD))
                return;
            player.closeInventory();
            autoLoaderGUI(player);
        });

        inv.setItem(17, getMaterial("PLAYER_HEAD", "SKULL_ITEM"), (byte) 3, "§7Spielerkopf geben", Arrays.asList("§8/§7skull §8[§eSpieler§8]"), false, clickType -> {
            SWAnvilInv anvilInv = new SWAnvilInv(player, "Spielerköpfe");
            anvilInv.setItem(Material.NAME_TAG);
            anvilInv.setCallback(s -> player.performCommand("skull " + s));
            anvilInv.open();
        });

        if (GlobalRegion.isGlobalRegion(region)) {
            inv.setItem(9, Material.BARRIER, "§eKeine Region", clickType -> {
            });
            inv.setItem(18, Material.BARRIER, "§eKeine Region", clickType -> {
            });
            inv.setItem(27, Material.BARRIER, "§eKeine Region", clickType -> {
            });
        } else {
            inv.setItem(27, getMaterial("HEAVY_WEIGHTED_PRESSURE_PLATE", "IRON_PLATE"), "§eRegion Reseten", getNoPermsLore(Arrays.asList("§8/§7reset"), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
                if (Welt.noPermission(player, Permission.WORLD))
                    return;
                confirmationInventory(player, "Region Reseten?", () -> player.performCommand("reset"), () -> openBauGui(player));
            });

            if (region.hasProtection()) {
                inv.setItem(18, Material.OBSIDIAN, "§eRegion Protecten", getNoPermsLore(Arrays.asList("§8/§7protect"), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
                    if (Welt.noPermission(player, Permission.WORLD))
                        return;
                    confirmationInventory(player, "Region Protecten", () -> player.performCommand("protect"), () -> openBauGui(player));
                });
            } else {
                inv.setItem(18, Material.BARRIER, "§eRegion nicht Protect bar", clickType -> {
                });
            }

            if (region.hasTestblock()) {
                inv.setItem(9, getMaterial("END_STONE", "ENDER_STONE"), "§eTestblock erneuern", getNoPermsLore(Arrays.asList("§8/§7testblock"), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
                    if (Welt.noPermission(player, Permission.WORLD))
                        return;
                    confirmationInventory(player, "Testblock erneuern", () -> player.performCommand("testblock"), () -> openBauGui(player));
                });
            } else {
                inv.setItem(9, Material.BARRIER, "§eDie Region hat keinen Testblock", clickType -> {
                });
            }
        }

        if (player.hasPotionEffect(PotionEffectType.NIGHT_VISION)) {
            inv.setItem(26, Material.POTION, "§7Nightvision: §eEingeschaltet", Collections.singletonList("§8/§7nv"), false, clickType -> {
                CommandNV.toggleNightvision(player);
                openBauGui(player);
            });
        } else {
            inv.setItem(26, Material.GLASS_BOTTLE, "§7Nightvision: §eAusgeschaltet", Collections.singletonList("§8/§7nv"), false, clickType -> {
                CommandNV.toggleNightvision(player);
                openBauGui(player);
            });
        }

        if (player.hasPotionEffect(PotionEffectType.WATER_BREATHING)) {
            inv.setItem(35, Material.WATER_BUCKET, "§7Waterbreathing: §eEingeschaltet", Collections.singletonList("§8/§7wv"), false, clickType -> {
                CommandGills.toggleGills(player);
                openBauGui(player);
            });
        } else {
            inv.setItem(35, Material.BUCKET, "§7Waterbreathing: §eAusgeschaltet", Collections.singletonList("§8/§7wv"), false, clickType -> {
                CommandGills.toggleGills(player);
                openBauGui(player);
            });
        }

        boolean isBuildArea = region.hasBuildRegion();
        List<String> tntLore = getNoPermsLore(Arrays.asList("§8/§7tnt §8[" + (isBuildArea ? "§eTB§7, " : "") + "§eOff §7oder §eOn§7]"), player, "§cDu hast keine Worldrechte", Permission.WORLD);
        switch (region.getTntMode()) {
            case OFF:
                inv.setItem(23, Material.MINECART, "§7TNT: §eAusgeschaltet", tntLore, false, clickType -> {
                    if (Welt.noPermission(player, Permission.WORLD))
                        return;
                    player.performCommand("tnt " + (isBuildArea ? "tb" : "on"));
                    updateInventories();
                });
                break;
            case ONLY_TB:
                inv.setItem(23, getMaterial("TNT_MINECART", "EXPLOSIVE_MINECART"), "§7TNT: §enur Testblock", tntLore, false, clickType -> {
                    if (Welt.noPermission(player, Permission.WORLD))
                        return;
                    player.performCommand("tnt on");
                    updateInventories();
                });
                break;
            default:
                inv.setItem(23, Material.TNT, "§7TNT: §eEingeschaltet", tntLore, false, clickType -> {
                    if (Welt.noPermission(player, Permission.WORLD))
                        return;
                    player.performCommand("tnt off");
                    updateInventories();
                });
        }

        if (region.isFreeze()) {
            inv.setItem(24, getMaterial("GUNPOWDER", "SULPHUR"), "§7Freeze: §eEingeschaltet", getNoPermsLore(Arrays.asList("§8/§7freeze"), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
                if (Welt.noPermission(player, Permission.WORLD))
                    return;
                player.performCommand("freeze");
                updateInventories();
            });
        } else {
            inv.setItem(24, Material.REDSTONE, "§7Freeze: §eAusgeschaltet", getNoPermsLore(Arrays.asList("§8/§7freeze"), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
                if (Welt.noPermission(player, Permission.WORLD))
                    return;
                player.performCommand("freeze");
                updateInventories();
            });
        }

        if (region.isFire()) {
            inv.setItem(3, getMaterial("FIREWORK_STAR", "FIREWORK_CHARGE"), "§7Fire: §eAusgeschaltet", getNoPermsLore(Arrays.asList("§8/§7fire"), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
                if (Welt.noPermission(player, Permission.WORLD))
                    return;
                player.performCommand("fire");
                updateInventories();
            });
        } else {
            inv.setItem(3, getMaterial("FIRE_CHARGE", "FIREBALL"), "§7Fire: §eEingeschaltet", getNoPermsLore(Arrays.asList("§8/§7fire"), player, "§cDu hast keine Worldrechte", Permission.WORLD), false, clickType -> {
                if (Welt.noPermission(player, Permission.WORLD))
                    return;
                player.performCommand("fire");
                updateInventories();
            });
        }

        inv.setItem(2, Material.ENDER_PEARL, "§7Teleporter", getNoPermsLore(Arrays.asList("§8/§7tp §8[§eSpieler§8]"), player, "", null), false, clickType -> {
            List<SWListInv.SWListEntry<String>> playerSWListEntry = new ArrayList<>();
            Bukkit.getOnlinePlayers().forEach(player1 -> {
                if (player1.equals(player))
                    return;
                playerSWListEntry.add(new SWListInv.SWListEntry<>(SWItem.getPlayerSkull(player1.getName()), player1.getName()));
            });
            SWListInv<String> playerSWListInv = new SWListInv<>(player, "Teleporter", playerSWListEntry, (clickType1, player1) -> {
                player.closeInventory();
                player.performCommand("tp " + player1);
            });
            playerSWListInv.open();
        });

        inv.open();
    }

    private static void traceGUI(Player player) {
        SWInventory inv = new SWInventory(player, 9, "Tracer");
        inv.setCallback(-1, clickType -> {
            if (!isRefreshing)
                OPEN_TRACER_INVS.remove(player);
        });
        List<String> stateLore = Arrays.asList("§7Aktuell: §e" + RecordStateMachine.getRecordStatus().getName(), "§8/§7trace §8[§estart§8, stop §8oder §eauto§8]");
        switch (RecordStateMachine.getRecordStatus()) {
            case IDLE:
                inv.setItem(0, getMaterial("SNOWBALL", "SNOW_BALL"), "§7Tracerstatus", stateLore, false, clickType -> {
                    RecordStateMachine.commandAuto();
                    updateInventories();
                });
                break;
            case IDLE_AUTO:
                inv.setItem(0, Material.ENDER_PEARL, "§7Tracerstatus", stateLore, false, clickType -> {
                    RecordStateMachine.commandStart();
                    updateInventories();
                });
                break;
            case RECORD:
            case RECORD_AUTO:
                inv.setItem(0, getMaterial("ENDER_EYE", "EYE_OF_ENDER"), "§7Tracerstatus", stateLore, false, clickType -> {
                    RecordStateMachine.commandStop();
                    updateInventories();
                });
        }
        if (TraceShowManager.hasActiveShow(player)) {
            inv.setItem(2, Material.TNT, "§7Showstatus", Arrays.asList("§7Aktuell: §eGezeigt", "§8/§7trace §8[§eshow§8/§ehide§8]"), false, clickType -> {
                player.performCommand("trace hide");
                traceGUI(player);
            });
        } else {
            inv.setItem(2, Material.GLASS, "§7Showstatus", Arrays.asList("§7Aktuell: §eVersteckt", "§8/§7trace §8[§eshow§8/§ehide§8]"), false, clickType -> {
                player.performCommand("trace show");
                traceGUI(player);
            });
        }

        inv.setItem(4, getMaterial("TNT_MINECART", "EXPLOSIVE_MINECART"), "§7Trace GUI", Collections.singletonList("§8/§7trace show gui"), false, clickType -> {
            player.closeInventory();
            player.performCommand("trace show gui");
        });

        inv.setItem(6, Material.BARRIER, "§7Trace löschen", Arrays.asList("§8/§7trace delete"), false, clickType -> confirmationInventory(player, "Trace löschen", () -> player.performCommand("trace delete"), () -> {
        }));

        inv.setItem(8, Material.ARROW, "§7Zurück", clickType -> {
            player.closeInventory();
            openBauGui(player);
            OPEN_INVS.add(player);
        });

        inv.open();
    }

    private static void scriptBooksGUI(Player player) {
        List<SWListInv.SWListEntry<PredefinedBook>> entries = new ArrayList<>();
        List<PredefinedBook> books = PredefinedBook.getBooks();
        books.forEach(predefinedBook -> entries.add(new SWListInv.SWListEntry<>(new SWItem(predefinedBook.getBookMat(), predefinedBook.getName(), predefinedBook.getLore(), false, clickType -> {
        }), predefinedBook)));
        SWListInv<PredefinedBook> inv = new SWListInv<>(player, "Script Bücher", entries, (clickType, predefinedBook) -> {
            player.closeInventory();
            player.getInventory().addItem(predefinedBook.toItemStack());
        });
        inv.open();
    }

    private static void autoLoaderGUI(Player player) {
        SWInventory inv = new SWInventory(player, 9, "Autoloader");

        boolean hasLoader = AutoLoader.hasLoader(player);

        if (hasLoader) {
            AutoLoader loader = AutoLoader.getLoader(player);
            if (loader.isSetup()) {
                inv.setItem(0, Material.DROPPER, "§7Loader Starten", Collections.singletonList("§8/§7loader start"), false, clickType -> {
                    loader.start();
                    autoLoaderGUI(player);
                });

                inv.setItem(2, Material.ARROW, "§7Letzte Aktion entfernen", Collections.singletonList("§8/§7loader undo"), false, clickType -> {

                });
            } else {
                inv.setItem(0, Material.BLAZE_ROD, "§7Loader Bearbeiten", Collections.singletonList("§8/§7loader setup"), false, clickType -> {
                    loader.setup();
                    autoLoaderGUI(player);
                });
            }

            inv.setItem(4, Material.COMPASS, "§7Schuss Delay", Arrays.asList("§7Aktuell: §e" + loader.getTicksBetweenShots(), "§8/§7loader wait §8[§eTicks§8]"), false, clickType -> {
                SWAnvilInv anvilInv = new SWAnvilInv(player, "Schuss Delay", loader.getTicksBetweenShots() + "");
                anvilInv.setItem(Material.STONE);
                anvilInv.setCallback(s -> {
                    player.performCommand("loader wait " + s);
                    autoLoaderGUI(player);
                });
                anvilInv.open();
            });

            inv.setItem(6, getMaterial("CLOCK", "WATCH"), "§7Block platzier Geschwindigkeit", Arrays.asList("§7Aktuell: §e" + loader.getTicksBetweenBlocks(), "§8/§7loader speed §8[§eTicks§8]"), false, clickType -> {
                SWAnvilInv anvilInv = new SWAnvilInv(player, "Platzier Geschwindigkeit", loader.getTicksBetweenBlocks() + "");
                anvilInv.setItem(Material.STONE);
                anvilInv.setCallback(s -> {
                    player.performCommand("loader speed " + s);
                    autoLoaderGUI(player);
                });
                anvilInv.open();
            });

            inv.setItem(8, Material.BARRIER, "§7Loader löschen", Collections.singletonList("§8/§7loader stop"), false, clickType -> confirmationInventory(player, "Loader löschen?", () -> {
                loader.stop();
                autoLoaderGUI(player);
            }, () -> autoLoaderGUI(player)));
        } else {
            inv.setItem(4, Material.GOLD_NUGGET, "§eNeuer Autoloader", clickType -> {
                AutoLoader.getLoader(player);
                player.closeInventory();
            });
            inv.setItem(8, Material.ARROW, "§7Zurück", clickType -> {
                player.closeInventory();
                openBauGui(player);
                OPEN_INVS.add(player);
            });
        }

        inv.open();
    }


    private static void confirmChatMessage(Player player, String command) {
        player.sendMessage(BauSystem.PREFIX + "§7Klicke auf die Nachricht zum bestätigen");
        TextComponent t = new TextComponent();
        t.setText("[Hier]");
        t.setColor(ChatColor.YELLOW);
        t.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command));
        t.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText("§7" + command)));
        player.spigot().sendMessage(t);
    }

    private static List<String> getNoPermsLore(List<String> lore, Player player, String noPerms, Permission perm) {
        if (perm != null && Welt.noPermission(player, perm)) {
            lore = new ArrayList<>(lore);
            lore.add(noPerms);
        }
        return lore;
    }

    private static void updateInventories() {
        isRefreshing = true;
        OPEN_INVS.forEach(CommandGUI::openBauGui);
        OPEN_TRACER_INVS.forEach(CommandGUI::traceGUI);
        isRefreshing = false;
    }

    private static void confirmationInventory(Player player, String title, Runnable confirm, Runnable decline) {
        SWInventory inv = new SWInventory(player, 9, title);
        inv.setItem(0, SWItem.getDye(1), (byte) 1, "§cAbbrechen", clickType -> {
            player.closeInventory();
            decline.run();
        });
        inv.setItem(8, SWItem.getDye(10), (byte) 10, "§aBestätigen", clickType -> {
            player.closeInventory();
            confirm.run();
        });
        inv.open();
    }

    private static Material getMaterial(String... names) {
        for (String name : names) {
            try {
                return Material.valueOf(name);
            } catch (IllegalArgumentException ignored) {
                //Ignored                      /\
            }
        }
        return null;
    }

    private static ItemStack wand(Player player, ItemStack base, String command, Permission permission, String noPermissionMessage) {
        base = base.clone();
        ItemMeta meta = base.getItemMeta();
        List<String> lore = meta.getLore();
        lore.add(command);
        if (permission != null && Welt.noPermission(player, permission))
            lore.add(noPermissionMessage);
        meta.setLore(lore);
        base.setItemMeta(meta);
        return base;
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)
            return;
        if (event.getItem() == null || event.getItem().getType() != Material.NETHER_STAR)
            return;
        openBauGui(event.getPlayer());
        OPEN_INVS.add(event.getPlayer());
    }

    @EventHandler
    public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
        if (LAST_F_PLAYER.contains(event.getPlayer())) {
            openBauGui(event.getPlayer());
            OPEN_INVS.add(event.getPlayer());
        } else {
            LAST_F_PLAYER.add(event.getPlayer());
        }
    }
}
