Newer
Older
ChallengeSystem / Challenge / src / main / java / de / fanta / challenge / challenges / AllItemsChallenge.java
@fanta fanta on 7 Jun 2024 6 KB start module system
package de.fanta.challenge.challenges;

import de.fanta.challenge.Challenges;
import de.fanta.challenge.schedular.CancellableTask;
import de.fanta.challenge.utils.ChatUtil;
import de.fanta.challenge.utils.Config;
import de.fanta.challenge.utils.CubesideModUtils;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;

public class AllItemsChallenge {

    public static CancellableTask ItemScheduler;
    public static BossBar bossBar;
    public static Material item;
    public static final List<Material> foundItems = new ArrayList<>();
    public static final List<Material> itemsToSearch = new ArrayList<>();

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

    private static int itemcount = 0;

    public static void start() {
        if (foundItems.isEmpty()) {
            loadItems();
        }

        if (itemsToSearch.isEmpty()) {
            for (Material mat : Material.values()) {
                if (mat.isItem() && !mat.isAir() && !plugin.getNotAvailableMaterials().contains(mat)) {
                    itemcount++;

                    if (!foundItems.contains(mat)) {
                        itemsToSearch.add(mat);
                    }
                }
            }
        }

        ItemStack configStack = Config.getItemStack("allitemscurrentitem");
        if (configStack != null) {
            item = configStack.getType();
        }

        if (item == null) {
            Random r = new Random();
            item = itemsToSearch.get(r.nextInt((itemsToSearch.size() - 1) + 1));
            Config.setValue("allitemscurrentitem", new ItemStack(item));
        }

        bossBar = BossBar.bossBar(Component.empty(), 0.f, BossBar.Color.GREEN, BossBar.Overlay.PROGRESS);
        for (Player pl : Bukkit.getOnlinePlayers()) {
            bossBar.addViewer(pl);
        }
        update();
    }

    public static void update() {
        ItemScheduler = plugin.getScheduler().runGlobalAtFixedRate(() -> {
            if (Config.getBoolean("allitems") && Challenges.getPlugin().getTimer().isRunning()) {
                if (!itemsToSearch.isEmpty()) {
                    for (Player pp : Bukkit.getOnlinePlayers()) {
                        bossBar.addViewer(pp);
                        if (pp.getInventory().contains(item)) {
                            next(pp, false);
                        }
                    }

                    bossBar.name(Component.text("Item » ", ChatUtil.GREEN).append(ChatUtil.getTrasnlateItemComponent(item).color(ChatUtil.BLUE).append(Component.text(" (", ChatUtil.BLUE).append(Component.text(foundItems.size() + "/" + itemcount, ChatUtil.GREEN).append(Component.text(")", ChatUtil.BLUE))))));
                    float progress = foundItems.size() * 100.0f / itemcount / 100.0f;
                    bossBar.progress(progress);
                } else {
                    bossBar.name(Component.text("Alle Items gesammelt!", ChatUtil.GREEN));
                    ChatUtil.sendBrodCastMessage(Component.text("Alle Items gesammelt!", ChatUtil.GREEN));
                    Challenges.getPlugin().getTimer().stopTimer();
                    for (Player pl : Bukkit.getOnlinePlayers()) {
                        pl.playSound(pl.getLocation(), Sound.UI_TOAST_CHALLENGE_COMPLETE, 0.2f, 1);
                    }
                    ItemScheduler.cancel();
                }
            } else {
                bossBar.viewers().forEach(bossBarViewer -> bossBar.removeViewer((Audience) bossBarViewer));
            }
        }, 1, 10);
    }

    public static void next(Player p, Boolean skipped) {
        Material old = item;
        itemsToSearch.remove(old);
        foundItems.add(old);
        saveItems();
        if (!itemsToSearch.isEmpty()) {
            Random r = new Random();
            item = itemsToSearch.get(r.nextInt((itemsToSearch.size() - 1) + 1));
            Config.setValue("allitemscurrentitem", new ItemStack(item));
            if (skipped) {
                ChatUtil.sendBrodCastMessage(Component.text("Item: ", ChatUtil.GREEN).append(ChatUtil.getTrasnlateItemComponent(old).color(ChatUtil.BLUE).append(Component.text(" wurde von " + p.getName() + " übersprungen.", ChatUtil.GREEN))));
            } else {
                ChatUtil.sendBrodCastMessage(Component.text("Item: ", ChatUtil.GREEN).append(ChatUtil.getTrasnlateItemComponent(old).color(ChatUtil.BLUE).append(Component.text(" wurde von " + p.getName() + " Registriert.", ChatUtil.GREEN))));
            }
            ChatUtil.sendBrodCastMessage(Component.text("Neues Item: ", ChatUtil.GREEN).append(ChatUtil.getTrasnlateItemComponent(item).color(ChatUtil.BLUE)).append(Component.text(" (Es fehlen noch ", ChatUtil.GREEN).append(Component.text((itemcount - foundItems.size()), ChatUtil.BLUE).append(Component.text(" Items)", ChatUtil.GREEN)))));
            for (Player pp : Bukkit.getOnlinePlayers()) {
                pp.playSound(pp.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1, 1);
                CubesideModUtils.sendFlashScreenToCubesideMod(plugin, pp, 50, ChatUtil.GREEN);
            }
        }
    }

    private static void saveItems() {
        FileConfiguration config = Challenges.getPlugin().getAllItemsConfig();
        ArrayList<String> itemStings = new ArrayList<>();
        foundItems.forEach(material -> itemStings.add(material.name()));
        config.set("items", itemStings);
        try {
            Challenges.getPlugin().AllItemsConfig.save(Challenges.getPlugin().getAllItemsConfigFile());
        } catch (IOException e) {
            plugin.getLogger().log(Level.SEVERE, "Error while saving Items", e);
        }
    }

    private static void loadItems() {
        FileConfiguration config = Challenges.getPlugin().getAllItemsConfig();
        if (config.isList("items")) {
            config.getStringList("items").forEach(itemSting -> foundItems.add(Material.valueOf(itemSting)));
        }
    }
}