123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- /*
- * 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;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.LineNumberReader;
- import java.util.Date;
- import java.util.List;
- import java.util.StringTokenizer;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javolution.text.TextBuilder;
- import javolution.util.FastList;
- import net.sf.l2j.Config;
- import net.sf.l2j.gameserver.cache.HtmCache;
- import net.sf.l2j.gameserver.clientpackets.Say2;
- import net.sf.l2j.gameserver.model.L2World;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- import net.sf.l2j.gameserver.network.SystemMessageId;
- import net.sf.l2j.gameserver.script.DateRange;
- import net.sf.l2j.gameserver.serverpackets.CreatureSay;
- import net.sf.l2j.gameserver.serverpackets.NpcHtmlMessage;
- import net.sf.l2j.gameserver.serverpackets.SystemMessage;
- /**
- * This class ...
- *
- * @version $Revision: 1.5.2.1.2.7 $ $Date: 2005/03/29 23:15:14 $
- */
- public class Announcements
- {
- private static Logger _log = Logger.getLogger(Announcements.class.getName());
- private static Announcements _instance;
- private List<String> _announcements = new FastList<String>();
- private List<List<Object>> _eventAnnouncements = new FastList<List<Object>>();
- public Announcements()
- {
- loadAnnouncements();
- }
- public static Announcements getInstance()
- {
- if (_instance == null)
- {
- _instance = new Announcements();
- }
- return _instance;
- }
- public void loadAnnouncements()
- {
- _announcements.clear();
- File file = new File(Config.DATAPACK_ROOT, "data/announcements.txt");
- if (file.exists())
- {
- readFromDisk(file);
- }
- else
- {
- _log.config("data/announcements.txt doesn't exist");
- }
- }
- public void showAnnouncements(L2PcInstance activeChar)
- {
- for (int i = 0; i < _announcements.size(); i++)
- {
- CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, activeChar.getName(), _announcements.get(i));
- activeChar.sendPacket(cs);
- }
- for (int i = 0; i < _eventAnnouncements.size(); i++)
- {
- List<Object> entry = _eventAnnouncements.get(i);
- DateRange validDateRange = (DateRange)entry.get(0);
- String[] msg = (String[])entry.get(1);
- Date currentDate = new Date();
- if (!validDateRange.isValid() || validDateRange.isWithinRange(currentDate))
- {
- SystemMessage sm = new SystemMessage(SystemMessageId.S1_S2);
- for (int j=0; j<msg.length; j++)
- {
- sm.addString(msg[j]);
- }
- activeChar.sendPacket(sm);
- }
- }
- }
- public void addEventAnnouncement(DateRange validDateRange, String[] msg)
- {
- List<Object> entry = new FastList<Object>();
- entry.add(validDateRange);
- entry.add(msg);
- _eventAnnouncements.add(entry);
- }
- public void listAnnouncements(L2PcInstance activeChar)
- {
- String content = HtmCache.getInstance().getHtmForce("data/html/admin/announce.htm");
- NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
- adminReply.setHtml(content);
- TextBuilder replyMSG = new TextBuilder("<br>");
- for (int i = 0; i < _announcements.size(); i++)
- {
- replyMSG.append("<table width=260><tr><td width=220>" + _announcements.get(i) + "</td><td width=40>");
- replyMSG.append("<button value=\"Delete\" action=\"bypass -h admin_del_announcement " + i + "\" width=60 height=15 back=\"sek.cbui94\" fore=\"sek.cbui92\"></td></tr></table>");
- }
- adminReply.replace("%announces%", replyMSG.toString());
- activeChar.sendPacket(adminReply);
- }
- public void addAnnouncement(String text)
- {
- _announcements.add(text);
- saveToDisk();
- }
- public void delAnnouncement(int line)
- {
- _announcements.remove(line);
- saveToDisk();
- }
- private void readFromDisk(File file)
- {
- LineNumberReader lnr = null;
- try
- {
- int i=0;
- String line = null;
- lnr = new LineNumberReader(new FileReader(file));
- while ( (line = lnr.readLine()) != null)
- {
- StringTokenizer st = new StringTokenizer(line,"\n\r");
- if (st.hasMoreTokens())
- {
- String announcement = st.nextToken();
- _announcements.add(announcement);
- i++;
- }
- }
- _log.config("Announcements: Loaded " + i + " Announcements.");
- }
- catch (IOException e1)
- {
- _log.log(Level.SEVERE, "Error reading announcements", e1);
- }
- finally
- {
- try
- {
- lnr.close();
- }
- catch (Exception e2)
- {
- // nothing
- }
- }
- }
- private void saveToDisk()
- {
- File file = new File("data/announcements.txt");
- FileWriter save = null;
- try
- {
- save = new FileWriter(file);
- for (int i = 0; i < _announcements.size(); i++)
- {
- save.write(_announcements.get(i));
- save.write("\r\n");
- }
- save.flush();
- save.close();
- save = null;
- }
- catch (IOException e)
- {
- _log.warning("saving the announcements file has failed: " + e);
- }
- }
- public void announceToAll(String text) {
- CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, "", text);
- for (L2PcInstance player : L2World.getInstance().getAllPlayers())
- {
- player.sendPacket(cs);
- }
- }
- public void announceToAll(SystemMessage sm) {
- for (L2PcInstance player : L2World.getInstance().getAllPlayers())
- {
- player.sendPacket(sm);
- }
- }
- // Method fo handling announcements from admin
- public void handleAnnounce(String command, int lengthToTrim)
- {
- try
- {
- // Announce string to everyone on server
- String text = command.substring(lengthToTrim);
- Announcements.getInstance().announceToAll(text);
- }
- // No body cares!
- catch (StringIndexOutOfBoundsException e)
- {
- // empty message.. ignore
- }
- }
- }
|