123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- /*
- * Copyright (C) 2004-2013 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server 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.
- *
- * L2J Server 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 <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.datatables;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.Collections;
- import java.util.Map;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javolution.util.FastMap;
- import javolution.util.FastSet;
- import com.l2jserver.Config;
- import com.l2jserver.L2DatabaseFactory;
- import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
- import com.l2jserver.gameserver.model.L2Spawn;
- import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
- import com.l2jserver.gameserver.model.interfaces.IL2Procedure;
- /**
- * Spawn data retriever.
- * @author Zoey76
- */
- public final class SpawnTable
- {
- private static final Logger _log = Logger.getLogger(SpawnTable.class.getName());
- // SQL
- private static final String SELECT_SPAWNS = "SELECT count, npc_templateid, locx, locy, locz, heading, respawn_delay, respawn_random, loc_id, periodOfDay FROM spawnlist";
- private static final String SELECT_CUSTOM_SPAWNS = "SELECT count, npc_templateid, locx, locy, locz, heading, respawn_delay, respawn_random, loc_id, periodOfDay FROM custom_spawnlist";
-
- private static final Map<Integer, Set<L2Spawn>> _spawnTable = new FastMap<Integer, Set<L2Spawn>>().shared();
-
- protected SpawnTable()
- {
- load();
- }
-
- /**
- * Wrapper to load all spawns.
- */
- public void load()
- {
- if (!Config.ALT_DEV_NO_SPAWNS)
- {
- fillSpawnTable(false);
- final int spawnCount = _spawnTable.size();
- _log.info(getClass().getSimpleName() + ": Loaded " + spawnCount + " npc spawns.");
- if (Config.CUSTOM_SPAWNLIST_TABLE)
- {
- fillSpawnTable(true);
- _log.info(getClass().getSimpleName() + ": Loaded " + (_spawnTable.size() - spawnCount) + " custom npc spawns.");
- }
- }
- }
-
- /**
- * Retrieves spawn data from database.
- * @param isCustom if {@code true} the spawns are loaded as custom from custom spawn table
- * @return the spawn count
- */
- private int fillSpawnTable(boolean isCustom)
- {
- int npcSpawnCount = 0;
- try (Connection con = L2DatabaseFactory.getInstance().getConnection();
- Statement s = con.createStatement();
- ResultSet rs = s.executeQuery(isCustom ? SELECT_CUSTOM_SPAWNS : SELECT_SPAWNS))
- {
- L2Spawn spawn;
- L2NpcTemplate npcTemplate;
- int npcId;
- while (rs.next())
- {
- npcId = rs.getInt("npc_templateid");
- npcTemplate = NpcTable.getInstance().getTemplate(npcId);
- if (npcTemplate == null)
- {
- _log.warning(getClass().getSimpleName() + ": Data missing in NPC table for ID: " + npcId + ".");
- continue;
- }
-
- if (npcTemplate.isType("L2SiegeGuard") || npcTemplate.isType("L2RaidBoss") || (!Config.ALLOW_CLASS_MASTERS && npcTemplate.isType("L2ClassMaster")))
- {
- // Don't spawn
- continue;
- }
-
- spawn = new L2Spawn(npcTemplate);
- spawn.setAmount(rs.getInt("count"));
- spawn.setX(rs.getInt("locx"));
- spawn.setY(rs.getInt("locy"));
- spawn.setZ(rs.getInt("locz"));
- spawn.setHeading(rs.getInt("heading"));
- spawn.setRespawnDelay(rs.getInt("respawn_delay"), rs.getInt("respawn_random"));
- spawn.setCustom(isCustom);
- int loc_id = rs.getInt("loc_id");
- spawn.setLocationId(loc_id);
-
- switch (rs.getInt("periodOfDay"))
- {
- case 0: // default
- npcSpawnCount += spawn.init();
- break;
- case 1: // Day
- DayNightSpawnManager.getInstance().addDayCreature(spawn);
- npcSpawnCount++;
- break;
- case 2: // Night
- DayNightSpawnManager.getInstance().addNightCreature(spawn);
- npcSpawnCount++;
- break;
- }
-
- addSpawn(spawn);
- }
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Spawn could not be initialized: " + e.getMessage(), e);
- }
- return npcSpawnCount;
- }
-
- public Map<Integer, Set<L2Spawn>> getSpawnTable()
- {
- return _spawnTable;
- }
-
- /**
- * Get the spawns for the NPC Id.
- * @param npcId the NPC Id
- * @return the spawn set for the given npcId
- */
- public Set<L2Spawn> getSpawns(int npcId)
- {
- return _spawnTable.containsKey(npcId) ? _spawnTable.get(npcId) : Collections.<L2Spawn> emptySet();
- }
-
- /**
- * Get the first NPC spawn.
- * @param npcId the NPC Id to search
- * @return the first not null spawn, if any
- */
- public L2Spawn getFirstSpawn(int npcId)
- {
- if (_spawnTable.containsKey(npcId))
- {
- for (L2Spawn spawn : _spawnTable.get(npcId))
- {
- if (spawn != null)
- {
- return spawn;
- }
- }
- }
- return null;
- }
-
- /**
- * Add a new spawn to the spawn table.
- * @param spawn the spawn to add
- * @param storeInDb if {@code true} it'll be saved in the database
- */
- public void addNewSpawn(L2Spawn spawn, boolean storeInDb)
- {
- addSpawn(spawn);
-
- if (storeInDb)
- {
- final String spawnTable = spawn.isCustom() && Config.CUSTOM_SPAWNLIST_TABLE ? "custom_spawnlist" : "spawnlist";
- try (Connection con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement insert = con.prepareStatement("INSERT INTO " + spawnTable + "(count,npc_templateid,locx,locy,locz,heading,respawn_delay,respawn_random,loc_id) values(?,?,?,?,?,?,?,?,?)"))
- {
- insert.setInt(1, spawn.getAmount());
- insert.setInt(2, spawn.getNpcid());
- insert.setInt(3, spawn.getX());
- insert.setInt(4, spawn.getY());
- insert.setInt(5, spawn.getZ());
- insert.setInt(6, spawn.getHeading());
- insert.setInt(7, spawn.getRespawnDelay() / 1000);
- insert.setInt(8, spawn.getRespawnMaxDelay() - spawn.getRespawnMinDelay());
- insert.setInt(9, spawn.getLocationId());
- insert.execute();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not store spawn in the DB:" + e.getMessage(), e);
- }
- }
- }
-
- /**
- * Delete an spawn from the spawn table.
- * @param spawn the spawn to delete
- * @param updateDb if {@code true} database will be updated
- */
- public void deleteSpawn(L2Spawn spawn, boolean updateDb)
- {
- if (!removeSpawn(spawn))
- {
- return;
- }
-
- if (updateDb)
- {
- try (Connection con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement delete = con.prepareStatement("DELETE FROM " + (spawn.isCustom() ? "custom_spawnlist" : "spawnlist") + " WHERE locx=? AND locy=? AND locz=? AND npc_templateid=? AND heading=?"))
- {
- delete.setInt(1, spawn.getX());
- delete.setInt(2, spawn.getY());
- delete.setInt(3, spawn.getZ());
- delete.setInt(4, spawn.getNpcid());
- delete.setInt(5, spawn.getHeading());
- delete.execute();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, getClass().getSimpleName() + ": Spawn " + spawn + " could not be removed from DB: " + e.getMessage(), e);
- }
- }
- }
-
- /**
- * Add a spawn to the spawn set if present, otherwise add a spawn set and add the spawn to the newly created spawn set.
- * @param spawn the NPC spawn to add
- */
- private void addSpawn(L2Spawn spawn)
- {
- if (!_spawnTable.containsKey(spawn.getNpcid()))
- {
- _spawnTable.put(spawn.getNpcid(), new FastSet<L2Spawn>().shared());
- }
- _spawnTable.get(spawn.getNpcid()).add(spawn);
- }
-
- /**
- * Remove a spawn from the spawn set, if the spawn set is empty, remove it as well.
- * @param spawn the NPC spawn to remove
- * @return {@code true} if the spawn was successfully removed, {@code false} otherwise
- */
- private boolean removeSpawn(L2Spawn spawn)
- {
- if (_spawnTable.containsKey(spawn.getNpcid()))
- {
- final Set<L2Spawn> set = _spawnTable.get(spawn.getNpcid());
- boolean removed = set.remove(spawn);
- if (set.isEmpty())
- {
- _spawnTable.remove(spawn.getNpcid());
- }
- return removed;
- }
- return false;
- }
-
- /**
- * Execute a procedure over all spawns.<br>
- * <font size="4" color="red">Do not use it!</font>
- * @param procedure the procedure to execute
- * @return {@code true} if all procedures were executed, {@code false} otherwise
- */
- public boolean forEachSpawn(IL2Procedure<L2Spawn> procedure)
- {
- for (Set<L2Spawn> set : _spawnTable.values())
- {
- for (L2Spawn spawn : set)
- {
- if (!procedure.execute(spawn))
- {
- return false;
- }
- }
- }
- return true;
- }
-
- public static SpawnTable getInstance()
- {
- return SingletonHolder._instance;
- }
-
- private static class SingletonHolder
- {
- protected static final SpawnTable _instance = new SpawnTable();
- }
- }
|