/**
   * Creates a new DeathmatchMinigame instance.
   *
   * @param arena the arena to be played.
   * @param teamA the players on team A.
   * @param teamB the players on team B.
   * @throws Exception If the match failed to register.
   */
  public DeathmatchMinigame(UltimateGamesArena arena, List<Player> teamA, List<Player> teamB)
      throws Exception {
    this.teamA = teamA;
    this.teamB = teamB;
    this.arena = arena;

    Objective.Builder objectiveBuilder =
        Sponge.getRegistry().createBuilder(Objective.Builder.class);

    Text title = Text.of(TextColors.AQUA, "UltimateGames");
    final Objective mainObjective =
        objectiveBuilder.name("<Arena>").criterion(Criteria.DUMMY).displayName(title).build();

    Score teamAName =
        mainObjective.getOrCreateScore(Text.of(TextColors.BLUE, TextStyles.BOLD, "Team A"));
    teamAName.setScore(4);

    this.teamAScoreboardTeam =
        Team.builder()
            .name("TeamA")
            .displayName(Text.of("TeamA"))
            .prefix(Text.of(TextColors.BLUE))
            .nameTagVisibility(Visibilities.ALL)
            .canSeeFriendlyInvisibles(true)
            .allowFriendlyFire(false)
            .members(Sets.newHashSet(Text.of(TextColors.BLUE, TextStyles.BOLD, "Team A")))
            .build();

    Score teamAScore = mainObjective.getOrCreateScore(Text.of(TextColors.BLUE, "Kills: "));
    teamAScore.setScore(3);

    Team teamAScoreTeam =
        Team.builder()
            .name("TeamAScore")
            .displayName(Text.of("TeamAScore"))
            .members(Sets.newHashSet(Text.of(TextColors.BLUE, "Kills: ")))
            .build();

    Score teamBName =
        mainObjective.getOrCreateScore(Text.of(TextColors.RED, TextStyles.BOLD, "Team B"));
    teamBName.setScore(2);

    this.teamBScoreboardTeam =
        Team.builder()
            .name("TeamB")
            .displayName(Text.of("TeamB"))
            .prefix(Text.of(TextColors.RED))
            .nameTagVisibility(Visibilities.ALL)
            .canSeeFriendlyInvisibles(true)
            .allowFriendlyFire(false)
            .members(Sets.newHashSet(Text.of(TextColors.RED, TextStyles.BOLD, "Team B")))
            .build();

    Score teamBScore = mainObjective.getOrCreateScore(Text.of(TextColors.RED, "Kills: "));
    teamBScore.setScore(1);

    Team teamBScoreTeam =
        Team.builder()
            .name("TeamBScore")
            .displayName(Text.of("TeamBScore"))
            .members(Sets.newHashSet(Text.of(TextColors.RED, "Kills: ")))
            .build();

    List<Objective> objectives = new ArrayList<Objective>();
    objectives.add(mainObjective);

    scoreboard = Scoreboard.builder().objectives(objectives).build();
    scoreboard.registerTeam(this.teamAScoreboardTeam);
    scoreboard.registerTeam(teamAScoreTeam);
    scoreboard.registerTeam(this.teamBScoreboardTeam);
    scoreboard.registerTeam(teamBScoreTeam);
    scoreboard.updateDisplaySlot(mainObjective, DisplaySlots.SIDEBAR);

    Scheduler scheduler = Sponge.getScheduler();

    scheduler
        .createTaskBuilder()
        .execute(
            () -> {
              try {
                teamAScoreTeam.setSuffix(Text.of(TextColors.GRAY, teamAPoints));
                teamBScoreTeam.setSuffix(Text.of(TextColors.GRAY, teamBPoints));
              } catch (Exception e) {
                e.printStackTrace();
              }
            })
        .interval(1, TimeUnit.MILLISECONDS)
        .name("UltimateGames - Update scoreboard")
        .submit(
            Sponge.getPluginManager()
                .getPlugin("io.github.hsyyid.ultimategames")
                .get()
                .getInstance()
                .get());

    Ember.register(arena, this);
  }
  @Listener
  public void onStart(MinigameStartedEvent event) {
    if (event.getMinigame().equals(this)) {
      for (Player player : players()) {
        player.sendMessage(
            Text.of(
                TextColors.BLUE, "[UltimateGames]: ", TextColors.GREEN, "Deathmatch starting..."));
      }

      for (Player player : this.teamA) {
        player.offer(Keys.GAME_MODE, GameModes.SURVIVAL);
        player.setScoreboard(scoreboard);
        this.teamAScoreboardTeam.addMember(player.getTeamRepresentation());

        if (this.arena.getTeamALoadout() != null)
          UltimateGames.game
              .getCommandManager()
              .process(
                  Sponge.getServer().getConsole(),
                  "kit " + this.arena.getTeamALoadout() + " " + player.getName());

        if (player
            .getWorld()
            .getUniqueId()
            .equals(this.arena.getTeamASpawn().getLocation().getExtent().getUniqueId())) {
          player.setLocation(this.arena.getTeamASpawn().getLocation());
        } else {
          player.transferToWorld(
              this.arena.getTeamASpawn().getLocation().getExtent().getUniqueId(),
              this.arena.getTeamASpawn().getLocation().getPosition());
        }

        player.sendMessage(
            Text.of(TextColors.BLUE, "[UltimateGames]: ", TextColors.GOLD, "May the games begin!"));
      }

      for (Player player : this.teamB) {
        player.offer(Keys.GAME_MODE, GameModes.SURVIVAL);
        player.setScoreboard(scoreboard);
        this.teamBScoreboardTeam.addMember(player.getTeamRepresentation());

        if (this.arena.getTeamBLoadout() != null)
          UltimateGames.game
              .getCommandManager()
              .process(
                  Sponge.getServer().getConsole(),
                  "kit " + this.arena.getTeamBLoadout() + " " + player.getName());

        if (player
            .getWorld()
            .getUniqueId()
            .equals(this.arena.getTeamBSpawn().getLocation().getExtent().getUniqueId())) {
          player.setLocation(this.arena.getTeamBSpawn().getLocation());
        } else {
          player.transferToWorld(
              this.arena.getTeamBSpawn().getLocation().getExtent().getUniqueId(),
              this.arena.getTeamBSpawn().getLocation().getPosition());
        }

        player.sendMessage(
            Text.of(TextColors.BLUE, "[UltimateGames]: ", TextColors.GOLD, "May the games begin!"));
      }

      Scheduler scheduler = UltimateGames.game.getScheduler();
      Task.Builder taskBuilder = scheduler.createTaskBuilder();

      taskBuilder
          .execute(
              () -> {
                try {
                  Ember.unregister(this.arena);
                } catch (Exception e) {
                  System.out.println(
                      "[UltimateGames]: Error when ending deathmatch in arena " + arena.getName());
                }
              })
          .delay(this.arena.getLength(), TimeUnit.MINUTES)
          .name("UltimateGames - End Deathmatch")
          .submit(
              UltimateGames.game
                  .getPluginManager()
                  .getPlugin("io.github.hsyyid.ultimategames")
                  .get()
                  .getInstance()
                  .get());
    }
  }