content: /*
   * BedWars1058 - A bed wars mini-game.
   * Copyright (C) 2021 Andrei Dascălu
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU 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 General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   *
   * Contact e-mail: andrew.dascalu@gmail.com
   */
  
  package com.andrei1058.bedwars.commands.bedwars.subcmds.regular;
  
  import com.andrei1058.bedwars.BedWars;
  import com.andrei1058.bedwars.api.arena.IArena;
  import com.andrei1058.bedwars.api.command.ParentCommand;
  import com.andrei1058.bedwars.api.command.SubCommand;
  import com.andrei1058.bedwars.api.server.ServerType;
  import com.andrei1058.bedwars.arena.Arena;
  import com.andrei1058.bedwars.arena.Misc;
  import com.andrei1058.bedwars.arena.SetupSession;
  import com.andrei1058.bedwars.commands.bedwars.MainCommand;
  import org.bukkit.command.CommandSender;
  import org.bukkit.command.ConsoleCommandSender;
  import org.bukkit.entity.Player;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.UUID;
  
  public class CmdLeave extends SubCommand {
  
      private static HashMap<UUID, Long> delay = new HashMap<>();
  
      public CmdLeave(ParentCommand parent, String name) {
          super(parent, name);
          setPriority(20);
          showInList(false);
          setDisplayInfo(com.andrei1058.bedwars.commands.bedwars.MainCommand.createTC("§6 ▪ §7/"+ MainCommand.getInstance().getName()+" leave", "/"+getParent().getName()+" "+getSubCommandName(), "§fLeave an arena."));
      }
  
      @Override
      public boolean execute(String[] args, CommandSender s) {
          if (s instanceof ConsoleCommandSender) return false;
          Player p = (Player) s;
  
          if (cancel(p.getUniqueId())) return true;
          update(p.getUniqueId());
          IArena a = Arena.getArenaByPlayer(p);
  
          Misc.moveToLobbyOrKick(p, a, a != null && a.isSpectator(p.getUniqueId()));
          return true;
      }
  
      @Override
      public List<String> getTabComplete() {
          return null;
      }
  
      @Override
      public boolean canSee(CommandSender s, com.andrei1058.bedwars.api.BedWars api) {
          if (s instanceof ConsoleCommandSender) return false;
  
          Player p = (Player) s;
  
          if (BedWars.getServerType() == ServerType.SHARED && !Arena.isInArena(p)) return false;
  
          if (SetupSession.isInSetupSession(p.getUniqueId())) return false;
          return hasPermission(s);
      }
  
      private static boolean cancel(UUID player){
          return delay.getOrDefault(player, 0L) > System.currentTimeMillis();
      }
  
      private static void update(UUID player){
          if (delay.containsKey(player)){
              delay.replace(player, System.currentTimeMillis() + 2500L);
              return;
          }
          delay.put(player, System.currentTimeMillis() + 2500L);
      }
  }
  