All pastes #392309 Raw Edit

Someone

public text v1 · immutable
·published 2007-03-12 20:13 UTC
rendered paste body
/* 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 2, 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package net.sf.l2j.gameserver.quote;

import net.sf.l2j.Config;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.ThreadPoolManager;
import net.sf.l2j.gameserver.model.L2World;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;

import java.sql.*;

/**
 * This pulls a random quote out of a database.
 * 
 * @version $Revision: $ $Date: $
 * @author  AresXP
 */
public class QuotesHandler
{
    private static QuotesHandler _instance;
    public static String GrabRandomQuote()
    {
        java.sql.Connection con = null;
        String quote = null;
        try
        {
            con = L2DatabaseFactory.getInstance().getConnection();
            PreparedStatement statement = con.prepareStatement("SELECT quote FROM quotes ORDER BY RAND() LIMIT 1");
            ResultSet rset = statement.executeQuery();
            quote = rset.getString("quote");
        } catch(Exception e)
        {
            e.printStackTrace();
        }
        return quote;
    }
    class MessageQuotes implements Runnable
    {
        public String quote = null;
        public void run()
        {
            quote = QuotesHandler.GrabRandomQuote();
            for(L2PcInstance player : L2World.getInstance().getAllPlayers())
            {
                player.sendMessage(quote);
            }
        }
    }
    public static QuotesHandler getInstance() 
    {
        if ( _instance == null ) 
            _instance = new QuotesHandler();
        return _instance;
    }
    private QuotesHandler()
    {
        ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new MessageQuotes(),0 , Config.RANDOM_QUOTES_INTERVAL);
    }
}