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.listeners;
  
  import com.andrei1058.bedwars.BedWars;
  import com.andrei1058.bedwars.api.arena.IArena;
  import com.andrei1058.bedwars.api.events.gameplay.EggBridgeThrowEvent;
  import com.andrei1058.bedwars.api.server.ServerType;
  import com.andrei1058.bedwars.arena.Arena;
  import com.andrei1058.bedwars.arena.tasks.EggBridgeTask;
  import org.bukkit.Bukkit;
  import org.bukkit.entity.Egg;
  import org.bukkit.entity.Player;
  import org.bukkit.event.EventHandler;
  import org.bukkit.event.Listener;
  import org.bukkit.event.entity.ProjectileHitEvent;
  import org.bukkit.event.entity.ProjectileLaunchEvent;
  
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Map;
  
  @SuppressWarnings("WeakerAccess")
  public class EggBridge implements Listener {
  
      //Active eggBridges
      private static HashMap<Egg, EggBridgeTask> bridges = new HashMap<>();
  
      @EventHandler
      public void onLaunch(ProjectileLaunchEvent event) {
          if (BedWars.getServerType() == ServerType.MULTIARENA) {
              if (event.getEntity().getLocation().getWorld().getName().equalsIgnoreCase(BedWars.getLobbyWorld())) {
                  event.setCancelled(true);
                  return;
              }
          }
          if (event.getEntity() instanceof Egg) {
              Egg projectile = (Egg) event.getEntity();
              if (projectile.getShooter() instanceof Player) {
                  Player shooter = (Player) projectile.getShooter();
                  IArena arena = Arena.getArenaByPlayer(shooter);
                  if (arena != null) {
                      if (arena.isPlayer(shooter)) {
                          EggBridgeThrowEvent throwEvent = new EggBridgeThrowEvent(shooter, arena);
                          Bukkit.getPluginManager().callEvent(throwEvent);
                          if (event.isCancelled()) {
                              event.setCancelled(true);
                              return;
                          }
                          bridges.put(projectile, new EggBridgeTask(shooter, projectile, arena.getTeam(shooter).getColor()));
                      }
                  }
              }
          }
      }
  
      @EventHandler
      public void onHit(ProjectileHitEvent e) {
          if (e.getEntity() instanceof Egg) {
              removeEgg((Egg) e.getEntity());
          }
      }
  
      /**
       * Remove an egg from the active eggs list
       *
       * @since API 7
       */
      public static void removeEgg(Egg e) {
          if (bridges.containsKey(e)) {
              if (bridges.get(e) != null) {
                  bridges.get(e).cancel();
              }
              bridges.remove(e);
          }
      }
  
      /**
       * Get active egg bridges.
       * Modified  in api 11
       *
       * @since API 11
       */
      public static Map<Egg, EggBridgeTask> getBridges() {
          return Collections.unmodifiableMap(bridges);
      }
  }
  