Announcements.java 6.4 KB

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