All pastes #59505 Raw Edit

Stuff

public text v1 · immutable
#59505 ·published 2006-05-29 04:37 UTC
rendered paste body
MINE FROM 25th!

package net.sf.l2j.gameserver.instancemanager;

/**
@author godson
**/

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Logger;

import javolution.util.FastList;
import javolution.util.FastMap;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.GmListTable;
import net.sf.l2j.gameserver.NpcTable;
import net.sf.l2j.gameserver.SpawnTable;
import net.sf.l2j.gameserver.ThreadPoolManager;
import net.sf.l2j.gameserver.model.L2Spawn;
import net.sf.l2j.gameserver.model.actor.instance.L2RaidBossInstance;
import net.sf.l2j.gameserver.templates.L2NpcTemplate;
import net.sf.l2j.gameserver.templates.StatsSet;

public class RaidBossSpawnManager {
    
    private static Logger _log = Logger.getLogger(RaidBossSpawnManager.class.getName());
    
    private static RaidBossSpawnManager _instance;
    
    private static Map<Integer, L2RaidBossInstance> _bosses;
    
    private static Map<Integer, StatsSet> _storedInfo;
    
    private static List<ScheduledFuture> _schedules;
    
    public enum status {
        ALIVE,
        DEAD
    }
    
    public RaidBossSpawnManager()
    {
        init();
    }
    
    public static RaidBossSpawnManager getInstance()
    {
        if (_instance == null)
            _instance = new RaidBossSpawnManager();
        
        return _instance;
    }
    
    private void init()
    {
        _bosses = new FastMap<Integer, L2RaidBossInstance>();
        _schedules = new FastList<ScheduledFuture>();
        _storedInfo = new FastMap<Integer, StatsSet>();
        
        Connection con = null;
        
        try
        {
            con = L2DatabaseFactory.getInstance().getConnection();
            
            PreparedStatement statement = con.prepareStatement("SELECT * from raidboss_spawnlist ORDER BY boss_id");
            ResultSet rset = statement.executeQuery();
            
            L2Spawn spawnDat;
            L2NpcTemplate template;
            long respawnTime;
            long time;
            
            while (rset.next())
            {
                template = NpcTable.getInstance().getTemplate(rset.getInt("boss_id"));
                if (template != null)
                {
                    spawnDat = new L2Spawn(template);
                    spawnDat.setLocx(rset.getInt("loc_x"));
                    spawnDat.setLocy(rset.getInt("loc_y"));
                    spawnDat.setLocz(rset.getInt("loc_z"));
                    spawnDat.setAmount(rset.getInt("amount"));
                    spawnDat.setHeading(rset.getInt("heading"));
                    spawnDat.setRespawnDelay(rset.getInt("respawn_delay"));
                    
                    respawnTime = rset.getLong("respawn_time");
                    
                    time = Calendar.getInstance().getTimeInMillis();
                    
                    if (respawnTime == 0 || (time > respawnTime))
                    {
                        SpawnTable.getInstance().addNewSpawn(spawnDat, false);
                        L2RaidBossInstance raidboss = (L2RaidBossInstance)spawnDat.doSpawn();
                        raidboss.setCurrentHp(rset.getDouble("currentHP"));
                        raidboss.setCurrentMp(rset.getDouble("currentMP"));
                        raidboss.setRaidStatus(status.ALIVE);
                        
                        StatsSet info = new StatsSet();
                        info.set("currentHP", raidboss.getCurrentHp());
                        info.set("currentMP", raidboss.getCurrentMp());
                        info.set("respawnTime", 0L);
                        
                        _storedInfo.put(raidboss.getNpcId(), info);
                        
                        _bosses.put(raidboss.getNpcId(), raidboss);
                    }
                    else
                    {
                        ScheduledFuture futureSpawn;
                        long spawnTime = respawnTime - Calendar.getInstance().getTimeInMillis();
                        
                        futureSpawn = ThreadPoolManager.getInstance().scheduleGeneral(new spawnSchedule(spawnDat), spawnTime);
                        
                        _schedules.add(futureSpawn);
                    }
                }
            }
            
            _log.info("RaidBossSpawnManager: Loaded " + _bosses.size() + " Instances");
            _log.info("RaidBossSpawnManager: Scheduled " + _schedules.size() + " Instances");
            
            rset.close();
            statement.close();
        }
        catch (SQLException e)
        {
            _log.warning("RaidBossSpawnManager: Couldnt load raidboss_spawnlist.sql");
        }
        catch (Exception e) {e.printStackTrace();}
        finally
        {
            try {con.close();} catch(Exception e) {e.printStackTrace();}
        }
    }
    
    private class spawnSchedule implements Runnable
    {
        private L2Spawn _spawnDat;
        
        public spawnSchedule(L2Spawn spawnDat)
        {
            _spawnDat = spawnDat;
        }
        
        public void run()
        {
            L2RaidBossInstance raidboss = (L2RaidBossInstance)_spawnDat.doSpawn();
            raidboss.setRaidStatus(status.ALIVE);
            
            StatsSet info = new StatsSet();
            info.set("currentHP", raidboss.getCurrentHp());
            info.set("currentMP", raidboss.getCurrentMp());
            info.set("respawnTime", 0L);
            
            _storedInfo.put(raidboss.getNpcId(), info);
            
            GmListTable.broadcastMessageToGMs("Spawning Raid Boss " + raidboss.getName());
            
            _bosses.put(raidboss.getNpcId(), raidboss);
        }
    }
    
    public void updateStatus(L2RaidBossInstance boss, boolean isBossDead)
    {
        
        StatsSet info = _storedInfo.get(boss.getNpcId()); 
        
        if (isBossDead)
        {
            boss.setRaidStatus(status.DEAD);
            
            long respawn_time;
            long respawn_delay = boss.getSpawn().getRespawnDelay();
            respawn_time = Calendar.getInstance().getTimeInMillis() + respawn_delay;
            
            info.set("currentHP", boss.getMaxHp());
            info.set("currentMP", boss.getMaxMp());
            info.set("respawnTime", respawn_time);
            
            _log.info("RaidBossSpawnManager: Updated " + boss.getName() + " respawn time to " + respawn_time);
        }
        else
        {  
            info.set("currentHP", boss.getCurrentHp());
            info.set("currentMP", boss.getCurrentMp());
            info.set("respawnTime", 0L);
            
            boss.setRaidStatus(status.ALIVE);
        }
        
        _storedInfo.remove(boss.getNpcId());
        _storedInfo.put(boss.getNpcId(), info);
    }
    
    private void updateDb()
    {
    	for (Integer npcId : _storedInfo.keySet()) 
	        { 
	            Connection con = null; 
             try 
             { 
    	 	 con = L2DatabaseFactory.getInstance().getConnection(); 
                L2RaidBossInstance boss = _bosses.get(npcId);
                
                if(boss.getRaidStatus().equals(status.ALIVE))
                    updateStatus(boss, false);
                
                StatsSet info = _storedInfo.get(npcId);
                
                PreparedStatement statement = con.prepareStatement("UPDATE raidboss_spawnlist set respawn_time = ?, currentHP = ?, currentMP = ? where boss_id = ?");
                statement.setLong(1, info.getLong("respawnTime"));
                statement.setDouble(2, info.getDouble("currentHP"));
                statement.setDouble(3, info.getDouble("currentMP"));
                statement.setInt(4, npcId);
                statement.execute();
                
                statement.close();
            }
        
    	catch (SQLException e){ _log.warning("RaidBossSpawnManager: Couldnt update raidboss_spawnlist.sql");} 
	            finally 
	            { 
	                try {con.close();} catch(Exception e) {e.printStackTrace();} 
            } }
    }
    
    public String[] getAllRaidBossStatus()
    {
        String[] msg = new String[_bosses.size()];
        
        if (_bosses == null)
        {
            msg[0] = "None";
            return msg;
        }
        
        int index = 0;
        
        for (int i : _bosses.keySet())
        {
            L2RaidBossInstance boss = _bosses.get(i);
            
            String name = boss.getName();
            String status = boss.getRaidStatus().name();
            
            msg[index] = name + ": " + status;
            index++;
        }
        
        return msg;
    }
    
    public String getRaidBossStatus(int bossId)
    {
        String msg = "RaidBoss Status....\n";
        
        if (_bosses == null)
        {
            msg += "None";
            return msg;
        }
        
        if (_bosses.containsKey(bossId))
        {
            L2RaidBossInstance boss = _bosses.get(bossId);
            
            String name = boss.getName();
            String status = boss.getRaidStatus().name();
            
            msg += name + ": " + status;
        }
        
        return msg;
    }
    
    public Map<Integer, L2RaidBossInstance> getBosses()
    {
        return _bosses;
    }
    
    public void reloadBosses()
    {
        init();
    }
    
    /**
     * Saves all raidboss status and then clears all info from memory,
     * including all schedules.
     */
    
    public void cleanUp()
    {
        updateDb();
        
        _bosses.clear();
        
        if (_schedules != null)
            for (ScheduledFuture f : _schedules)
                f.cancel(true);
        
        _schedules.clear();
        
        _storedInfo.clear();
    }
}




MINE FROM 19! (was workin perfectly)


package net.sf.l2j.gameserver.instancemanager;

/**
@author godson
**/

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Logger;

import javolution.util.FastList;
import javolution.util.FastMap;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.GmListTable;
import net.sf.l2j.gameserver.NpcTable;
import net.sf.l2j.gameserver.SpawnTable;
import net.sf.l2j.gameserver.ThreadPoolManager;
import net.sf.l2j.gameserver.model.L2Spawn;
import net.sf.l2j.gameserver.model.actor.instance.L2RaidBossInstance;
import net.sf.l2j.gameserver.templates.L2NpcTemplate;
import net.sf.l2j.gameserver.templates.StatsSet;

public class RaidBossSpawnManager {
    
    private static Logger _log = Logger.getLogger(RaidBossSpawnManager.class.getName());
    
    private static RaidBossSpawnManager _instance;
    
    private static Map<Integer, L2RaidBossInstance> _bosses;
    
    private static Map<Integer, StatsSet> _storedInfo;
    
    private static List<ScheduledFuture> _schedules;
    
    public enum status {
        ALIVE,
        DEAD
    }
    
    public RaidBossSpawnManager()
    {
        init();
    }
    
    public static RaidBossSpawnManager getInstance()
    {
        if (_instance == null)
            _instance = new RaidBossSpawnManager();
        
        return _instance;
    }
    
    private void init()
    {
        _bosses = new FastMap<Integer, L2RaidBossInstance>();
        _schedules = new FastList<ScheduledFuture>();
        _storedInfo = new FastMap<Integer, StatsSet>();
        
        Connection con = null;
        
        try
        {
            con = L2DatabaseFactory.getInstance().getConnection();
            
            PreparedStatement statement = con.prepareStatement("SELECT * from raidboss_spawnlist ORDER BY boss_id");
            ResultSet rset = statement.executeQuery();
            
            L2Spawn spawnDat;
            L2NpcTemplate template;
            long respawnTime;
            long time;
            
            while (rset.next())
            {
                template = NpcTable.getInstance().getTemplate(rset.getInt("boss_id"));
                if (template != null)
                {
                    spawnDat = new L2Spawn(template);
                    spawnDat.setLocx(rset.getInt("loc_x"));
                    spawnDat.setLocy(rset.getInt("loc_y"));
                    spawnDat.setLocz(rset.getInt("loc_z"));
                    spawnDat.setAmount(rset.getInt("amount"));
                    spawnDat.setHeading(rset.getInt("heading"));
                    spawnDat.setRespawnDelay(rset.getInt("respawn_delay"));
                    
                    respawnTime = rset.getLong("respawn_time");
                    
                    time = Calendar.getInstance().getTimeInMillis();
                    
                    if (respawnTime == 0 || (time > respawnTime))
                    {
                        SpawnTable.getInstance().addNewSpawn(spawnDat, false);
                        L2RaidBossInstance raidboss = (L2RaidBossInstance)spawnDat.doSpawn();
                        raidboss.setCurrentHp(rset.getDouble("currentHP"));
                        raidboss.setCurrentMp(rset.getDouble("currentMP"));
                        raidboss.setRaidStatus(status.ALIVE);
                        
                        StatsSet info = new StatsSet();
                        info.set("currentHP", raidboss.getCurrentHp());
                        info.set("currentMP", raidboss.getCurrentMp());
                        info.set("respawnTime", 0L);
                        
                        _storedInfo.put(raidboss.getNpcId(), info);
                        
                        _bosses.put(raidboss.getNpcId(), raidboss);
                    }
                    else
                    {
                        ScheduledFuture futureSpawn;
                        long spawnTime = respawnTime - Calendar.getInstance().getTimeInMillis();
                        
                        futureSpawn = ThreadPoolManager.getInstance().scheduleGeneral(new spawnSchedule(spawnDat), spawnTime);
                        
                        _schedules.add(futureSpawn);
                    }
                }
            }
            
            _log.info("RaidBossSpawnManager: Loaded " + _bosses.size() + " Instances");
            _log.info("RaidBossSpawnManager: Scheduled " + _schedules.size() + " Instances");
            
            rset.close();
            statement.close();
        }
        catch (SQLException e)
        {
            _log.warning("RaidBossSpawnManager: Couldnt load raidboss_spawnlist.sql");
        }
        catch (Exception e) {e.printStackTrace();}
        finally
        {
            try {con.close();} catch(Exception e) {e.printStackTrace();}
        }
    }
    
    private class spawnSchedule implements Runnable
    {
        private L2Spawn _spawnDat;
        
        public spawnSchedule(L2Spawn spawnDat)
        {
            _spawnDat = spawnDat;
        }
        
        public void run()
        {
            L2RaidBossInstance raidboss = (L2RaidBossInstance)_spawnDat.doSpawn();
            raidboss.setRaidStatus(status.ALIVE);
            
            StatsSet info = new StatsSet();
            info.set("currentHP", raidboss.getCurrentHp());
            info.set("currentMP", raidboss.getCurrentMp());
            info.set("respawnTime", 0L);
            
            _storedInfo.put(raidboss.getNpcId(), info);
            
            GmListTable.broadcastMessageToGMs("Spawning Raid Boss " + raidboss.getName());
            
            _bosses.put(raidboss.getNpcId(), raidboss);
        }
    }
    
    public void updateStatus(L2RaidBossInstance boss, boolean isBossDead)
    {
        if (!_storedInfo.containsKey(boss.getNpcId()))
            return;
        
        StatsSet info = _storedInfo.get(boss.getNpcId()); 
        
        if (isBossDead)
        {
            boss.setRaidStatus(status.DEAD);
            
            long respawn_time;
            long respawn_delay = boss.getSpawn().getRespawnDelay();
            respawn_time = Calendar.getInstance().getTimeInMillis() + respawn_delay;
            
            info.set("currentHP", boss.getMaxHp());
            info.set("currentMP", boss.getMaxMp());
            info.set("respawnTime", respawn_time);
            
            _log.info("RaidBossSpawnManager: Updated " + boss.getName() + " respawn time");
        }
        else
        {  
            info.set("currentHP", boss.getCurrentHp());
            info.set("currentMP", boss.getCurrentMp());
            info.set("respawnTime", 0L);
            
            boss.setRaidStatus(status.ALIVE);
        }
        
        _storedInfo.remove(boss.getNpcId());
        _storedInfo.put(boss.getNpcId(), info);
    }
    
    private void updateDb()
    {
        Connection con = null;
        
        try
        {
            con = L2DatabaseFactory.getInstance().getConnection();
            
            for (Integer npcId : _storedInfo.keySet())
            {
                L2RaidBossInstance boss = _bosses.get(npcId);
                
                if(boss.isDead())
                    updateStatus(boss, true);
                else
                    updateStatus(boss, false);
                
                StatsSet info = _storedInfo.get(npcId);
                
                PreparedStatement statement = con.prepareStatement("UPDATE raidboss_spawnlist set respawn_time = ? currentHP = ? currentMP = ? where boss_id = ?");
                statement.setLong(1, info.getInteger("respawnTime"));
                statement.setDouble(2, info.getDouble("currentHP"));
                statement.setDouble(3, info.getDouble("currentMP"));
                statement.setInt(4, npcId);
                statement.execute();
                
                statement.close();
            }
        }
        catch (SQLException e){ _log.warning("RaidBossSpawnManager: Couldnt update raidboss_spawnlist.sql");}
        finally
        {
            try {con.close();} catch(Exception e) {e.printStackTrace();}
        }
    }
    
    public String[] getAllRaidBossStatus()
    {
        String[] msg = new String[_bosses.size()];
        
        if (_bosses == null)
        {
            msg[0] = "None";
            return msg;
        }
        
        int index = 0;
        
        for (int i : _bosses.keySet())
        {
            L2RaidBossInstance boss = _bosses.get(i);
            
            String name = boss.getName();
            String status = boss.getRaidStatus().name();
            
            msg[index] = name + ": " + status;
            index++;
        }
        
        return msg;
    }
    
    public String getRaidBossStatus(int bossId)
    {
        String msg = "RaidBoss Status....\n";
        
        if (_bosses == null)
        {
            msg += "None";
            return msg;
        }
        
        if (_bosses.containsKey(bossId))
        {
            L2RaidBossInstance boss = _bosses.get(bossId);
            
            String name = boss.getName();
            String status = boss.getRaidStatus().name();
            
            msg += name + ": " + status;
        }
        
        return msg;
    }
    
    public Map<Integer, L2RaidBossInstance> getBosses()
    {
        return _bosses;
    }
    
    public void reloadBosses()
    {
        init();
    }
    
    /**
     * Saves all raidboss status and then clears all info from memory,
     * including all schedules.
     */
    
    public void cleanUp()
    {
        updateDb();
        
        _bosses.clear();
        
        if (_schedules != null)
            for (ScheduledFuture f : _schedules)
                f.cancel(true);
        
        _schedules.clear();
        
        _storedInfo.clear();
    }
}