Newer
Older
ChallengeSystem / src / main / java / de / fanta / challenges / guis / TimerGui.java
@fanta fanta on 31 May 2024 12 KB components :')
package de.fanta.challenges.guis;

import de.fanta.challenges.Challenges;
import de.fanta.challenges.ServerType;
import de.fanta.challenges.Timer;
import de.fanta.challenges.utils.ChatUtil;
import de.fanta.challenges.utils.Config;
import de.fanta.challenges.utils.ItemUtils;
import de.iani.cubesideutils.bukkit.inventory.AbstractWindow;
import de.iani.cubesideutils.bukkit.items.CustomHeads;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

public class TimerGui extends AbstractWindow {

    private static final Challenges plugin = Challenges.getPlugin();

    public static final int INVENTORY_SIZE = 27;

    private static final int TIMER_TOGGLE_INDEX = 0;
    private static final int CLOSE_INDEX = 8;

    private static final int TIME_DAYS_INDEX = 10;
    private static final int TIME_HOURS_INDEX = 11;
    private static final int TIME_MINUTES_INDEX = 12;
    private static final int TIME_SECONDS_INDEX = 13;


    private static final int DIRECTION_INDEX = 15;
    private static final int SHOW_TIMER_INDEX = 16;

    public TimerGui(Player player) {
        super(player, Bukkit.createInventory(player, INVENTORY_SIZE, plugin.getGuiPrefix().append(Component.text(" >> Timer"))));
    }

    @Override
    public void onItemClicked(InventoryClickEvent event) {
        Player player = getPlayer();
        if (!mayAffectThisInventory(event)) {
            return;
        }

        event.setCancelled(true);
        if (!getInventory().equals(event.getClickedInventory())) {
            return;
        }

        int slot = event.getSlot();
        switch (slot) {
            case DIRECTION_INDEX -> {
                if (plugin.getTimer().isReverse()) {
                    plugin.getTimer().reverseTimer();
                    ChatUtil.sendTitleToAll(Component.text("Timer"), Component.text("Der Timer läuft jetzt vorwärts.", ChatUtil.GREEN));
                } else {
                    plugin.getTimer().reverseTimer();
                    ChatUtil.sendTitleToAll(Component.text("Timer"), Component.text("Der Timer läuft jetzt rückwärts.", ChatUtil.BLUE));
                }
                if (plugin.getServerType() == ServerType.CHALLENGE) {
                    if (!Config.getBoolean("editsettings")) {
                        Config.setValue("editsettings", true, false);
                        plugin.getLogger().info("Diese Challenge ist kein SpeedRun mehr da der Timer bearbeitet wurde.");
                        if (plugin.getCurrentEditor() != null) {
                            ChatUtil.sendWarningMessage(plugin.getCurrentEditor(), "Diese Challenge wird nicht mehr als Speed Run gezählt, da du etwas am Timer geändert hast.");
                        }
                    }
                }
                rebuildInventory();
            }
            case SHOW_TIMER_INDEX -> {
                if (Config.getBoolean("showtimer")) {
                    Config.setValue("showtimer", false, false);
                    ChatUtil.sendTitleToAll(Component.text("Timer"), Component.text("Der Timer wird jetzt nicht mehr angezeigt!", ChatUtil.RED));
                } else {
                    Config.setValue("showtimer", true, false);
                    ChatUtil.sendTitleToAll(Component.text("Timer"), Component.text("Der Timer wird jetzt wieder angezeigt!", ChatUtil.GREEN));
                }
                rebuildInventory();
            }
            case TIMER_TOGGLE_INDEX -> {
                if (plugin.getTimer().isRunning()) {
                    plugin.getTimer().stopTimer();
                    plugin.setDayLightCircle(false);

                    if (!Config.getBoolean("editsettings")) {
                        Config.setValue("editsettings", true, false);
                        plugin.getLogger().info("Diese Challenge ist kein SpeedRun mehr da der Timer bearbeitet wurde.");
                        if (plugin.getCurrentEditor() != null) {
                            ChatUtil.sendWarningMessage(plugin.getCurrentEditor(), "Diese Challenge wird nicht mehr als Speed Run gezählt, da du etwas am Timer geändert hast.");
                        }
                    }
                    ChatUtil.sendTitleToAll(Component.text("Timer"), Component.text("Der Timer wurde pausiert.", ChatUtil.RED), 10, 60, 10, true);
                } else {
                    plugin.getTimer().startTimer();
                    if (!Config.getBoolean("editsettings")) {
                        if (!plugin.isCubesideStatisticsInstalled()) {
                            return;
                        }
                        plugin.getStatistics().addSpeedRunPlayed();
                    }
                    plugin.setDayLightCircle(true);
                    ChatUtil.sendTitleToAll(Component.text("Timer"), Component.text("Der Timer wurde gestartet.", ChatUtil.GREEN), 10, 60, 10, true);
                }
                player.closeInventory();
            }
            case TIME_DAYS_INDEX -> {
                Timer timer = plugin.getTimer();
                if (event.isLeftClick()) {
                    timer.addTime(!event.isShiftClick() ? 60 * 60 * 24 : 60 * 60 * 24 * 10);
                }

                if (event.isRightClick()) {
                    timer.removeTime(!event.isShiftClick() ? 60 * 60 * 24 : 60 * 60 * 24 * 10);
                }
                rebuildInventory();
            }

            case TIME_HOURS_INDEX -> {
                Timer timer = plugin.getTimer();
                if (event.isLeftClick()) {
                    timer.addTime(!event.isShiftClick() ? 60 * 60 : 60 * 60 * 10);
                }

                if (event.isRightClick()) {
                    timer.removeTime(!event.isShiftClick() ? 60 * 60 : 60 * 60 * 10);
                }
                rebuildInventory();
            }

            case TIME_MINUTES_INDEX -> {
                Timer timer = plugin.getTimer();
                if (event.isLeftClick()) {
                    timer.addTime(!event.isShiftClick() ? 60 : 60 * 10);
                }

                if (event.isRightClick()) {
                    timer.removeTime(!event.isShiftClick() ? 60 : 60 * 10);
                }
                rebuildInventory();
            }

            case TIME_SECONDS_INDEX -> {
                Timer timer = plugin.getTimer();
                if (event.isLeftClick()) {
                    timer.addTime(!event.isShiftClick() ? 1 : 10);
                }

                if (event.isRightClick()) {
                    timer.removeTime(!event.isShiftClick() ? 1 : 10);
                }
                rebuildInventory();
            }
            default -> {
            }
        }

        if (slot == CLOSE_INDEX) {
            player.closeInventory();
        }
    }

    @Override
    protected void rebuildInventory() {
        for (int i = 0; i < INVENTORY_SIZE; i++) {
            ItemStack item;
            switch (i) {
                case DIRECTION_INDEX -> {
                    if (plugin.getTimer().isReverse()) {
                        item = ItemUtils.createGuiItem(Material.MAGENTA_GLAZED_TERRACOTTA, Component.text("Timer Richtung - rückwärts", ChatUtil.BLUE));
                    } else {
                        item = ItemUtils.createGuiItem(Material.MAGENTA_GLAZED_TERRACOTTA, Component.text("Timer Richtung - vorwärts", ChatUtil.GREEN));
                    }
                }
                case SHOW_TIMER_INDEX -> {
                    if (Config.getBoolean("showtimer")) {
                        item = ItemUtils.createGuiItem(Material.OBSERVER, Component.text("Timer wird angezeigt!", ChatUtil.GREEN), true);
                    } else {
                        item = ItemUtils.createGuiItem(Material.OBSERVER, Component.text("Timer wird nicht angezeigt!", ChatUtil.RED), false);
                    }
                }
                case TIMER_TOGGLE_INDEX -> {
                    if (plugin.getTimer().isRunning()) {
                        item = ItemUtils.createGuiItem(Material.CLOCK, Component.text("Timer gestartet", ChatUtil.GREEN), true);
                    } else {
                        item = ItemUtils.createGuiItem(Material.CLOCK, Component.text("Timer pausiert", ChatUtil.RED));
                    }
                }
                case TIME_DAYS_INDEX ->
                        item = ItemUtils.createGuiItem(Material.GOLD_BLOCK, Component.text("Tage", ChatUtil.GREEN), Component.text(">> ", ChatUtil.ORANGE).append(plugin.getTimer().formatTime(ChatUtil.BLUE)), Component.empty(), Component.text("+1 Tag", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("+10 Tage", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)), Component.text("                                    ", Style.style(ChatUtil.BLUE, TextDecoration.STRIKETHROUGH)), Component.text("-1 Tag", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("-10 Tage", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)));
                case TIME_HOURS_INDEX ->
                        item = ItemUtils.createGuiItem(Material.GOLD_INGOT, Component.text("Stunden", ChatUtil.GREEN), Component.text(">> ", ChatUtil.ORANGE).append(plugin.getTimer().formatTime(ChatUtil.BLUE)), Component.empty(), Component.text("+1 Stunde", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("+10 Stunden", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)), Component.text("                                    ", Style.style(ChatUtil.BLUE, TextDecoration.STRIKETHROUGH)), Component.text("-1 Stunde", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("-10 Stunden", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)));
                case TIME_MINUTES_INDEX -> {
                    try {
                        item = ItemUtils.createGuiItem(Material.RAW_GOLD, Component.text("Minuten", ChatUtil.GREEN), Component.text(">> ", ChatUtil.ORANGE).append(plugin.getTimer().formatTime(ChatUtil.BLUE)), Component.empty(), Component.text("+1 Minute", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("+10 Minuten", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)), Component.text("                                    ", Style.style(ChatUtil.BLUE, TextDecoration.STRIKETHROUGH)), Component.text("-1 Minute", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("-10 Minuten", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)));
                    } catch (NoSuchFieldError ex) {
                        item = ItemUtils.createGuiItem(Material.DANDELION, Component.text("Minuten", ChatUtil.GREEN), Component.text(">> ", ChatUtil.ORANGE).append(plugin.getTimer().formatTime(ChatUtil.BLUE)), Component.empty(), Component.text("+1 Minute", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("+10 Minuten", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)), Component.text("                                    ", Style.style(ChatUtil.BLUE, TextDecoration.STRIKETHROUGH)), Component.text("-1 Minute", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("-10 Minuten", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)));
                    }
                }
                case TIME_SECONDS_INDEX ->
                        item = ItemUtils.createGuiItem(Material.GOLD_NUGGET, Component.text("Sekunden", ChatUtil.GREEN), Component.text(">> ", ChatUtil.ORANGE).append(plugin.getTimer().formatTime(ChatUtil.BLUE)), Component.empty(), Component.text("+1 Sekunde", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("+10 Sekunden", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)), Component.text("                                    ", Style.style(ChatUtil.BLUE, TextDecoration.STRIKETHROUGH)), Component.text("-1 Sekunde", ChatUtil.GREEN).append(Component.text(" ‣ Links-Klick", ChatUtil.BLUE)), Component.text("-10 Sekunden", ChatUtil.GREEN).append(Component.text(" ‣ Shift-Links-Klick", ChatUtil.BLUE)));
                case CLOSE_INDEX -> item = CustomHeads.RAINBOW_X.getHead(Component.text("Menü verlassen", ChatUtil.RED));
                default -> item = ItemUtils.EMPTY_ICON;
            }
            this.getInventory().setItem(i, item);
        }
    }
}