content: /**
   * This file is part of Aion-Lightning <aion-lightning.org>.
   *
   *  Aion-Lightning 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.
   *
   *  Aion-Lightning 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 Aion-Lightning.
   *  If not, see <http://www.gnu.org/licenses/>.
   */
  package com.aionemu.gameserver.spawnengine;
  
  import com.aionemu.gameserver.controllers.StaticObjectController;
  import com.aionemu.gameserver.dataholders.DataManager;
  import com.aionemu.gameserver.model.gameobjects.StaticObject;
  import com.aionemu.gameserver.model.gameobjects.VisibleObject;
  import com.aionemu.gameserver.model.templates.VisibleObjectTemplate;
  import com.aionemu.gameserver.model.templates.spawns.SpawnGroup2;
  import com.aionemu.gameserver.model.templates.spawns.SpawnTemplate;
  import com.aionemu.gameserver.utils.idfactory.IDFactory;
  import com.aionemu.gameserver.world.World;
  import com.aionemu.gameserver.world.knownlist.PlayerAwareKnownList;
  
  /**
   * @author ATracer
   */
  public class StaticObjectSpawnManager {
  
  	/**
  	 * @param spawnGroup
  	 * @param instanceIndex
  	 */
  	public static void spawnTemplate(SpawnGroup2 spawn, int instanceIndex) {
  		VisibleObjectTemplate objectTemplate = DataManager.ITEM_DATA.getItemTemplate(spawn.getNpcId());
  		if (objectTemplate == null) {
  			return;
  		}
  
  		if (spawn.hasPool()) {
  			spawn.resetTemplates(instanceIndex);
  			for (int i = 0; i < spawn.getPool(); i++) {
  				SpawnTemplate template = spawn.getRndTemplate(instanceIndex);
  				int objectId = IDFactory.getInstance().nextId();
  				StaticObject staticObject = new StaticObject(objectId, new StaticObjectController(), template, objectTemplate);
  				staticObject.setKnownlist(new PlayerAwareKnownList(staticObject));
  				bringIntoWorld(staticObject, template, instanceIndex);
  			}
  		}
  		else {
  			for (SpawnTemplate template : spawn.getSpawnTemplates()) {
  				int objectId = IDFactory.getInstance().nextId();
  				StaticObject staticObject = new StaticObject(objectId, new StaticObjectController(), template, objectTemplate);
  				staticObject.setKnownlist(new PlayerAwareKnownList(staticObject));
  				bringIntoWorld(staticObject, template, instanceIndex);
  			}
  		}
  	}
  
  	/**
  	 * @param visibleObject
  	 * @param spawn
  	 * @param instanceIndex
  	 */
  	private static void bringIntoWorld(VisibleObject visibleObject, SpawnTemplate spawn, int instanceIndex) {
  		World world = World.getInstance();
  		world.storeObject(visibleObject);
  		world.setPosition(visibleObject, spawn.getWorldId(), instanceIndex, spawn.getX(), spawn.getY(), spawn.getZ(), spawn.getHeading());
  		world.spawn(visibleObject);
  	}
  }
  