Newer
Older
ChallengeSystem / src / main / java / de / fanta / challenges / commands / event / EventStartCommand.java
package de.fanta.challenges.commands.event;

import de.fanta.challenges.Challenges;
import de.fanta.challenges.ServerType;
import de.fanta.challenges.teams.TeamUtils;
import de.fanta.challenges.utils.ChatUtil;
import de.fanta.challenges.utils.guiutils.GUIUtils;
import de.iani.cubesideutils.bukkit.commands.SubCommand;
import de.iani.cubesideutils.commands.ArgsParser;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

public class EventStartCommand extends SubCommand {
    private final Challenges plugin;

    public EventStartCommand(Challenges plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) {
        if (!(sender instanceof Player)) {
            ChatUtil.sendErrorMessage(sender, "You are not a Player :>");
            return true;
        }
        if (sender.hasPermission("challenges.event")) {

            AtomicInteger i = new AtomicInteger(4);
            Bukkit.getScheduler().runTaskTimer(plugin, bukkitTask -> {
                int j = i.decrementAndGet();

                GUIUtils.sendTitleToAll("Event", j > 0 ? "" + j : "Go", ChatUtil.BLUE);

                if(j <= 0){
                    bukkitTask.cancel();
                    World world = Bukkit.getWorld("world");
                    if (plugin.getServerType() != ServerType.ADVENTURE) {
                        world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, true);
                    }
                    for (Player pp : Bukkit.getOnlinePlayers()) {
                        pp.getInventory().removeItem(TeamUtils.selctItem());
                    }
                    plugin.getTimer().startTimer();

                    if (!plugin.getConfig().getBoolean("event.teams")) {
                        Bukkit.getOnlinePlayers().forEach(p -> plugin.getScoreManager().updateScore(p, 0));
                    } else {
                        TeamUtils.setAllPlayerWithoutTeamToTeam();
                        Bukkit.getOnlinePlayers().forEach(p -> plugin.getScoreManager().updateTeamScore(p, 0));
                    }
                }
            }, 10, 20);



            return true;
        } else {
            ChatUtil.sendErrorMessage(sender, "Keine Berechtigung!");
        }
        return false;
    }
}