content: /*
      This file is part of the HeavenMS MapleStory Server
      Copyleft (L) 2016 - 2019 RonanLana
  
      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU Affero General Public License as
      published by the Free Software Foundation version 3 as published by
      the Free Software Foundation. You may not use, modify or distribute
      this program under any other version of the GNU Affero General Public
      License.
  
      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 Affero General Public License for more details.
  
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
  package server.maps;
  
  import java.util.HashMap;
  import java.util.Map;
  import net.server.audit.locks.MonitoredLockType;
  import net.server.audit.locks.MonitoredReadLock;
  import net.server.audit.locks.MonitoredReentrantReadWriteLock;
  import net.server.audit.locks.MonitoredWriteLock;
  import net.server.audit.locks.factory.MonitoredReadLockFactory;
  import net.server.audit.locks.factory.MonitoredWriteLockFactory;
  import scripting.event.EventInstanceManager;
  
  public class MapleMapManager {
  
      private int channel, world;
      private EventInstanceManager event;
      
      private Map<Integer, MapleMap> maps = new HashMap<>();
      
      private MonitoredReadLock mapsRLock;
      private MonitoredWriteLock mapsWLock;
  
      public MapleMapManager(EventInstanceManager eim, int world, int channel) {
          this.world = world;
          this.channel = channel;
          this.event = eim;
  
          MonitoredReentrantReadWriteLock rrwl = new MonitoredReentrantReadWriteLock(MonitoredLockType.MAP_MANAGER);
          this.mapsRLock = MonitoredReadLockFactory.createLock(rrwl);
          this.mapsWLock = MonitoredWriteLockFactory.createLock(rrwl);
      }
  
      public MapleMap resetMap(int mapid) {
          mapsWLock.lock();
          try {
              maps.remove(mapid);
          } finally {
              mapsWLock.unlock();
          }
  
          return getMap(mapid);
      }
  
      private synchronized MapleMap loadMapFromWz(int mapid, boolean cache) {
          MapleMap map;
  
          if (cache) {
              mapsRLock.lock();
              try {
                  map = maps.get(mapid);
              } finally {
                  mapsRLock.unlock();
              }
  
              if (map != null) {
                  return map;
              }
          }
  
          map = MapleMapFactory.loadMapFromWz(mapid, world, channel, event);
  
          if (cache) {
              mapsWLock.lock();
              try {
                  maps.put(mapid, map);
              } finally {
                  mapsWLock.unlock();
              }
          }
  
          return map;
      }
  
      public MapleMap getMap(int mapid) {
          MapleMap map;
  
          mapsRLock.lock();
          try {
              map = maps.get(mapid);
          } finally {
              mapsRLock.unlock();
          }
  
          return (map != null) ? map : loadMapFromWz(mapid, true);
      }
      
      public MapleMap getDisposableMap(int mapid) {
          return loadMapFromWz(mapid, false);
      }
  
      public boolean isMapLoaded(int mapId) {
          mapsRLock.lock();
          try {
              return maps.containsKey(mapId);
          } finally {
              mapsRLock.unlock();
          }
      }
  
      public Map<Integer, MapleMap> getMaps() {
          mapsRLock.lock();
          try {
              return new HashMap<>(maps);
          } finally {
              mapsRLock.unlock();
          }
      }
      
      public void updateMaps() {
          for (MapleMap map : getMaps().values()) {
              map.respawn();
              map.mobMpRecovery();
          }
      }
      
      public void dispose() {
          for (MapleMap map : getMaps().values()) {
              map.dispose();
          }
  
          this.event = null;
      }
  
  }
  