Announcements.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.FileWriter;
  23. import java.io.IOException;
  24. import java.io.LineNumberReader;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.StringTokenizer;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import javolution.text.TextBuilder;
  31. import javolution.util.FastList;
  32. import net.sf.l2j.Config;
  33. import net.sf.l2j.gameserver.cache.HtmCache;
  34. import net.sf.l2j.gameserver.clientpackets.Say2;
  35. import net.sf.l2j.gameserver.model.L2World;
  36. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  37. import net.sf.l2j.gameserver.network.SystemMessageId;
  38. import net.sf.l2j.gameserver.script.DateRange;
  39. import net.sf.l2j.gameserver.serverpackets.CreatureSay;
  40. import net.sf.l2j.gameserver.serverpackets.NpcHtmlMessage;
  41. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  42. /**
  43. * This class ...
  44. *
  45. * @version $Revision: 1.5.2.1.2.7 $ $Date: 2005/03/29 23:15:14 $
  46. */
  47. public class Announcements
  48. {
  49. private static Logger _log = Logger.getLogger(Announcements.class.getName());
  50. private static Announcements _instance;
  51. private List<String> _announcements = new FastList<String>();
  52. private List<List<Object>> _eventAnnouncements = new FastList<List<Object>>();
  53. public Announcements()
  54. {
  55. loadAnnouncements();
  56. }
  57. public static Announcements getInstance()
  58. {
  59. if (_instance == null)
  60. {
  61. _instance = new Announcements();
  62. }
  63. return _instance;
  64. }
  65. public void loadAnnouncements()
  66. {
  67. _announcements.clear();
  68. File file = new File(Config.DATAPACK_ROOT, "data/announcements.txt");
  69. if (file.exists())
  70. {
  71. readFromDisk(file);
  72. }
  73. else
  74. {
  75. _log.config("data/announcements.txt doesn't exist");
  76. }
  77. }
  78. public void showAnnouncements(L2PcInstance activeChar)
  79. {
  80. for (int i = 0; i < _announcements.size(); i++)
  81. {
  82. CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, activeChar.getName(), _announcements.get(i));
  83. activeChar.sendPacket(cs);
  84. }
  85. for (int i = 0; i < _eventAnnouncements.size(); i++)
  86. {
  87. List<Object> entry = _eventAnnouncements.get(i);
  88. DateRange validDateRange = (DateRange)entry.get(0);
  89. String[] msg = (String[])entry.get(1);
  90. Date currentDate = new Date();
  91. if (!validDateRange.isValid() || validDateRange.isWithinRange(currentDate))
  92. {
  93. SystemMessage sm = new SystemMessage(SystemMessageId.S1_S2);
  94. for (int j=0; j<msg.length; j++)
  95. {
  96. sm.addString(msg[j]);
  97. }
  98. activeChar.sendPacket(sm);
  99. }
  100. }
  101. }
  102. public void addEventAnnouncement(DateRange validDateRange, String[] msg)
  103. {
  104. List<Object> entry = new FastList<Object>();
  105. entry.add(validDateRange);
  106. entry.add(msg);
  107. _eventAnnouncements.add(entry);
  108. }
  109. public void listAnnouncements(L2PcInstance activeChar)
  110. {
  111. String content = HtmCache.getInstance().getHtmForce("data/html/admin/announce.htm");
  112. NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  113. adminReply.setHtml(content);
  114. TextBuilder replyMSG = new TextBuilder("<br>");
  115. for (int i = 0; i < _announcements.size(); i++)
  116. {
  117. replyMSG.append("<table width=260><tr><td width=220>" + _announcements.get(i) + "</td><td width=40>");
  118. 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>");
  119. }
  120. adminReply.replace("%announces%", replyMSG.toString());
  121. activeChar.sendPacket(adminReply);
  122. }
  123. public void addAnnouncement(String text)
  124. {
  125. _announcements.add(text);
  126. saveToDisk();
  127. }
  128. public void delAnnouncement(int line)
  129. {
  130. _announcements.remove(line);
  131. saveToDisk();
  132. }
  133. private void readFromDisk(File file)
  134. {
  135. LineNumberReader lnr = null;
  136. try
  137. {
  138. int i=0;
  139. String line = null;
  140. lnr = new LineNumberReader(new FileReader(file));
  141. while ( (line = lnr.readLine()) != null)
  142. {
  143. StringTokenizer st = new StringTokenizer(line,"\n\r");
  144. if (st.hasMoreTokens())
  145. {
  146. String announcement = st.nextToken();
  147. _announcements.add(announcement);
  148. i++;
  149. }
  150. }
  151. _log.config("Announcements: Loaded " + i + " Announcements.");
  152. }
  153. catch (IOException e1)
  154. {
  155. _log.log(Level.SEVERE, "Error reading announcements", e1);
  156. }
  157. finally
  158. {
  159. try
  160. {
  161. lnr.close();
  162. }
  163. catch (Exception e2)
  164. {
  165. // nothing
  166. }
  167. }
  168. }
  169. private void saveToDisk()
  170. {
  171. File file = new File("data/announcements.txt");
  172. FileWriter save = null;
  173. try
  174. {
  175. save = new FileWriter(file);
  176. for (int i = 0; i < _announcements.size(); i++)
  177. {
  178. save.write(_announcements.get(i));
  179. save.write("\r\n");
  180. }
  181. save.flush();
  182. save.close();
  183. save = null;
  184. }
  185. catch (IOException e)
  186. {
  187. _log.warning("saving the announcements file has failed: " + e);
  188. }
  189. }
  190. public void announceToAll(String text) {
  191. CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, "", text);
  192. for (L2PcInstance player : L2World.getInstance().getAllPlayers())
  193. {
  194. player.sendPacket(cs);
  195. }
  196. }
  197. public void announceToAll(SystemMessage sm) {
  198. for (L2PcInstance player : L2World.getInstance().getAllPlayers())
  199. {
  200. player.sendPacket(sm);
  201. }
  202. }
  203. // Method fo handling announcements from admin
  204. public void handleAnnounce(String command, int lengthToTrim)
  205. {
  206. try
  207. {
  208. // Announce string to everyone on server
  209. String text = command.substring(lengthToTrim);
  210. Announcements.getInstance().announceToAll(text);
  211. }
  212. // No body cares!
  213. catch (StringIndexOutOfBoundsException e)
  214. {
  215. // empty message.. ignore
  216. }
  217. }
  218. }